我正在尝试实时绘制来自光电管传感器的数据,无论如何使用matplotlib做到这一点?我试图找到如何做到这一点,但我没有找到直接从GPIO bin绘制数据的方法,而没有从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()
答案 0 :(得分:0)
为什么不喜欢
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
或执行自递归(同时避免递归深度超过..)
迎接眼镜蛇博士