我想开发一个可以测量车轮转速的 Python 脚本
I want to develop a Python script which can measure the RPM of a wheel
我正在做一个项目,我需要使用霍尔效应传感器和 Raspberry Pi 测量车轮的转速。我已经开发了一个脚本来执行此操作,但它没有给我所需的结果。如果磁铁靠近传感器,它会直接显示高值,否则会显示 0。我想要一个脚本,每秒显示 40、39、38、36、9、8、0 之类的结果,就像自行车速度计一样。
我该怎么办?
这是我制作的脚本。
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
你会得到很多0,因为你在else中打印了一个0。
我不知道 python 是不是最好的语言来做这个控制,这取决于你的轮子有多快以及停留多长时间 GPIO.input(12)== 0
。
如果速度太快,您将损失很多转速。
可能你必须对最后 N 个措施进行平均。或者,不要看车轮完成一个完整的转速需要多长时间,而是测量在最后 N 秒内完成了多少转速。
可能是因为 GPIO.input(12) stais 0 太长,所以你在 GPIO.input(12) == 0
情况下输入相同的 rev。要算作一个新的转速必须改变 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)
我正在做一个项目,我需要使用霍尔效应传感器和 Raspberry Pi 测量车轮的转速。我已经开发了一个脚本来执行此操作,但它没有给我所需的结果。如果磁铁靠近传感器,它会直接显示高值,否则会显示 0。我想要一个脚本,每秒显示 40、39、38、36、9、8、0 之类的结果,就像自行车速度计一样。
我该怎么办?
这是我制作的脚本。
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
你会得到很多0,因为你在else中打印了一个0。
我不知道 python 是不是最好的语言来做这个控制,这取决于你的轮子有多快以及停留多长时间 GPIO.input(12)== 0
。
如果速度太快,您将损失很多转速。
可能你必须对最后 N 个措施进行平均。或者,不要看车轮完成一个完整的转速需要多长时间,而是测量在最后 N 秒内完成了多少转速。
可能是因为 GPIO.input(12) stais 0 太长,所以你在 GPIO.input(12) == 0
情况下输入相同的 rev。要算作一个新的转速必须改变 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)