运动#

介绍#

电机控制机器人各部件的运动。电机可以用来抬起机械臂、转动机械爪、旋转车轮或移动机器人的其他部件。两个电机可以协同工作,组成一个驱动系统,从而驱动整个机器人移动和旋转。

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.

VEX GO 电机,前进方向用加号表示,箭头表示向左(或逆时针)旋转。 VEX GO 电机显示反向,用减号和箭头表示它向右(顺时针)旋转。

电机编程方法有很多种。以下列出了所有运动控制方法:

操作——停止和旋转电机。

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

变异器 — 调整电机设置。

  • 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 — 检查电机状态。

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

位置值是绝对值。这意味着旋转方向取决于电机的当前位置。

例如,如果电机从0度开始旋转到720度,它将正转两圈。如果它接着旋转到360度,它将反转一圈,因为360小于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.

每个项目开始时,每个电机默认以 50% 的速度旋转。

**注意:**更高的速度会使电机转速更快,但精度可能会降低。更低的速度会使电机转速更慢,但精度可能会更高。

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#

扭矩是一种旋转力。它表示电机旋转时能够产生的推力或拉力的大小。

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

较高的百分比可以让电机输出更大的功率,例如在举起重物时。较低的百分比则会限制电机的输出功率。这有助于在电机卡住或达到运动极限时保护机器人。

每个项目开始时,每个电机的扭矩默认设置为 50%。

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.

例如,如果电机旋转了 180 度,将其位置设置为 0 度会将该位置从 180 度重置为 0 度。然后,电机可以根据这个新值旋转到其他位置。

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.

项目开始时,电机位置设置为0度。如果电机正转一圈,则位置为360度或1圈。如果电机反转,则位置为负值。

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

正值表示电机正转,负值表示电机反转。

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

电流值越高,意味着电机消耗的功率越大。这种情况可能发生在电机提升重物、推动物体或试图在卡住时移动时。

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)