์ƒˆ์†Œ์‹

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

๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด ์„ผ์„œ๊ฐ’ ์ฝ์–ด์™€์„œ ์‹ค์‹œ๊ฐ„ ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ (Matplotlib , animation)

  • -

 

์ด์ „์— ์†Œ๊ฐœํ–ˆ๋˜ ํŒŒ์ด์ฌ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ค‘์— ๋ฐ์ดํ„ฐ๋ฅผ 2D ๋˜๋Š” 3D๊ทธ๋ž˜ํ”„๋กœ ์‹œ๊ฐํ™” ํ•  ์ˆ˜ ์žˆ๋Š” Matplot์ด๋ผ๋Š”๊ฒŒ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

https://infinitt.tistory.com/37

 

matplotlib (pylab) - 2D, 3D ๋ฐ์ดํ„ฐ ์ž๋ฃŒ ์‹œ๊ฐํ™”

matplot ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐจํŠธ(chart)๋‚˜ ํ”Œ๋กฏ(plot) ํ˜•ํƒœ, 2D,3D๋กœ ์‹œ๊ฐํ™”ํ•˜๋„๋ก ๊ธฐ๋Šฅํ•ฉ๋‹ˆ๋‹ค. https://matplotlib.org/3.1.1/gallery/index.html Gallery — Matplotlib 3.1.1 documentation Gallery..

infinitt.tistory.com

 

๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด์—์„œ ๋ฐ›์•„์˜จ ์„ผ์„œ๊ฐ’ (๊ฐ€๋ณ€์ €ํ•ญ , ์ดˆ์ŒํŒŒ ๊ฑฐ๋ฆฌ์ธก์ • , ์˜จ์Šต๋„ (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()

 

 

 

Contents

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

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