传动系统#

介绍#

123 型机器人采用双轮驱动系统,使其能够行驶和转弯。驱动系统控制着机器人的运动方式。

传动系统可以通过两种方式测量距离:步数毫米。一相当于123格子上的一个方格。

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

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

  • drive — Moves the robot forward or reverse forever.

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

  • drive_until — Moves the robot forward or reverse until it detects an object, crash, or line.

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

  • stop — Stops the robot’s movement.

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

  • set_timeout — Sets how long the robot will try to finish a movement.

  • set_heading — Changes the robot’s current heading to a new heading.

获取者 — 查看移动状态。

  • is_stopped — Returns whether the robot has stopped moving, as a Boolean value.

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

行动#

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:
robot.drive(direction)

参数

描述

direction

The direction the robot moves: FORWARD or REVERSE.

# Drive for 2 seconds.
robot.drive(FORWARD)
wait(2, SECONDS)
robot.stop()

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:
robot.drive_for(direction, distance, unit)

参数

描述

direction

The direction the robot moves: FORWARD or REVERSE.

distance

机器人行驶的距离。可以是整数或小数。

unit

The distance unit: MM (millimeters) or STEPS (one square on a 123 Field).

# Drive back and forth.
robot.drive_for(FORWARD, 3, STEPS)
robot.drive_for(REVERSE, 3, STEPS)

drive_until#

drive_until moves the robot forward or reverse until it detects an object, a crash, or a line.

Usage:
robot.drive_until(direction)

参数

描述

direction

The direction the robot moves: FORWARD or REVERSE.

# Reverse after a crash.
robot.drive_until(FORWARD)
robot.drive_for(REVERSE, 1, STEPS)

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:
robot.turn(direction)

参数

描述

direction

The direction the robot turns: LEFT or RIGHT.

# Turn for 2 seconds.
robot.turn(RIGHT)
wait(2, SECONDS)
robot.stop()

turn_for#

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

Usage:
robot.turn_for(direction, degrees)

参数

描述

direction

The direction the robot turns: LEFT or RIGHT.

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 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:
robot.turn_to_heading(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 stops the robot’s movement.

Usage:
robot.stop()

参数

描述

此方法没有参数。

# Stop driving after 4 seconds.
robot.drive(FORWARD)
wait(4, SECONDS)
robot.stop()

突变体#

set_timeout#

set_timeout sets how many seconds 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:
robot.set_timeout(time, unit)

参数

描述

time

允许动作进行的时间量。可以是整数或小数。

unit

The time unit: MSEC or SECONDS.

# 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 changes the robot’s current heading to a new heading value.

例如,如果机器人转向右侧,将航向设置为 0 度,则该右侧位置将成为新的 0 度。

Usage:
robot.set_heading(heading)

参数

描述

heading

要设置的机器人航向值(以度为单位)。

# Face the new 0 degree heading.
robot.set_heading(90)
robot.turn_to_heading(0)

获取器#

is_stopped#

is_stopped returns a Boolean that reports whether the robot has stopped moving.

  • True — The robot is stopped.

  • False — The robot is still moving.

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 returns the direction the robot is currently facing as a whole number from 0 to 359 degrees.

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

Usage:
robot.get_heading()

参数

描述

此方法没有参数。

# Display the heading after turning.
robot.turn_for(RIGHT, 450)
console.print(robot.get_heading())