[英]Problem handling interruption with Raspberry Pi 4 with a DC Motor
I have some troubles handling interruptions with my Raspberry Pi 4, using Python.我在使用 Python 处理 Raspberry Pi 4 的中断时遇到了一些麻烦。
I have a DC motor with an encoder , I would like to make a speed control of this motor.我有一个带编码器的直流电机,我想控制这个电机的速度。 But I have some issues with reading the encoder values with my Raspberry.但是我在用我的 Raspberry 读取编码器值时遇到了一些问题。
Here is the code I run :这是我运行的代码:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
HallA = 5 # GPIO pin for Hall A
HallB = 6 # GPIO pin for Hall B
GPIO.setup(HallA, GPIO.IN) #set up the input
GPIO.setup(HallB, GPIO.IN)
incmot = 0 # set up the counter
def encodeur_inc(channel) : #function of the interruption
B = GPIO.input(HallB) #read the second signal
global incmot
if B == 1 :
incmot = incmot +1
else :
incmot = incmot -1
GPIO.add_event_detect(HallA, GPIO.RISING, callback = encodeur_inc) #setting up the interruption
try :
while True :
print(incmot)
except :
GPIO.cleanup()
The problem is that, for the same number of revolutions, I get a different number of pulses each time (from 480 to 650 pulses per revolution, while the manufacturer announces 690).问题是,对于相同的转数,我每次得到不同数量的脉冲(从每转 480 到 650 个脉冲,而制造商宣布为 690)。 I tried to identify where the problem could come from:我试图确定问题可能来自哪里:
GPIO.output(20, GPIO.HIGH) #at the beginning of the function
GPIO.output(20, GPIO.LOW) #at the end of the function
So I don't see where the inconsistencies I see could come from.所以我看不出我看到的不一致来自哪里。 I you have any clue that could help me don't hesitate.我有任何线索可以帮助我不要犹豫。
Thanks for your help !谢谢你的帮助 !
Thanks to @quamrana, I understood where the problem came from.感谢@quamrana,我明白问题出在哪里。 When the program was interrupted, the time taken to execute the interrupt was variable and, going clockwise, Hall B
could be at 1
or 0
, instead of 1
all the time.当程序被中断时,执行中断所花费的时间是可变的,顺时针方向, Hall B
可能是1
或0
,而不是一直是1
。
To get around this problem, using a D latch allows Python time to execute the interrupt and correctly read whether the engine is moving forward or backward.为了解决这个问题,使用 D 锁存器可以让 Python 有时间执行中断并正确读取引擎是向前还是向后移动。 Hall A
is the clock of the latch and Hall B
is the data. Hall A
是锁存器的时钟, Hall B
是数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.