3

I am working on a project which sensed four speed by using hall sensor attached to wheel by using raspberry pi.

I have wrote program for that only issue with that it gives fairly good results when wrote program for one sensor, and when using all sensor (using interrupt add_event_detect by pull up resistor), readings are quite different either one or for every sensor. One thing I noticed is that it gives good results when I run 4 program for 4 sensor differently. So I conclude that may be the issue is to convert code to multithread or something schedule task one sensor to another until first is finished.

One more issue it never gives 0 value even I unplugged sensor (I know the reason behind it there is no statement in code to do that) actually I don't know much coding hence help me to figure out this problem.

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()
4
  • 1
    it is unclear what problem you are trying to solve .... it would help a lot if you actually asked a question that could be answered
    – jsotola
    Jan 23, 2021 at 8:15
  • 2
    My question is why results are not acceptable when 4 sensor used simultaneously in a code and results are quite accurate when 4 different program of 4 sensor give individual results simultaneously. Jan 23, 2021 at 8:55
  • Agree, actually i want to know if someone can solve my problem, i want to convert this program to multithread. Jan 24, 2021 at 5:49
  • 1
    try running a separate script for each wheel ... run a 5th script as the main control script .... duckduckgo.com/?q=python+multiple+scripts+share+data&ia=qa
    – jsotola
    Jan 24, 2021 at 17:54

1 Answer 1

0

Try this: Have event processors for the falling and rising events for each GPIO. So, you'll have 8 event processors instead of 4. In the event processors, just store the timestamp of the event. In your main loop, do the time diff and speed calc of each wheel. You could decide the speed is zero if there have been no events in the last x sec.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.