运动#

介绍#

VEX AIM 编码机器人采用全向驱动系统,可以前进、后退、左转、右转或以一定角度移动,并能独立转向。运动控制方法用于控制机器人的移动和转向方式、移动速度以及其 x 轴和 y 轴位置的跟踪。

圆形罗盘刻度盘,刻度范围为 0 到 315 度,环绕着深灰色显示屏和深蓝色屏幕。机器人的正面朝向罗盘刻度盘上的 0 度。

以下是可用方法的列表:

动作——移动和转向机器人。

  • move_at — Moves the robot at a specific angle forever.

  • move_for — Moves the robot at a specific angle for a specific distance.

  • move_with_vectors — Moves the robot using vector-based x, y, and rotation values.

  • turn — Turns the robot left or right forever.

  • turn_for — Turns the robot left or right for a specific number of degrees.

  • turn_to — Turns the robot to face a specific heading.

  • stop_all_movement — Stops all movement of the robot.

变异器——设置机器人移动值。

获取器 — 返回机器人状态和位置。

行动#

move_at#

move_at moves the robot forever at a specific angle. The angle is relative to the current position of the robot. The robot will continue to move until it is given another action, like moving at a different angle, turning, or stopping.

用法:

robot.move_at(angle, velocity, units)

参数

描述

angle

机器人移动的角度,以度为单位。该值可以是介于 -360 到 360 之间的整数或小数。

velocity

Optional. The velocity to move with from 0% to 100% when using PERCENT, or from 0 to 200 millimeters per second when using MMPS. This can be an integer or decimal (float). If no velocity is provided, the robot moves at the current move velocity.

units

Optional. The velocity unit: PERCENT (default) or MMPS (millimeters per second).

# Move right, then move forward and stop.
robot.move_at(90)
wait(1,SECONDS)
robot.move_at(0)
wait(1,SECONDS)
robot.stop_all_movement()

# Move right slowly, move in reverse quickly and stop.
robot.move_at(90, 25)
wait(2, SECONDS)
robot.move_at(180, 100, PERCENT)
wait(1, SECONDS)
robot.stop_all_movement()

# Move diagonally to the right and stop.
robot.move_at(45.33)
wait(1,SECONDS)
robot.stop_all_movement()

move_for#

move_for moves the robot at a specific angle for a specific distance. The angle is relative to the current position of the robot. The project will wait until the robot is done moving before the next line of code runs.

用法:

robot.move_for(distance, angle, velocity, units, wait)

参数

描述

distance

机器人移动的距离,单位为毫米(mm)。可以是整数或浮点数(十进制数)。

angle

机器人移动的角度,以度为单位。该值可以是介于 -360 到 360 之间的整数或小数。

velocity

Optional. The velocity to move with from 0% to 100% when using PERCENT, or from 0 to 200 millimeters per second when using MMPS. This can be an integer or decimal (float). If no velocity is provided, the robot moves at the current move velocity.

units

Optional. The velocity unit: PERCENT (default) or MMPS (millimeters per second).

wait

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

# Move right, then move forward.
robot.move_for(50, 90)
robot.move_for(100, 0)

# Move in reverse slowly, then move forward quickly.
robot.move_for(100, 180, 25)
robot.move_for(100, 0, 100, PERCENT)

# Drive forward and blink all LEDs red.
robot.move_for(100, 0, wait=False)
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, BLACK)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, BLACK)

move_with_vectors#

A vector is a way to describe how fast something moves, and what direction it moves in. move_with_vectors moves the robot using vector-based motion. The robot combines forward, rightward, and rotation velocity values to create on smooth movement. The robot will continue to move using this vector until it is given another action, like moving at a different angle, turning, or stopping.

Note: This method sets movement and turn velocity values directly, so it does not use set_move_velocity or set_turn_velocity.

用法:

robot.move_with_vectors(forward, rightward, rotation)

参数

描述

forward

机器人沿y轴方向的速度,范围从-100%到100%。负值使机器人后退,正值使机器人前进。

rightward

机器人沿 x 轴的速度,范围从 -100% 到 100%。负值使机器人向左移动,正值使机器人向右移动。

rotation

机器人的转弯速度,范围从-100%到100%。负值使机器人逆时针旋转,正值使机器人顺时针旋转。

# Move at 15°
robot.move_with_vectors(48.3, 12.95, 0)
wait(2, SECONDS)
robot.stop_all_movement()

# Move at 45° while turning counterclockwise.
robot.move_with_vectors(50, 50, -30)
wait(2, SECONDS)
robot.stop_all_movement()

# Scaling movement using the controller joystick
while True:
    # Move forward (and backwards) slower than axis1 input
    forward   = controller.axis1.position() * 0.7
    
    # Move right (and left) faster than axis2 input
    rightward = controller.axis2.position() * 1.5    

    robot.move_with_vectors(forward, rightward, 0)

    wait(5, MSEC)

turn#

turn turns the robot left or right forever. The robot will continue to turn until it is given another action, like moving or stopping.

用法:

robot.turn(direction, velocity, units)

参数

描述

direction

The direction the robot turns: LEFT or RIGHT.

velocity

Optional. The velocity to turn with from 0% to 100% when using PERCENT, or from 0 to 150 degrees per second when using DPS. This can be an integer or decimal (float). If no velocity is provided, the robot turns at the current turn velocity.

units

Optional. The velocity unit: DPS (degrees per second, default) or PERCENT.

# Turn left, then stop.
robot.turn(LEFT)
wait(1, SECONDS)
robot.stop_all_movement()

# Turn left quickly, turn right slowly, then stop.
robot.turn(LEFT, 80)
wait(2, SECONDS)
robot.turn(RIGHT, 20, PERCENT)
wait(3, SECONDS)
robot.stop_all_movement()

turn_for#

turn_for turns the robot left or right for a specific number of degrees. The turn is relative to the current direction the robot is facing. The project will wait until the robot is done turning before the next line of code runs.

用法:

robot.turn_for(direction, angle, velocity, units, wait)

参数

描述

direction

The direction the robot turns: LEFT or RIGHT.

angle

机器人旋转的角度(以度为单位),范围从 -360 度到 360 度。该值可以是整数或十进制数(浮点数)。

velocity

Optional. The velocity to turn with from 0% to 100% when using PERCENT, or from 0 to 150 degrees per second when using DPS. This can be an integer or decimal (float). If no velocity is provided, the robot turns at the current turn velocity.

units

Optional. The velocity unit: DPS (degrees per second, default) or PERCENT.

wait

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

# Turn left, then turn around to the right.
robot.turn_for(LEFT, 90)
robot.turn_for(RIGHT, 180)

# Turn left quickly, then turn right slowly.
robot.turn_for(LEFT, 90, 100)
robot.turn_for(RIGHT, 180, 15, PERCENT)

# Turn right and blink all LEDs blue.
robot.turn_for(RIGHT, 180, wait=False)
robot.led.on(ALL_LEDS, BLUE)
wait(0.5, SECONDS)
robot.led.off(ALL_LEDS)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, BLUE)
wait(0.5, SECONDS)
robot.led.off(ALL_LEDS)

turn_to#

A heading is the direction the robot is facing, measured in degrees. turn_to turns the robot to face a specific heading. The robot will turn the shortest direction to reach the target heading.

机器人的初始航向角为0度。

程序会等到机器人完成转向后才运行下一行代码。

用法:

robot.turn_to(heading, velocity, units, wait)

参数

描述

heading

机器人应面向的方向,范围从 -360 度到 360 度。

velocity

Optional. The velocity to turn with from 0% to 100% when using PERCENT, or from 0 to 150 degrees per second when using DPS. This can be an integer or decimal (float). If no velocity is provided, the robot turns at the current turn velocity.

units

Optional. The velocity unit: DPS (degrees per second, default) or PERCENT.

wait

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

# Turn to face each cardinal directions.
robot.turn_to(90)
wait(2, SECONDS)
robot.turn_to(180)
wait(2, SECONDS)
robot.turn_to(270)
wait(2, SECONDS)
robot.turn_to(0)

# Turn to face backward slowly, then face forward quickly.
robot.turn_to(180, 25)
wait(1, SECONDS)
robot.turn_to(0, 90, PERCENT)

# Turn around quickly and blink all LEDs green.
robot.turn_to(180, 100, wait=False)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
robot.led.off(ALL_LEDS)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
robot.led.off(ALL_LEDS)

stop_all_movement#

stop_all_movement stops all movement of the robot.

用法:

robot.stop_all_movement()

参数

描述

该方法没有参数。

# Turn right, then stop moving
robot.turn(RIGHT)
wait(1, SECONDS)
robot.stop_all_movement()

修改器#

set_move_velocity#

set_move_velocity tells the robot how fast to move. A higher percentage makes the robot move faster and a lower percentage makes the robot move slower.

每个项目开始时,机器人默认以 50% 的速度移动。

100% 的移动速度相当于每秒 200 毫米。

用法:

robot.set_move_velocity(velocity, units)

参数

描述

velocity

The velocity to move with from 0% to 100% when using PERCENT, or from 0 to 200 millimeters per second when using MMPS. This can be an integer or decimal (float).

units

Optional. The velocity unit: PERCENT (default) or MMPS (millimeters per second).

# Move forward at the default velocity,
robot.set_move_velocity(50)
robot.move_for(100, 0)
wait(1, SECONDS)
# Move slower than the default velocity
robot.set_move_velocity(20)
robot.move_for(100, 0)
wait(1, SECONDS)
# Move faster than the default velocity
robot.set_move_velocity(100)
robot.move_for(100, 0)

set_turn_velocity#

set_turn_velocity tells the robot how fast to turn. A higher percentage makes the robot turn faster and a lower percentage makes the robot turn slower.

每个项目开始时,机器人默认以 50% 的速度(每秒 75 度)旋转。

用法:

robot.set_turn_velocity(velocity, units)

参数

描述

velocity

The velocity to turn with from 0% to 100% when using PERCENT, or from 0 to 150 degrees per second when using DPS. This can be an integer or decimal (float).

units

Optional. The velocity unit: DPS (degrees per second, default) or PERCENT.

# Turn around at default velocity
robot.set_turn_velocity(50)
robot.turn_for(RIGHT, 180)
wait(1, SECONDS)
# Turn around slower than the default velocity
robot.set_turn_velocity(20)
robot.turn_for(RIGHT, 180)
wait(1, SECONDS)
# Turn around faster than the default velocity
robot.set_turn_velocity(100)
robot.turn_for(RIGHT, 180)

set_xy_position#

set_xy_position changes the robot’s current x and y position to new values.

例如,如果机器人偏离了起始位置,将 x 和 y 都设置为 0,则机器人的当前位置将变为新的 (0, 0) 位置。然后,机器人可以根据这个新值跟踪未来的位置。

用法:

robot.set_xy_position(x, y)

参数

描述

x

要设置的机器人 x 轴位置值,单位为毫米。可以是整数或十进制数(浮点数)。

y

要设置的机器人y轴位置值,单位为毫米。可以是整数或十进制数(浮点数)。

# Set the robot's current position
# Move forward and print the new coordinate
robot.set_xy_position(100, 50)
robot.move_for(150, 0)
robot.screen.print("X:", robot.get_x_position())
robot.screen.next_row()
robot.screen.print("Y:", robot.get_y_position())

吸气剂#

get_x_position#

get_x_position returns the robot’s current x-coordinate in millimeters.

At the beginning of a project, the robot’s x-position is set to 0. The x-position changes as the robot moves left or right and can be set using the set_xy_position method.

用法:

robot.get_x_position()

参数

描述

该方法没有参数。

# Print the start and end x-value 
# of the robot's position after moving
robot.screen.print("Start X:", robot.get_x_position())
robot.move_for(200, 90)
robot.screen.next_row()
robot.screen.print("End X:", robot.get_x_position())

get_y_position#

get_y_position returns the robot’s current y-coordinate in millimeters.

At the beginning of a project, the robot’s y-position is set to 0. The y-position changes as the robot moves forward or reverse and can be set using the set_xy_position method.

用法:

robot.get_y_position()

参数

描述

该方法没有参数。

# Print the start and end y-value 
# of the robot's position after moving
robot.screen.print("Start Y:", robot.get_y_position())
robot.move_for(200, 0)
robot.screen.next_row()
robot.screen.print("End Y:", robot.get_y_position())

is_move_active#

is_move_active returns whether the robot is moving, as a Boolean value. This can be used to control the timing of other behaviors based on the robot’s movement.

  • True — The robot is moving.

  • False — The robot is not moving.

This method works together with Motion methods that have the wait parameter, such as move_for.

用法:

robot.is_move_active()

参数

描述

该方法没有参数。

# Blink all the LEDs when the robot is moving.
robot.move_for(200, 0, wait=False)

while robot.is_move_active():
    robot.led.on(ALL_LEDS, ORANGE)
    wait(0.5, SECONDS)
    robot.led.on(ALL_LEDS, CYAN)
    wait(0.5, SECONDS)

robot.led.off(ALL_LEDS)

is_turn_active#

is_turn_active returns whether the robot is turning, as a Boolean value. This can be used to control the timing of other behaviors based on the robot’s movement.

  • True — The robot is turning.

  • False — The robot is not turning.

This method works together with Motion methods that have the wait parameter, such as turn_for and turn_to.

用法:

robot.is_turn_active()

参数

描述

该方法没有参数。

# Blink all the LEDs while the robot is turning
robot.turn_for(RIGHT, 180, wait=False) 

while robot.is_turn_active():
    robot.led.on(ALL_LEDS, GREEN)
    wait(0.5, SECONDS)
    robot.led.on(ALL_LEDS, CYAN)
    wait(0.5, SECONDS)

robot.led.off(ALL_LEDS)

is_stopped#

is_stopped returns whether the robot is neither moving nor turning, as a Boolean value. This can be used to control the timing of other behaviors based on the robot’s movement.

  • True — The robot is not moving or turning.

  • False — The robot is moving or turning.

用法:

robot.is_stopped()

参数

描述

该方法没有参数。

# Blink all the LEDs while the robot is moving or turning

def light_show():
    # Flash LEDs while the robot is moving or turning
    while not robot.is_stopped():
        robot.led.on(ALL_LEDS, GREEN)
        wait(.5, SECONDS)
        robot.led.on(ALL_LEDS, PURPLE)
        wait(.5, SECONDS)

    robot.led.off(ALL_LEDS)

robot.move_for(200, 0, wait=False)
light_show()

robot.turn_for(RIGHT, 180, wait=False)
light_show()