Drivetrain#
Introduction#
The Drivetrain controls a robot’s movement, allowing it to drive forward, turn, and stop with precision.
It can also function as a smart drivetrain when configured with a Gyro Sensor or a Brain Inertial Sensor.
For the examples below, the configured drivetrain will be named drivetrain
and will be used in all subsequent examples throughout this API documentation when referring to DriveTrain
and SmartDrive
class methods.
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.
turn – Turns the drivetrain left or right indefinitely.
turn_for – Turns the drivetrain for a set distance.
turn_to_heading – Turns the smart drivetrain to a specified heading using sensors.
turn_to_rotation – Turns the smart drivetrain to a specific rotational value.
stop – Stops a drivetrain with configurable behavior.
calibrate_drivetrain – Calibrate the Inertial Sensor.
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 smart drivetrain’s heading to a specific value.
set_rotation – Sets a smart drivetrain’s rotational value to a specific value.
set_turn_threshold – Sets the turn threshold for a smart drivetrain.
set_turn_constant – Sets the turn constant for a smart drivetrain.
set_turn_direction_reverse – Sets a smart drivetrain to have its direction be reversed.
Getters – Return robot state and position.
heading – Returns a smart drivetrain’s current heading.
rotation – Returns a smart drivetrain’s current rotational value.
velocity – Returns a drivetrain’s current velocity.
current – Returns the current of the drivetrain.
is_moving – Returns whether a drivetrain is currently moving.
is_done – Returns whether a drivetrain is currently not moving.
is_turning – Returns whether a drivetrain is currently turning.
power – Returns the amount of power being used by a drivetrain.
torque – Returns the torque generated by a drivetrain.
efficiency – Returns the efficiency of a drivetrain.
temperature – Returns the temperature of the drivetrain.
Constructors – Manually initialize and configure the drivetrain.
DriveTrain – Creates a basic drivetrain.
SmartDrive – Creates a drivetrain configured with a Gyro Sensor or Brain Inertial Sensor.
Actions#
drive#
drive
moves the drivetrain in a specified direction indefinitely.
Usage:
drivetrain.drive(direction, velocity, units)
Parameters |
Description |
---|---|
|
The direction in which to drive:
|
|
Optional. The velocity at which the drivetrain will move as a float or integer. If the velocity is not specified, the default velocity is 50%. |
|
Optional. The unit that represents the velocity:
|
# Drive forward then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()
# Drive slowly in reverse then stop
drivetrain.drive(REVERSE, 20, PERCENT)
wait(2, SECONDS)
drivetrain.stop()
drive_for#
drive_for
moves the drivetrain in a specified direction for a set distance.
Usage:
drivetrain.drive_for(direction, distance, units, velocity, units_v, 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. The velocity at which the drivetrain will move as a float or integer. If the velocity is not specified, the default velocity is 50%. |
|
Optional. The unit that represents the velocity:
|
|
Optional.
|
# Drive forward and backward
drivetrain.drive_for(FORWARD, 10)
drivetrain.drive_for(REVERSE, 10)
# Quickly drive forward and backward
drivetrain.drive_for(FORWARD, 200, MM, 100, PERCENT)
drivetrain.drive_for(REVERSE, 200, MM, 100, PERCENT)
turn#
turn
turns the drivetrain left or right indefinitely.
Usage:
drivetrain.turn(direction, velocity, units)
Parameters |
Description |
---|---|
|
The direction in which to turn:
|
|
Optional. The velocity at which the drivetrain will turn as a float or integer. If the velocity is not specified, the default velocity is 50%. |
|
Optional. The unit that represents the velocity:
|
# Turn right and left, then stop
drivetrain.turn(RIGHT)
wait(2, SECONDS)
drivetrain.turn(LEFT)
wait(2, SECONDS)
drivetrain.stop()
# Quickly turn right and left, then stop
drivetrain.turn(RIGHT, 100, PERCENT)
wait(2, SECONDS)
drivetrain.turn(LEFT, 100, PERCENT)
wait(2, SECONDS)
drivetrain.stop()
turn_for#
turn_for
turns the drivetrain left or right for a specified angle or rotations.
Usage:
drivetrain.turn_for(direction, angle, units, velocity, units_v, wait)
Parameters |
Description |
---|---|
|
The direction in which to turn:
|
|
The amount of degrees the drivetrain will turn as a float or integer. |
|
The unit that represents the rotational value:
|
|
Optional. The velocity at which the drivetrain will turn as a float or integer. If the velocity is not specified, the default velocity is 50%. |
|
Optional. The unit that represents the velocity:
|
|
Optional.
|
# Turn the robot right and left
drivetrain.turn_for(RIGHT, 90, DEGREES)
wait(1, SECONDS)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Quickly turn the robot right and left
drivetrain.turn_for(RIGHT, 90, DEGREES, 100, PERCENT)
wait(1, SECONDS)
drivetrain.turn_for(LEFT, 90, DEGREES, 100, PERCENT)
turn_to_heading#
turn_to_heading
turns a smart drivetrain to a specified heading.
Note: This method will only work with a drivetrain that has been configured with a Gyro Sensor or Brain Inertial Sensor.
Usage:
drivetrain.turn_to_heading(angle, units, velocity, units_v, wait)
Parameters |
Description |
---|---|
|
The heading to turn the drivetrain to face as a float or integer. |
|
The unit that represents the rotational value:
|
|
Optional. The velocity at which the motor or motor group will spin as a float or integer. If the velocity is not specified, the default velocity is 50%. |
|
Optional. The unit that represents the velocity:
|
|
Optional.
|
# Turn to face the cardinal directions
drivetrain.turn_to_heading(90, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_heading(180, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_heading(270, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_heading(0, DEGREES)
# Turn twice slowly
drivetrain.turn_to_heading(90, DEGREES, 20, PERCENT)
wait(1, SECONDS)
drivetrain.turn_to_heading(180, DEGREES, 20, PERCENT)
turn_to_rotation#
turn_to_rotation
turns a smart drivetrain to a specified rotational value.
Note: This method will only work with a drivetrain that has been configured with a Gyro Sensor or Brain Inertial Sensor.
Usage:
drivetrain.turn_to_rotation(angle, units, velocity, units_v, wait)
Parameters |
Description |
---|---|
|
The rotational value to turn the drivetrain to face as a float or integer. |
|
The unit that represents the rotational value:
|
|
Optional. The velocity at which the motor or motor group will spin as a float or integer. If the velocity is not specified, the default velocity is 50%. |
|
Optional. The unit that represents the velocity:
|
|
Optional.
|
# Turn left, then spin in a circle
# clockwise and face right
drivetrain.turn_to_rotation(-90, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_rotation(450, DEGREES)
# Turn left then slowly spin in a
# circle clockwise
drivetrain.turn_to_rotation(-90, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_rotation(450, DEGREES, 20, PERCENT)
stop#
stop
stops a drivetrain.
Usage:
drivetrain.stop(mode)
Parameters |
Description |
---|---|
|
Optional. How the drivetrain will stop:
|
# Drive forward then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()
# Drive forward and coast to a stop
drivetrain.set_drive_velocity(100, PERCENT)
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop(COAST)
calibrate_drivetrain#
calibrate_drivetrain
calibrates the Gyro Sensor or Brain Inertial Sensor that is configured with the drivetrain. Calibration should be done when the drivetrain is not moving.
Note: The Brain will automatically calibrate at the start of each project.
Usage:
calibrate_drivetrain()
Parameters |
Description |
---|---|
This method has no parameters. |
# Calibrate the DriveTrain after turning
drivetrain.turn_for(RIGHT, 90, DEGREES)
calibrate_drivetrain()
brain.screen.print("Done!")
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 if a specific velocity is not provided.
Usage:
drivetrain.set_drive_velocity(velocity, units)
Parameters |
Description |
---|---|
|
The velocity at which the drivetrain will move as a float or integer. |
|
Optional. The unit that represents the velocity:
|
# Drive forward at different velocities
# Default velocity
drivetrain.drive_for(FORWARD, 150, MM)
wait(1, SECONDS)
# Drive slower
drivetrain.set_drive_velocity(20, PERCENT)
drivetrain.drive_for(FORWARD, 150, MM)
wait(1, SECONDS)
# Drive faster
drivetrain.set_drive_velocity(100, PERCENT)
drivetrain.drive_for(FORWARD, 150, MM)
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 if a specific velocity is not provided.
Usage:
drivetrain.set_turn_velocity(velocity, units)
Parameters |
Description |
---|---|
|
The velocity at which the drivetrain will turn as a float or integer. |
|
Optional. The unit that represents the velocity:
|
# Turn at different velocities
# Default velocity
drivetrain.turn_for(RIGHT, 360)
wait(1, SECONDS)
# Turn slower
drivetrain.set_turn_velocity(20, PERCENT)
drivetrain.turn_for(RIGHT, 360)
wait(1, SECONDS)
# Turn faster
drivetrain.set_turn_velocity(100, PERCENT)
drivetrain.turn_for(RIGHT, 360)
set_stopping#
set_stopping
sets the stopping mode for a drivetrain.
Usage:
drivetrain.set_stopping(mode)
Parameters |
Description |
---|---|
|
How the drivetrain will stop:
|
# Drive forward and coast to a stop
drivetrain.set_drive_velocity(100, PERCENT)
drivetrain.set_stopping(COAST)
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()
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:
|
# Turn right after driving forward
drivetrain.set_timeout(1, SECONDS)
drivetrain.drive_for(FORWARD, 25, INCHES)
drivetrain.turn_for(RIGHT, 90)
set_heading#
set_heading
sets the heading of a smart drivetrain.
Note: This method will only work with a drivetrain that has been configured with a Gyro Sensor or Brain Inertial Sensor.
Usage:
drivetrain.set_heading(heading, units)
Parameters |
Description |
---|---|
|
The new heading as a float or integer. |
|
Optional. The unit that represents the heading:
|
# Face the new 0 degrees
drivetrain.set_heading(90, DEGREES)
drivetrain.turn_to_heading(0, DEGREES)
set_rotation#
set_rotation
method sets the rotation for the smart drivetrain.
Note: This method will only work with a drivetrain that has been configured with a Gyro Sensor or Brain Inertial Sensor.
Usage:
drivetrain.set_rotation(rotation, units)
Parameters |
Description |
---|---|
|
The new rotational value as a float or integer. |
|
Optional. The unit that represents the heading:
|
# Spin counterclockwise two times
drivetrain.set_rotation(720, DEGREES)
drivetrain.turn_to_rotation(0, DEGREES)
set_turn_threshold#
set_turn_threshold
method sets the turn threshold for a smart drivetrain. The threshold value is used to determine that turns are complete. If this is too large, then turns will not be accurate. If too small, then turns may not complete.
Note: This method will only work with a drivetrain that has been configured with a Gyro Sensor or Brain Inertial Sensor.
Usage:
drivetrain.set_turn_threshold(value)
Parameters |
Description |
---|---|
|
The turn threshold to set in degrees as a float or integer. The default turn threshold is 1 degree. |
# Turn and display the heading with different turn thresholds
brain.screen.set_font(FontType.PROP20)
brain.screen.print("Default threshold: ")
drivetrain.turn_for(RIGHT, 90, DEGREES)
brain.screen.print(drivetrain.heading())
brain.screen.next_row()
wait(2, SECONDS)
brain_inertial.reset_heading()
brain.screen.print("20 degree Threshold: ")
drivetrain.set_turn_threshold(20)
drivetrain.turn_for(RIGHT, 90, DEGREES)
brain.screen.print(drivetrain.heading())
set_turn_constant#
set_turn_constant
sets the turn constant for a smart drivetrain. Smart drivetrains use a simple P controller when doing turns. This constant, generally known as kp, is the gain used in the equation that turns angular error into motor velocity.
Note: This method will only work with a drivetrain that has been configured with a Gyro Sensor or Brain Inertial Sensor.
Usage:
drivetrain.set_turn_constant(value)
Parameters |
Description |
---|---|
|
The new turn constant in the range 0.1 - 4.0. The default is 1.0. |
# Turn and display the heading with different turn constants
brain.screen.set_font(FontType.PROP20)
brain.screen.print("Default Constant: ")
drivetrain.turn_for(RIGHT, 90, DEGREES)
brain.screen.print(drivetrain.heading())
brain.screen.next_row()
wait(2, SECONDS)
brain_inertial.reset_heading()
brain.screen.print("0.2 Turn Constant: ")
drivetrain.set_turn_constant(0.2)
drivetrain.turn_for(RIGHT, 90, DEGREES)
brain.screen.print(drivetrain.heading())
set_turn_direction_reverse#
set_turn_direction_reverse(value)
sets the smart drivetrain to be reversed. This method works the same as setting the reverse
parameter to True
when constructing a SmartDrive
.
Note: This method will only work with a drivetrain that has been configured with a Gyro Sensor or Brain Inertial Sensor.
Usage:
drivetrain.set_turn_direction_reverse(value)
Parameters |
Description |
---|---|
|
Boolean value to set the direction reversed or not:
|
# Example coming soon
Getters#
heading#
heading
returns the current heading of a smart drivetrain as a float.
Note: This method will only work with a drivetrain that has been configured with a Gyro Sensor or Brain Inertial Sensor.
Usage:
drivetrain.heading(units)
Parameters |
Description |
---|---|
|
Optional. The unit that represents the rotational value:
|
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print("Heading: ")
brain.screen.print(drivetrain.heading())
rotation#
rotation
returns the current rotational value of a smart drivetrain as a float.
Note: This method will only work with a drivetrain that has been configured with a Gyro Sensor or Brain Inertial Sensor.
Usage:
drivetrain.rotation(units)
Parameters |
Description |
---|---|
|
Optional. The unit that represents the rotational value:
|
# Display the rotation after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print("Rotation: ")
brain.screen.print(drivetrain.rotation())
velocity#
velocity
returns the current velocity of a drivetrain as a float.
Usage:
drivetrain.velocity(units)
Parameters |
Description |
---|---|
|
Optional. The unit that represents the velocity:
|
# Display the robot's velocity before
# and after moving
brain.screen.print(drivetrain.velocity())
brain.screen.next_row()
drivetrain.drive(FORWARD)
wait(1, SECONDS)
brain.screen.print(drivetrain.velocity())
drivetrain.stop()
current#
current
returns the current of the drivetrain in amps.
Usage:
drivetrain.current(units)
Parameters |
Description |
---|---|
|
Optional. The unit that represents the current:
|
# Example coming soon
is_moving#
is_moving
returns a Boolean indicating whether a drivetrain is currently moving.
True
– The drivetrain is moving.False
– The drivetrain is not moving.
Usage:
drivetrain.is_moving()
Parameters |
Description |
---|---|
This method has no parameters. |
# Stop the Drivetrain after moving
# forward for some time
drivetrain.drive_for(FORWARD, 200, MM, wait=False)
while drivetrain.is_moving():
brain.screen.set_cursor(1, 1)
brain.screen.print("Still Moving...")
wait (0.1, SECONDS)
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print("Done!")
is_done()#
is_done
returns a Boolean indicating whether a drivetrain is not currently moving.
True
– The drivetrain is not moving.False
– The drivetrain is moving.
Usage:
drivetrain.is_done()
Parameters |
Description |
---|---|
This method has no parameters. |
# Stop the Drivetrain and turn right
# after moving forward for some time
drivetrain.drive_for(FORWARD, 200, MM, wait=False)
while True:
if drivetrain.is_done():
drivetrain.turn_for(RIGHT, 360)
break
else:
brain.screen.set_cursor(1, 1)
brain.screen.print("Still Moving...")
wait (0.1, SECONDS)
brain.screen.clear_screen()
is_turning#
is_moving
returns a Boolean indicating whether a smart drivetrain is currently turning.
True
– The smart drivetrain is turning.False
– The smart drivetrain is not turning.
Note: This method will only work with a drivetrain that has been configured with a Gyro Sensor or Brain Inertial Sensor.
Usage:
drivetrain.is_turning()
Parameters |
Description |
---|---|
This method has no parameters. |
# Detect when the Drivetrain is still turning
drivetrain.turn_for(RIGHT, 180, DEGREES, wait=False)
while drivetrain.is_turning():
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print("Turning...")
wait (0.1, SECONDS)
brain.screen.set_cursor(1, 1)
brain.screen.print("Done!")
power#
power
returns the average power of the drivetrain in watts.
Usage:
drivetrain.power()
Parameters |
Description |
---|---|
This method has no parameters. |
# Example coming soon
torque#
torque
returns the average torque of the drivetrain.
Usage:
drivetrain.torque(units)
Parameters |
Description |
---|---|
|
The unit that represents the torque:
|
# Example coming soon
efficiency#
efficiency
returns the average efficiency of the drivetrain in percent.
Usage:
drivetrain.efficiency()
Parameters |
Description |
---|---|
This method has no parameters. |
# Example coming soon
temperature#
temperature
returns the average temperature of the drivetrain.
Usage:
drivetrain.temperature(units)
Parameters |
Description |
---|---|
|
Optional. The units that represent the temperature:
|
# Example coming soon
Constructors#
Constructors are used to manually create DriveTrain
and SmartDrive
objects, which are necessary for configuring a drivetrain outside of VEXcode.
Note: In order to create a drivetrain, at least two Motor
or MotorGroup
objects must already have been created.
Drivetrain#
DriveTrain
creates a drivetrain without a Gyro Sensor or Brain Inertial Sensor.
Usage:
DriveTrain(lm, rm, wheelTravel, trackWidth, wheelBase, units, externalGearRatio)
Parameter |
Description |
---|---|
|
The name of a previously created left motor or motor group. |
|
The name of a previously created right motor or motor group. |
|
Optional. The circumference of the drivetrain’s wheels. The default is 300 millimeters. |
|
Optional. The track width of the drivetrain. The default is 320 millimeters. |
|
Optional. The wheel base of the drivetrain. The default is 320 millimeters. |
|
Optional. The unit that represents the
|
|
Optional. The gear ratio used to compensate drive distances if gearing is used as an integer. |
# Create the Motors
left_drive_smart = Motor(Ports.PORT1, 1.0, False)
right_drive_smart = Motor(Ports.PORT2, 1.0, True)
# Construct a 2-Motor Drivetrain "drivetrain" with the
# DriveTrain class
drivetrain = DriveTrain(left_drive_smart, right_drive_smart, 200, 173, 76, MM, 1)
drivetrain.drive_for(FORWARD, 200, MM)
drivetrain.drive_for(REVERSE, 200, MM)
# Create the left Motors and group them under the
# MotorGroup "left_motors"
left_motor_a = Motor(Ports.PORT1, 1.0, False)
left_motor_b = Motor(Ports.PORT2, 1.0, False)
left_drive_smart = MotorGroup(left_motor_a, left_motor_b)
# Create the right Motors and group them under the
# MotorGroup "right_motors"
right_motor_a = Motor(Ports.PORT3, 1.0, True)
right_motor_b = Motor(Ports.PORT4, 1.0, True)
right_drive_smart = MotorGroup(right_motor_a, right_motor_b)
# Construct a 4-Motor Drivetrain "drivetrain" with the
# DriveTrain class
drivetrain = DriveTrain(left_drive_smart, right_drive_smart, 200, 173, 76, MM, 1)
drivetrain.drive_for(FORWARD, 200, MM)
drivetrain.drive_for(REVERSE, 200, MM)
Smart Drivetrain#
SmartDrive
creates a drivetrain with a Gyro Sensor or Brain Inertial Sensor.
Usage:
SmartDrive(lm, rm, g, wheelTravel, trackWidth, wheelBase, units, externalGearRatio)
Parameter |
Description |
---|---|
|
The name of a previously created left motor or motor group. |
|
The name of a previously created right motor or motor group. |
|
The name of a previously created Inertial Sensor or Gyro Sensor. |
|
Optional. The circumference of the drivetrain’s wheels. The default is 300 millimeters. |
|
Optional. The track width of the drivetrain. The default is 320 millimeters. |
|
Optional. The wheel base of the drivetrain. The default is 320 millimeters. |
|
Optional. The unit that represents the
|
|
Optional. The gear ratio used to compensate drive distances if gearing is used as an integer. |
# Construct a SmartDrive Drivetrain with 2 motors
brain_inertial = Inertial()
left_drive_smart = Motor(Ports.PORT1, 1.0, False)
right_drive_smart = Motor(Ports.PORT6, 1.0, True)
drivetrain = SmartDrive(left_drive_smart, right_drive_smart, brain_inertial, 200)
drivetrain.drive_for(FORWARD, 200, MM)
drivetrain.drive_for(REVERSE, 200, MM)
If making a smart drivetrain with multiple motors, you need to create the Motors separately before grouping them into a Motor Group.
# Construct a SmartDrive Drivetrain with 4 motors
brain_inertial = Inertial()
left_motor_a = Motor(Ports.PORT1, 1.0, False)
left_motor_b = Motor(Ports.PORT2, 1.0, False)
left_drive_smart = MotorGroup(left_motor_a, left_motor_b)
right_motor_a = Motor(Ports.PORT5, 1.0, True)
right_motor_b = Motor(Ports.PORT6, 1.0, True)
right_drive_smart = MotorGroup(right_motor_a, right_motor_b)
drivetrain = SmartDrive(left_drive_smart, right_drive_smart, brain_inertial, 200)
drivetrain.drive_for(FORWARD, 200, MM)
drivetrain.drive_for(REVERSE, 200, MM)