Skip to content

brian.motors.Motor

brian.motors

Motor class objects

class Motor()

A class to manage and control motor operations.

limits

@property
def limits() -> 'MotorLimits'

Configure various controller limits.

Returns:

MotorLimits object that can be used for configuring the limits.

motor_type

@property
def motor_type() -> 'MotorType'

Check what motor type was this object initialized with.

Returns:

Properties and default settings of the connected motor type.

__init__

def __init__(port: MotorPort)

Tries to autodetect a motor, connected to the given port and initialize a new motor class.

Arguments:

  • port: Motor port to use.

Raises:

  • MotorInitializationFailedError: If autodetect fails (motor is not connected, unknown type of the connected motor).
  • MotorPortAlreadyInUse: When trying to create new Motor on a port that is already in use.

__del__

def __del__()

Release the motor port for other uses.

close_motor

def close_motor()

Release the motor port for other uses.

is_connected

def is_connected() -> bool

Check if something is connected to the port.

Returns:

True if a non-empty port was detected; False otherwise.

is_ready

def is_ready() -> bool

Check if the motor is connected, of the correct type, and ready to be controlled.

Ready-state indicates that the attempt to control the motor will succeed.

When the motor is not connected, this function returns False. When the wrong motor type is connected, this function raises an exception.

Returns:

True if the motor meets the readiness criteria, False if not connected.

Raises:

  • brian.motors.MotorIncompatibleTypeError: If wrong motor type is connected.

wait_until_ready

def wait_until_ready(timeout_ms: Optional[int] = None,
                     optimism_level: Optional[MotorWaitOptimismLevel] = None) -> bool

Waits until the motor is ready. This function is blocking.

Arguments:

  • timeout_ms: Milliseconds to wait for readiness.
    • If None, there is no per-call wall-clock limit (wait until ready).
    • If 0 or negative, return immediately without waiting for readiness.
    • If positive, wait at most that many milliseconds.
  • optimism_level: Level of optimism to use when checking readiness (see MotorWaitOptimismLevel). Optimism level for wait_until_ready() calls.
    • WAIT_UNTIL_CORRECT_TYPE: More optimistic — returns as soon as correct type is detected.
    • WAIT_UNTIL_FULLY_READY: Less optimistic — waits until motor is fully ready (default).

Returns:

True if the motor is ready at the specified optimism level.

Raises:

  • brian.motors.MotorNotConnectedError: If no motor is connected to the port.
  • brian.motors.MotorIncompatibleTypeError: If wrong motor type is connected.
  • brian.motors.MotorIsNotReadyError: If motor is not ready for other reasons.

current_angle

def current_angle() -> int

Query the current motor angle.

Returns:

Motor axle angle in degrees.

reset_angle

def reset_angle(new_value: int = 0) -> None

Set the accumulated angle to the provided position.

Assuming that the motor will not move, current_angle() will start returning the value in newValue.

Arguments:

  • new_value: New motor position in degrees.

current_speed

def current_speed() -> int

Query the current motor rotational speed.

Returns:

Motor axle speed in degrees/second.

current_torque

def current_torque() -> int

Query the current estimated motor torque.

Returns:

Motor torque in milli-newton-meters.

is_stalled

def is_stalled() -> bool

Check if the motor is currently stalled.

Returns:

True if the motor is exceeding some limit, False otherwise.

coast

def coast() -> None

Let the motor spin freely.

This will float the motor windings.

brake

def brake() -> None

Passively brake the motor.

This will short the motor windings.

hold

def hold() -> None

Actively brake the motor at the current position.

This will actively control the motor to stay at the current position.

run_unregulated

def run_unregulated(fraction: float) -> None

Run the motor at a given fraction of the maximum available voltage.

Arguments:

  • fraction: Value between -1.0 and +1.0 that determines the duty cycle.

run_at_voltage

def run_at_voltage(volts: float) -> None

Run the motor at the given voltage.

Arguments:

  • volts: Desired voltage on the motors, in volts. Useful range is -battery voltage to +battery voltage (this is cca. -8V to +8V). The maximum range accepted by this function is -12V to +12V.

run_at_speed

def run_at_speed(deg_per_sec: int) -> None

Run the motor at a constant speed.

Arguments:

  • deg_per_sec: Desired rotational speed, in degrees per second.

rotate_by_angle

def rotate_by_angle(angle: int,
                    speed: int,
                    timeout_ms: Optional[int] = None) -> 'MovementEnd'

Turn the motor to a new position, relative to the current position.

Arguments:

  • angle: Angle to rotate by, in degrees.
  • speed: Speed to use for the maneuver, in degrees per second. If the provided speed is negative, absolute value is used.
  • timeout_ms: How long to wait for the maneuver to complete, in milliseconds. If None, wait until the move completes (no wall-clock cap). If 0 or negative, return from the wait immediately (typically TIMED_OUT if still moving). If the timeout expires, the motor is not stopped.

Returns:

Whether the wait-for-end was successful or why it ended, if it ended early.

rotate_to_angle

def rotate_to_angle(position: int,
                    speed: int,
                    timeout_ms: Optional[int] = None) -> 'MovementEnd'

Turn the motor to a new position, relative to the zero position.

Arguments:

  • position: Angle to rotate to, in degrees.
  • speed: Speed to use for the maneuver, in degrees per second. If the provided speed is negative, absolute value is used.
  • timeout_ms: How long to wait for the maneuver to complete, in milliseconds. If None, wait until the move completes (no wall-clock cap). If 0 or negative, return from the wait immediately (typically TIMED_OUT if still moving). If the timeout expires, the motor is not stopped.

Returns:

Whether the wait-for-end was successful or why it ended, if it ended early.

rotate_to_angle_without_speed_control

def rotate_to_angle_without_speed_control(position: int) -> None

Try to get as fast as possible to the specified position.

This will ignore any speed and acceleration limits - you must provide these yourself by periodically calling this function with new positions.

Arguments:

  • position: Angle to rotate to relative to the zero position, in degrees.

movement_done

def movement_done() -> bool

Check whether the last invoked position command has completed.

Returns:

True if the motor has reached the goal. True if the maneuver had to be interrupted (e.g., motor was unplugged). False if the motor is still moving.

wait_for_movement

def wait_for_movement(timeout_ms: Optional[int] = None) -> 'MovementEnd'

Wait for the motor to complete the last position command.

Arguments:

  • timeout_ms: How long to wait for the maneuver to complete, in milliseconds. If None, wait until the move completes (no wall-clock cap). If 0 or negative, return from the wait immediately (typically TIMED_OUT if still moving). If the timeout expires, the motor is not stopped.

Returns:

Whether the wait-for-end was successful or why it ended, if it ended early.