Motor classes

Tacho motor

class ev3dev.core.Motor(address=None, name_pattern='*', name_exact=False, **kwargs)

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.

The way to configure a motor is to set the ‘_sp’ attributes when calling a command or before. Only in ‘run_direct’ mode attribute changes are processed immediately, in the other modes they only take place when a new command is issued.

COMMAND_RESET = 'reset'

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

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_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_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_STOP = 'stop'

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

ENCODER_POLARITY_INVERSED = 'inversed'

Sets the inversed polarity of the rotary encoder.

ENCODER_POLARITY_NORMAL = 'normal'

Sets the normal polarity of the rotary encoder.

POLARITY_INVERSED = 'inversed'

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

POLARITY_NORMAL = 'normal'

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

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_RAMPING = 'ramping'

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

STATE_RUNNING = 'running'

Power is being sent to the motor.

STATE_STALLED = 'stalled'

The motor is not turning when it should be.

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_COAST = 'coast'

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

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_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)

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)

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)

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_ramping

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

is_running

Power is being sent to the motor.

is_stalled

The motor is not turning when it should be.

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.

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_d

The derivative constant for the position PID.

position_i

The integral constant for the position PID.

position_p

The proportional 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 counts_per_rot to convert tacho counts to/from rotations or degrees.

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.

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.

reset(**kwargs)

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

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.

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_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.

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_d

The derivative constant for the speed regulation PID.

speed_i

The integral constant for the speed regulation PID.

speed_p

The proportional constant for the speed regulation PID.

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.

state

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

stop(**kwargs)

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

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.

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(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_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_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')

Large EV3 Motor

class ev3dev.core.LargeMotor(address=None, name_pattern='*', name_exact=False, **kwargs)

Bases: ev3dev.core.Motor

EV3/NXT large servo motor

Medium EV3 Motor

class ev3dev.core.MediumMotor(address=None, name_pattern='*', name_exact=False, **kwargs)

Bases: ev3dev.core.Motor

EV3 medium servo motor

DC Motor

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

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.

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_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_STOP = 'stop'

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

POLARITY_INVERSED = 'inversed'

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

POLARITY_NORMAL = 'normal'

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

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_COAST = 'coast'

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

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.

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.

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.

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(**kwargs)

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

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.

Servo Motor

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

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

COMMAND_FLOAT = 'float'

Remove power from the motor.

COMMAND_RUN = 'run'

Drive servo to the position set in the position_sp attribute.

POLARITY_INVERSED = 'inversed'

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

POLARITY_NORMAL = 'normal'

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

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.

float(**kwargs)

Remove power from the motor.

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.

run(**kwargs)

Drive servo to the position set in the position_sp attribute.

state

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