[英]Plotting a Photocell Sensor Data in the Real-time
我正在嘗試實時繪制來自光電傳感器的數據,無論如何,是否可以通過使用matplotlib來做到這一點? 我正在嘗試找到方法,但沒有找到一種方法來直接從GPIO容器中繪制數據而不從txt文件中獲取數據。
提前謝謝了
這是我的代碼:
#!/usr/local/bin/python
## Reading data from a photocell sensor
import RPi.GPIO as GPIO
import time
# Tell the GPIO library to use Broadcom GPIO references
GPIO.setmode(GPIO.BOARD)
#define the pin that goes to the circuit
Pin = 7
def RCtime (Pin):
measurement = 0
#Output on the pin for # Discharge capacitor
GPIO.setup(Pin, GPIO.OUT)
GPIO.output(Pin, GPIO.LOW)
time.sleep(0.0001)
GPIO.setup(Pin, GPIO.IN)
# Count loops until voltage across capacitor reads high on GPIO
while (GPIO.input(Pin) == GPIO.LOW):
measurement += 1
return measurement
# Main program loop
i = 1
while True:
file1 = open("Data%i.txt" %i ,"w")
i += 1
c = 1
while c <= 50:
print RCtime (Pin)*1.000
c += 1
file1.write(str(RCtime (Pin)))
file1.write("\n")
else:
file1.close()
我編輯了如下代碼,現在出現此錯誤:
File "/home/pi/Qt_Project/plot.py", line 43
for RCtime (Pin) in range(0, 500):
SyntaxError: can't assign to function call
新代碼:
import RPi.GPIO as GPIO
import time
import matplotlib.pyplot as plt
import threading
GPIO.setmode(GPIO.BOARD)
Pin = 7
threading.Timer(2, RCtime (Pin)).start()
def RCtime (Pin):
measurement = 0
GPIO.setup(Pin, GPIO.OUT)
GPIO.output(Pin, GPIO.LOW)
time.sleep(0.0001)
GPIO.setup(Pin, GPIO.IN)
while (GPIO.input(Pin) == GPIO.LOW):
measurement += 1
return measurement
i = 1
while True:
file1 = open("Data%i.txt" %i ,"w")
i += 1
c = 1
while c <= 50:
print RCtime (Pin)*1.000
c += 1
file1.write(str(RCtime (Pin)))
file1.write("\n")
else:
file1.close()
figure, axis = plt.subplots(figsize=(9, 9))
for RCtime (Pin) in range(0, 500):
axis.plot(time.time(), RCtime (Pin), '0-')
plt.show()
為什么不這樣
import matplotlib.pyplot as plt
figure, axis = plt.subplots(figsize=(7.6, 6.1))
for x in range(0, 500):
axis.plot(x, x*2, 'o-')
plt.show()
結合
import threading
threading.Timer(2, RCtime (Pin)).start() #calls func "RCtime" every 2 sec
或執行自我遞歸(以避免遞歸深度超過..)
向眼鏡蛇博士致意
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.