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
, andSpeedDPM
.
-
class
ev3dev2.motor.
SpeedPercent
(percent)¶ Speed as a percentage of the motor’s maximum rated speed.
-
class
ev3dev2.motor.
SpeedNativeUnits
(native_counts)¶ Speed in tacho counts per second.
-
class
ev3dev2.motor.
SpeedRPS
(rotations_per_second)¶ Speed in rotations-per-second.
-
class
ev3dev2.motor.
SpeedRPM
(rotations_per_minute)¶ Speed in rotations-per-minute.
-
class
ev3dev2.motor.
SpeedDPS
(degrees_per_second)¶ Speed in degrees-per-second.
-
class
ev3dev2.motor.
SpeedDPM
(degrees_per_minute)¶ 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)¶ 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)
isTrue
. The condition is checked when there is an I/O event related to thestate
attribute. Exits early whentimeout
(in milliseconds) is reached.Returns
True
if the condition is met, andFalse
if the timeout is reached.
-
wait_until_not_moving
(timeout=None)¶ Blocks until one of the following conditions are met: -
running
is not inself.state
-stalled
is inself.state
-holding
is inself.state
The condition is checked when there is an I/O event related to thestate
attribute. Exits early whentimeout
(in milliseconds) is reached.Returns
True
if the condition is met, andFalse
if the timeout is reached.Example:
m.wait_until_not_moving()
-
wait_until
(s, timeout=None)¶ Blocks until
s
is inself.state
. The condition is checked when there is an I/O event related to thestate
attribute. Exits early whentimeout
(in milliseconds) is reached.Returns
True
if the condition is met, andFalse
if the timeout is reached.Example:
m.wait_until('stalled')
-
wait_while
(s, timeout=None)¶ Blocks until
s
is not inself.state
. The condition is checked when there is an I/O event related to thestate
attribute. Exits early whentimeout
(in milliseconds) is reached.Returns
True
if the condition is met, andFalse
if the timeout is reached.Example:
m.wait_while('running')
-
on_for_rotations
(speed, rotations, brake=True, block=True)¶ Rotate the motor at
speed
forrotations
speed
can be a percentage or aev3dev2.motor.SpeedValue
object, enabling use of other units.
-
on_for_degrees
(speed, degrees, brake=True, block=True)¶ Rotate the motor at
speed
fordegrees
speed
can be a percentage or aev3dev2.motor.SpeedValue
object, enabling use of other units.
-
on_to_position
(speed, position, brake=True, block=True)¶ Rotate the motor at
speed
toposition
speed
can be a percentage or aev3dev2.motor.SpeedValue
object, enabling use of other units.
-
on_for_seconds
(speed, seconds, brake=True, block=True)¶ Rotate the motor at
speed
forseconds
speed
can be a percentage or aev3dev2.motor.SpeedValue
object, enabling use of other units.
-
on
(speed, brake=True, block=False)¶ Rotate the motor at
speed
for foreverspeed
can be a percentage or aev3dev2.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)¶ 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)¶ 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.
-
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 ofoff
. This is deprecated but helps keep the API for MotorSet somewhat similar to Motor which has bothstop
andoff
.
-
Move Tank¶
-
class
ev3dev2.motor.
MoveTank
(left_motor_port, right_motor_port, desc=None, motor_class=<class 'ev3dev2.motor.LargeMotor'>)¶ 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
andright_speed
forever. Speeds can be percentages or any SpeedValue implementation.
-
Move Steering¶
-
class
ev3dev2.motor.
MoveSteering
(left_motor_port, right_motor_port, desc=None, motor_class=<class 'ev3dev2.motor.LargeMotor'>)¶ 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
forseconds
.
-
on
(steering, speed)¶ Start rotating the motors according to the provided
steering
andspeed
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'>)¶ 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.
-