车轮打滑检测

物联网 树莓派 传感器 Python
2021-06-02 12:00:41

我正在从事一个项目,该项目通过使用树莓派连接到车轮的霍尔传感器来感应四速。

我已经为这个唯一的问题编写了程序,当为一个传感器编写程序时,它给出了相当好的结果,当使用所有传感器(使用中断 add_event_detect 通过上拉电阻)时,一个或每个传感器的读数都大不相同。我注意到的一件事是,当我以不同方式为 4 个传感器运行 4 个程序时,它给出了很好的结果。所以我得出结论,问题可能是将代码转换为多线程或将一个传感器的任务调度到另一个传感器,直到第一个传感器完成。

还有一个问题,即使我拔掉了传感器,它也永远不会给出 0 值(我知道背后的原因,代码中没有这样做的声明)实际上我不知道太多的编码,因此帮助我找出这个问题。

import RPi.GPIO as GPIO
import time
import requests
import sys
import csv


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

distance1 = 0
distance2 = 0
distance3 = 0
distance4 = 0

rps1 = 0.00
rps2 = 0.00
rps3 = 0.00
rps4 = 0.00

speed1 = 0.00
speed2 = 0.00
speed3 = 0.00
speed4 = 0.00

start_time1 = 0.00
start_time2 = 0.00
start_time3 = 0.00
start_time4 = 0.00

end_time1 = 0.00
end_time2 = 0.00
end_time3 = 0.00
end_time4 = 0.00

wheel_c = 2.874

hall1 = 2
hall2 = 3
hall3 = 20
hall4 = 21
elapse1 = 0.00
elapse2 = 0.00
elapse3 = 0.00
elapse4 = 0.00

GPIO.setup(hall1, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(hall2, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(hall3, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(hall4, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

def get_pulse1(number):
    global elapse1, distance1, start_time1, end_time1, speed1, rps1
    while GPIO.input(hall1) == 0:
        start_time1 = time.time()
        
    while GPIO.input(hall1) == 1:
        end_time1 = time.time()

    elapse1 = end_time1 - start_time1

    rps1 = (1/elapse1) / 2
    speed1 = (rps1*1293)/10000

    distance1 += 1.293 * elapse1
    
def get_pulse2(number):
    global elapse2,distance2,start_time2,end_time2,speed2,rps2
    while GPIO.input(hall2) == 0:
        start_time2 = time.time()
        
    while GPIO.input(hall2) == 1:
        end_time2 = time.time()

    elapse2 = end_time2 - start_time2

    rps2 = (1/elapse2) / 2
    speed2 = (rps2*1293)/10000
 
    distance2 += 1.293 * elapse2

def get_pulse3(number):
    global elapse3,distance3,start_time3,end_time3,speed3,rps3
    while GPIO.input(hall3) == 0:
        start_time3 = time.time()
        
    while GPIO.input(hall3) == 1:
        end_time3 = time.time()

    elapse3 = end_time3 - start_time3

    rps3 = (1/elapse3) / 2
    speed3 = (rps3*1293)/10000

    distance3 += 1.293 * elapse3

def get_pulse4(number):
    global elapse4,distance4,start_time4,end_time4,speed4,rps4
    while GPIO.input(hall4) == 0:
        start_time4 = time.time()
        
    while GPIO.input(hall4) == 1:
        end_time4 = time.time()

    elapse4 = end_time4 - start_time4

    rps4 = (1/elapse4) / 2
    speed4 = (rps4*1294)/10000

    distance4 += 1.294 * elapse4
    
try:
    print('1 seconds...')
    time.sleep(1)

    GPIO.add_event_detect(hall1,GPIO.FALLING,callback = get_pulse1,bouncetime=20)
    GPIO.add_event_detect(hall2,GPIO.FALLING,callback = get_pulse2,bouncetime=20)
    GPIO.add_event_detect(hall3,GPIO.FALLING,callback = get_pulse3,bouncetime=20)
    GPIO.add_event_detect(hall4,GPIO.FALLING,callback = get_pulse4,bouncetime=20)
    
    while True:
        print(speed1,speed2,speed3,speed4)      
        time.sleep(2) #to reduce CPU load, print every 100 milliseconds
        
        data = str(speed1) + "@" + str(speed2) + "@" + str(speed3) + "@" + str(speed4)
    
        with open('test.csv', "a") as file:
            writer = csv.writer(file)
            writer.writerow([time.ctime(),speed1,distance1,speed2,distance2,speed3,distance3,speed4,distance4])
        try:
        
            r = requests.post('http://dgprojects.co.in/Park/Hardware.php',params={'sensor': data})
            a = (r.text[0:1500])
                    
            
        except:
            print("exeption");
                    
except KeyboardInterrupt:
   print('You have pressed Ctrl+C! How dare you stopped this beautiful thing?!')
   GPIO.cleanup()
1个回答

试试这个:为每个 GPIO 的下降和上升事件配备事件处理器。因此,您将有 8 个事件处理器而不是 4 个。在事件处理器中,只需存储事件的时间戳。在您的主循环中,计算每个车轮的时间差异和速度。如果在最后 x 秒内没有任何事件,您可以决定速度为零。