Motor classes

Units

Most methods which run motors will accept a speed argument. While this can be provided as an integer which will be interpreted as a percentage of max speed, you can also specify an instance of any of the following classes, each of which represents a different unit system:

class ev3dev2.motor.SpeedValue

A base class for other unit types. Don’t use this directly; instead, see SpeedPercent, SpeedRPS, SpeedRPM, SpeedDPS, and SpeedDPM.

class ev3dev2.motor.SpeedPercent(percent, desc=None)

Speed as a percentage of the motor’s maximum rated speed.

class ev3dev2.motor.SpeedNativeUnits(native_counts, desc=None)

Speed in tacho counts per second.

class ev3dev2.motor.SpeedRPS(rotations_per_second, desc=None)

Speed in rotations-per-second.

class ev3dev2.motor.SpeedRPM(rotations_per_minute, desc=None)

Speed in rotations-per-minute.

class ev3dev2.motor.SpeedDPS(degrees_per_second, desc=None)

Speed in degrees-per-second.

class ev3dev2.motor.SpeedDPM(degrees_per_minute, desc=None)

Speed in degrees-per-minute.

Example:

from ev3dev2.motor import SpeedRPM

# later...

# rotates the motor at 200 RPM (rotations-per-minute) for five seconds.
my_motor.on_for_seconds(SpeedRPM(200), 5)

Common motors

Tacho Motor (Motor)

class ev3dev2.motor.Motor(address=None, name_pattern='*', name_exact=False, **kwargs)

Bases: ev3dev2.Device

The motor class provides a uniform interface for using motors with positional and directional feedback such as the EV3 and NXT motors. This feedback allows for precise control of the motors. This is the most common type of motor, so we just call it motor.

COMMAND_RUN_FOREVER = 'run-forever'

Run the motor until another command is sent.

COMMAND_RUN_TO_ABS_POS = 'run-to-abs-pos'

Run to an absolute position specified by position_sp and then stop using the action specified in stop_action.

COMMAND_RUN_TO_REL_POS = 'run-to-rel-pos'

Run to a position relative to the current position value. The new position will be current position + position_sp. When the new position is reached, the motor will stop using the action specified by stop_action.

COMMAND_RUN_TIMED = 'run-timed'

Run the motor for the amount of time specified in time_sp and then stop the motor using the action specified by stop_action.

COMMAND_RUN_DIRECT = 'run-direct'

Run the motor at the duty cycle specified by duty_cycle_sp. Unlike other run commands, changing duty_cycle_sp while running will take effect immediately.

COMMAND_STOP = 'stop'

Stop any of the run commands before they are complete using the action specified by stop_action.

COMMAND_RESET = 'reset'

Reset all of the motor parameter attributes to their default value. This will also have the effect of stopping the motor.

ENCODER_POLARITY_NORMAL = 'normal'

Sets the normal polarity of the rotary encoder.

ENCODER_POLARITY_INVERSED = 'inversed'

Sets the inversed polarity of the rotary encoder.

POLARITY_NORMAL = 'normal'

With normal polarity, a positive duty cycle will cause the motor to rotate clockwise.

POLARITY_INVERSED = 'inversed'

With inversed polarity, a positive duty cycle will cause the motor to rotate counter-clockwise.

STATE_RUNNING = 'running'

Power is being sent to the motor.

STATE_RAMPING = 'ramping'

The motor is ramping up or down and has not yet reached a constant output level.

STATE_HOLDING = 'holding'

The motor is not turning, but rather attempting to hold a fixed position.

STATE_OVERLOADED = 'overloaded'

The motor is turning, but cannot reach its speed_sp.

STATE_STALLED = 'stalled'

The motor is not turning when it should be.

STOP_ACTION_COAST = 'coast'

Power will be removed from the motor and it will freely coast to a stop.

STOP_ACTION_BRAKE = 'brake'

Power will be removed from the motor and a passive electrical load will be placed on the motor. This is usually done by shorting the motor terminals together. This load will absorb the energy from the rotation of the motors and cause the motor to stop more quickly than coasting.

STOP_ACTION_HOLD = 'hold'

Does not remove power from the motor. Instead it actively try to hold the motor at the current position. If an external force tries to turn the motor, the motor will push back to maintain its position.

address

Returns the name of the port that this motor is connected to.

command

Sends a command to the motor controller. See commands for a list of possible values.

commands

Returns a list of commands that are supported by the motor controller. Possible values are run-forever, run-to-abs-pos, run-to-rel-pos, run-timed, run-direct, stop and reset. Not all commands may be supported.

  • run-forever will cause the motor to run until another command is sent.
  • run-to-abs-pos will run to an absolute position specified by position_sp and then stop using the action specified in stop_action.
  • run-to-rel-pos will run to a position relative to the current position value. The new position will be current position + position_sp. When the new position is reached, the motor will stop using the action specified by stop_action.
  • run-timed will run the motor for the amount of time specified in time_sp and then stop the motor using the action specified by stop_action.
  • run-direct will run the motor at the duty cycle specified by duty_cycle_sp. Unlike other run commands, changing duty_cycle_sp while running will take effect immediately.
  • stop will stop any of the run commands before they are complete using the action specified by stop_action.
  • reset will reset all of the motor parameter attributes to their default value. This will also have the effect of stopping the motor.
count_per_rot

Returns the number of tacho counts in one rotation of the motor. Tacho counts are used by the position and speed attributes, so you can use this value to convert rotations or degrees to tacho counts. (rotation motors only)

count_per_m

Returns the number of tacho counts in one meter of travel of the motor. Tacho counts are used by the position and speed attributes, so you can use this value to convert from distance to tacho counts. (linear motors only)

driver_name

Returns the name of the driver that provides this tacho motor device.

duty_cycle

Returns the current duty cycle of the motor. Units are percent. Values are -100 to 100.

duty_cycle_sp

Writing sets the duty cycle setpoint. Reading returns the current value. Units are in percent. Valid values are -100 to 100. A negative value causes the motor to rotate in reverse.

full_travel_count

Returns the number of tacho counts in the full travel of the motor. When combined with the count_per_m atribute, you can use this value to calculate the maximum travel distance of the motor. (linear motors only)

polarity

Sets the polarity of the motor. With normal polarity, a positive duty cycle will cause the motor to rotate clockwise. With inversed polarity, a positive duty cycle will cause the motor to rotate counter-clockwise. Valid values are normal and inversed.

position

Returns the current position of the motor in pulses of the rotary encoder. When the motor rotates clockwise, the position will increase. Likewise, rotating counter-clockwise causes the position to decrease. Writing will set the position to that value.

position_p

The proportional constant for the position PID.

position_i

The integral constant for the position PID.

position_d

The derivative constant for the position PID.

position_sp

Writing specifies the target position for the run-to-abs-pos and run-to-rel-pos commands. Reading returns the current value. Units are in tacho counts. You can use the value returned by count_per_rot to convert tacho counts to/from rotations or degrees.

max_speed

Returns the maximum value that is accepted by the speed_sp attribute. This may be slightly different than the maximum speed that a particular motor can reach - it’s the maximum theoretical speed.

speed

Returns the current motor speed in tacho counts per second. Note, this is not necessarily degrees (although it is for LEGO motors). Use the count_per_rot attribute to convert this value to RPM or deg/sec.

speed_sp

Writing sets the target speed in tacho counts per second used for all run-* commands except run-direct. Reading returns the current value. A negative value causes the motor to rotate in reverse with the exception of run-to-*-pos commands where the sign is ignored. Use the count_per_rot attribute to convert RPM or deg/sec to tacho counts per second. Use the count_per_m attribute to convert m/s to tacho counts per second.

ramp_up_sp

Writing sets the ramp up setpoint. Reading returns the current value. Units are in milliseconds and must be positive. When set to a non-zero value, the motor speed will increase from 0 to 100% of max_speed over the span of this setpoint. The actual ramp time is the ratio of the difference between the speed_sp and the current speed and max_speed multiplied by ramp_up_sp.

ramp_down_sp

Writing sets the ramp down setpoint. Reading returns the current value. Units are in milliseconds and must be positive. When set to a non-zero value, the motor speed will decrease from 0 to 100% of max_speed over the span of this setpoint. The actual ramp time is the ratio of the difference between the speed_sp and the current speed and max_speed multiplied by ramp_down_sp.

speed_p

The proportional constant for the speed regulation PID.

speed_i

The integral constant for the speed regulation PID.

speed_d

The derivative constant for the speed regulation PID.

state

Reading returns a list of state flags. Possible flags are running, ramping, holding, overloaded and stalled.

stop_action

Reading returns the current stop action. Writing sets the stop action. The value determines the motors behavior when command is set to stop. Also, it determines the motors behavior when a run command completes. See stop_actions for a list of possible values.

stop_actions

Returns a list of stop actions supported by the motor controller. Possible values are coast, brake and hold. coast means that power will be removed from the motor and it will freely coast to a stop. brake means that power will be removed from the motor and a passive electrical load will be placed on the motor. This is usually done by shorting the motor terminals together. This load will absorb the energy from the rotation of the motors and cause the motor to stop more quickly than coasting. hold does not remove power from the motor. Instead it actively tries to hold the motor at the current position. If an external force tries to turn the motor, the motor will ‘push back’ to maintain its position.

time_sp

Writing specifies the amount of time the motor will run when using the run-timed command. Reading returns the current value. Units are in milliseconds.

run_forever(**kwargs)

Run the motor until another command is sent.

run_to_abs_pos(**kwargs)

Run to an absolute position specified by position_sp and then stop using the action specified in stop_action.

run_to_rel_pos(**kwargs)

Run to a position relative to the current position value. The new position will be current position + position_sp. When the new position is reached, the motor will stop using the action specified by stop_action.

run_timed(**kwargs)

Run the motor for the amount of time specified in time_sp and then stop the motor using the action specified by stop_action.

run_direct(**kwargs)

Run the motor at the duty cycle specified by duty_cycle_sp. Unlike other run commands, changing duty_cycle_sp while running will take effect immediately.

stop(**kwargs)

Stop any of the run commands before they are complete using the action specified by stop_action.

reset(**kwargs)

Reset all of the motor parameter attributes to their default value. This will also have the effect of stopping the motor.

is_running

Power is being sent to the motor.

is_ramping

The motor is ramping up or down and has not yet reached a constant output level.

is_holding

The motor is not turning, but rather attempting to hold a fixed position.

is_overloaded

The motor is turning, but cannot reach its speed_sp.

is_stalled

The motor is not turning when it should be.

wait(cond, timeout=None)

Blocks until cond(self.state) is True. The condition is checked when there is an I/O event related to the state attribute. Exits early when timeout (in milliseconds) is reached.

Returns True if the condition is met, and False if the timeout is reached.

wait_until_not_moving(timeout=None)

Blocks until running is not in self.state or stalled is in self.state. The condition is checked when there is an I/O event related to the state attribute. Exits early when timeout (in milliseconds) is reached.

Returns True if the condition is met, and False if the timeout is reached.

Example:

m.wait_until_not_moving()
wait_until(s, timeout=None)

Blocks until s is in self.state. The condition is checked when there is an I/O event related to the state attribute. Exits early when timeout (in milliseconds) is reached.

Returns True if the condition is met, and False if the timeout is reached.

Example:

m.wait_until('stalled')
wait_while(s, timeout=None)

Blocks until s is not in self.state. The condition is checked when there is an I/O event related to the state attribute. Exits early when timeout (in milliseconds) is reached.

Returns True if the condition is met, and False if the timeout is reached.

Example:

m.wait_while('running')
on_for_rotations(speed, rotations, brake=True, block=True)

Rotate the motor at speed for rotations

speed can be a percentage or a ev3dev2.motor.SpeedValue object, enabling use of other units.

on_for_degrees(speed, degrees, brake=True, block=True)

Rotate the motor at speed for degrees

speed can be a percentage or a ev3dev2.motor.SpeedValue object, enabling use of other units.

on_to_position(speed, position, brake=True, block=True)

Rotate the motor at speed to position

speed can be a percentage or a ev3dev2.motor.SpeedValue object, enabling use of other units.

on_for_seconds(speed, seconds, brake=True, block=True)

Rotate the motor at speed for seconds

speed can be a percentage or a ev3dev2.motor.SpeedValue object, enabling use of other units.

on(speed, brake=True, block=False)

Rotate the motor at speed for forever

speed can be a percentage or a ev3dev2.motor.SpeedValue object, enabling use of other units.

Note that block is False by default, this is different from the other on_for_XYZ methods.

Large EV3 Motor

class ev3dev2.motor.LargeMotor(address=None, name_pattern='*', name_exact=False, **kwargs)

Bases: ev3dev2.motor.Motor

EV3/NXT large servo motor.

Same as Motor, except it will only successfully initialize if it finds a “large” motor.

Medium EV3 Motor

class ev3dev2.motor.MediumMotor(address=None, name_pattern='*', name_exact=False, **kwargs)

Bases: ev3dev2.motor.Motor

EV3 medium servo motor.

Same as Motor, except it will only successfully initialize if it finds a “medium” motor.

Additional motors

DC Motor

class ev3dev2.motor.DcMotor(address=None, name_pattern='motor*', name_exact=False, **kwargs)

Bases: ev3dev2.Device

The DC motor class provides a uniform interface for using regular DC motors with no fancy controls or feedback. This includes LEGO MINDSTORMS RCX motors and LEGO Power Functions motors.

address

Returns the name of the port that this motor is connected to.

command

Sets the command for the motor. Possible values are run-forever, run-timed and stop. Not all commands may be supported, so be sure to check the contents of the commands attribute.

commands

Returns a list of commands supported by the motor controller.

driver_name

Returns the name of the motor driver that loaded this device. See the list of [supported devices] for a list of drivers.

duty_cycle

Shows the current duty cycle of the PWM signal sent to the motor. Values are -100 to 100 (-100% to 100%).

duty_cycle_sp

Writing sets the duty cycle setpoint of the PWM signal sent to the motor. Valid values are -100 to 100 (-100% to 100%). Reading returns the current setpoint.

polarity

Sets the polarity of the motor. Valid values are normal and inversed.

ramp_down_sp

Sets the time in milliseconds that it take the motor to ramp down from 100% to 0%. Valid values are 0 to 10000 (10 seconds). Default is 0.

ramp_up_sp

Sets the time in milliseconds that it take the motor to up ramp from 0% to 100%. Valid values are 0 to 10000 (10 seconds). Default is 0.

state

Gets a list of flags indicating the motor status. Possible flags are running and ramping. running indicates that the motor is powered. ramping indicates that the motor has not yet reached the duty_cycle_sp.

stop_action

Sets the stop action that will be used when the motor stops. Read stop_actions to get the list of valid values.

stop_actions

Gets a list of stop actions. Valid values are coast and brake.

time_sp

Writing specifies the amount of time the motor will run when using the run-timed command. Reading returns the current value. Units are in milliseconds.

COMMAND_RUN_FOREVER = 'run-forever'

Run the motor until another command is sent.

COMMAND_RUN_TIMED = 'run-timed'

Run the motor for the amount of time specified in time_sp and then stop the motor using the action specified by stop_action.

COMMAND_RUN_DIRECT = 'run-direct'

Run the motor at the duty cycle specified by duty_cycle_sp. Unlike other run commands, changing duty_cycle_sp while running will take effect immediately.

COMMAND_STOP = 'stop'

Stop any of the run commands before they are complete using the action specified by stop_action.

POLARITY_NORMAL = 'normal'

With normal polarity, a positive duty cycle will cause the motor to rotate clockwise.

POLARITY_INVERSED = 'inversed'

With inversed polarity, a positive duty cycle will cause the motor to rotate counter-clockwise.

STOP_ACTION_COAST = 'coast'

Power will be removed from the motor and it will freely coast to a stop.

STOP_ACTION_BRAKE = 'brake'

Power will be removed from the motor and a passive electrical load will be placed on the motor. This is usually done by shorting the motor terminals together. This load will absorb the energy from the rotation of the motors and cause the motor to stop more quickly than coasting.

run_forever(**kwargs)

Run the motor until another command is sent.

run_timed(**kwargs)

Run the motor for the amount of time specified in time_sp and then stop the motor using the action specified by stop_action.

run_direct(**kwargs)

Run the motor at the duty cycle specified by duty_cycle_sp. Unlike other run commands, changing duty_cycle_sp while running will take effect immediately.

stop(**kwargs)

Stop any of the run commands before they are complete using the action specified by stop_action.

Servo Motor

class ev3dev2.motor.ServoMotor(address=None, name_pattern='motor*', name_exact=False, **kwargs)

Bases: ev3dev2.Device

The servo motor class provides a uniform interface for using hobby type servo motors.

address

Returns the name of the port that this motor is connected to.

command

Sets the command for the servo. Valid values are run and float. Setting to run will cause the servo to be driven to the position_sp set in the position_sp attribute. Setting to float will remove power from the motor.

driver_name

Returns the name of the motor driver that loaded this device. See the list of [supported devices] for a list of drivers.

max_pulse_sp

Used to set the pulse size in milliseconds for the signal that tells the servo to drive to the maximum (clockwise) position_sp. Default value is 2400. Valid values are 2300 to 2700. You must write to the position_sp attribute for changes to this attribute to take effect.

mid_pulse_sp

Used to set the pulse size in milliseconds for the signal that tells the servo to drive to the mid position_sp. Default value is 1500. Valid values are 1300 to 1700. For example, on a 180 degree servo, this would be 90 degrees. On continuous rotation servo, this is the ‘neutral’ position_sp where the motor does not turn. You must write to the position_sp attribute for changes to this attribute to take effect.

min_pulse_sp

Used to set the pulse size in milliseconds for the signal that tells the servo to drive to the miniumum (counter-clockwise) position_sp. Default value is 600. Valid values are 300 to 700. You must write to the position_sp attribute for changes to this attribute to take effect.

polarity

Sets the polarity of the servo. Valid values are normal and inversed. Setting the value to inversed will cause the position_sp value to be inversed. i.e -100 will correspond to max_pulse_sp, and 100 will correspond to min_pulse_sp.

position_sp

Reading returns the current position_sp of the servo. Writing instructs the servo to move to the specified position_sp. Units are percent. Valid values are -100 to 100 (-100% to 100%) where -100 corresponds to min_pulse_sp, 0 corresponds to mid_pulse_sp and 100 corresponds to max_pulse_sp.

rate_sp

Sets the rate_sp at which the servo travels from 0 to 100.0% (half of the full range of the servo). Units are in milliseconds. Example: Setting the rate_sp to 1000 means that it will take a 180 degree servo 2 second to move from 0 to 180 degrees. Note: Some servo controllers may not support this in which case reading and writing will fail with -EOPNOTSUPP. In continuous rotation servos, this value will affect the rate_sp at which the speed ramps up or down.

state

Returns a list of flags indicating the state of the servo. Possible values are: * running: Indicates that the motor is powered.

COMMAND_RUN = 'run'

Drive servo to the position set in the position_sp attribute.

COMMAND_FLOAT = 'float'

Remove power from the motor.

POLARITY_NORMAL = 'normal'

With normal polarity, a positive duty cycle will cause the motor to rotate clockwise.

POLARITY_INVERSED = 'inversed'

With inversed polarity, a positive duty cycle will cause the motor to rotate counter-clockwise.

run(**kwargs)

Drive servo to the position set in the position_sp attribute.

float(**kwargs)

Remove power from the motor.

Actuonix L12 50 Linear Servo Motor

class ev3dev2.motor.ActuonixL1250Motor(address=None, name_pattern='linear*', name_exact=False, **kwargs)

Bases: ev3dev2.motor.Motor

Actuonix L12 50 linear servo motor.

Same as Motor, except it will only successfully initialize if it finds an Actuonix L12 50 linear servo motor

Actuonix L12 100 Linear Servo Motor

class ev3dev2.motor.ActuonixL12100Motor(address=None, name_pattern='linear*', name_exact=False, **kwargs)

Bases: ev3dev2.motor.Motor

Actuonix L12 100 linear servo motor.

Same as Motor, except it will only successfully initialize if it finds an Actuonix L12 100linear servo motor

Multiple-motor groups

Motor Set

class ev3dev2.motor.MotorSet(motor_specs, desc=None)
off(motors=None, brake=True)

Stop motors immediately. Configure motors to brake if brake is set.

stop(motors=None, brake=True)

stop is an alias of off. This is deprecated but helps keep the API for MotorSet somewhat similar to Motor which has both stop and off.

Move Tank

class ev3dev2.motor.MoveTank(left_motor_port, right_motor_port, desc=None, motor_class=<class 'ev3dev2.motor.LargeMotor'>)

Bases: ev3dev2.motor.MotorSet

Controls a pair of motors simultaneously, via individual speed setpoints for each motor.

Example:

tank_drive = MoveTank(OUTPUT_A, OUTPUT_B)
# drive in a turn for 10 rotations of the outer motor
tank_drive.on_for_rotations(50, 75, 10)
on_for_degrees(left_speed, right_speed, degrees, brake=True, block=True)

Rotate the motors at ‘left_speed & right_speed’ for ‘degrees’. Speeds can be percentages or any SpeedValue implementation.

If the left speed is not equal to the right speed (i.e., the robot will turn), the motor on the outside of the turn will rotate for the full degrees while the motor on the inside will have its requested distance calculated according to the expected turn.

on_for_rotations(left_speed, right_speed, rotations, brake=True, block=True)

Rotate the motors at ‘left_speed & right_speed’ for ‘rotations’. Speeds can be percentages or any SpeedValue implementation.

If the left speed is not equal to the right speed (i.e., the robot will turn), the motor on the outside of the turn will rotate for the full rotations while the motor on the inside will have its requested distance calculated according to the expected turn.

on_for_seconds(left_speed, right_speed, seconds, brake=True, block=True)

Rotate the motors at ‘left_speed & right_speed’ for ‘seconds’. Speeds can be percentages or any SpeedValue implementation.

on(left_speed, right_speed)

Start rotating the motors according to left_speed and right_speed forever. Speeds can be percentages or any SpeedValue implementation.

follow_line(kp, ki, kd, speed, target_light_intensity=None, follow_left_edge=True, white=60, off_line_count_max=20, sleep_time=0.01, follow_for=<function follow_for_forever>, **kwargs)

PID line follower

kp, ki, and kd are the PID constants.

speed is the desired speed of the midpoint of the robot

target_light_intensity is the reflected light intensity when the color sensor
is on the edge of the line. If this is None we assume that the color sensor is on the edge of the line and will take a reading to set this variable.

follow_left_edge determines if we follow the left or right edge of the line

white is the reflected_light_intensity that is used to determine if we have
lost the line
off_line_count_max is how many consecutive times through the loop the
reflected_light_intensity must be greater than white before we declare the line lost and raise an exception
sleep_time is how many seconds we sleep on each pass through
the loop. This is to give the robot a chance to react to the new motor settings. This should be something small such as 0.01 (10ms).
follow_for is called to determine if we should keep following the
line or stop. This function will be passed self (the current MoveTank object). Current supported options are: - follow_for_forever - follow_for_ms

**kwargs will be passed to the follow_for function

Example:

from ev3dev2.motor import OUTPUT_A, OUTPUT_B, MoveTank, SpeedPercent, follow_for_ms
from ev3dev2.sensor.lego import ColorSensor

tank = MoveTank(OUTPUT_A, OUTPUT_B)
tank.cs = ColorSensor()

try:
    # Follow the line for 4500ms
    tank.follow_line(
        kp=11.3, ki=0.05, kd=3.2,
        speed=SpeedPercent(30),
        follow_for=follow_for_ms,
        ms=4500
    )
except LineFollowErrorTooFast:
    tank.stop()
    raise
follow_gyro_angle(kp, ki, kd, speed, target_angle=0, sleep_time=0.01, follow_for=<function follow_for_forever>, **kwargs)

PID gyro angle follower

kp, ki, and kd are the PID constants.

speed is the desired speed of the midpoint of the robot

target_angle is the angle we want to maintain

sleep_time is how many seconds we sleep on each pass through
the loop. This is to give the robot a chance to react to the new motor settings. This should be something small such as 0.01 (10ms).
follow_for is called to determine if we should keep following the
desired angle or stop. This function will be passed self (the current MoveTank object). Current supported options are: - follow_for_forever - follow_for_ms

**kwargs will be passed to the follow_for function

Example:

from ev3dev2.motor import OUTPUT_A, OUTPUT_B, MoveTank, SpeedPercent, follow_for_ms
from ev3dev2.sensor.lego import GyroSensor

# Instantiate the MoveTank object
tank = MoveTank(OUTPUT_A, OUTPUT_B)

# Initialize the tank's gyro sensor
tank.gyro = GyroSensor()

# Calibrate the gyro to eliminate drift, and to initialize the current angle as 0
tank.gyro.calibrate()

try:

    # Follow the target_angle for 4500ms
    tank.follow_gyro_angle(
        kp=11.3, ki=0.05, kd=3.2,
        speed=SpeedPercent(30),
        target_angle=0,
        follow_for=follow_for_ms,
        ms=4500
    )
except FollowGyroAngleErrorTooFast:
    tank.stop()
    raise
turn_degrees(speed, target_angle, brake=True, error_margin=2, sleep_time=0.01)

Use a GyroSensor to rotate in place for target_angle

speed is the desired speed of the midpoint of the robot

target_angle is the number of degrees we want to rotate

brake hit the brakes once we reach target_angle

error_margin is the +/- angle threshold to control how accurate the turn should be

sleep_time is how many seconds we sleep on each pass through
the loop. This is to give the robot a chance to react to the new motor settings. This should be something small such as 0.01 (10ms).

Rotate in place for target_degrees at speed

Example:

from ev3dev2.motor import OUTPUT_A, OUTPUT_B, MoveTank, SpeedPercent
from ev3dev2.sensor.lego import GyroSensor

# Instantiate the MoveTank object
tank = MoveTank(OUTPUT_A, OUTPUT_B)

# Initialize the tank's gyro sensor
tank.gyro = GyroSensor()

# Calibrate the gyro to eliminate drift, and to initialize the current angle as 0
tank.gyro.calibrate()

# Pivot 30 degrees
tank.turn_degrees(
    speed=SpeedPercent(5),
    target_angle=30
)
turn_right(speed, degrees, brake=True, error_margin=2, sleep_time=0.01)

Rotate clockwise degrees in place

turn_left(speed, degrees, brake=True, error_margin=2, sleep_time=0.01)

Rotate counter-clockwise degrees in place

Move Steering

class ev3dev2.motor.MoveSteering(left_motor_port, right_motor_port, desc=None, motor_class=<class 'ev3dev2.motor.LargeMotor'>)

Bases: ev3dev2.motor.MoveTank

Controls a pair of motors simultaneously, via a single “steering” value and a speed.

steering [-100, 100]:
  • -100 means turn left on the spot (right motor at 100% forward, left motor at 100% backward),
  • 0 means drive in a straight line, and
  • 100 means turn right on the spot (left motor at 100% forward, right motor at 100% backward).

“steering” can be any number between -100 and 100.

Example:

steering_drive = MoveSteering(OUTPUT_A, OUTPUT_B)
# drive in a turn for 10 rotations of the outer motor
steering_drive.on_for_rotations(-20, SpeedPercent(75), 10)
on_for_rotations(steering, speed, rotations, brake=True, block=True)

Rotate the motors according to the provided steering.

The distance each motor will travel follows the rules of MoveTank.on_for_rotations().

on_for_degrees(steering, speed, degrees, brake=True, block=True)

Rotate the motors according to the provided steering.

The distance each motor will travel follows the rules of MoveTank.on_for_degrees().

on_for_seconds(steering, speed, seconds, brake=True, block=True)

Rotate the motors according to the provided steering for seconds.

on(steering, speed)

Start rotating the motors according to the provided steering and speed forever.

get_speed_steering(steering, speed)

Calculate the speed_sp for each motor in a pair to achieve the specified steering. Note that calling this function alone will not make the motors move, it only calculates the speed. A run_* function must be called afterwards to make the motors move.

steering [-100, 100]:
  • -100 means turn left on the spot (right motor at 100% forward, left motor at 100% backward),
  • 0 means drive in a straight line, and
  • 100 means turn right on the spot (left motor at 100% forward, right motor at 100% backward).
speed:
The speed that should be applied to the outmost motor (the one rotating faster). The speed of the other motor will be computed automatically.

Move Joystick

class ev3dev2.motor.MoveJoystick(left_motor_port, right_motor_port, desc=None, motor_class=<class 'ev3dev2.motor.LargeMotor'>)

Bases: ev3dev2.motor.MoveTank

Used to control a pair of motors via a single joystick vector.

on(x, y, radius=100.0)

Convert x,``y`` joystick coordinates to left/right motor speed percentages and move the motors.

This will use a classic “arcade drive” algorithm: a full-forward joystick goes straight forward and likewise for full-backward. Pushing the joystick all the way to one side will make it turn on the spot in that direction. Positions in the middle will control how fast the vehicle moves and how sharply it turns.

x, y:
The X and Y coordinates of the joystick’s position, with (0,0) representing the center position. X is horizontal and Y is vertical.
radius (default 100):
The radius of the joystick, controlling the range of the input (x, y) values. e.g. if “x” and “y” can be between -1 and 1, radius should be set to “1”.
static angle_to_speed_percentage(angle)

The following graphic illustrates the motor power outputs for the left and right motors based on where the joystick is pointing, of the form (left power, right power):

                         (1, 1)
                      . . . . . . .
                   .        |        .
                .           |           .
       (0, 1) .             |             . (1, 0)
            .               |               .
           .                |                 .
          .                 |                  .
         .                  |                   .
        .                   |                   .
        .                   |     x-axis        .
(-1, 1) .---------------------------------------. (1, -1)
        .                   |                   .
        .                   |                   .
         .                  |                  .
          .                 | y-axis          .
            .               |               .
      (0, -1) .             |             . (-1, 0)
                .           |           .
                   .        |        .
                      . . . . . . .
                         (-1, -1)

The joystick is a circle within a circle where the (x, y) coordinates of the joystick form an angle with the x-axis. Our job is to translate this angle into the percentage of power that should be sent to each motor. For instance if the joystick is moved all the way to the top of the circle we want both motors to move forward with 100% power…that is represented above by (1, 1). If the joystick is moved all the way to the right side of the circle we want to rotate clockwise so we move the left motor forward 100% and the right motor backwards 100%…so (1, -1). If the joystick is at 45 degrees then we move apply (1, 0) to move the left motor forward 100% and the right motor stays still.

The 8 points shown above are pretty easy. For the points in between those 8 we do some math to figure out what the percentages should be. Take 11.25 degrees for example. We look at how the motors transition from 0 degrees to 45 degrees: - the left motor is 1 so that is easy - the right motor moves from -1 to 0

We determine how far we are between 0 and 45 degrees (11.25 is 25% of 45) so we know that the right motor should be 25% of the way from -1 to 0…so -0.75 is the percentage for the right motor at 11.25 degrees.

Move Differential

class ev3dev2.motor.MoveDifferential(left_motor_port, right_motor_port, wheel_class, wheel_distance_mm, desc=None, motor_class=<class 'ev3dev2.motor.LargeMotor'>)

Bases: ev3dev2.motor.MoveTank

MoveDifferential is a child of MoveTank that adds the following capabilities:

  • drive in a straight line for a specified distance
  • rotate in place in a circle (clockwise or counter clockwise) for a specified number of degrees
  • drive in an arc (clockwise or counter clockwise) of a specified radius for a specified distance

Odometry can be use to enable driving to specific coordinates and rotating to a specific angle.

New arguments:

wheel_class - Typically a child class of ev3dev2.wheel.Wheel. This is used to get the circumference of the wheels of the robot. The circumference is needed for several calculations in this class.

wheel_distance_mm - The distance between the mid point of the two wheels of the robot. You may need to do some test drives to find the correct value for your robot. It is not as simple as measuring the distance between the midpoints of the two wheels. The weight of the robot, center of gravity, etc come into play.

You can use utils/move_differential.py to call on_arc_left() to do some test drives of circles with a radius of 200mm. Adjust your wheel_distance_mm until your robot can drive in a perfect circle and stop exactly where it started. It does not have to be a circle with a radius of 200mm, you can test with any size circle but you do not want it to be too small or it will be difficult to test small adjustments to wheel_distance_mm.

Example:

from ev3dev2.motor import OUTPUT_A, OUTPUT_B, MoveDifferential, SpeedRPM
from ev3dev2.wheel import EV3Tire

STUD_MM = 8

# test with a robot that:
# - uses the standard wheels known as EV3Tire
# - wheels are 16 studs apart
mdiff = MoveDifferential(OUTPUT_A, OUTPUT_B, EV3Tire, 16 * STUD_MM)

# Rotate 90 degrees clockwise
mdiff.turn_right(SpeedRPM(40), 90)

# Drive forward 500 mm
mdiff.on_for_distance(SpeedRPM(40), 500)

# Drive in arc to the right along an imaginary circle of radius 150 mm.
# Drive for 700 mm around this imaginary circle.
mdiff.on_arc_right(SpeedRPM(80), 150, 700)

# Enable odometry
mdiff.odometry_start()

# Use odometry to drive to specific coordinates
mdiff.on_to_coordinates(SpeedRPM(40), 300, 300)

# Use odometry to go back to where we started
mdiff.on_to_coordinates(SpeedRPM(40), 0, 0)

# Use odometry to rotate in place to 90 degrees
mdiff.turn_to_angle(SpeedRPM(40), 90)

# Disable odometry
mdiff.odometry_stop()
on_for_distance(speed, distance_mm, brake=True, block=True)

Drive in a straight line for distance_mm

on_arc_right(speed, radius_mm, distance_mm, brake=True, block=True)

Drive clockwise in a circle with ‘radius_mm’ for ‘distance_mm’

on_arc_left(speed, radius_mm, distance_mm, brake=True, block=True)

Drive counter-clockwise in a circle with ‘radius_mm’ for ‘distance_mm’

turn_degrees(speed, degrees, brake=True, block=True, error_margin=2, use_gyro=False)

Rotate in place degrees. Both wheels must turn at the same speed for us to rotate in place. If the following conditions are met the GryoSensor will be used to improve the accuracy of our turn: - use_gyro, brake and block are all True - A GyroSensor has been defined via self.gyro = GyroSensor()

turn_right(speed, degrees, brake=True, block=True, error_margin=2, use_gyro=False)

Rotate clockwise degrees in place

turn_left(speed, degrees, brake=True, block=True, error_margin=2, use_gyro=False)

Rotate counter-clockwise degrees in place

turn_to_angle(speed, angle_target_degrees, brake=True, block=True, error_margin=2, use_gyro=False)

Rotate in place to angle_target_degrees at speed

odometry_start(theta_degrees_start=90.0, x_pos_start=0.0, y_pos_start=0.0, sleep_time=0.005)

Ported from: http://seattlerobotics.org/encoder/200610/Article3/IMU%20Odometry,%20by%20David%20Anderson.htm

A thread is started that will run until the user calls odometry_stop() which will set odometry_thread_run to False

odometry_stop()

Signal the odometry thread to exit

on_to_coordinates(speed, x_target_mm, y_target_mm, brake=True, block=True)

Drive to (x_target_mm, y_target_mm) coordinates at speed