MotorPair

This class can be used to control pairs of motors. You create MotorPair objects using the pair() method of a Motor object.

class MotorPair

Control two motors simultaneously in a synchronized way.

id()int

Gets the motor pair identifier.

Returns

The motor pair identifier.

primary()Motor

Gets the motor object on which pair was called to create this pair object.

Returns

The primary motor.

secondary()Motor

Gets the motor object that was the parameter in the pair call to create this pair object.

Returns

The secondary motor.

unpair()bool

Uncouples the two motors so they can be used by other pairs.

Returns

True if the operation succeeded, False otherwise.

float()None

Floats (coasts) both motors, as if disconnected from the hub.

brake()None

Passively brakes both motors, as if shorting the motor terminals.

hold()None

Actively holds both motor in their current position.

pwm(pwm_0: int, pwm_1: int)None

Applies PWM signals to the power pins of both motors.

A PWM value of 0 has the same effect as float().

Parameters
  • pwm_0 – PWM value between -100 and +100 for the primary motor. The polarity of the PWM signal matches the sign of the value. A value of 0 stops the PWM signal and leaves the port driver in the floating state.

  • pwm_1 – PWM value for the secondary motor, as above.

run_at_speed(speed_0: int, speed_1: int)None
run_at_speed(speed_0: int, speed_1: int, max_power: int, acceleration: int, deceleration: int)None

Starts running both motor at given speeds.

If a keyword argument is not given, its default value will be used.

Parameters
  • speed_0 – Sets the speed of the primary motor as a percentage of the rated speed for this motor. Positive means clockwise, negative means counterclockwise.

  • speed_1 – Sets the speed of the secondary motor, as above.

Keyword Arguments
  • max_power – Sets percentage of maximum power used during this command.

  • acceleration – The time in milliseconds (0-10000) for a motor to reach maximum rated speed from standstill.

  • deceleration – The time in milliseconds (0-10000) for a motor to stop when starting from the maximum rated speed.

run_for_time(msec: int)None
run_for_time(msec: int, speed_0: int, speed_1: int, max_power: int, acceleration: int, deceleration: int, stop: int)None

Runs both motors for a given amount of time.

If a keyword argument is not given, its default value will be used.

Parameters

msec – How long the motors should run in milliseconds. Negative values will be treated as zero.

Keyword Arguments
  • speed_0 – Sets the speed of the primary motor as a percentage of the rated speed for this motor. Positive means clockwise, negative means counterclockwise.

  • speed_1 – Sets the speed of the secondary motor, as above.

  • max_power – Sets percentage of maximum power used during this command.

  • acceleration – The time in milliseconds (0-10000) for a motor to reach maximum rated speed from standstill.

  • deceleration – The time in milliseconds (0-10000) for a motor to stop when starting from the maximum rated speed.

  • stop – How to stop. Choose type: Choose STOP_FLOAT, STOP_BRAKE, or STOP_HOLD.

run_for_degrees(degrees: int)None
run_for_degrees(degrees: int, speed_0: int, speed_1: int, max_power: int, acceleration: int, deceleration: int, stop: int)None

Runs the motors for the given number of degrees, on average.

If a keyword argument is not given, its default value will be used.

Parameters

degrees – How many degrees to rotate relative to the starting point, on average. The ratio of the speeds will determine how many degrees each motor rotates. The sign of degrees is ignored. The directions are determined by the speed arguments, if given.

Keyword Arguments
  • speed_0 – Sets the speed of the primary motor as a percentage of the rated speed for this motor. Positive means clockwise, negative means counterclockwise.

  • speed_1 – Sets the speed of the secondary motor, as above.

  • max_power – Sets percentage of maximum power used during this command.

  • max_power – Sets percentage of maximum power used during this command.

  • acceleration – The time in milliseconds (0-10000) for a motor to reach maximum rated speed from standstill.

  • deceleration – The time in milliseconds (0-10000) for a motor to stop when starting from the maximum rated speed.

  • stop – How to stop. Choose type: Choose STOP_FLOAT, STOP_BRAKE, or STOP_HOLD.

run_to_position(position_0: int, position_1: int)None
run_to_position(position_0: int, position_1: int, speed: int, max_power: int, acceleration: int, deceleration: int, stop: int)None

Runs both motors to a given angular position.

If a keyword argument is not given, its default value will be used.

Parameters
  • position_0 – Angular position target for the primary motor.

  • position_1 – Angular position target for the secondary motor.

Keyword Arguments
  • speed – Sets the speed of the motor that has to travel the farthest, as a percentage of the rated speed for this motor. The other motor speed will be selected such that both motors arrive at their respective positions at the same time.

  • max_power – Sets percentage of maximum power used during this command.

  • max_power – Sets percentage of maximum power used during this command.

  • acceleration – The time in milliseconds (0-10000) for a motor to reach maximum rated speed from standstill.

  • deceleration – The time in milliseconds (0-10000) for a motor to stop when starting from the maximum rated speed.

  • stop – How to stop. Choose type: Choose STOP_FLOAT, STOP_BRAKE, or STOP_HOLD.

preset(position_0: int, position_1: int)None

Presets the starting positions used by run_to_position.

Parameters
  • position_0 – The new position preset of the primary motor.

  • position_1 – The new position preset of the secondary motor.

callback(function: Callable[[int], None])None

Sets the callback function that is called when a command is completed, interrupted, or stalled.

The function must accept one argument, which indicates why the callback was called. It will receive either EVENT_COMPLETED, EVENT_INTERRUPTED, or EVENT_STALLED.

Parameters

function – Callable function that takes one argument. Choose None to disable the callback.

pid(p: int, i: int, d: int)None

Sets the p, i, and d constants of the MotorPair PID controllers.

Parameters
  • p – Proportional constant.

  • i – Integral constant.

  • d – Derivative constant.