Drivetrain#
Introduction#
A drivetrain allows the robot to move continuously or for set distances, rotate by degrees or to a heading, and respond to changes in its rotational orientation.
If the drivetrain is configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor, the drivetrain will be able to make more precise forward, reverse, and turning movements by tracking the sensor’s heading and rotation.
The Drivetrain category also includes configuration methods that let you set drive and turn speeds, define stopping behavior, apply timeouts to avoid execution stalls, and manually update the robot’s heading or rotation values. These features provide flexibility when designing autonomous behaviors or real-time adjustments.
This page uses drivetrain as the example Drivetrain name. Replace it with your own configured name as needed.
Below is a list of available methods:
Actions – Move or turn the robot.
drive– Drives the robot continuously forward or in reverse using the current drive velocity.drive_for– Drives the robot forward or in reverse for a set distance.turn– Rotates the robot continuously left or right using the current turn velocity.turn_for– Rotates the robot left or right for a specified angle.turn_to_heading– Rotates the robot to face a specific absolute heading.turn_to_rotation– Rotates the robot to reach a specific cumulative rotation.stop– Stops the drivetrain using the configured stopping mode.calibrate_drivetrain– Calibrates the drivetrain’s configured Inertial, GPS, or Gyro Sensor.
Mutators – Configure speeds, stopping behavior, and timeouts.
set_drive_velocity– Sets the default drive velocity used by drive methods.set_turn_velocity– Sets the default turn velocity used by turn methods.set_stopping– Sets how the drivetrain behaves when it stops.set_timeout– Sets how long drive and turn methods try to reach their target before timing out.set_heading– Manually sets the robot’s heading value.set_rotation– Manually sets the robot’s cumulative rotation value.set_turn_threshold– Sets the turn threshold for a drivetrain.set_turn_constant– Sets the turn constant for a drivetrain.set_turn_direction_reverse– Sets a drivetrain to have its direction be reversed.
Getters – Check whether the drivetrain is moving.
is_done– Returns whether the drivetrain is not currently moving.is_moving– Returns whether the drivetrain is currently moving.heading– Returns the drivetrain’s heading angle (0 to 359.99 degrees).rotation– Returns how much the drivetrain has turned since the project started.velocity– Returns the drivetrain’s current velocity.current– Returns the drivetrain’s current draw.power– Returns the drivetrain’s power usage.torque– Returns the drivetrain’s torque.efficiency– Returns the drivetrain’s efficiency.temperature– Returns the drivetrain motors’ temperature.get_timeout– Returns the current timeout setting.
Constructor – Manually initialize a Drivetrain.
DriveTrain– Creates a drivetrain without an Inertial Sensor, GPS Sensor, or Gyro Sensor.SmartDrive– Creates a drivetrain configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor.
Actions#
drive#
drive moves the drivetrain forward or in reverse using the current drive velocity. This method runs continuously until another Drivetrain method interrupts it or the project stops.
Usage:
drivetrain.drive(direction, velocity, units)
Parámetros |
Descripción |
|---|---|
|
The direction in which the robot drives:
|
|
Optional. The velocity at which the drivetrain will move as a float or integer. If the velocity is not specified or previously set, the default velocity is 50%. |
|
Optional. The unit to represent the velocity:
|
# Drive forward, then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()
# Drive forward at 360 degrees per second
drivetrain.drive(FORWARD, 360.0, VelocityUnits.DPS)
drive_for#
drive_for moves the drivetrain forward or in reverse for a specified distance using the current drive velocity.
Usage:
drivetrain.drive_for(direction, distance, units, velocity, units_v, wait)
Parámetros |
Descripción |
|---|---|
|
The direction in which the robot drives:
|
|
The distance the Drivetrain will move. |
|
The units representing the distance:
|
|
Optional. The velocity at which the drivetrain will move as a float or integer. If the velocity is not specified or previously set, the default velocity is 50%. |
|
Optional. The unit to represent the velocity:
|
|
Optional.
|
# Drive forwards and backwards
drivetrain.drive_for(FORWARD, 5)
wait(1, SECONDS)
drivetrain.drive_for(REVERSE, 5)
turn#
turn rotates the drivetrain continuously left or right using the current turn velocity. The drivetrain will keep turning until another Drivetrain method runs or the project stops.
Usage:
drivetrain.turn(direction, velocity, units)
Parámetros |
Descripción |
|---|---|
|
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 or previously set, the default velocity is 50%. |
|
Optional. The unit that represents the velocity:
|
# Turn right, then stop
drivetrain.turn(RIGHT)
wait(2, SECONDS)
drivetrain.stop()
turn_for#
turn_for rotates the drivetrain left or right for a specific number of degrees using the current turn velocity.
Usage:
drivetrain.turn_for(direction, angle, units, velocity, units_v, wait)
Parámetros |
Descripción |
|---|---|
|
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 or previously set, the default velocity is 50%. |
|
Optional. The unit that represents the velocity:
|
|
Optional.
|
# Turn left, then turn around to the right
drivetrain.turn_for(LEFT, 90)
drivetrain.turn_for(RIGHT, 180)
turn_to_heading#
turn_to_heading rotates the drivetrain to face a specific rotational value using the current turn velocity.
Note: This method is only available if the drivetrain is configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor in the Devices window.
Usage:
drivetrain.turn_to_heading(angle, units, velocity, units_v, wait)
Parámetros |
Descripción |
|---|---|
|
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 drivetrain will turn as a float or integer. If the velocity is not specified or previously set, 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 rotates the drivetrain to face a specific rotational value using the current turn velocity.
Note: This method is only available if the drivetrain is configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor in the Devices window.
Usage:
drivetrain.turn_to_rotation(angle, units, velocity, units_v, wait)
Parámetros |
Descripción |
|---|---|
|
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 drivetrain will turn as a float or integer. If the velocity is not specified or previously set, 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 immediately stops all movement of the drivetrain.
Usage:
drivetrain.stop(mode)
Parámetros |
Descripción |
|---|---|
mode |
Optional. The stopping behavior to use when the drivetrain stops:
BRAKE. |
# Drive forward, then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()
calibrate_drivetrain#
calibrate_drivetrain calibrates the drivetrain’s configured Inertial, GPS, or Gyro Sensor. All subsequent commands will wait for the calibration to complete before executing. Calibration is an internal procedure that measures and compensates for sensor noise and drift over a specified period. During this time, the sensor must remain completely still (i.e., on a stable surface without any external movement). Movement during calibration will produce inaccurate results.
Note: This method is only available if the drivetrain is configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor in the Devices window.
Usage:
calibrate_drivetrain()
Parámetros |
Descripción |
|---|---|
This method has no parameters. |
Mutators#
set_drive_velocity#
set_drive_velocity sets the movement velocity as a percentage for all subsequent drive methods in the project. By default, this is 50%.
Usage:
drivetrain.set_drive_velocity(velocity, units)
Parámetros |
Descripción |
|---|---|
|
The velocity at which the drivetrain will move as a float or integer. |
|
Optional. The unit that represents the velocity:
|
# Drive forward at the default velocity
drivetrain.drive_for(FORWARD, 100, MM)
wait(1, SECONDS)
# Drive slower
drivetrain.set_drive_velocity(20, PERCENT)
drivetrain.drive_for(FORWARD, 100, MM)
wait(1, SECONDS)
# Drive faster
drivetrain.set_drive_velocity(100, PERCENT)
drivetrain.drive_for(FORWARD, 100, MM)
wait(1, SECONDS)
set_turn_velocity#
set_turn_velocity(velocity, units) sets the default velocity as a percentage for all subsequent turn methods in the project. By default, this is 50%.
Usage:
drivetrain.set_turn_velocity(velocity, units)
Parámetros |
Descripción |
|---|---|
|
The velocity at which the drivetrain will turn as a float or integer. |
|
Optional. The unit that represents the velocity:
|
# Turn at the default velocity
drivetrain.turn_for(RIGHT, 100)
wait(1, SECONDS)
# Turn slower
drivetrain.set_turn_velocity(20, PERCENT)
drivetrain.turn_for(RIGHT, 100)
wait(1, SECONDS)
# Turn faster
drivetrain.set_turn_velocity(100, PERCENT)
drivetrain.turn_for(RIGHT, 100)
wait(1, SECONDS)
set_stopping#
set_stopping sets how the drivetrain behaves when drivetrain movement ends.
Usage:
drivetrain.set_stopping(mode)
Parámetros |
Descripción |
|---|---|
|
How the drivetrain will stop:
|
set_timeout#
set_timeout sets a time limit for how long a Drivetrain command will wait to reach its target. If the robot cannot complete the movement within the set time, it will stop automatically and continue with the next block.
Note: The drivetrain’s time limit is used to prevent Drivetrain commands that do not reach their target position from stopping the execution of other commands in the stack.
Usage:
drivetrain.set_timeout(timeout, units)
Parámetros |
Descripción |
|---|---|
|
The maximum number of seconds a motor command will run before stopping and moving to the next command as an integer or float. |
|
Optional. The unit that represents the time:
|
set_heading#
set_heading sets the robot’s current heading to a specified value.
Note: This method is only available if the drivetrain is configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor in the Devices window.
Usage:
drivetrain.set_heading(heading, units)
Parámetros |
Descripción |
|---|---|
|
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 sets the robot’s current cumulative rotation value.
Note: This method is only available if the drivetrain is configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor in the Devices window.
Usage:
drivetrain.set_rotation(rotation, units)
Parámetros |
Descripción |
|---|---|
|
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 is only available if the drivetrain is configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor in the Devices window.
Usage:
drivetrain.set_turn_threshold(value)
Parámetros |
Descripción |
|---|---|
|
The turn threshold to set in degrees as a float or integer. The default turn threshold is 1 degree. |
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 is only available if the drivetrain is configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor in the Devices window.
Usage:
drivetrain.set_turn_constant(value)
Parámetros |
Descripción |
|---|---|
|
The new turn constant in the range 0.1 - 4.0. The default is 1.0. |
set_turn_direction_reverse#
set_turn_direction_reverse 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 is only available if the drivetrain is configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor in the Devices window.
Usage:
drivetrain.set_turn_direction_reverse(value)
Parámetros |
Descripción |
|---|---|
|
Boolean value to set the direction reversed or not:
|
Getters#
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.
Note: is_done only detects movement from methods that have a wait parameter.
Usage:
drivetrain.is_done()
Parámetros |
Descripción |
|---|---|
This method has no parameters. |
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.
Note: is_moving only detects movement from methods that have a wait parameter.
Usage:
Usage:
drivetrain.is_moving()
Parámetros |
Descripción |
|---|---|
This method has no parameters. |
heading#
heading returns the drivetrain’s heading angle as a float.
Note: This method is only available if the drivetrain is configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor in the Devices window.
Usage:
drivetrain.heading(units)
Parámetros |
Descripción |
|---|---|
|
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 how much the drivetrain has turned since the project started as a float: positive for clockwise, negative for counterclockwise.
Note: This method is only available if the drivetrain is configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor in the Devices window.
Usage:
drivetrain.rotation(units)
Parámetros |
Descripción |
|---|---|
|
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)
Parámetros |
Descripción |
|---|---|
|
Optional. The unit that represents the velocity:
|
current#
current returns the current of the drivetrain in amps.
Usage:
drivetrain.current(units)
Parámetros |
Descripción |
|---|---|
|
Optional. The unit that represents the current:
|
power#
power returns the average power of the drivetrain in watts.
Usage:
drivetrain.power()
Parámetros |
Descripción |
|---|---|
This method has no parameters. |
torque#
torque returns the average torque of the drivetrain.
Usage:
drivetrain.torque(units)
Parámetros |
Descripción |
|---|---|
|
The unit that represents the torque:
|
efficiency#
efficiency returns the average efficiency of the drivetrain in percent.
Usage:
drivetrain.efficiency()
Parámetros |
Descripción |
|---|---|
This method has no parameters. |
temperature#
temperature returns the average temperature of the drivetrain.
Usage:
drivetrain.temperature(units)
Parámetros |
Descripción |
|---|---|
|
Optional. The units that represent the temperature:
|
get_timeout#
get_timeout returns the currently set timeout in milliseconds.
drivetrain.get_timeout()
Parámetros |
Descripción |
|---|---|
This method has no parameters. |
Constructor#
Constructors are used to manually create DriveTrain and SmartDrive objects, which are necessary for configuring a drivetrain outside of VEXcode.
DriveTrain#
DriveTrain creates a drivetrain without an Inertial Sensor, GPS Sensor, or Gyro Sensor.
Usage:
DriveTrain(lm, rm, wheelTravel, trackWidth, wheelBase, units, externalGearRatio)
Parámetro |
Descripción |
|---|---|
|
The name of the Left Motor or Motor Group. |
|
The name of the Right Motor or Motor Group. |
|
Optional. The circumference of the driven 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 units that represent
|
|
Optional. The gear ratio used to compensate drive distances if gearing is used. |
Note: Motors and/or Motor Groups must be created first before they can be used to create an object with the DriveTrain constructor.
# Create the Motors
left_motor = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
right_motor = Motor(Ports.PORT2, GearSetting.RATIO_18_1, True)
# Create a drivetrain with default values
drivetrain = DriveTrain(left_motor, right_motor)
# Create the Motors
left_motor = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
right_motor = Motor(Ports.PORT2, GearSetting.RATIO_18_1, True)
"""
Create a drivetrain with the following values:
- wheelTravel = 319.19
- trackWidth = 295
- wheelBase = 40
- units = MM (Millimeters)
- externalGearRatio - 1
"""
drivetrain = DriveTrain(left_motor, right_motor, 319.19, 295, 40, MM, 1)
SmartDrive#
SmartDrive creates a drivetrain configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor.
Usage:
SmartDrive(lm, rm, g, wheelTravel, trackWidth, wheelBase, units, externalGearRatio)
Parámetro |
Descripción |
|---|---|
|
The name of the Left Motor or Motor Group. |
|
The name of the Right Motor or Motor Group. |
|
The name of the Inertial Sensor, GPS Sensor, or Gyro Sensor. |
|
Optional. The circumference of the driven 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 units that represent
|
|
Optional. The gear ratio used to compensate drive distances if gearing is used. |
Note: Motors and/or Motor Groups must be created first before they can be used to create an object with the SmartDrive constructor.
# Create the Motors
left_motor = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
right_motor = Motor(Ports.PORT2, GearSetting.RATIO_18_1, True)
# Create a Gyro Sensor in Port A
gyro_a = Gyro(brain.three_wire_port.a)
# Create a drivetrain with default values
drivetrain = DriveTrain(left_motor, right_motor, gyro_a)
# Create the Motors
left_motor = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
right_motor = Motor(Ports.PORT2, GearSetting.RATIO_18_1, True)
# Create a Gyro Sensor in Port A
gyro_a = Gyro(brain.three_wire_port.a)
"""
Create a drivetrain with the following values:
- wheelTravel = 319.19
- trackWidth = 295
- wheelBase = 40
- units = MM (Millimeters)
- externalGearRatio - 1
"""
drivetrain = DriveTrain(left_motor, right_motor, gyro_a, 319.19, 295, 40, MM, 1)