Motor#
Introducción#
Motors and motor groups control how parts of a robot move. A Motor controls one configured IQ Smart Motor, while a MotorGroup controls multiple configured IQ Smart Motors together so they move in tandem. Motors and motor groups can be used to raise an arm, turn a claw, spin a wheel, or move another part of a build. Two motors can work together as a drivetrain to move and turn the whole robot.
Each motor or motor group is configured in the Devices window. Depending on the build, motor and motor group names can change. This page uses motor_1 and motor_group_1 as example names. Replace them with your own configured names as needed.
By default, FORWARD spins a motor counterclockwise, and REVERSE spins a motor clockwise. If a motor is set to reverse, those directions will be switched.
There are many ways to code motors and motor groups. Below is a list of all Motor and MotorGroup methods:
Actions — Stop and spin motors and motor groups.
spin— Spins a motor or motor group forward or reverse forever.spin_for— Spins a motor or motor group for a specified distance.spin_to_position— Spins a motor or motor group to a specific position.stop— Stops a motor or motor group from spinning.
Mutators — Adjust motor and motor group settings.
set_velocity— Tells a motor or motor group how fast to spin.set_max_torque— Sets how hard a motor or motor group is allowed to push while spinning.set_position— Changes the motor or motor group’s current position to a new value.set_stopping— Sets how a motor or motor group will stop moving: by braking, coasting, or holding.set_timeout— Sets how much time a motor or motor group will try to finish a movement.set_reversed— Changes whether a motor’s spin direction is reversed.reset_position— Changes a motor or motor group’s current position to 0.
Getters — Check motor and motor group status.
position— Returns the motor or motor group’s current position.velocity— Returns how fast the motor or motor group is spinning.current— Returns how much electrical current the motor or motor group is using.is_done— Returns whether the motor or motor group is finished moving, as a Boolean value.is_spinning— Returns whether the motor or motor group is spinning, as a Boolean value.direction— Returns the direction the motor or motor group is spinning.power— Returns how much power the motor or motor group is using.torque— Returns how much torque the motor or motor group is using.efficiency— Returns how efficiently the motor or motor group is using power.temperature— Returns the temperature of the motor or motor group.count— Returns the number of motors in a motor group.get_timeout— Returns the motor or motor group’s current timeout duration.
Constructores: Inicialice y configure manualmente los motores y los grupos de motores.
Motor— Creates a motor.MotorGroup— Creates a motor group.
Comportamiento#
spin#
spin spins a motor or motor group forward or reverse forever. The motor or motor group will continue to spin until it is given another action, like spinning in a different direction or stopping.
Usage:
motor_1.spin(direction, velocity, units)
Parámetros |
Descripción |
|---|---|
|
The direction the motor or motor group spins: |
|
Optional. The velocity to spin with from 0% to 100% when using |
|
Optional. The velocity unit:
|
# Spin the motor forward, then stop
motor_1.spin(FORWARD)
wait(1, SECONDS)
motor_1.stop()
spin_for#
spin_for spins a motor or motor group for a specific distance. The spin is relative to the current position of the motor or motor group. The project will wait until the motor or motor group is done spinning before the next line of code runs.
Usage:
motor_1.spin_for(direction, angle, units, velocity, units_v, wait)
Parámetros |
Descripción |
|---|---|
|
The direction the motor or motor group spins: |
|
The distance the motor or motor group spins. This can be an |
|
Optional. The distance unit: |
|
Optional. The velocity to spin with from 0% to 100% when using |
|
Optional. The velocity unit:
|
|
Optional.
|
# Spin the motor forward, then put it
# at 90 degrees
motor_1.spin(FORWARD)
wait(2, SECONDS)
motor_1.spin_to_position(90, DEGREES)
# Spin the motor at different velocities
# Using default velocity
motor_1.spin_for(FORWARD, 150, MM)
wait(1, SECONDS)
# Spin slower
motor_1.spin_for(REVERSE, 150, MM, 20, PERCENT)
wait(1, SECONDS)
# Spin faster
motor_1.spin_for(FORWARD, 150, MM, 100, PERCENT)
spin_to_position#
spin_to_position spins a motor or motor group to a specific position.
A motor or motor group’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. At the beginning of a project, the motor or motor group position is set to 0 degrees. The motor or motor group position can also be set using the set_position method.
Position values are absolute. This means the direction of the spin depends on the motor or motor group’s current position.
For example, if the motor or motor group starts at 0 degrees and spins to a position of 720 degrees, it will spin forward two turns. If it then spins to a position of 360 degrees, it will spin reverse one turn, because 360 is less than 720.
Usage:
motor_1.spin_to_position(rotation, units, velocity, units_v, wait)
Parámetros |
Descripción |
|---|---|
|
The position value the motor or motor group will spin to. This can be an |
|
The position unit: |
|
Optional. The velocity to spin with from 0% to 100% when using |
|
Optional. The velocity unit:
|
|
Optional.
|
# Spin the motor forward, then
# put it at 90 degrees
motor_1.spin(FORWARD)
wait(2, SECONDS)
motor_1.spin_to_position(90, DEGREES)
# Spin forward then quickly reverse
# to 0 degrees
motor_1.spin(FORWARD)
wait(2, SECONDS)
motor_1.spin_to_position(0, DEGREES, 100, PERCENT, wait=True)
stop#
stop stops a motor or motor group from spinning.
Usage:
motor_1.stop(mode)
Parámetros |
Descripción |
|---|---|
|
Optional. How the motor or motor group will stop:
|
# Spin the motor forward, then stop
motor_1.spin(FORWARD)
wait(1, SECONDS)
motor_1.stop()
# Spin the motor and coast to a stop
motor_1.set_velocity(100, PERCENT)
motor_1.spin(FORWARD)
wait(2, SECONDS)
motor_1.stop(COAST)
Mutadores#
set_velocity#
set_velocity tells a motor or motor group how fast to spin. A higher percentage makes the motor or motor group spin faster and a lower percentage makes the motor or motor group spin slower.
Every project begins with each motor or motor group spinning at 50% velocity by default.
Note: A higher velocity makes the motor or motor group spin faster, but it may be less precise. A lower velocity makes the motor or motor group spin slower, but it can be more precise.
Usage:
motor_1.set_velocity(velocity, units)
Parámetros |
Descripción |
|---|---|
|
The velocity to spin with from 0% to 100% when using |
|
Optional. The velocity unit:
|
# Spin the motor at different velocities
# Using default velocity
motor_1.spin_for(FORWARD, 150, MM)
wait(1, SECONDS)
# Spin slower
motor_1.set_velocity(20, PERCENT)
motor_1.spin_for(REVERSE, 150, MM)
wait(1, SECONDS)
# Spin faster
motor_1.set_velocity(100, PERCENT)
motor_1.spin_for(FORWARD, 150, MM)
set_max_torque#
Torque shows how hard a motor or motor group can push or pull while it spins.
set_max_torque sets the most torque a motor or motor group is allowed to use.
A higher percentage lets the motor or motor group push harder, like when lifting a heavy object. A lower percentage limits how hard the motor or motor group can push. This can help protect the robot if the motor or motor group gets stuck or reaches the end of how far it can move.
Every project begins with each motor or motor group’s torque at 50% by default.
Usage:
motor_1.set_max_torque(value, units)
Parámetros |
Descripción |
|---|---|
|
The max torque the motor or motor group can use. This can be an |
|
The torque unit:
|
# Spin the motor at different torque values
# Default
motor_1.spin_to_position(45, DEGREES)
# Spin at a lower torque
motor_1.set_max_torque(20, PERCENT)
motor_1.spin_to_position(0, DEGREES)
# Spin at the max torque
motor_1.set_max_torque(100, PERCENT)
motor_1.spin_to_position(45, DEGREES)
set_position#
A motor or motor group’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. set_position changes the motor or motor group’s current position to a new value.
For example, if a motor or motor group has spun to 180 degrees, setting the position to 0 degrees will reset that position from 180 to 0 degrees. Then the motor or motor group can spin to positions based on that new value.
Usage:
motor_1.set_position(position, units)
Parámetros |
Descripción |
|---|---|
|
The position value to set for the motor or motor group. This can be an |
|
The position unit: |
# Position the motor at the
# new 0 degrees
motor_1.set_position(180, DEGREES)
motor_1.spin_to_position(0)
set_stopping#
set_stopping sets how a motor or motor group will stop moving: by braking, coasting, or holding.
Usage:
motor_1.set_stopping(mode)
Parámetros |
Descripción |
|---|---|
|
How the motor or motor group will stop:
|
# Spin the motor and coast to a stop
motor_1.set_velocity(100, PERCENT)
motor_1.set_stopping(COAST)
motor_1.spin(FORWARD)
wait(2, SECONDS)
motor_1.stop()
set_timeout#
set_timeout sets how much time a motor or motor group will try to finish a movement. If the motor or motor group cannot finish in that time, it will stop trying and move on to the next line of code. This keeps the motor or motor group from getting stuck on a movement.
Usage:
motor_1.set_timeout(value, units)
Parámetros |
Descripción |
|---|---|
|
The amount of time the motor or motor group can try to finish a movement. This can be a positive |
|
Optional. The unit of time:
|
# Set a 0.5 second timeout for motor actions
motor_1.set_timeout(0.5, SECONDS)
# Try a long move that will time out early
motor_1.spin_for(FORWARD, 2, TURNS)
motor_1.spin_to_position(0, DEGREES)
set_reversed#
set_reversed changes whether a motor’s spin direction is reversed. This method works the same as setting the reverse parameter to True when constructing a Motor.
Nota: Este método solo funcionará con un motor y no con un grupo de motores.
Usage:
motor_1.set_reversed(value)
Parámetros |
Descripción |
|---|---|
|
Whether the motor’s direction is reversed:
|
# Change the reverse direction while spinning
motor_1.spin_for(FORWARD, 45)
motor_1.set_reversed(True)
motor_1.spin_for(FORWARD, 45)
reset_position#
reset_position changes the motor or motor group’s current position to 0.
Usage:
motor_1.reset_position()
Parámetros |
Descripción |
|---|---|
Este método no tiene parámetros. |
# Spin to 360 degrees, then reset the encoder
motor_1.spin_to_position(360, DEGREES)
motor_1.reset_position()
# Move again to 90 degrees from new zero point
motor_1.spin_to_position(90, DEGREES)
Captadores#
position#
A motor or motor group’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. position returns the motor or motor group’s current position.
At the beginning of a project, the motor or motor group position is set to 0 degrees. If the motor or motor group spins one turn forward, the position will be 360 degrees or 1 turn. If the motor or motor group spins the other direction, the position will be negative.
Usage:
motor_1.position(units)
Parámetros |
Descripción |
|---|---|
|
Optional. The unit to return the motor or motor group position in: |
# Spin the motor and display the
# ending position
motor_1.spin(FORWARD)
wait(1, SECONDS)
motor_1.stop()
brain.screen.print(motor_1.position())
velocity#
velocity returns how fast the motor or motor group is spinning.
A positive value means the motor or motor group is spinning forward. A negative value means it is spinning in reverse.
Usage:
motor_1.velocity(units)
Parámetros |
Descripción |
|---|---|
|
Optional. The velocity unit to return:
|
current#
current returns how much electrical current the motor or motor group is using, measured in amps from 0.0 to 1.2 A. Current is the amount of electricity flowing through the motor or motor group.
A higher current value means the motor or motor group is using more electrical current. This can happen when the motor or motor group is lifting something heavy, pushing against an object, or trying to move when it is stuck.
This can be used to check if the motor or motor group is struggling during a movement. If current stays high, the motor may get warmer or use power less efficiently.
Usage:
motor_1.current(units)
Parámetros |
Descripción |
|---|---|
|
Optional. The current unit to return:
|
is_done#
is_done returns whether the motor or motor group is finished moving, as a Boolean value. This can be used to control the timing of other behaviors based on the motor’s movement.
True— The motor or motor group is finished moving.False— The motor or motor group is still moving.
This method works together with the following Motion methods that have the wait parameter: spin_for and spin_to_position.
Usage:
motor_1.is_done()
Parámetros |
Descripción |
|---|---|
Este método no tiene parámetros. |
is_spinning#
is_spinning returns whether the motor or motor group is spinning, as a Boolean value. This can be used to control the timing of other behaviors based on the motor’s movement.
True— The motor or motor group is spinning.False— The motor or motor group is not spinning.
This method works together with the following Motion methods that have the wait parameter: spin_for and spin_to_position.
Usage:
motor_1.is_spinning()
Parámetros |
Descripción |
|---|---|
Este método no tiene parámetros. |
direction#
direction returns the current direction a motor or motor group is spinning in.
FORWARDREVERSE
Usage:
motor_1.direction()
Parámetros |
Descripción |
|---|---|
Este método no tiene parámetros. |
power#
power returns how much power the motor or motor group is using in watts. Power shows how quickly the motor or motor group is using energy.
A higher power value means the motor or motor group is using energy faster. This can happen when the motor or motor group is lifting something heavy, pushing against an object, or trying to move when it is stuck.
This can be used to compare movements or check if the motor or motor group is struggling. If power stays high, the motor may get warmer or use energy less efficiently.
Usage:
motor_1.power()
Parámetros |
Descripción |
|---|---|
Este método no tiene parámetros. |
torque#
Torque shows how hard a motor or motor group is twisting, pushing, or pulling while it spins.
torque returns how much torque the motor or motor group is using.
A higher torque value means the motor or motor group is pushing or pulling harder. This can happen when the motor or motor group is lifting something heavy, pushing against an object, or trying to move when it is stuck.
This can be used to check if the motor or motor group is struggling or to compare how much push different movements need.
To set the torque of a motor or motor group, use set_max_torque.
Usage:
motor_1.torque(units)
Parámetros |
Descripción |
|---|---|
|
The torque unit to return:
|
efficiency#
efficiency returns how efficiently the motor or motor group is using power, as a percentage from 0% to 100%.
Efficiency shows how much of the motor or motor group’s power is being used for movement. A higher efficiency value means more of the motor or motor group’s power is being used to move. A lower efficiency value can happen when the motor or motor group is working hard but not moving much, like when it is stuck or pushing against an object.
This can be used to compare movements or check if the motor or motor group is wasting power instead of using it for movement.
Usage:
motor_1.efficiency()
Parámetros |
Descripción |
|---|---|
Este método no tiene parámetros. |
temperature#
temperature returns the temperature of the motor or motor group.
Motor temperature shows how warm the motor or motor group is. A higher temperature means the motor or motor group is getting warmer while it works. The motor should stay below 55 degrees Celsius to keep working at full performance.
If the motor or motor group gets too hot, it will lower its maximum current to protect itself. At 70 degrees Celsius, a motor will stop running until it cools down.
This can be used to check if the motor or motor group is getting too hot during repeated movements, long runs, or when it is pushing against an object.
Usage:
motor_1.temperature(units)
Parámetros |
Descripción |
|---|---|
|
Optional. The temperature unit to return:
|
count#
count returns the number of motors in a motor group.
Nota: Este método solo funcionará con un grupo de motores y no con un motor.
Usage:
motor_group_1.count()
Parámetros |
Descripción |
|---|---|
Este método no tiene parámetros. |
get_timeout#
get_timeout returns the motor or motor group’s current timeout in milliseconds.
Nota: Este método solo funcionará con un motor y no con un grupo de motores.
Usage:
motor_1.get_timeout()
Parámetros |
Descripción |
|---|---|
Este método no tiene parámetros. |
Constructores#
Constructors are used to manually create Motor and MotorGroup objects outside of the Devices window.
Motor#
Motor creates a motor.
Usage:
motor_1 = Motor(smartport, gears, reverse)
Parámetro |
Descripción |
|---|---|
|
The Smart Port that the motor is connected to, written as |
|
Optional. The motor’s gear ratio:
|
|
Optional. Sets whether the motor’s spin direction is reversed:
|
# Create a reversed motor
motor_1 = Motor(Ports.PORT1, 1.0, True)
Motor Group#
MotorGroup creates a motor group.
Usage:
motor_group_1 = MotorGroup(motors)
Parámetro |
Descripción |
|---|---|
|
The names of previously created |
# Create two motors
motor_group_1_motor_a = Motor(Ports.PORT1, False)
motor_group_1_motor_b = Motor(Ports.PORT6, True)
# Create a motor group with those motors
motor_group_1 = MotorGroup(motor_group_1_motor_a, motor_group_1_motor_b)