2016-07-19 25 views
0

我正在研究一個項目,在該項目中,我需要使用霍爾效應傳感器和Raspberry Pi來測量車輪的RPM。我已經開發了一個腳本來這樣做,但它不會給我所需的結果。如果磁鐵靠近傳感器,它會直接顯示高值,否則顯示爲0.我想要一個腳本,它會每秒鐘顯示40,39,38,36,9,8,0的結果,就像自行車車速表一樣。我想開發一個Python腳本,它可以測量車輪的轉速

我該怎麼辦?

這是我所做的腳本。

import RPi.GPIO as GPIO 
 
import time 
 
from time import sleep 
 
import datetime 
 

 
j=10000 
 

 
sensor=12 
 

 
ts=datetime.time() 
 
w=0 
 
new_time=0 
 
old_time=0 
 
temp=0 
 

 
GPIO.setmode(GPIO.BOARD) 
 
GPIO.setup(sensor,GPIO.IN,pull_up_down=GPIO.PUD_UP) 
 
while j: 
 
     if (GPIO.input(12)== 0): 
 
      new_time=time.time() 
 
      old_time=temp 
 
      delta_t=new_time-old_time 
 
      temp=new_time 
 
      w=60/delta_t 
 
      v=0.5 * w * 0.10472 * 3.6 
 
      print (v) 
 
      time.sleep(0.1) 
 
     else: 
 
      time.sleep(1) 
 
      print("0") 
 
     j=j-1

這些是我收到

7.73038658487e-09 
0 
0 
5.14198236739 
85.7996578022 
88.3574855171 
88.6053761182 
0 
9.71547048724 
86.4257204462 
0 
9.57686374353 
0 
0 
0 
3.46828868213 
86.5939003971 
87.7296673227 
85.2723314196 
87.1933291677 
86.5891584273 
85.6995234282 
86.6861559057 
85.5173717138 
86.9547003788 
87.3698228295 
87.2335755975 
0 
9.6387741631 
0 
0 
0 
3.4536179304 
82.6103044073 
83.5581939399 
83.8193788137 
82.5174720536 
84.0056662004 
82.471707599 
83.8201193552 
86.8997440944 
82.6851820147 
0 

回答

1

,因爲您在其他打印0您將獲得大量的0的結果。 我不知道如果python是最好的語言來做這個控制,它取決於你的輪子有多快,多久保持GPIO.input(12)== 0。 如果速度太快,您會失去很多轉速。 也許你必須做最後的N個測量的平均值。或者,不要看輪子需要多久才能完成一次轉速,而要測量最近N秒鐘內完成的轉數。

可能是因爲GPIO.input(12)stais 0太長而在同一轉速的GPIO.input(12) == 0情況下輸入。要計算作爲一種新的轉速有可能改變GPIO狀態:

last_state = 1 
while j: 
     if (GPIO.input(12) == last_state) 
      last_state = GPIO.input(12) 
      if (GPIO.input(12)== 0): 
        new_time=time.time() 
        old_time=temp 
        delta_t=new_time-old_time 
        temp=new_time 
        w=60/delta_t 
        v=0.5 * w * 0.10472 * 3.6 
        print (v) 
        time.sleep(0.1) 
      else: 
        time.sleep(1) 
     j=j-1 

我不是一個Python程序員,你必須檢查時間單位和語法:

revs = 0 
last_state = 1 
end_time = time.time() + 10 
while time.time() < end_time: 
     # count revs: 
     if (GPIO.input(12) == last_state) 
      last_state = GPIO.input(12) 
      if (GPIO.input(12) == 0): 
        revs = revs + 1; 

     # print revs every 0.5 secs: 
     if (time.time() - old_time > 0.5) 
      delta_t = time.time() - old_time 
      old_time = time.time() 
      w = 60/delta_t 
      v = revs * 0.5 * w * 0.10472 * 3.6 
      print(v) 
      revs = 0 
     else 
      time.sleep(0.01) 
+0

謝謝你的回答,先生但還有一個問題是我將如何獲得每0.5秒的平均讀數 –

相關問題