광전지 센서에서 나오는 데이터를 실시간으로 플롯하려고하는데, 어쨌든 matplotlib를 사용하여 그렇게 할 수 있습니까? 나는 그것을하는 방법을 찾으려고 노력하고 있지만 txt 파일에서 데이터를 가져 오지 않고 GPIO bin에서 직접 내 데이터를 플로팅하는 방법을 찾지 못했습니다.실시간으로 광전자 센서 데이터를 그려야합니다.
#!/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()
짧은 대답은 '예를 접견 matplotlib를 사용하여 데이터를 플롯 할 수 있습니다. 그러나 데이터 '측정'이 어떻게 보이는지 정확히 알 수 없기 때문에 정확히 무엇을 계획하고 싶은지, 우리는 당신을 도울 수 없습니다. matplotlib에서 데이터를 플롯하는 방법을 조금 읽고, 실제로 작동하지 않는 코드를 작성한 다음 질문을 편집하십시오. – ImportanceOfBeingErnest