运动#

介绍#

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

每个电机都在“设备”窗口中进行配置。根据版本不同,电机名称可能会有所变化。例如,Competition Advanced 2.0 版本中有一个名为 arm_motor电机。自定义机器人可能会使用不同的电机名称。

默认情况下, FORWARD 会使电机逆时针旋转, REVERSE 会使电机顺时针旋转。如果在“设备”窗口中将电机设置为反转,则旋转方向将会互换。

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

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

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

  • spin — 使电机永远正转或反转。

  • spin_for — 使电机旋转指定的距离。

  • spin_to_position — 将电机旋转到特定位置。

  • stop — 停止电机旋转。

变异器 — 调整电机设置。

  • set_velocity — 告诉电机旋转的速度。

  • set_max_torque — 设置电机在旋转时允许施加的最大扭矩。

  • set_position — 将电机的当前位置更改为新值。

  • set_stopping — 设置电机停止运动的方式:通过制动、滑行或保持。

  • set_timeout — 设置电机尝试完成一次运动的时间。

Getters — 检查电机状态。

  • get_position — 返回电机的当前位置。

  • get_velocity — 返回电机旋转的速度,以 -100% 到 100% 的百分比表示。

  • get_current — 返回电机从电池使用的功率百分比,从 0% 到 100%。

  • is_stopped — 返回电机是否停止运转,以布尔值表示。

  • is_moving — 返回电机是否旋转,以布尔值表示。

行动#

spin#

spin 表示电机无限循环旋转。电机将持续旋转,直到收到其他指令,例如改变旋转方向或停止。

Usage:
arm_motor.spin(direction)

参数

描述

direction

电机旋转方向: FORWARD 或 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 会使电机旋转指定距离。旋转距离是相对于电机当前位置的。程序会等待电机停止旋转后才执行下一行代码。

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

参数

描述

direction

电机旋转方向: FORWARD 或 REVERSE。

angle

电机旋转的距离。0 度 DEGREES 使用整数)。2 圈 TURNS 可以使用整数或小数)。

units

可选。距离单位: DEGREES (默认)或 TURNS。

wait

可选 wait=True (默认值)表示程序会等待电机停止旋转后再运行下一行代码。2 wait=False 表示程序会立即运行下一行代码。

# 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 将电机旋转到特定位置。

电机的位置是指它旋转的距离,以 DEGREES 或 TURNS测量。一圈等于 360 度。在项目开始时,电机位置设置为 0 度。也可以使用 set_position 方法设置电机位置。

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

例如,如果电机从0度开始旋转到720度,它将正转两圈。如果它接着旋转到360度,它将反转一圈,因为360小于720。

Usage:
arm_motor.spin_to_position(angle, units, wait)

参数

描述

angle

电机旋转到的位置值。0 度 DEGREES 使用整数)。2 圈 TURNS 可以使用整数或小数)。

units

可选。位置单位: DEGREES (默认)或 TURNS。

wait

可选 wait=True (默认值)表示程序会等待电机停止旋转后再运行下一行代码。2 wait=False 表示程序会立即运行下一行代码。

# 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 告诉电机旋转的速度。百分比越高,电机旋转速度越快;百分比越低,电机旋转速度越慢。

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

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

Usage:
arm_motor.set_velocity(velocity)

参数

描述

velocity

旋转速度,范围从 0% 到 100%。可以是整数 integerfloat)。

# 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 设置电机允许使用的最大扭矩。

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

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

Usage:
arm_motor.set_max_torque(value)

参数

描述

value

电机可使用的最大扭矩,范围从 0% 到 100%。这可以是整数 integerfloat)。

# 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#

电机的位置是指它旋转的距离,以 DEGREES 或 TURNS测量。 set_position 用于将电机的当前位置更改为新值。

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

Usage:
arm_motor.set_position(position, units)

参数

描述

position

电机位置值设置。0 度 DEGREES 使用整数。2 圈 TURNS 可以使用整数或小数。

units

可选。位置单位: DEGREES (默认)或 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 设置电机停止运动的方式:通过制动、滑行或保持。

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.

例如,如果使用 Competition Advanced 2.0 版本,并将 arm_motor 设置为 HOLD,则电机将保持机械臂在当前位置,直到发生下一次机械臂运动。这意味着,如果电机在机械臂抬起时停止,则电机将克服重力使机械臂保持在空中。

如果不使用此方法,电机停止时将使用 BRAKE。

# 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 设置电机尝试完成一次运动的超时时间。如果电机无法在规定时间内完成,它将停止尝试并执行下一行代码。这可以防止电机卡在某个运动中。

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

可选。时间单位: SECONDS (默认)或 MSEC (毫秒)。

# 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#

电机的位置是指它旋转了多少圈,以 DEGREES 或 TURNS测量。 get_position 返回电机的当前位置。

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

Usage:
arm_motor.get_position(units)

参数

描述

units

可选。电机位置的返回单位: DEGREES (默认)或 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 返回电机旋转的速度,以百分比表示,范围从 -100% 到 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 返回电机从电池使用的电量,以百分比表示,范围从 0% 到 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 返回电机是否停止运转,返回值为布尔值。这可用于根据电机的运转状态控制其他行为的时序。

  • True — 电机停止运转。

  • False ​​— 电机仍在运转。

此方法与以下具有 wait 参数的 Motion 方法配合使用:spin_for 和 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 返回电机是否正在旋转,返回值为布尔值。这可用于根据电机的运动状态控制其他行为的时序。

  • True — 电机正在旋转。

  • False ​​— 电机不转动。

此方法与以下具有 wait 参数的 Motion 方法配合使用:spin_for 和 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)