[英]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.