Forum Archive

Faster quadrature decoder loops with Python code

ailee

I'm working with a BeagleBone Black and using Adafruit's IO Python library. Wrote a simple quadrature decoding function and it works perfectly fine when the motor runs at about 1800 RPM. But when the motor runs at higher speeds, the code starts missing some of the interrupts and the encoder counts start to accumulate errors. Do you guys have any suggestions as to how I can make the code more efficient or if there are functions which can cycle the interrupts at a higher frequency.

Thanks, Kel

Here's the code:
```

Define encoder count function

def encodercount(term):
global counts
global Encoder_A
global Encoder_A_old
global Encoder_B
global Encoder_B_old
global error

Encoder_A = GPIO.input('P8_7') # stores the value of the encoders at time of interrupt
Encoder_B = GPIO.input('P8_8')

if Encoder_A == Encoder_A_old and Encoder_B == Encoder_B_old:

this will be an error

error += 1
print 'Error count is %s' %error

elif (Encoder_A == 1 and Encoder_B_old == 0) or (Encoder_A == 0 and Encoder_B_old == 1):

this will be clockwise rotation

counts += 1
print 'Encoder count is %s' %counts
print 'AB is %s %s' % (Encoder_A, Encoder_B)

elif (Encoder_A == 1 and Encoder_B_old == 1) or (Encoder_A == 0 and Encoder_B_old == 0):

this will be counter-clockwise rotation

counts -= 1
print 'Encoder count is %s' %counts
print 'AB is %s %s' % (Encoder_A, Encoder_B)

else:

this will be an error as well

error += 1
print 'Error count is %s' %error

Encoder_A_old = Encoder_A # store the current encoder values as old values to be used as comparison in the next loop
Encoder_B_old = Encoder_B

Initialize the interrupts - these trigger on the both the rising and falling

GPIO.add_event_detect('P8_7', GPIO.BOTH, callback = encodercount) # Encoder A
GPIO.add_event_detect('P8_8', GPIO.BOTH, callback = encodercount) # Encoder B

This is the part of the code which runs normally in the background

while True:
time.sleep(1)```

Evan03Davis

I am new to Pythonista and don't know if there is any way to copy the code from the forum? I tried copy and paste but it won't let me copy the code part. Also is there any good newbie books or tutorials? Thanks and I look forward learning python with your amazing app!

JonB

Hard to say exactly without seeing the whole picture... but have you timed the function, and compared it to how quickly you expect counts to cycle at 1800 rpm?

I am guessing two or four per revoloution? meaning your code needs to run at 120Hz which is sporty for such a device...

ccc

This post is spam to encourage users to go to the kynix website. The question is copy and pasted from http://stackoverflow.com/questions/25439475/faster-quadrature-decoder-loops-with-python-code

JonB

@ccc wow. all of ailee's posts seem to have the same issue -- they randomly link to kynix. Seems this is a common kynix scheme

https://forum.mysensors.org/topic/6090/is-there-an-easier-way-to-compare-the-voltages-of-these-two-sensors/3

omz

I've banned the account. Gotta admit, this is a pretty clever scheme for a spam bot!

I'll keep this thread for reference, but will delete similar things without comment in the future.

dgelessus

Now the spambot just needs to figure out how to post on forums relevant to what it's trying to promote. Then it would almost not be a spambot anymore...

xkcd 810