์ƒˆ์†Œ์‹

๐Ÿ‡ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด

Matplotlib -์„ผ์„œ๊ฐ’์„ ์ด์šฉํ•œ ์‹ค์‹œ๊ฐ„ ๊ทธ๋ž˜ํ”„ 2๊ฐœ ๊ตฌํ˜„

  • -
import RPi.GPIO as GPIO
from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np
import random
import time
import spidev

spi=spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz=1000000
def ReadChannel(channel):
    adc=spi.xfer2([1,(8+channel)<<4,0])
    data=((adc[1]&3)<<8)+adc[2]
    return data

mcp3008_channel=0




fig = plt.figure()     #figure(๋„ํ‘œ) ์ƒ์„ฑ

ax = plt.subplot(211, xlim=(0, 50), ylim=(0, 1024))
ax_2 = plt.subplot(212, xlim=(0, 50), ylim=(0, 512))



max_points = 50
max_points_2 = 50
line, = ax.plot(np.arange(max_points), 
                np.ones(max_points, dtype=np.float)*np.nan, lw=1, c='blue',ms=1)
line_2, = ax_2.plot(np.arange(max_points_2), 
                np.ones(max_points, dtype=np.float)*np.nan, lw=1,ms=1)


def init():
    return line
def init_2():
    return line_2


def animate(i):
    y = ReadChannel(mcp3008_channel)
    old_y = line.get_ydata()
    new_y = np.r_[old_y[1:], y]
    line.set_ydata(new_y)
    print(new_y)
    return line

def animate_2(i):
    y_2 = random.randint(0,512)
    old_y_2 = line_2.get_ydata()
    new_y_2 = np.r_[old_y_2[1:], y_2]
    line_2.set_ydata(new_y_2)
    print(new_y_2)
    return line_2


anim = animation.FuncAnimation(fig, animate  , init_func= init ,frames=200, interval=50, blit=False)
anim_2 = animation.FuncAnimation(fig, animate_2  , init_func= init_2 ,frames=200, interval=10, blit=False)
plt.show()

๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด์—์„œ, ์„ผ์„œ๊ฐ’์„ ๋ฐ›์•„ 2๊ฐœ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋ ค๋Š” ๋ชฉ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. (๋‹ค๋งŒ ์„ผ์„œ๊ฐ€ ํ•˜๋‚˜๋ฟ์ด๋ผ ํ•˜๋‚˜๋Š” ๋žœ๋คํ•จ์ˆ˜๋กœ ๋Œ€์ฒด)

์œ„ ๊ทธ๋ž˜ํ”„ : ๊ฐ€๋ณ€์ €ํ•ญ ์„ผ์„œ๊ฐ’

์•„๋ž˜ : ๋žœ๋คํ•จ์ˆ˜

 

 

 

 

* ์ •๋ฆฌํ•œ ์ฝ”๋“œ

import RPi.GPIO as GPIO
from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np
import random, time, spidev

spi=spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz=1000000

def ReadChannel(channel):
    adc=spi.xfer2([1,(8+channel)<<4,0])
    data=((adc[1]&3)<<8)+adc[2]
    return data
mcp3008_channel=0
fig = plt.figure()    
ax = plt.subplot(211, xlim=(0, 50), ylim=(0, 1024))
ax_2 = plt.subplot(212, xlim=(0, 50), ylim=(0, 512))

max_points = 50
max_points_2 = 50

line, = ax.plot(np.arange(max_points), 
                np.ones(max_points, dtype=np.float)*np.nan, lw=1, c='blue',ms=1)
line_2, = ax_2.plot(np.arange(max_points_2), 
                np.ones(max_points, dtype=np.float)*np.nan, lw=1,ms=1)

def animate(i):
    y = ReadChannel(mcp3008_channel)
    # y = random.randint(0,1000)
    old_y = line.get_ydata()
    new_y = np.r_[old_y[1:], y]
    line.set_ydata(new_y)
    print(new_y)
    return line
    
def animate_2(i):
    y_2 = random.randint(0,512)
    old_y_2 = line_2.get_ydata()
    new_y_2 = np.r_[old_y_2[1:], y_2]
    line_2.set_ydata(new_y_2)
    print(new_y_2)
    return line_2


anim = animation.FuncAnimation(fig, animate ,interval = 10)
anim_2 = animation.FuncAnimation(fig, animate_2  , interval=10)
plt.show()

 

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.