0

I am currently working with a differential drive robot which has two wheel encoders connected to GPIO Pins 5 and 23. I managed to obtain velocity information based on the pulse time information obtained from the encoders via GPIO event callback.

Code:

def left_wheel_callback(pin):
    if (pin == 5):
        if (counter == 0):
            start_time = time.time()
            counter = 1
        else:
            left_pulse_time = time.time() - start_time
            left_vel = dist / left_pul_time
            left_rpm = (left_vel * 60) / (2 * math.pi * 7.5)
            counter = 0
            print("left encoder pul: ", left_pulse_time)
            print("left RPM: ", left_rpm)

def right_wheel_callback(pin):
    if (pin == 23):
        if (counter == 0):
            start_time = time.time()
            counter = 1
        else:
            right_pulse_time = time.time() - start_time
            right_vel = dist / right_pul_time
            right_rpm = (right_vel * 60) / (2 * math.pi * 7.5)
            counter = 0
            print("right encoder pul: ", right_pulse_time)
            print("right RPM: ", right_rpm)

GPIO.add_event_detect(5, GPIO.RISING, callback=left_wheel_callback)
GPIO.add_event_detect(23, GPIO.RISING, callback=right_wheel_callback)

When testing the encoders by using single GPIO.add_event_detect callback on the wheels separately, the results is as expected.

GPIO.add_event_detect(5, GPIO.RISING, callback=left_wheel_callback)
#GPIO.add_event_detect(23, GPIO.RISING, callback=right_wheel_callback)

# simply commenting one callback out to test for both wheels respectively.

Output:

left encoder pul:  0.0592479705811
left rpm:  28.1303587333
left encoder pul:  0.0584199428558
left rpm:  28.5290704714
left encoder pul:  0.056792974472
left rpm:  29.346352822
left encoder pul:  0.0551149845123
left rpm:  30.2398101245
left encoder pul:  0.0555770397186
left rpm:  29.9884030367

right encoder pul:  0.0564210414886
right rpm:  29.539806829
right encoder pul:  0.056529045105
right rpm:  29.4833684802
right encoder pul:  0.0555770397186
right rpm:  29.9884030367
right encoder pul:  0.0557889938354
right rpm:  29.8744707887
right encoder pul:  0.0559689998627
right rpm:  29.7783893004

Issue:

When testing the encoders by using two GPIO.add_event_detect callback for both wheels all together, the results are not as accurate.

GPIO.add_event_detect(5, GPIO.RISING, callback=left_wheel_callback)
GPIO.add_event_detect(23, GPIO.RISING, callback=right_wheel_callback)

# using both callbacks this time

Output:

left encoder pul:  0.0172209739685
left rpm:  96.7812081776
right encoder pul:  0.0593919754028
right rpm:  28.0621524265
left encoder pul:  0.00982093811035
left rpm:  169.705444423
right encoder pul:  0.0587668418884
right rpm:  28.3606641621
left encoder pul:  0.00218605995178
left rpm:  762.406660123
right encoder pul:  0.0027379989624
right rpm:  608.717055614
left encoder pul:  0.0545420646667
left rpm:  30.5574546334
right encoder pul:  0.0100808143616
right rpm:  165.330558315
left encoder pul:  0.0545239448547
left rpm:  30.5676097191
right encoder pul:  0.0164430141449
right rpm:  101.360167423

It seems that the callbacks are running sequentially which explains the unstable pulse time resulting in inaccurate velocity information. How can I run multiple callbacks concurrently?

Edit: The output are as expected, I had to simply make two class instance as a work around for now and everything seem to have worked.

  • 1
    You are using “counter “ without defining it - although it may be defined elsewhere. Using the same variable in 2 callbacks will be unpredictable. – Milliways Jan 16 at 21:26
  • I will try using different variable for both callbacks. I will update if it works. – Zero Jan 17 at 4:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.