传动系统#
介绍#
动力系统控制着VR机器人的行驶和转向。动力系统可以前进或后退、左转或右转、转向特定航向并跟踪其旋转。
传动系统编码方式有很多种。以下列出了所有传动系统编码方法:
动作——移动和转向机器人。
drive— 使机器人无限向前或向后移动。drive_for— 使机器人向前或向后移动指定的距离。turn— 使机器人永远向左或向右转弯。turn_for— 使机器人向左或向右旋转指定的度数。turn_to_heading— 将机器人转向 -359 到 359 度之间的特定航向。turn_to_rotation— 将机器人旋转到指定的方向。stop— 停止机器人的运动。
变异器 — 调整传动系统设置。
set_heading— 将机器人的当前航向更改为新的航向。set_rotation— 将机器人的当前旋转更改为新的旋转。set_timeout— 设置机器人尝试完成一次动作的时间。set_drive_velocity— 告诉机器人以多快的速度行驶。set_turn_velocity— 告诉机器人转弯的速度。
Getters — 检查传动系统状态。
行动#
drive#
drive 使机器人无限前进或后退。机器人将持续移动,直到收到其他指令,例如转向或停止。
Usage:
drivetrain.drive(direction)
参数 |
描述 |
|---|---|
|
机器人移动方向: |
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 使机器人前进或后退指定距离。程序会等待机器人完成移动后才执行下一行代码。
Usage:
drivetrain.drive_for(direction, distance, units, wait)
参数 |
描述 |
|---|---|
|
机器人移动方向: |
|
机器人行驶的距离。这可以是整数 |
|
可选。距离单位: |
|
可选 |
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 表示机器人无限向左或向右转向。机器人将持续转向,直到收到其他指令,例如行驶或停止。
Usage:
drivetrain.turn(direction)
参数 |
描述 |
|---|---|
|
机器人转向的方向: |
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 参数使机器人向左或向右旋转指定角度。旋转角度是相对于机器人当前位置的。程序会等待机器人完成旋转后才执行下一行代码。
Usage:
drivetrain.turn_for(direction, angle, units, wait)
参数 |
描述 |
|---|---|
|
机器人转向的方向: |
|
The number of degrees the robot turns. This can be an |
|
可选。旋转单位: |
|
可选 |
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#
航向角是指机器人朝向的方向,以度为单位。`turn_to_heading 将机器人转向 -359 度到 359 度之间的特定航向角。机器人将沿最短路径转向以达到目标航向角。
机器人的初始航向角为0度。
程序会等到机器人完成转向后才运行下一行代码。
Usage:
drivetrain.turn_to_heading(angle, units, wait)
参数 |
描述 |
|---|---|
|
机器人应面向的方向,以度为单位。可以是 |
|
可选。旋转单位: |
|
可选 |
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 将机器人旋转到指定角度。
旋转角度是指机器人转动的程度,以度为单位。在项目开始时,旋转角度值设置为 0 度。也可以使用 set_rotation 方法设置旋转角度。
旋转角度是绝对值。这意味着转弯方向取决于机器人当前的旋转角度。向右转弯会增加旋转角度,向左转弯会减少旋转角度。
例如,如果机器人从0度开始,你将其旋转720度,它会向右转两次。如果你再将其旋转360度,它会向左转一次,因为360小于720。
程序会等到机器人完成转向后才运行下一行代码。
Usage:
drivetrain.turn_to_rotation(angle, units, wait)
参数 |
描述 |
|---|---|
|
机器人将旋转到的旋转值,以度为单位。这可以是整数 |
|
可选。旋转单位: |
|
可选 |
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 停止机器人的移动。
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#
航向角是指机器人面向的方向,以度为单位。0 set_heading 将机器人的当前航向角更改为新的航向角值。
例如,如果机器人转向右侧,将航向设置为 0 度,则该右侧位置将成为新的 0 度航向。然后,机器人可以根据新的航向转向其他位置。
Usage:
drivetrain.set_heading(heading, units)
参数 |
描述 |
|---|---|
|
The heading value, in degrees, to set for the robot. This can be an |
|
可选。航向单位: |
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#
旋转角度是指机器人转动的程度,以度为单位。项目开始时,旋转角度值设置为 0 度。`set_rotation 会将机器人的当前旋转角度更改为新值。
例如,如果机器人向右转了两圈,它的旋转角度将为 720 度。将旋转角度设置为 0 度会将旋转角度从 720 度重置为 0 度。然后,机器人可以根据这个新的旋转角度进行旋转。
Usage:
drivetrain.set_rotation(rotation, units)
参数 |
描述 |
|---|---|
|
要设置的机器人旋转值,以度为单位。这可以是整数 |
|
可选。旋转单位: |
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 设置机器人尝试完成一个动作的超时时间。如果机器人无法在规定时间内完成,它将停止尝试并执行下一行代码。这可以防止机器人卡在某个动作上。
Usage:
drivetrain.set_timeout(value, units)
参数 |
描述 |
|---|---|
|
The amount of time the robot can try to finish a movement. This can be a positive |
|
可选。时间单位: |
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 告诉机器人行驶速度。百分比越高,机器人行驶速度越快;百分比越低,机器人行驶速度越慢。
每个项目开始时,机器人默认以 50% 的速度行驶。
注意:速度越高,机器人行驶速度越快,但精度可能越低;速度越低,机器人行驶速度越慢,但精度可能越高。
Usage:
drivetrain.set_drive_velocity(velocity, units)
参数 |
描述 |
|---|---|
|
行驶速度,范围从 0% 到 100%。可以是整数 |
|
可选。速度单位: |
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 告诉机器人转弯的速度。百分比越高,机器人转弯越快;百分比越低,机器人转弯越慢。
每个项目开始时,机器人默认以 50% 的速度旋转。
注意:速度越高,机器人转弯越快,但精度可能越低;速度越低,机器人转弯越慢,但精度可能越高。
Usage:
drivetrain.set_turn_velocity(velocity, units)
参数 |
描述 |
|---|---|
|
转弯速度,范围从 0% 到 100%。可以是整数 |
|
可选。速度单位: |
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)
参数 |
描述 |
|---|---|
|
可选。航向单位: |
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)
参数 |
描述 |
|---|---|
|
可选。旋转单位: |
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 返回机器人是否已完成移动,返回值为布尔值。这可用于根据机器人的移动状态控制其他行为的执行时间。
True— 机器人已停止移动。False— 机器人仍在移动。
此方法与以下具有 wait 参数的传动系统方法配合使用:drive_for, turn_for, turn_to_heading, 和 turn_to_rotation。
Usage:
drivetrain.is_done()
参数 |
描述 |
|---|---|
此方法没有参数。 |
is_moving#
is_moving 返回机器人是否正在移动,返回值为布尔值。这可用于根据机器人的移动状态控制其他行为的执行时间。
True— 机器人正在移动。False— 机器人没有移动。
Usage:
drivetrain.is_moving()
参数 |
描述 |
|---|---|
此方法没有参数。 |