繁体   English   中英

使用Python代码更快的正交解码器循环

[英]Faster quadrature decoder loops with Python code

我正在使用BeagleBone Black,并使用Adafruit的IO Python库。 编写了一个简单的正交解码功能,当电动机以大约1800 RPM的速度运行时,它可以很好地工作。 但是,当电动机以更高的速度运行时,代码开始缺少某些中断,并且编码器计数开始累积错误。 你们对我如何提高代码效率或是否有一些函数可以使中断以更高的频率循环有任何建议。

谢谢,凯尔

这是代码:

# 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)

使代码更高效...

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

Encoder_A,Encoder_B = GPIO.input('P8_7'),GPIO.input('P8_8')

if ((Encoder_A,Encoder_B_old) == (1,0)) or ((Encoder_A,Encoder_B_old) == (0,1)):
# this will be clockwise rotation
    counts += 1
    print 'Encoder count is %s\nAB is %s %s' % (counts, Encoder_A, Encoder_B)

elif ((Encoder_A,Encoder_B_old) == (1,1)) or ((Encoder_A,Encoder_B_old) == (0,0)):
# this will be counter-clockwise rotation
    counts -= 1
    print 'Encoder count is %s\nAB is %s %s' % (counts, Encoder_A, Encoder_B)

else:
#this will be an error
    error += 1
    print 'Error count is %s' %error

Encoder_A_old,Encoder_B_old = Encoder_A,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)

最大的好处将来自于一次 print 通常,打印到stdout的速度很慢,这将限制程序的性能。 您应该考虑仅每20次打印一次或更少地打印一次。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM