added safety timeout for motor control

This commit is contained in:
jpunkt 2021-11-10 13:55:54 +01:00
parent 08d79cdcb8
commit aee4312fc6

View file

@ -126,11 +126,12 @@ def motor_hor(*opts):
_motorctrl(scroll_h, *opts) _motorctrl(scroll_h, *opts)
return 'OK' return 'OK'
def _motorctrl(scroll: Scroll, steps: int, *opts): def _motorctrl(scroll: Scroll, steps: int, timeout_ms: int = 500, *opts):
end = scroll.c + steps end = scroll.c + steps
print(f'motorctrl, end={end}, scroll.c={scroll.c}') print(f'motorctrl, end={end}, scroll.c={scroll.c}')
try: try:
while True: startime = time.ticks_ms()
while time.ticks_diff(startime, time.ticks_ms()) < timeout_ms:
err = end - scroll.c err = end - scroll.c
if abs(err) > 4: if abs(err) > 4:
speed = (err / (2 * steps)) + 0.5 speed = (err / (2 * steps)) + 0.5
@ -138,7 +139,7 @@ def _motorctrl(scroll: Scroll, steps: int, *opts):
else: else:
scroll.speed = 0 scroll.speed = 0
break break
time.sleep_ms(5) time.sleep_us(50)
finally: finally:
scroll.speed = 0 scroll.speed = 0