-
-
Notifications
You must be signed in to change notification settings - Fork 45
Open
Description
Hi, I am on a robotics team, and I am trying to use stall detection. My code is running, but its not detecting the stall and is still running. Here is the code, and could you help us?
from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor, ForceSensor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.tools import wait, StopWatch
hub = PrimeHub()
arm_motor = Motor(Port.D)
def run_until_stalled(motor, speed, timeout=1000, check_interval=50):
"""
Run a motor until it stalls or until no movement is detected for timeout ms.
Args:
motor: The motor to run.
speed: Speed to run at (positive or negative).
timeout: Milliseconds without movement before stall is detected.
check_interval: How often to check the motor angle.
Returns:
True if stalled, False if timed out.
"""
motor.run(speed)
last_angle = motor.angle()
timer = StopWatch()
while True:
wait(check_interval)
angle = motor.angle()
if angle != last_angle:
last_angle = angle
timer.reset()
else:
if timer.time() >= timeout:
motor.stop()
return True
# Optional safety timeout to avoid infinite loops
if timer.time() > timeout * 5:
motor.stop()
return FalseReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels