运动#

介绍#

Motors control how parts of a robot move. Motors 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 is configured in the Devices window. Depending on the build, motor names can change. For example, the Competition Advanced 2.0 build has a motor named arm_motor. A custom robot may use different motor names.

By default, FORWARD spins a motor counterclockwise, and REVERSE spins a motor clockwise. If a motor is set to reverse in the Devices window, those directions will be switched.

A VEX GO Motor showing the forward direction indicated by a plus sign and an arrow indicating it is rotating left, or counterclockwise. A VEX GO Motor showing the reverse direction indicated by a minus sign and an arrow indicating it is rotating right, or clockwise.

There are many ways to code motors. Below is a list of all Motion methods:

Actions — Stop and spin motors.

  • spin — Spins a motor forward or reverse forever.

  • spin_for — Spins a motor for a specific distance.

  • spin_to_position — Spins a motor to a specific position.

  • stop — Stops a motor from spinning.

Mutators — Adjust motor settings.

  • set_velocity — Tells a motor how fast to spin.

  • set_max_torque — Sets how hard a motor is allowed to push while spinning.

  • set_position — Changes the motor’s current position to a new value.

  • set_stopping — Sets how a motor will stop moving: by braking, coasting, or holding.

  • set_timeout — Sets how much time a motor will try to finish a movement.

Getters — Check motor status.

  • get_position — Returns the motor’s current position.

  • get_velocity — Returns how fast the motor is spinning, as a percentage from -100% to 100%.

  • get_current — Returns how much power the motor is using from the battery, as a percentage from 0% to 100%.

  • is_stopped — Returns whether the motor is finished moving, as a Boolean value.

  • is_moving — Returns whether the motor is spinning, as a Boolean value.

行动#

spin#

spin spins a motor forward or reverse forever. The motor will continue to spin until it is given another action, like spinning in a different direction or stopping.

Usage:
arm_motor.spin(direction)

参数

描述

direction

The direction the motor spins: FORWARD or REVERSE.

# Build Used: Competition Advanced 2.0
def main():
    # Raise the ArmMotor, then stop
    arm_motor.spin(REVERSE)
    wait(1, SECONDS)
    arm_motor.stop()

# Start threads — Do not delete
start_thread(main)

spin_for#

spin_for spins a motor for a specific distance. The spin is relative to the current position of the motor. The project will wait until the motor is done spinning before the next line of code runs.

Usage:
arm_motor.spin_for(direction, angle, units, wait)

参数

描述

direction

The direction the motor spins: FORWARD or REVERSE.

angle

The distance the motor spins. DEGREES use integers. TURNS can use integers or decimals.

units

Optional. The distance unit: DEGREES (default) or TURNS.

wait

Optional. wait=True (default) makes the project wait until the motor is done spinning before the next line of code runs. wait=False makes the next line of code run right away.

# Build Used: Competition Advanced 2.0
def main():
    # Raise and lower the ArmMotor
    arm_motor.spin_for(REVERSE, 180, DEGREES)
    wait(1, SECONDS)
    arm_motor.spin_for(FORWARD, 180, DEGREES)

# Start threads — Do not delete
start_thread(main)

spin_to_position#

spin_to_position spins a motor to a specific position.

A motor’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 position is set to 0 degrees. The motor 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’s current position.

For example, if the motor 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:
arm_motor.spin_to_position(angle, units, wait)

参数

描述

angle

The position value the motor will spin to. DEGREES use integers. TURNS can use integers or decimals.

units

Optional. The position unit: DEGREES (default) or TURNS.

wait

Optional. wait=True (default) makes the project wait until the motor is done spinning before the next line of code runs. wait=False makes the next line of code run right away.

# Build Used: Competition Advanced 2.0
def main():
    # Return ArmMotor to 0 degrees after raising
    arm_motor.spin_for(REVERSE, 180, DEGREES)
    wait(1, SECONDS)
    arm_motor.spin_to_position(0, DEGREES)

# Start threads — Do not delete
start_thread(main)

stop#

stop 停止电机旋转。

Usage:
arm_motor.stop()

参数

描述

该方法没有参数。

# Build Used: Competition Advanced 2.0
def main():
    # Raise the ArmMotor
    arm_motor.spin(REVERSE)
    wait(1, SECONDS)
    arm_motor.stop()

# Start threads — Do not delete
start_thread(main)

修改器#

set_velocity#

set_velocity tells a motor how fast to spin. A higher percentage makes the motor spin faster and a lower percentage makes the motor spin slower.

Every project begins with each motor spinning at 50% velocity by default.

Note: A higher velocity makes the motor spin faster, but it may be less precise. A lower velocity makes the motor spin slower, but it can be more precise.

Usage:
arm_motor.set_velocity(velocity)

参数

描述

velocity

The velocity to spin with from 0% to 100%. This can be an integer or decimal (float).

# Build Used: Competition Advanced 2.0
def main():
    # Move the ArmMotor at different velocities
    arm_motor.spin_for(REVERSE, 180, DEGREES)
    wait(1, SECONDS)
    # Move slow
    arm_motor.set_velocity(20)
    arm_motor.spin_for(FORWARD, 180, DEGREES)
    wait(1, SECONDS)
    # Move fast
    arm_motor.set_velocity(100)
    arm_motor.spin_for(REVERSE, 180, DEGREES)

# Start threads — Do not delete
start_thread(main)

set_max_torque#

Torque is a turning force. It shows how hard a motor can push or pull when it spins.

set_max_torque sets the most torque a motor is allowed to use.

A higher percentage lets the motor push harder, like when lifting a heavy object. A lower percentage limits how hard the motor can push. This can help protect the robot if the motor gets stuck or reaches the end of how far it can move.

Every project begins with each motor’s torque at 50% by default.

Usage:
arm_motor.set_max_torque(value)

参数

描述

value

The max torque the motor can use from 0% to 100%. This can be an integer or decimal (float).

# Build Used: Competition Advanced 2.0
def main():
    # Move the ArmMotor at different torques
    arm_motor.spin_for(REVERSE, 180, DEGREES)
    wait(1, SECONDS)
    # Move with less torque
    arm_motor.set_max_torque(20)
    arm_motor.spin_for(FORWARD, 180, DEGREES)
    wait(1, SECONDS)
    # Move with maximum torque
    arm_motor.set_max_torque(100)
    arm_motor.spin_for(REVERSE, 180, DEGREES)

# Start threads — Do not delete
start_thread(main)

set_position#

A motor’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’s current position to a new value.

For example, if a motor has spun to 180 degrees, setting the position to 0 degrees will reset that position from 180 to 0 degrees. Then the motor can spin to positions based on that new value.

Usage:
arm_motor.set_position(position, units)

参数

描述

position

The position value to set for the motor. DEGREES use integers. TURNS can use integers or decimals.

units

Optional. The position unit: DEGREES (default) or TURNS.

# Build Used: Competition Advanced 2.0
def main():
    # Return ArmMotor to 0 degrees after raising
    arm_motor.set_position(0, DEGREES)
    arm_motor.spin_for(REVERSE, 180, DEGREES)
    wait(1, SECONDS)
    arm_motor.spin_to_position(0, DEGREES)

# Start threads — Do not delete
start_thread(main)

set_stopping#

set_stopping sets how a motor will stop moving: by braking, coasting, or holding.

Usage:
arm_motor.set_stopping(mode)

参数

描述

mode

How the motor will stop:

  • BRAKE — Stops immediately.
  • COAST — Slows to a stop.
  • HOLD — Stops immediately and holds the motor’s position.

For example, if a Competition Advanced 2.0 build is used and arm_motor is set to HOLD, the motor will hold the arm at its current position until another arm movement happens. This means the motor will work against gravity to keep the arm in the air if the motor was stopped with the arm raised.

If this method is not used, the motor will use BRAKE when stopping.

# Build Used: Competition Advanced 2.0
def main():
    # Move the ArmMotor, then coast to a stop
    arm_motor.set_velocity(20)
    arm_motor.set_stopping(COAST)
    arm_motor.spin(REVERSE)
    wait(1, SECONDS)
    arm_motor.stop()

# Start threads — Do not delete
start_thread(main)

set_timeout#

set_timeout sets how much time a motor will try to finish a movement. If the motor cannot finish in that time, it will stop trying and move on to the next line of code. This keeps the motor from getting stuck on a movement.

Usage:
arm_motor.set_timeout(value, units)

参数

描述

value

The amount of time the motor can try to finish a movement. This can be a positive integer or decimal (float).

units

Optional. The unit of time: SECONDS (default) or MSEC (milliseconds).

# Build Used: Competition Advanced 2.0
def main():
    # Move ArmMotor as far as possible for 1 second before resetting
    arm_motor.set_position(0, DEGREES)
    arm_motor.set_timeout(1, SECONDS)
    arm_motor.spin_for(REVERSE, 180, DEGREES)
    arm_motor.spin_to_position(0, DEGREES)

# Start threads — Do not delete
start_thread(main)

吸气剂#

get_position#

A motor’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. get_position returns the motor’s current position.

At the beginning of a project, the motor position is set to 0 degrees. If the motor spins one turn forward, the position will be 360 degrees or 1 turn. If the motor spins the other direction, the position will be negative.

Usage:
arm_motor.get_position(units)

参数

描述

units

Optional. The unit to return the motor position in: DEGREES (default) or TURNS.

# Build Used: Competition Advanced 2.0
def main():
    # Display how far the ArmMotor moves after one second
    arm_motor.set_position(0, DEGREES)
    console.print("Start: ")
    console.print(arm_motor.get_position(DEGREES))
    console.new_line()
    arm_motor.spin(REVERSE)
    wait(1, SECONDS)
    arm_motor.stop()
    console.print("After: ")
    console.print(arm_motor.get_position(DEGREES))

# Start threads — Do not delete
start_thread(main)

get_velocity#

get_velocity returns how fast the motor is spinning, as a percentage from -100% to 100%.

A positive value means the motor is spinning forward. A negative value means the motor is spinning in reverse.

Usage:
arm_motor.get_velocity()

参数

描述

该方法没有参数。

# Build Used: Competition Advanced 2.0
def main():
    # Display the ArmMotor's velocity when it moves
    console.print("Resting: ")
    console.print(arm_motor.get_velocity())
    console.new_line()
    arm_motor.spin(REVERSE)
    wait(1, SECONDS)
    console.print("Moving: ")
    console.print(arm_motor.get_velocity())
    arm_motor.stop()

# Start threads — Do not delete
start_thread(main)

get_current#

get_current returns how much power the motor is using from the battery, as a percentage from 0% to 100%.

A higher current value means the motor is using more power. This can happen when the motor is lifting something heavy, pushing against an object, or trying to move when it is stuck.

Usage:
arm_motor.get_current()

参数

描述

该方法没有参数。

# Build Used: Competition Advanced 2.0
def main():
    # Display the motor's current when it moves
    console.print("Resting: ")
    console.print(arm_motor.get_current())
    console.new_line()
    arm_motor.spin(REVERSE)
    wait(1, SECONDS)
    console.print("Moving: ")
    console.print(arm_motor.get_current())
    arm_motor.stop()

# Start threads — Do not delete
start_thread(main)

is_stopped#

is_stopped returns whether the motor 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 is finished moving.

  • False — The motor is still moving.

This method works together with the following Motion methods that have the wait parameter: spin_for and spin_to_position.

Usage:
arm_motor.is_stopped()

参数

描述

该方法没有参数。

# Build Used: Competition Advanced 2.0
def main():
    # Turn the Eye Sensor's light on while ArmMotor is moving
    arm_motor.spin_for(REVERSE, 180, DEGREES, wait=False)
    wait(0.1, SECONDS)
    while True:
        if arm_motor.is_stopped():
            eye.set_light(OFF)
        else:
            eye.set_light(ON)

# Start threads — Do not delete
start_thread(main)

is_moving#

is_moving returns whether the motor 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 is spinning.

  • False — The motor is not spinning.

This method works together with the following Motion methods that have the wait parameter: spin_for and spin_to_position.

Usage:
arm_motor.is_moving()

参数

描述

该方法没有参数。

# Build Used: Competition Advanced 2.0
def main():
    # Turn the Eye Sensor's light on while ArmMotor is moving
    arm_motor.spin_for(REVERSE, 180, DEGREES, wait=False)
    wait(0.1, SECONDS)
    while True:
        if arm_motor.is_moving():
            eye.set_light(ON)
        else:
            eye.set_light(OFF)

# Start threads — Do not delete
start_thread(main)