gpt4 book ai didi

python - 使用 Python 代码实现更快的正交解码器循环

转载 作者:行者123 更新时间:2023-12-01 05:04:00 26 4
gpt4 key购买 nike

我正在使用 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 次或稍低的频率打印一次。

关于python - 使用 Python 代码实现更快的正交解码器循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25439475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com