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.

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.

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)

参数

描述

direction

The direction in which the robot drives:

  • FORWARD
  • REVERSE

velocity

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

units

Optional. The unit to represent the velocity:

  • RPM (default) – Rotations per minute
  • PERCENT
  • VelocityUnits.DPS – Degrees per second
# 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)

参数

描述

direction

The direction in which the robot drives:

  • FORWARD
  • REVERSE

distance

The distance the Drivetrain will move.

units

The units representing the distance:

  • INCHES (default)
  • MM – Millimeters

velocity

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

units_v

Optional. The unit to represent the velocity:

  • RPM (default) – Rotations per minute
  • PERCENT
  • VelocityUnits.DPS – Degrees per second

wait

Optional.

  • wait=True (default) – The project waits until drive_for is complete before executing the next line of code.
  • wait=False - The project starts the action and moves on to the next line of code right away, without waiting for drive_for to finish.

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

参数

描述

direction

The direction in which to turn:

  • LEFT
  • RIGHT

velocity

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

units

Optional. The unit that represents the velocity:

  • PERCENT
  • RPM (default) – Rotations per minute
  • VelocityUnits.DPS – Degrees per second

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

参数

描述

direction

The direction in which to turn:

  • LEFT
  • RIGHT

angle

The amount of degrees the drivetrain will turn as a float or integer.

units

The unit that represents the rotational value:

  • DEGREES (default)
  • TURNS

velocity

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

units_v

Optional. The unit that represents the velocity:

  • PERCENT
  • RPM (default) – Rotations per minute
  • VelocityUnits.DPS – Degrees per second

wait

Optional.

  • wait=True (default) – The project waits until turn_for is complete before executing the next line of code.
  • wait=False - The project starts the action and moves on to the next line of code right away, without waiting for turn_for to finish.

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

参数

描述

angle

The heading to turn the drivetrain to face as a float or integer.

units

The unit that represents the rotational value:

  • DEGREES

velocity

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

units_v

Optional. The unit that represents the velocity:

  • PERCENT
  • RPM (default) – Rotations per minute
  • VelocityUnits.DPS – Degrees per second

wait

Optional.

  • wait=True (default) – The project waits until turn_to_heading is complete before executing the next line of code.
  • wait=False - The project starts the action and moves on to the next line of code right away, without waiting for turn_to_heading to finish.

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

参数

描述

angle

The rotational value to turn the drivetrain to face as a float or integer.

units

The unit that represents the rotational value:

  • DEGREES
  • TURNS

velocity

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

units_v

Optional. The unit that represents the velocity:

  • PERCENT
  • RPM (default) – Rotations per minute
  • VelocityUnits.DPS – Degrees per second

wait

Optional.

  • wait=True (default) – The project waits until turn_to_rotation is complete before executing the next line of code.
  • wait=False - The project starts the action and moves on to the next line of code right away, without waiting for turn_to_heading to finish.

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

参数

描述

mode

Optional. The stopping behavior to use when the drivetrain stops:

  • BRAKE – Stops immediately.
  • COAST – Slows gradually to a stop.
  • HOLD – Stops and resists movement using motor feedback.
If the stopping mode is not specified or previously set, the default behavior is 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()

参数

描述

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)

参数

描述

velocity

The velocity at which the drivetrain will move as a float or integer.

units

Optional. The unit that represents the velocity:

  • PERCENT
  • RPM (default) – Rotations per minute
  • VelocityUnits.DPS – Degrees per second

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

参数

描述

velocity

The velocity at which the drivetrain will turn as a float or integer.

units

Optional. The unit that represents the velocity:

  • PERCENT
  • RPM (default) – Rotations per minute
  • VelocityUnits.DPS – Degrees per second

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

参数

描述

mode

How the drivetrain will stop:

  • BRAKE – Stops immediately.
  • COAST – Slows gradually to a stop.
  • HOLD – Stops and resists movement using motor feedback.

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)

参数

描述

value

The maximum number of seconds a motor command will run before stopping and moving to the next command as an integer or float.

units

Optional. The unit that represents the time:

  • SECONDS
  • MSEC (default) – Milliseconds

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)

参数

描述

heading

The new heading as a float or integer.

units

Optional. The unit that represents the heading:

  • DEGREES (default)
  • TURNS

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

参数

描述

rotation

The new rotational value as a float or integer.

units

Optional. The unit that represents the heading:

  • DEGREES (default)
  • TURNS

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

参数

描述

value

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)

参数

描述

value

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)

参数

描述

value

Boolean value to set the direction reversed or not:

  • True – Reverse the smart drivetrain’s direction
  • False – Return the smart drivetrain’s direction to its default

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

参数

描述

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

参数

描述

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)

参数

描述

units

Optional. The unit that represents the rotational value:

  • DEGREES (default) – 0.00 to 359.99
  • TURNS

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

参数

描述

units

Optional. The unit that represents the rotational value:

  • DEGREES (default)
  • TURNS

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

参数

描述

units

Optional. The unit that represents the velocity:

  • PERCENT
  • RPM (default) – Rotations per minute
  • VelocityUnits.DPS – Degrees per second

current#

current returns the current of the drivetrain in amps.

Usage:
drivetrain.current(units)

参数

描述

units

Optional. The unit that represents the current:

  • CurrentUnits.AMP – Amps

power#

power returns the average power of the drivetrain in watts.

Usage:
drivetrain.power()

参数

描述

This method has no parameters.

torque#

torque returns the average torque of the drivetrain.

Usage:
drivetrain.torque(units)

参数

描述

units

The unit that represents the torque:

  • TorqueUnits.NM (default) – Newton meters
  • TorqueUnits.INLB – Inch pounds

efficiency#

efficiency returns the average efficiency of the drivetrain in percent.

Usage:
drivetrain.efficiency()

参数

描述

This method has no parameters.

temperature#

temperature returns the average temperature of the drivetrain.

Usage:
drivetrain.temperature(units)

参数

描述

units

Optional. The units that represent the temperature:

  • TemperatureUnits.CELSIUS (default)
  • TemperatureUnits.FAHRENHEIT

get_timeout#

get_timeout returns the currently set timeout in milliseconds.

drivetrain.get_timeout()

参数

描述

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)

范围

描述

lm

The name of the Left Motor or Motor Group.

rm

The name of the Right Motor or Motor Group.

wheelTravel

Optional. The circumference of the driven wheels. The default is 300 millimeters.

trackWidth

Optional. The track width of the Drivetrain. The default is 320 millimeters.

wheelBase

Optional. The wheel base of the Drivetrain. The default is 320 millimeters.

units

Optional. The units that represent wheelTravel, trackWidth and wheelBase:

  • MM (default) – Millimeters
  • INCHES

externalGearRatio

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)

范围

描述

lm

The name of the Left Motor or Motor Group.

rm

The name of the Right Motor or Motor Group.

g

The name of the Inertial Sensor, GPS Sensor, or Gyro Sensor.

wheelTravel

Optional. The circumference of the driven wheels. The default is 300 millimeters.

trackWidth

Optional. The track width of the Drivetrain. The default is 320 millimeters.

wheelBase

Optional. The wheel base of the Drivetrain. The default is 320 millimeters.

units

Optional. The units that represent wheelTravel, trackWidth and wheelBase:

  • MM (default) – Millimeters
  • INCHES

externalGearRatio

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)