传动系统#

介绍#

动力系统控制着VR机器人的行驶和转向。动力系统可以前进或后退、左转或右转、转向特定航向并跟踪其旋转。

传动系统编码方式有很多种。以下列出了所有传动系统编码方法:

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

  • drive — Moves the robot forward or reverse forever.

  • drive_for — Moves the robot forward or reverse for a specific distance.

  • turn — Turns the robot left or right forever.

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

  • turn_to_heading — Turns the robot to face a specific heading from -359 to 359 degrees.

  • turn_to_rotation — Turns the robot to a specific rotation.

  • stop — Stops the robot’s movement.

变异器 — 调整传动系统设置。

Getters — 检查传动系统状态。

  • heading — Returns the robot’s current heading from 0 to 359.99 degrees.

  • rotation — Returns the robot’s current rotation.

  • is_done — Returns whether the robot is finished moving, as a Boolean value.

  • is_moving — Returns whether the robot is moving, as a Boolean value.

行动#

drive#

drive moves the robot forward or reverse forever. The robot will continue to move until it is given another action, like turning or stopping.

Usage:
drivetrain.drive(direction)

参数

描述

direction

The direction the robot moves: FORWARD or REVERSE.

def main():
    # Drive forward then stop
    drivetrain.drive(FORWARD)
    wait(2, SECONDS)
    drivetrain.stop()

# VR threads — Do not delete
vr_thread(main)

drive_for#

drive_for moves the robot forward or reverse for a specific distance. The project will wait until the robot is done moving before the next line of code runs.

Usage:
drivetrain.drive_for(direction, distance, units, wait)

参数

描述

direction

The direction the robot moves: FORWARD or REVERSE.

distance

The distance the robot drives. This can be an integer or decimal (float).

units

Optional. The distance unit: INCHES (default) or MM (millimeters).

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.

def main():
    # Drive forward and backward
    drivetrain.drive_for(FORWARD, 200, MM)
    drivetrain.drive_for(REVERSE, 200, MM)

# VR threads — Do not delete
vr_thread(main)

turn#

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

Usage:
drivetrain.turn(direction)

参数

描述

direction

The direction the robot turns: LEFT or RIGHT.

def main():
    # Turn right and left, then stop
    drivetrain.turn(RIGHT)
    wait(2, SECONDS)
    drivetrain.turn(LEFT)
    wait(2, SECONDS)
    drivetrain.stop()

# VR threads — Do not delete
vr_thread(main)

turn_for#

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

Usage:
drivetrain.turn_for(direction, angle, units, wait)

参数

描述

direction

The direction the robot turns: LEFT or RIGHT.

angle

The number of degrees the robot turns. This can be an integer or decimal (float).

units

Optional. The rotation unit: DEGREES (default).

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.

def main():
    # Turn the robot right and left
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    wait(1, SECONDS)
    drivetrain.turn_for(LEFT, 90, DEGREES)

# VR threads — Do not delete
vr_thread(main)

turn_to_heading#

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

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

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

Usage:
drivetrain.turn_to_heading(angle, units, wait)

参数

描述

angle

The direction the robot should face, in degrees. This can be an integer or decimal (float) from -359 to 359.

units

Optional. The rotation unit: DEGREES (default).

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.

def main():
    # Turn to face the cardinal directions
    drivetrain.turn_to_heading(90, DEGREES)
    wait(1, SECONDS)
    drivetrain.turn_to_heading(180, DEGREES)
    wait(1, SECONDS)
    drivetrain.turn_to_heading(270, DEGREES)
    wait(1, SECONDS)
    drivetrain.turn_to_heading(0, DEGREES)

# VR threads — Do not delete
vr_thread(main)

turn_to_rotation#

turn_to_rotation turns the robot to a specific rotation.

Rotation is how much the robot has turned, measured in degrees. At the beginning of a project, the rotation value is set to 0 degrees. Rotation can also be set using the set_rotation method.

旋转角度是绝对值。这意味着转弯方向取决于机器人当前的旋转角度。向右转弯会增加旋转角度,向左转弯会减少旋转角度。

例如,如果机器人从0度开始,你将其旋转720度,它会向右转两次。如果你再将其旋转360度,它会向左转一次,因为360小于720。

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

Usage:
drivetrain.turn_to_rotation(angle, units, wait)

参数

描述

angle

The rotation value, in degrees, that the robot will turn to. This can be an integer or decimal (float).

units

Optional. The rotation unit: DEGREES (default).

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.

def main():
    # Turn left, then spin in a circle
    # clockwise and face right
    drivetrain.turn_to_rotation(-90, DEGREES)
    wait(1, SECONDS)
    drivetrain.turn_to_rotation(450, DEGREES)

# VR threads — Do not delete
vr_thread(main)

stop#

stop stops the robot’s movement.

Usage:
drivetrain.stop()

参数

描述

此方法没有参数。

def main():
    # Drive forward then stop
    drivetrain.drive(FORWARD)
    wait(2, SECONDS)
    drivetrain.stop()

# VR threads — Do not delete
vr_thread(main)

变异体#

set_heading#

A heading is the direction the robot is facing, measured in degrees. set_heading changes the robot’s current heading to a new heading value.

例如,如果机器人转向右侧,将航向设置为 0 度,则该右侧位置将成为新的 0 度航向。然后,机器人可以根据新的航向转向其他位置。

Usage:
drivetrain.set_heading(heading, units)

参数

描述

heading

The heading value, in degrees, to set for the robot. This can be an integer or decimal (float).

units

Optional. The heading unit: DEGREES (default).

def main():
    # Face the new 0 degrees
    drivetrain.set_heading(90, DEGREES)
    drivetrain.turn_to_heading(0, DEGREES)

# VR threads — Do not delete
vr_thread(main)

set_rotation#

Rotation is how much the robot has turned, measured in degrees. At the beginning of a project, the rotation value is set to 0 degrees. set_rotation changes the robot’s current rotation to a new value.

例如,如果机器人向右转了两圈,它的旋转角度将为 720 度。将旋转角度设置为 0 度会将旋转角度从 720 度重置为 0 度。然后,机器人可以根据这个新的旋转角度进行旋转。

Usage:
drivetrain.set_rotation(rotation, units)

参数

描述

rotation

The rotation value, in degrees, to set for the robot. This can be an integer or decimal (float).

units

Optional. The rotation unit: DEGREES (default).

def main():
    # Spin counterclockwise two times
    drivetrain.set_rotation(720, DEGREES)
    drivetrain.turn_to_rotation(0, DEGREES)

# VR threads — Do not delete
vr_thread(main)

set_timeout#

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

Usage:
drivetrain.set_timeout(value, units)

参数

描述

value

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

units

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

def main():
    # Turn right after driving forward
    drivetrain.set_timeout(1, SECONDS)
    drivetrain.drive_for(FORWARD, 25, INCHES)
    drivetrain.turn_for(RIGHT, 90)

# VR threads — Do not delete
vr_thread(main)

set_drive_velocity#

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

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

**注意:**速度越高,机器人行驶速度越快,但精度可能越低;速度越低,机器人行驶速度越慢,但精度可能越高。

Usage:
drivetrain.set_drive_velocity(velocity, units)

参数

描述

velocity

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

units

Optional. The velocity unit: PERCENT (default).

def main():
    # Drive forward at different velocities
    # Default velocity
    drivetrain.drive_for(FORWARD, 150, MM)
    wait(1, SECONDS)
    # Drive slower
    drivetrain.set_drive_velocity(20, PERCENT)
    drivetrain.drive_for(FORWARD, 150, MM)
    wait(1, SECONDS)
    # Drive faster
    drivetrain.set_drive_velocity(100, PERCENT)
    drivetrain.drive_for(FORWARD, 150, MM)

# VR threads — Do not delete
vr_thread(main)

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% 的速度旋转。

**注意:**速度越高,机器人转弯越快,但精度可能越低;速度越低,机器人转弯越慢,但精度可能越高。

Usage:
drivetrain.set_turn_velocity(velocity, units)

参数

描述

velocity

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

units

Optional. The velocity unit: PERCENT (default).

def main():
    # Turn at different velocities
    # Default velocity
    drivetrain.turn_for(RIGHT, 120, DEGREES)
    wait(1, SECONDS)
    # Turn slower
    drivetrain.set_turn_velocity(20, PERCENT)
    drivetrain.turn_for(RIGHT, 120, DEGREES)
    wait(1, SECONDS)
    # Turn faster
    drivetrain.set_turn_velocity(100, PERCENT)
    drivetrain.turn_for(RIGHT, 120, DEGREES)

# VR threads — Do not delete
vr_thread(main)

获取器#

heading#

A heading is the direction the robot is facing, measured in degrees. heading returns the robot’s current heading from 0 to 359.99 degrees.

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

Usage:
drivetrain.heading(units)

参数

描述

units

Optional. The heading unit: DEGREES (default).

def main():
    # Display the heading after turning
    drivetrain.turn_for(RIGHT, 450, DEGREES)
    brain.print("Heading: ")
    brain.print(drivetrain.heading(DEGREES))

# VR threads — Do not delete
vr_thread(main)

rotation#

Rotation is how much the robot has turned, measured in degrees. At the beginning of a project, the rotation value is set to 0 degrees. rotation returns the robot’s current rotation.

向右转会增加旋转角度,向左转会减少旋转角度。例如,向右转两圈将得到 720 度的旋转角度。

Usage:
drivetrain.rotation(units)

参数

描述

units

Optional. The rotation unit: DEGREES (default).

def main():
    # Display the rotation after turning
    drivetrain.turn_for(RIGHT, 450, DEGREES)
    brain.print("Rotation: ")
    brain.print(drivetrain.rotation(DEGREES))

# VR threads — Do not delete
vr_thread(main)

is_done#

is_done returns whether the robot is finished 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 finished moving.

  • False — The robot is still moving.

This method works together with the following Drivetrain methods that have the wait parameter: drive_for, turn_for, turn_to_heading, and turn_to_rotation.

Usage:
drivetrain.is_done()

参数

描述

此方法没有参数。

is_moving#

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

Usage:
drivetrain.is_moving()

参数

描述

此方法没有参数。