Skip to content

Stall Detection Error #101

@dragonsrul

Description

@dragonsrul

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 False

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions