์ด์ ์ ์๊ฐํ๋ ํ์ด์ฌ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ค์ ๋ฐ์ดํฐ๋ฅผ 2D ๋๋ 3D๊ทธ๋ํ๋ก ์๊ฐํ ํ ์ ์๋ Matplot์ด๋ผ๋๊ฒ ์์์ต๋๋ค.
https://infinitt.tistory.com/37
๋ผ์ฆ๋ฒ ๋ฆฌํ์ด์์ ๋ฐ์์จ ์ผ์๊ฐ (๊ฐ๋ณ์ ํญ , ์ด์ํ ๊ฑฐ๋ฆฌ์ธก์ , ์จ์ต๋ (DHT11))์ ์ด์ฉํ์ฌ ์ค์๊ฐ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ ค๋ณด๋ ค๊ณ ํฉ๋๋ค.
sudo apt update
sudo apt install python3-matplotlib
์ผ๋จ ์ค์น๋ ํฐ๋ฏธ๋์์ ์์ ๊ฐ์ด ํด์ฃผ์๋ฉด ๋ฉ๋๋ค.
*์ฒซ๋ฒ์งธ๋ ๊ฐ๋ณ์ ํญ๊ฐ์ผ๋ก ๊ทธ๋ํ ์์ฑํ๊ธฐ. (ํ๋กํ๋ ์ด์ ๊ธ ์ฐธ์กฐ)
animate๋ผ๋ ํจ์์์ y๊ฐ (๊ทธ๋ํ์ y๊ฐ) ๋ถ๋ถ์ด ์ด๋ ํ ๋ฐ์ดํฐ๋ก ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆด์ง ๊ฒฐ์ ํ๋ฏ๋ก ์ด๋ถ๋ถ๋ง ๋ณ๊ฒฝํด์ฃผ๋ฉด ๋ฉ๋๋ค.
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()
ax = plt.axes(xlim=(0, 50), ylim=(0, 1024))
line, = ax.plot([], [], lw=1, c='blue', marker='d',ms=2)
max_points = 50
line, = ax.plot(np.arange(max_points),
np.ones(max_points, dtype=np.float)*np.nan, lw=1, c='blue',marker='d',ms=2)
def init():
return line
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,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=False)
plt.show()
*์ด์ํ ์ผ์๋ก ๊ทธ๋ํ ๊ทธ๋ฆฌ๊ธฐ
import RPi.GPIO as GPIO
from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np
import random
import time
GPIO.setmode(GPIO.BCM)
TRIG=13
ECHO=26
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG, GPIO.LOW)
def distance_check() :
GPIO.output(TRIG, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(TRIG, GPIO.LOW)
stop=0
start=0
while GPIO.input(ECHO)== GPIO.LOW :
start=time.time()
while GPIO.input(ECHO)== GPIO.HIGH :
stop=time.time()
duration=stop-start
distance=(duration*340*100)/2
return distance
fig = plt.figure()
ax = plt.axes(xlim=(0, 50), ylim=(4, 13))
line, = ax.plot([], [], lw=1, c='blue', marker='d',ms=2)
max_points = 50
line, = ax.plot(np.arange(max_points),
np.ones(max_points, dtype=np.float)*np.nan, lw=1, c='blue',marker='d',ms=2)
def init():
return line
def animate(i):
y = distance_check()
old_y = line.get_ydata()
new_y = np.r_[old_y[1:], y]
line.set_ydata(new_y)
print(new_y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=False)
plt.show()
*DHT11 ( ์จ๋ ์ต๋ ์ผ์)๋ก ๊ทธ๋ํ ๊ทธ๋ฆฌ๊ธฐ
from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np
import time
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 4
fig = plt.figure()
ax = plt.axes(xlim=(0, 50), ylim=(15, 45))
line, = ax.plot([], [], lw=1, c='blue',marker='d',ms=2)
max_points = 50
line, = ax.plot(np.arange(max_points),
np.ones(max_points, dtype=np.float)*np.nan, lw=1, c='blue',marker='d',ms=2)
def init():
return line
h, t = Adafruit_DHT.read_retry(sensor, pin)
def get_y() :
h, t = Adafruit_DHT.read_retry(sensor, pin)
return h
def animate(i):
y = get_y()
old_y = line.get_ydata()
new_y = np.r_[old_y[1:], y]
line.set_ydata(new_y)
print(new_y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=False)
plt.show()