Drivetrain#
Introduction#
The drivetrain controls a robot’s movement, allowing it to drive forward, turn, and stop with precision.
Below is a list of all methods:
Actions – Move and turn the robot.
drive– Moves the drivetrain in a specified direction indefinitely.drive_for– Moves the drivetrain for a set distance.drive_until– Moves the drivetrain until a condition is met.turn– Turns the drivetrain left or right indefinitely.turn_for– Turns the drivetrain for a set distance.turn_to_heading– Turns the drivetrain to a specified heading.turn_to_rotation– Turns the drivetrain to a specified rotation.stop– Stops a drivetrain with configurable behavior.
Mutators – Set default movement and turn speeds.
set_drive_velocity– Sets the default moving velocity for the drivetrain.set_turn_velocity– Sets the turning moving velocity for the drivetrain.set_stopping– Sets the stop behavior (brake, coast, or hold).set_timeout– Limits how long a drivetrain function waits before giving up if movement is blocked.set_heading– Sets a drivetrain’s heading to a specific value.set_rotation– Sets a drivetrain’s rotation to a specific value.
Getters – Return robot state and position.
get_heading– Returns a drivetrain’s current heading.get_rotation– Returns a drivetrain’s current rotation.get_velocity– Returns a drivetrain’s current velocity.get_yaw– Returns the yaw of the robot in degrees.get_roll– Returns the roll of the robot in degrees.get_pitch– Returns the pitch of the robot in degrees.get_crashed– Returns whether the robot has crashed.is_stopped– Returns whether a drivetrain is currently not moving.
Actions#
drive#
drive moves the drivetrain in a specified direction indefinitely.
Usage:
drivetrain.drive(direction)
Parameters |
Description |
|---|---|
|
The direction in which to drive:
|
# Build Used: Super 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 drivetrain in a specified direction for a set distance.
Usage:
drivetrain.drive_for(direction, distance, units, wait)
Parameters |
Description |
|---|---|
|
The direction in which to drive:
|
|
The distance for the drivetrain to move as a float or integer. |
|
The unit that represents the distance:
|
|
Optional.
|
# Build Used: Super 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 drivetrain forward until a certain condition is met.
Usage:
drivetrain.drive_until(condition, wait)
Parameters |
Description |
|---|---|
|
The condition that stops the drivetrain:
|
|
Optional.
|
# Build Used: Super 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 drivetrain left or right indefinitely.
Usage:
drivetrain.turn(direction)
Parameters |
Description |
|---|---|
|
The direction in which to turn:
|
# Build Used: Super 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 drivetrain left or right for a specified angle or rotations.
Usage:
drivetrain.turn_for(direction, angle, wait)
Parameters |
Description |
|---|---|
|
The direction in which to turn:
|
|
The amount of degrees the drivetrain will turn as a float or integer. |
|
Optional.
|
# Build Used: Super 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#
turn_to_heading turns a drivetrain to a specified heading.
Usage:
drivetrain.turn_to_heading(angle, wait)
Parameters |
Description |
|---|---|
|
The heading to turn the drivetrain to face as a float or integer in degrees. |
|
Optional.
|
# Build Used: Super 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 a drivetrain to a specified rotation.
Usage:
drivetrain.turn_to_rotation(angle, wait)
Parameters |
Description |
|---|---|
|
The rotation to turn the drivetrain to face as a float or integer in degrees. |
|
Optional.
|
# Build Used: Super Code Base 2.0
def main():
# Spin around twice
drivetrain.turn_to_rotation(720)
# Start threads — Do not delete
start_thread(main)
stop#
stop stops a drivetrain.
Usage:
drivetrain.stop()
Parameters |
Description |
|---|---|
This method has no parameters. |
# Build Used: Super 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)
Mutators#
set_drive_velocity#
set_drive_velocity sets the default moving velocity for a drivetrain. This velocity setting will be used for subsequent calls to any drivetrain functions.
Usage:
drivetrain.set_drive_velocity(velocity)
Parameters |
Description |
|---|---|
|
The velocity at which the drivetrain will move as a float or integer from 0% to 100%. |
# Build Used: Super Code Base 2.0
def main():
# Drive at different velocities
drivetrain.drive_for(FORWARD, 150, MM)
wait(1, SECONDS)
# Drive slow
drivetrain.set_drive_velocity(20)
drivetrain.drive_for(REVERSE, 50, MM)
wait(1, SECONDS)
# Drive fast
drivetrain.set_drive_velocity(100)
drivetrain.drive_for(FORWARD, 150, MM)
# Start threads — Do not delete
start_thread(main)
set_turn_velocity#
set_turn_velocity sets the default turning velocity for a drivetrain. This velocity setting will be used for subsequent calls to any drivetrain functions.
Usage:
drivetrain.set_turn_velocity(velocity)
Parameters |
Description |
|---|---|
|
The velocity at which the drivetrain will turn as a float or integer from 0% to 100%. |
# Build Used: Super 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 the stopping mode for a drivetrain.
Usage:
drivetrain.set_stopping(brake)
Parameters |
Description |
|---|---|
|
How the drivetrain will stop:
|
# Build Used: Super Code Base 2.0
def main():
drivetrain.set_drive_velocity(100)
# Drive, then coast to a stop
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 a time limit for how long a drivetrain function will wait to reach its target. If the drivetrain cannot complete the movement within the set time, it will stop automatically and continue with the next function.
Note: The drivetrain’s time limit is used to prevent drivetrain functions that do not reach their target position from stopping the execution of the rest of the project.
Usage:
drivetrain.set_timeout(value, units)
Parameters |
Description |
|---|---|
|
The maximum number of seconds a motor function will run before stopping and moving to the next function as an integer or float. |
|
Optional. The unit that represents the time:
|
# Build Used: Super Code Base 2.0
def main():
# Turn right after driving forward
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#
set_heading sets the heading of the drivetrain to a specified value.
Usage:
drivetrain.set_heading(value)
Parameters |
Description |
|---|---|
|
The value to use for the new heading in degrees. |
# Build Used: Super 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#
set_rotation sets the rotation of the drivetrain to a specified value.
Usage:
drivetrain.set_rotation(value)
Parameters |
Description |
|---|---|
|
The value to use for the new rotation in degrees. |
# Build Used: Super 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)
Getters#
get_heading#
get_heading returns the current heading of the drivetrain as a float in degrees.
Usage:
drivetrain.get_heading()
Parameters |
Description |
|---|---|
This method has no parameters. |
# Build Used: Super Code Base 2.0
def main():
# Monitor the heading while turning
monitor_sensor("drivetrain.get_heading")
drivetrain.turn_for(RIGHT, 450)
# Start threads — Do not delete
start_thread(main)
get_rotation#
get_rotation returns the current rotation of the Inertial Sensor in degrees.
Usage:
drivetrain.get_rotation()
Parameters |
Description |
|---|---|
This method has no parameters. |
# Build Used: Super Code Base 2.0
def main():
# Monitor 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 the current velocity of the drivetrain as a float in percent.
Usage:
drivetrain.get_velocity()
Parameters |
Description |
|---|---|
This method has no parameters. |
# Build Used: Super 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(1, SECONDS)
console.print("Moving: ")
console.print(drivetrain.get_velocity())
drivetrain.stop()
# Start threads — Do not delete
start_thread(main)
get_yaw#
get_yaw returns the current yaw of the robot as a float in degrees.
Usage:
drivetrain.get_yaw()
Parameters |
Description |
|---|---|
This method has no parameters. |
# 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#
get_roll returns the current roll of the robot as a float in degrees.
Usage:
drivetrain.get_roll()
Parameters |
Description |
|---|---|
This method has no parameters. |
# 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#
get_pitch returns the current pitch of the robot as a float in degrees.
Usage:
drivetrain.get_pitch()
Parameters |
Description |
|---|---|
This method has no parameters. |
# 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 a Boolean indicating whether or not the robot has crashed.
True– The robot has crashed.False– The robot has not crashed.
Usage:
drivetrain.get_crashed()
Parameters |
Description |
|---|---|
This method has no parameters. |
# 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 a Boolean indicating whether a drivetrain is not currently moving.
True– The drivetrain is stopped.False– The drivetrain is moving.
Usage:
drivetrain.is_stopped()
Parameters |
Description |
|---|---|
This method has no parameters. |
# Build Used: Super Code Base 2.0
def main():
# Turn when the drivetrain is done moving forward
drivetrain.drive_for(FORWARD, 200, 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)