传动系统#
介绍#
123 型机器人采用双轮驱动系统,使其能够行驶和转弯。驱动系统控制着机器人的运动方式。
传动系统可以通过两种方式测量距离:步数和毫米。一步相当于123格子上的一个方格。
传动系统编码方式有很多种。以下列出了所有传动系统编码方法:
动作——移动和转向机器人。
drive— 使机器人无限向前或向后移动。drive_for— 使机器人向前或向后移动指定的距离。drive_until— 使机器人前进或后退,直到检测到物体、碰撞或线路。turn— 使机器人永远向左或向右转弯。turn_for— 使机器人向左或向右旋转指定的度数。turn_to_heading— 将机器人转向 -359 到 359 度之间的特定航向。stop— 停止机器人的运动。
变异器 — 调整传动系统设置。
set_timeout— 设置机器人尝试完成一次动作的时间。set_heading— 将机器人的当前航向更改为新的航向。
获取者 — 查看移动状态。
is_stopped— 返回机器人是否停止移动,以布尔值表示。get_heading— 返回机器人的当前航向,范围从 0 到 359 度。
行动#
drive#
drive 使机器人无限前进或后退。机器人将持续移动,直到收到其他指令,例如转向或停止。
Usage:
robot.drive(direction)
参数 |
描述 |
|---|---|
|
机器人移动方向: |
# Drive for 2 seconds.
robot.drive(FORWARD)
wait(2, SECONDS)
robot.stop()
drive_for#
drive_for 使机器人前进或后退指定距离。程序会等待机器人完成移动后才执行下一行代码。
Usage:
robot.drive_for(direction, distance, unit)
参数 |
描述 |
|---|---|
|
机器人移动方向: |
|
机器人行驶的距离。可以是整数或小数。 |
|
距离单位: |
# Drive back and forth.
robot.drive_for(FORWARD, 3, STEPS)
robot.drive_for(REVERSE, 3, STEPS)
drive_until#
drive_until 使机器人前进或后退,直到检测到物体、碰撞或线条。
Usage:
robot.drive_until(direction)
参数 |
描述 |
|---|---|
|
机器人移动方向: |
# Reverse after a crash.
robot.drive_until(FORWARD)
robot.drive_for(REVERSE, 1, STEPS)
turn#
turn 表示机器人无限向左或向右转向。机器人将持续转向,直到收到其他指令,例如行驶或停止。
Usage:
robot.turn(direction)
参数 |
描述 |
|---|---|
|
机器人转向的方向: |
# Turn for 2 seconds.
robot.turn(RIGHT)
wait(2, SECONDS)
robot.stop()
turn_for#
turn_for 参数使机器人向左或向右旋转指定角度。程序会等待机器人完成旋转后才执行下一行代码。
Usage:
robot.turn_for(direction, degrees)
参数 |
描述 |
|---|---|
|
机器人转向的方向: |
|
需要旋转的角度数。可以是整数或小数。 |
# Turn left, then turn around to the right.
robot.turn_for(LEFT, 90)
robot.turn_for(RIGHT, 180)
turn_to_heading#
turn_to_heading 使机器人转向 -359 度到 359 度之间的特定航向。机器人将沿最短路径转向以达到目标航向。
起始航向为0度。
程序会等到机器人完成转向后才运行下一行代码。
Usage:
robot.turn_to_heading(heading)
参数 |
描述 |
|---|---|
|
机器人应面向的航向,范围从 -359 度到 359 度。 |
# Turn to face the cardinal directions.
robot.turn_to_heading(90)
wait(2, SECONDS)
robot.turn_to_heading(180)
wait(2, SECONDS)
robot.turn_to_heading(270)
wait(2, SECONDS)
robot.turn_to_heading(0)
wait(2, SECONDS)
stop#
stop 停止机器人的移动。
Usage:
robot.stop()
参数 |
描述 |
|---|---|
此方法没有参数。 |
# Stop driving after 4 seconds.
robot.drive(FORWARD)
wait(4, SECONDS)
robot.stop()
突变体#
set_timeout#
set_timeout 设置机器人尝试完成一个动作的超时时间(以秒为单位)。如果机器人无法在规定时间内完成,它将停止尝试并执行下一行代码。这可以防止机器人卡在某个动作中。
Usage:
robot.set_timeout(time, unit)
参数 |
描述 |
|---|---|
|
允许动作进行的时间量。可以是整数或小数。 |
|
时间单位: |
# Turn right after driving for 1 second.
robot.set_timeout(1, SECONDS)
robot.drive_for(FORWARD, 1000, MM)
robot.turn_for(RIGHT, 90)
set_heading#
set_heading 将机器人的当前航向更改为新的航向值。
例如,如果机器人转向右侧,将航向设置为 0 度,则该右侧位置将成为新的 0 度。
Usage:
robot.set_heading(heading)
参数 |
描述 |
|---|---|
|
要设置的机器人航向值(以度为单位)。 |
# Face the new 0 degree heading.
robot.set_heading(90)
robot.turn_to_heading(0)
获取器#
is_stopped#
is_stopped 返回一个 Boolean,报告机器人是否已停止移动。
True— 机器人已停止。False— 机器人仍在移动。
Usage:
robot.is_stopped()
参数 |
描述 |
|---|---|
此方法没有参数。 |
# Glow blue after a crash.
robot.drive_until(FORWARD)
while True:
if robot.is_stopped():
robot.led.glow(BLUE)
else:
robot.led.glow(GREEN)
wait(0.05, SECONDS)
get_heading#
get_heading 返回机器人当前面向的方向,以 0 到 359 度之间的整数表示。
机器人的初始航向角为0度。
Usage:
robot.get_heading()
参数 |
描述 |
|---|---|
此方法没有参数。 |
# Display the heading after turning.
robot.turn_for(RIGHT, 450)
console.print(robot.get_heading())