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

编程入门 行业动态 更新时间:2024-10-08 12:43:09
本文介绍了使用 Python 代码实现更快的正交解码器循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 BeagleBone Black 并使用 Adafruit 的 IO Python 库.编写了一个简单的正交解码函数,当电机以大约 1800 RPM 运行时,它工作得非常好.但是当电机以更高的速度运行时,代码开始丢失一些中断,编码器计数开始累积错误.你们对我如何使代码更高效或者是否有可以以更高频率循环中断的函数有什么建议吗.

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.

谢谢,凯尔

代码如下:

# 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 AB 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 AB 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 次打印一次或稍微减少打印一次.

The greatest benefit will come from the single call of print. Printing to the stdout is slow in general and this will limit the performance of your program. You should consider to print out only every 20th time or somewhat less often.

更多推荐

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

本文发布于:2023-11-29 15:46:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正交   解码器   更快   代码   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!