2016-10-25 3 views
0

광전지 센서에서 나오는 데이터를 실시간으로 플롯하려고하는데, 어쨌든 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() 
+0

짧은 대답은 '예를 접견 matplotlib를 사용하여 데이터를 플롯 할 수 있습니다. 그러나 데이터 '측정'이 어떻게 보이는지 정확히 알 수 없기 때문에 정확히 무엇을 계획하고 싶은지, 우리는 당신을 도울 수 없습니다. matplotlib에서 데이터를 플롯하는 방법을 조금 읽고, 실제로 작동하지 않는 코드를 작성한 다음 질문을 편집하십시오. – ImportanceOfBeingErnest

답변

0

(초과 재귀 깊이를 피하는 동안 ..)

import threading 
threading.Timer(2, RCtime (Pin)).start() #calls func "RCtime" every 2 sec 

아니면 자기 재귀를 수행와 함께

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() 

좋아하지 뭔가

박사 코브라

+0

답변을 주신 Dr. Cobra에게 "질문 편집 후"위의 코드를 편집하여 성공적으로 수치를 얻었지만 업데이트하지 않습니다. 왜 그런지 알고 있습니까? 고마워, - M. Lagha 6 분 전 –

+0

먼저 던져라. –

+0

GPIO.output 값을 "axis.plot (time.time(), ** GPIO 출력 값 추가 **, 'o-')에 플롯해야합니다." –

관련 문제