传动系统#
介绍#
驱动系统包括车轮和电机,用于控制机器人的行驶和转向。驱动系统常见于 Code Base 2.0、Super Car 等机器人或定制机器人中。
驱动系统利用大脑中的惯性传感器(Inertial.md)来检测碰撞,并帮助机器人精确地移动和转向。在每个项目开始时,驱动系统会自动校准惯性传感器。校准期间,请保持机器人静止约2秒钟,以便机器人能够正确地移动和转向。
传动系统编码方式有很多种。以下列出了所有传动系统编码方法:
动作——移动和转向机器人。
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 until the Eye Sensor detects an object or the robot detects a crash.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. The robot will turn the shortest direction to reach the target heading.turn_to_rotation— Turns the robot to a specific rotation.stop— Stops the robot’s movement.
变异器 — 调整传动系统设置。
set_drive_velocity— Tells the robot how fast to drive.set_turn_velocity— Tells the robot how fast to turn.set_stopping— Tells how the robot will stop moving: by braking, coasting, or holding.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.set_rotation— Changes the robot’s current rotation to a new rotation.
获取者 — 查看移动状态。
get_heading— Returns the robot’s current heading from 0 to 359 degrees.get_rotation— Returns the robot’s current rotation.get_velocity— Returns how fast the robot is driving, as a percentage from -100% to 100%.get_yaw— Returns the robot’s current yaw.get_roll— Returns the robot’s current roll.get_pitch— Returns the robot’s current pitch.get_crashed— Returns whether the robot has detected a crash, as a Boolean value.is_stopped— Returns whether the robot is finished 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.
用法:
drivetrain.drive(direction)
参数 |
描述 |
|---|---|
|
The direction the robot moves: |
# Build Used: Code Base 2.0
def main():
# Drive forward then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()
# Start threads — Do not delete
start_thread(main)
drive_for#
drive_for moves the robot forward or reverse for a specific distance.
Usage:
drivetrain.drive_for(direction, distance, units, wait)
参数 |
描述 |
|---|---|
|
The direction the robot moves: |
|
机器人行驶的距离。可以是整数,也可以是小数。 |
|
Optional. The distance unit: |
|
Optional. |
# Build Used: Code Base 2.0
def main():
# Drive back and forth
drivetrain.drive_for(FORWARD, 100, MM)
drivetrain.drive_for(REVERSE, 4, INCHES)
# Start threads — Do not delete
start_thread(main)
drive_until#
drive_until moves the robot forward until the Eye Sensor detects an object or the robot detects a crash.
用法:
drivetrain.drive_until(condition, wait)
参数 |
描述 |
|---|---|
|
The condition that stops the robot: |
|
Optional. |
# Build Used: Code Base 2.0
def main():
# Turn right after a crash
drivetrain.drive_until(CRASH)
drivetrain.turn_for(RIGHT, 90)
# Start threads — Do not delete
start_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 to stopping.
用法:
drivetrain.turn(direction)
参数 |
描述 |
|---|---|
|
The direction the robot turns: |
# Build Used: Code Base 2.0
def main():
# Turn right and left, then stop
drivetrain.turn(RIGHT)
wait(2, SECONDS)
drivetrain.turn(LEFT)
wait(2, SECONDS)
drivetrain.stop()
# Start threads — Do not delete
start_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.
用法:
drivetrain.turn_for(direction, angle, wait)
参数 |
描述 |
|---|---|
|
The direction the robot turns: |
|
机器人旋转的角度数。可以是整数,也可以是小数。 |
|
Optional. |
# Build Used: Code Base 2.0
def main():
# Turn right then left
drivetrain.turn_for(RIGHT, 90)
drivetrain.turn_for(LEFT, 90)
# Start threads — Do not delete
start_thread(main)
turn_to_heading#
A heading is the direction the robot’s brain 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度。

用法:
drivetrain.turn_to_heading(angle, wait)
参数 |
描述 |
|---|---|
|
机器人应面向的方向,以整数表示,范围从 -359 度到 359 度。 |
|
Optional. |
# Build Used: Code Base 2.0
def main():
# Turn to face the cardinal directions
drivetrain.turn_to_heading(90)
wait(1, SECONDS)
drivetrain.turn_to_heading(180)
wait(1, SECONDS)
drivetrain.turn_to_heading(270)
wait(1, SECONDS)
drivetrain.turn_to_heading(0)
# Start threads — Do not delete
start_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 set_rotation.
旋转角度是绝对值。这意味着转弯方向取决于机器人当前的旋转角度。向右转弯会增加旋转角度,向左转弯会减少旋转角度。
例如,如果机器人从0度开始,你将其旋转720度,它会向右转两次。如果你再将其旋转360度,它会向左转一次,因为360小于720。

用法:
drivetrain.turn_to_rotation(angle, wait)
参数 |
描述 |
|---|---|
|
机器人将要旋转的角度值,以度为单位。该值可以是整数。 |
|
Optional. |
# Build Used: Code Base 2.0
def main():
# Make one full turn to the right
drivetrain.turn_to_rotation(360)
# Make another full turn to the right
drivetrain.turn_to_rotation(720)
# Make one full turn to the left, returning to 360 degrees
drivetrain.turn_to_rotation(360)
# Start threads — Do not delete
start_thread(main)
stop#
stop stops the robot’s movement.
用法:
drivetrain.stop()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Code Base 2.0
def main():
# Drive forward then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()
# Start threads — Do not delete
start_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% 的速度行驶。
注意: 速度越高,机器人行驶速度越快,但精度可能越低。速度越低,机器人行驶速度越慢,但精度越高。
用法:
drivetrain.set_drive_velocity(velocity)
参数 |
描述 |
|---|---|
|
行驶速度,范围从 0% 到 100%。 |
# Build Used: Code Base 2.0
def main():
# Drive at different velocities
drivetrain.drive_for(FORWARD, 100, MM)
wait(1, SECONDS)
# Drive slow
drivetrain.set_drive_velocity(20)
drivetrain.drive_for(FORWARD, 100, MM)
wait(1, SECONDS)
# Drive fast
drivetrain.set_drive_velocity(100)
drivetrain.drive_for(FORWARD, 100, MM)
# Start threads — Do not delete
start_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% 的速度旋转。
注意: 速度越高,机器人转弯越快,但精度可能越低。速度越低,机器人转弯越慢,但精度越高。
用法:
drivetrain.set_turn_velocity(velocity)
参数 |
描述 |
|---|---|
|
转弯速度从 0% 到 100%。 |
# Build Used: Code Base 2.0
def main():
# Turn at different velocities
drivetrain.turn_for(RIGHT, 180)
wait(1, SECONDS)
# Turn fast
drivetrain.set_turn_velocity(100)
drivetrain.turn_for(RIGHT, 180)
# Start threads — Do not delete
start_thread(main)
set_stopping#
set_stopping sets how the robot will stop moving: by braking, coasting, or holding.
用法:
drivetrain.set_stopping(brake)
参数 |
描述 |
|---|---|
|
How the robot will stop:
|
If this method is not used, the robot will use BRAKE when stopping.
# Build Used: Code Base 2.0
def main():
# Drive, then coast to a stop
drivetrain.set_drive_velocity(100)
drivetrain.set_stopping(COAST)
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()
# Start threads — Do not delete
start_thread(main)
set_timeout#
set_timeout sets how long 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.
用法:
drivetrain.set_timeout(value, units)
参数 |
描述 |
|---|---|
|
机器人尝试完成一次动作的时间。该值可以是整数或小数。 |
|
Optional. The unit of time: |
# Build Used: Code Base 2.0
def main():
# Drive as far as possible for 1 second before turning right
drivetrain.set_timeout(1, SECONDS)
drivetrain.drive_for(FORWARD, 25, INCHES)
drivetrain.turn_for(RIGHT, 90)
# Start threads — Do not delete
start_thread(main)
set_heading#
A heading is the direction the robot’s brain is facing, measured in degrees. set_heading changes the robot’s current heading to a new heading value.
例如,如果机器人转向右侧,将航向设置为 0 度,则该右侧位置将成为新的 0 度航向。然后,机器人可以根据新的航向转向其他位置。
用法:
drivetrain.set_heading(value)
参数 |
描述 |
|---|---|
|
要设置的机器人航向角值,单位为度。该值可以是 0 到 359 之间的整数。 |
# Build Used: Code Base 2.0
def main():
# Face the new 0 degree heading
drivetrain.set_heading(90)
drivetrain.turn_to_heading(0)
# Start threads — Do not delete
start_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 度。然后,机器人可以根据这个新的旋转角度进行旋转。
用法:
drivetrain.set_rotation(value)
参数 |
描述 |
|---|---|
|
机器人旋转角度值,以度为单位。该值可以是整数。 |
# Build Used: Code Base 2.0
def main():
# Spin counterclockwise two times
drivetrain.set_rotation(720)
drivetrain.turn_to_rotation(0)
# Start threads — Do not delete
start_thread(main)
吸气剂#
get_heading#
A heading is the direction the robot’s brain is facing, measured in degrees. get_heading returns that heading from 0 to 359 degrees.
机器人的初始航向角为0度。

用法:
drivetrain.get_heading()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Code Base 2.0
def main():
# Display the heading while turning
monitor_sensor("drivetrain.get_heading")
drivetrain.turn_for(RIGHT, 450)
# Start threads — Do not delete
start_thread(main)
get_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. get_rotation returns the robot’s current rotation.
向右转会增加旋转角度,向左转会减少旋转角度。例如,向右转两圈将得到 720 度的旋转角度。

用法:
drivetrain.get_rotation()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Code Base 2.0
def main():
# Display the rotation while turning
monitor_sensor("drivetrain.get_rotation")
drivetrain.turn_for(RIGHT, 450)
# Start threads — Do not delete
start_thread(main)
get_velocity#
get_velocity returns how fast the robot is driving, as a percentage from -100% to 100%.
正值表示机器人向前行驶,负值表示机器人向后行驶。
用法:
drivetrain.get_velocity()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Code Base 2.0
def main():
# Display the velocity of the robot before and while moving
console.print("Start: ")
console.print(drivetrain.get_velocity())
console.new_line()
drivetrain.drive(FORWARD)
wait(0.5, SECONDS)
console.print("Moving: ")
console.print(drivetrain.get_velocity())
drivetrain.stop()
# Start threads — Do not delete
start_thread(main)
get_yaw#
Yaw is how much the robot has turned left or right from its starting position, measured in degrees. get_yaw returns the robot’s current yaw value.
向右转弯会增加偏航角,向左转弯会减小偏航角。
用法:
drivetrain.get_yaw()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Super Code Base 2.0
def main():
# Change the LED color based on the yaw while
# moving the robot by hand
monitor_sensor("drivetrain.get_yaw")
while True:
if drivetrain.get_yaw() > 0:
bumper.set_color(GREEN)
elif drivetrain.get_yaw() < 0:
bumper.set_color(RED)
else:
bumper.set_color(NONE)
# Start threads — Do not delete
start_thread(main)
get_roll#
Roll is how much the robot tilts to the left or right, measured in degrees. get_roll returns the robot’s current roll value.
大脑朝向前方时,向左倾斜会增加滚动值,向右倾斜会减少滚动值。
用法:
drivetrain.get_roll()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Super Code Base 2.0
def main():
# Change the LED color based on the roll while
# moving the robot by hand
monitor_sensor("drivetrain.get_roll")
while True:
if drivetrain.get_roll() > 0:
bumper.set_color(GREEN)
elif drivetrain.get_roll() < 0:
bumper.set_color(RED)
else:
bumper.set_color(NONE)
# Start threads — Do not delete
start_thread(main)
get_pitch#
Pitch is how much the robot tilts forward or backward, measured in degrees. get_pitch returns the robot’s current pitch value.
大脑朝向前方,向前倾斜会增加音调值,向后倾斜会降低音调值。
用法:
drivetrain.get_pitch()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Super Code Base 2.0
def main():
# Change the LED color based on the pitch while
# moving the robot by hand
monitor_sensor("drivetrain.get_pitch")
while True:
if drivetrain.get_pitch() > 0:
bumper.set_color(GREEN)
elif drivetrain.get_pitch() < 0:
bumper.set_color(RED)
else:
bumper.set_color(NONE)
# Start threads — Do not delete
start_thread(main)
get_crashed#
get_crashed returns whether the robot has detected a crash, as a Boolean value.
True— A crash has been detected.False— A crash has not been detected.
用法:
drivetrain.get_crashed()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Super Code Base 2.0
def main():
# Drive until a crash
drivetrain.drive(FORWARD)
while not drivetrain.get_crashed():
wait(50, MSEC)
drivetrain.stop()
drivetrain.turn_for(RIGHT, 90)
# Start threads — Do not delete
start_thread(main)
is_stopped#
is_stopped 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, drive_until, turn_for, turn_to_heading, and turn_to_rotation.
用法:
drivetrain.is_stopped()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Code Base 2.0
def main():
# Turn when the drivetrain is done moving forward
drivetrain.drive_for(FORWARD, 100, MM, wait=False)
wait(0.25, SECONDS)
while True:
if drivetrain.is_stopped():
drivetrain.turn_for(RIGHT, 180)
break
else:
console.print("Still moving...")
wait(0.1, SECONDS)
console.clear()
# Start threads — Do not delete
start_thread(main)