Drivetrain#

Introduction#

The drivetrain includes wheels and motors that control how the robot drives and turns. Drivetrains can be found in builds like the Code Base 2.0, Super Car, or a custom robot.

Drivetrains use the Inertial Sensor in the Brain to detect crashes and help the robot move and turn precisely. At the start of each project, the drivetrain calibrates the Inertial Sensor automatically. Keep the robot still for about 2 seconds during calibration, so the robot can move and turn correctly.

There are many ways to code the drivetrain. Below is a list of all Drivetrain methods:

Actions — Move and turn the robot.

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

Mutators — Adjust drivetrain settings.

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

Getters — Check movement status.

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

Actions#

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

Parameters

Description

direction

The direction the robot moves: FORWARD or REVERSE.

# 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)

Parameters

Description

direction

The direction the robot moves: FORWARD or REVERSE.

distance

The distance the robot drives. This can be an integer or a decimal.

units

Optional. The distance unit: MM (millimeters, default) or INCHES.

wait

Optional. wait=True (default) makes the project wait until the robot is done moving before the next line of code runs. wait=False makes the next line of code run right away.

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

Usage:
drivetrain.drive_until(condition, wait)

Parameters

Description

condition

The condition that stops the robot: OBJECT or CRASH.

wait

Optional. wait=True (default) makes the project wait until the robot is done moving before the next line of code runs. wait=False makes the next line of code run right away.

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

Usage:
drivetrain.turn(direction)

Parameters

Description

direction

The direction the robot turns: LEFT or RIGHT.

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

Usage:
drivetrain.turn_for(direction, angle, wait)

Parameters

Description

direction

The direction the robot turns: LEFT or RIGHT.

angle

The number of degrees the robot turns. This can be an integer or a decimal.

wait

Optional. wait=True (default) makes the project wait until the robot is done turning before the next line of code runs. wait=False makes the next line of code run right away.

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

The starting heading is 0 degrees.

A VEX GO Code Base 2.0 showing heading directions: 0 degrees forward, 90 degrees right, 180 degrees backward, and 270 degrees left.

Usage:
drivetrain.turn_to_heading(angle, wait)

Parameters

Description

angle

The direction the robot should face as an integer, from -359 to 359 degrees.

wait

Optional. wait=True (default) makes the project wait until the robot is done turning before the next line of code runs. wait=False makes the next line of code run right away.

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

Rotation values are absolute. This means the direction of the turn depends on the robot’s current rotation. Turning right increases the rotation, and turning left decreases the rotation.

For example, if the robot starts at 0 degrees and you turn to a rotation of 720 degrees, it will turn right twice. If you then turn to a rotation of 360 degrees, it will turn left once, because 360 is less than 720.

A VEX GO Code Base 2.0 showing arrows rotating to the right and left. The left arrow shows "minus" and the right arrow shows "plus".

Usage:
drivetrain.turn_to_rotation(angle, wait)

Parameters

Description

angle

The rotation value, in degrees, that the robot will turn to. This can be an integer.

wait

Optional. wait=True (default) makes the project wait until the robot is done turning before the next line of code runs. wait=False makes the next line of code run right away.

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

Usage:
drivetrain.stop()

Parameters

Description

This method has no parameters.

# 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)

Mutators#

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.

Every project begins with the robot driving at 50% velocity by default.

Note: A higher velocity makes the robot drive faster, but it may be less precise. A lower velocity makes the robot drive slower, but be more precise.

Usage:
drivetrain.set_drive_velocity(velocity)

Parameters

Description

velocity

The velocity to drive with from 0% to 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.

Every project begins with the robot turning at 50% velocity by default.

Note: A higher velocity makes the robot turn faster, but it may be less precise. A lower velocity makes the robot turn slower, but be more precise.

Usage:
drivetrain.set_turn_velocity(velocity)

Parameters

Description

velocity

The velocity to turn with from 0% to 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.

Usage:
drivetrain.set_stopping(brake)

Parameters

Description

brake

How the robot will stop:

  • BRAKE — Stops immediately.
  • COAST — Slows to a stop.
  • HOLD — Stops immediately and holds the wheels’ position.

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.

Usage:
drivetrain.set_timeout(value, units)

Parameters

Description

value

The amount of time the robot can try to finish a movement. This can be a whole number or a decimal.

units

Optional. The unit of time: SECONDS (default) or MSEC (milliseconds).

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

For example, if the robot has turned to face right, setting the heading to 0 degrees makes that right-facing position the new 0 degrees. Then the robot can turn to other positions based on that new heading.

Usage:
drivetrain.set_heading(value)

Parameters

Description

value

The heading value, in degrees, to set for the robot. This can be a whole number from 0 to 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.

For example, if the robot has made two full turns to the right, its rotation value will be 720 degrees. Setting the rotation to 0 degrees will reset that rotation from 720 to 0 degrees. Then the robot can turn to rotations based on that new value.

Usage:
drivetrain.set_rotation(value)

Parameters

Description

value

The rotation value, in degrees, to set for the robot. This can be an integer.

# 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)

Getters#

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.

The robot’s starting heading is 0 degrees.

A VEX GO Brain showing heading directions: 0 degrees forward, 90 degrees right, 180 degrees backward, and 270 degrees left.

Usage:
drivetrain.get_heading()

Parameters

Description

This method has no parameters.

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

Turning right increases the rotation, and turning left decreases the rotation. For example, making two full turns to the right will return a rotation of 720 degrees.

A VEX GO Code Base 2.0 showing arrows rotating to the right and left. The left arrow shows "minus" and the right arrow shows "plus".

Usage:
drivetrain.get_rotation()

Parameters

Description

This method has no parameters.

# 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%.

A positive value means the robot is driving forward. A negative value means the robot is driving in reverse.

Usage:
drivetrain.get_velocity()

Parameters

Description

This method has no parameters.

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

Turning right increases the yaw value, and turning left decreases the yaw value.

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#

Roll is how much the robot tilts to the left or right, measured in degrees. get_roll returns the robot’s current roll value.

With the Brain facing forward, tilting left increases the roll value, and tilting right decreases the roll value.

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#

Pitch is how much the robot tilts forward or backward, measured in degrees. get_pitch returns the robot’s current pitch value.

With the Brain facing forward, tilting forward increases the pitch value, and tilting backward decreases the pitch value.

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 whether the robot has detected a crash, as a Boolean value.

  • True — A crash has been detected.

  • False — A crash has not been detected.

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

Usage:
drivetrain.is_stopped()

Parameters

Description

This method has no parameters.

# 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)