Drivetrain#
Introduction#
The VEX IQ (2nd gen) robot uses a drivetrain to drive and turn. A drivetrain is made of motors and wheels that work together to move the robot.
A configured drivetrain can use a connected Gyro Sensor, the Brain’s built-in Inertial Sensor, or no sensor. When a sensor is used, the drivetrain uses it to help the robot move and turn precisely. At the start of each project, the drivetrain calibrates the sensor automatically. Keep the robot still for about 2 seconds during calibration, so the robot can move and turn correctly.
This page uses drivetrain as the example drivetrain name. Replace it with your own configured name as needed.
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.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.calibrate_drivetrain— Calibrates the sensor configured with the drivetrain.
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 much time 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.set_turn_threshold— Tells how close the robot must get to a target angle before a turn is complete.set_turn_constant— Changes how strongly the robot corrects its turns.
Getters — Check movement status.
heading— Returns the robot’s current heading from 0 to 359 degrees.rotation— Returns the robot’s current rotation.velocity— Returns how fast the robot is driving.current— Returns how much electrical current the drivetrain is using.is_moving— Returns whether the robot is moving, as a Boolean value.is_done— Returns whether the robot is finished moving, as a Boolean value.is_turning— Returns whether the robot is turning, as a Boolean value.power— Returns how quickly the drivetrain is using energy.torque— Returns how much torque the drivetrain is using.efficiency— Returns how efficiently the drivetrain is using power.temperature— Returns how warm the drivetrain is.
Constructors — Manually create and configure the drivetrain.
DriveTrain— Creates a drivetrain without a Gyro Sensor or Inertial Sensor.SmartDrive— Creates a drivetrain with a Gyro Sensor or Inertial Sensor.
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, velocity, units)
Parameters |
Description |
|---|---|
|
The direction the robot moves: |
|
Optional. The velocity to drive with from 0% to 100% when using |
|
Optional. The velocity unit: |
# 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 robot forward or reverse for a specific distance. The project will wait until the robot is done moving before the next line of code runs.
Usage:
drivetrain.drive_for(direction, distance, units, velocity, units_v, wait)
Parameters |
Description |
|---|---|
|
The direction the robot moves: |
|
The distance the robot drives. This can be an |
|
Optional. The distance unit: |
|
Optional. The velocity to drive with from 0% to 100% when using |
|
Optional. The velocity unit: |
|
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 robot left or right forever. The robot will continue to turn until it is given another action, like driving or stopping.
Usage:
drivetrain.turn(direction, velocity, units)
Parameters |
Description |
|---|---|
|
The direction the robot turns: |
|
Optional. The velocity to turn with from 0% to 100% when using |
|
Optional. The velocity unit: |
# 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 robot left or right for a specific number of degrees or turns. The turn is relative to the current position of the robot. The project will wait until the robot is done turning before the next line of code runs.
Usage:
drivetrain.turn_for(direction, angle, units, velocity, units_v, wait)
Parameters |
Description |
|---|---|
|
The direction the robot turns: |
|
The amount the robot turns. This can be an |
|
Optional. The turn unit: |
|
Optional. The velocity to turn with from 0% to 100% when using |
|
Optional. The velocity unit: |
|
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#
A heading is the direction the robot 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 robot’s starting heading is 0 degrees.
The project will wait until the robot is done turning before the next line of code runs.
Usage:
drivetrain.turn_to_heading(angle, units, velocity, units_v, wait)
Parameters |
Description |
|---|---|
|
The direction the robot should face, in degrees. This can be an |
|
Optional. The heading unit: |
|
Optional. The velocity to turn with from 0% to 100% when using |
|
Optional. The velocity unit: |
|
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 the robot to a specific rotation.
Rotation is how much the robot has turned, measured in degrees or turns. At the beginning of a project, the rotation value is set to 0 degrees. Rotation can also be set using the set_rotation method.
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.
The project will wait until the robot is done turning before the next line of code runs.
Usage:
drivetrain.turn_to_rotation(angle, units, velocity, units_v, wait)
Parameters |
Description |
|---|---|
|
The rotation value, in degrees or turns, that the robot will turn to. This can be an |
|
Optional. The rotation unit: |
|
Optional. The velocity to turn with from 0% to 100% when using |
|
Optional. The velocity unit: |
|
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 the robot’s movement.
Usage:
drivetrain.stop(mode)
Parameters |
Description |
|---|---|
|
Optional. How the robot 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 is a built-in function that calibrates the Gyro Sensor or Inertial Sensor configured with the drivetrain.
Calibration helps the sensor measure movement correctly. Keep the robot still for about 2 seconds during calibration. If the robot moves during calibration, the sensor may not measure movement correctly.
The project will wait until calibration is done before the next line of code runs.
Note: This function cannot be used inside of VS Code.
Usage:
calibrate_drivetrain()
Parameters |
Description |
|---|---|
This function has no parameters. |
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 it can be more precise.
Usage:
drivetrain.set_drive_velocity(velocity, units)
Parameters |
Description |
|---|---|
|
The velocity to drive with from 0% to 100% when using |
|
Optional. The velocity unit: |
# 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 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 it can be more precise.
Usage:
drivetrain.set_turn_velocity(velocity, units)
Parameters |
Description |
|---|---|
|
The velocity to turn with from 0% to 100% when using |
|
Optional. The velocity unit: |
# 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 how the robot will stop moving: by braking, coasting, or holding.
Usage:
drivetrain.set_stopping(mode)
Parameters |
Description |
|---|---|
|
How the robot will stop:
|
If this method is not used, the robot will use BRAKE when stopping.
set_timeout#
set_timeout sets how much time 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 |
|---|---|
|
The amount of time the robot can try to finish a movement. This can be a positive |
|
Optional. The unit of time: |
# Turn right after driving forward
drivetrain.set_timeout(1, SECONDS)
drivetrain.drive_for(FORWARD, 25, INCHES)
drivetrain.turn_for(RIGHT, 90)
set_heading#
A heading is the direction the robot 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(heading, units)
Parameters |
Description |
|---|---|
|
The heading value, in degrees or turns, to set for the robot. This can be an |
|
Optional. The heading unit: |
# Face the new 0 degrees
drivetrain.set_heading(90, DEGREES)
drivetrain.turn_to_heading(0, DEGREES)
set_rotation#
Rotation is how much the robot has turned, measured in degrees or turns. 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(rotation, units)
Parameters |
Description |
|---|---|
|
The rotation value, in degrees or turns, to set for the robot. This can be an |
|
Optional. The rotation unit: |
# Spin counterclockwise two times
drivetrain.set_rotation(720, DEGREES)
drivetrain.turn_to_rotation(0, DEGREES)
set_turn_threshold#
set_turn_threshold sets how close the robot must get to a target angle before a turn is complete.
A smaller threshold can make turns more precise, but the robot may take longer to finish turning. A larger threshold can make turns finish sooner, but they may be less precise.
Usage:
drivetrain.set_turn_threshold(value)
Parameters |
Description |
|---|---|
|
The turn threshold, in degrees. This can be an |
# Set the turn threshold to 5 degrees
drivetrain.set_turn_threshold(5)
set_turn_constant#
set_turn_constant changes how strongly the robot corrects its turns.
A higher value makes the robot correct turns more strongly. A lower value makes the robot correct turns more gently. The default value is 1.0.
Usage:
drivetrain.set_turn_constant(value)
Parameters |
Description |
|---|---|
|
The turn constant. This can be a decimal ( |
# Increase the turn constant to 2.0
drivetrain.set_turn_constant(2.0)
Getters#
heading#
A heading is the direction the robot is facing, measured in degrees. heading returns the robot’s current heading from 0 to 359 degrees.
The robot’s starting heading is 0 degrees.
Usage:
drivetrain.heading(units)
Parameters |
Description |
|---|---|
|
Optional. The unit to return heading in: |
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print("Heading: ")
brain.screen.print(drivetrain.heading())
rotation#
Rotation is how much the robot has turned, measured in degrees or turns. At the beginning of a project, the rotation value is set to 0 degrees. 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.
Usage:
drivetrain.rotation(units)
Parameters |
Description |
|---|---|
|
Optional. The unit to return rotation in: |
# Display the rotation after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print("Rotation: ")
brain.screen.print(drivetrain.rotation())
velocity#
velocity returns how fast the robot is driving.
It can return velocity as a percentage from -100% to 100%, in RPM from -127 rpm to 127 rpm, or in VelocityUnits.DPS (degrees per second). A positive value means the robot is driving forward. A negative value means the robot is driving in reverse.
Usage:
drivetrain.velocity(units)
Parameters |
Description |
|---|---|
|
Optional. The unit to return velocity in: |
current#
current returns how much electrical current the drivetrain is using, measured in amps (amperes). Current is the amount of electricity flowing through the drivetrain.
A higher current value means the drivetrain is using more electrical current. This can happen when the robot is pushing against an object or trying to move when it is stuck.
This can be used to check if the drivetrain is struggling during a movement. If current stays high, the drivetrain may get warmer or use power less efficiently.
Usage:
drivetrain.current(units)
Parameters |
Description |
|---|---|
|
Optional. The unit to return current in: |
is_moving#
is_moving returns whether the robot is 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 moving.False— The robot is not moving.
This method works together with the following Drivetrain methods that have the wait parameter: drive_for, turn_for, turn_to_heading, and turn_to_rotation.
Usage:
drivetrain.is_moving()
Parameters |
Description |
|---|---|
This method has no parameters. |
is_done#
is_done 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, turn_for, turn_to_heading, and turn_to_rotation.
Usage:
drivetrain.is_done()
Parameters |
Description |
|---|---|
This method has no parameters. |
is_turning#
is_turning returns whether the robot is turning, 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 turning.False— The robot is not turning.
This method works together with the following Drivetrain methods that have the wait parameter: turn_for, turn_to_heading, and turn_to_rotation.
Usage:
drivetrain.is_turning()
Parameters |
Description |
|---|---|
This method has no parameters. |
power#
power returns how much power the drivetrain is using, measured in watts. Power shows how quickly the drivetrain is using energy.
A higher power value means the drivetrain is using energy faster. This can happen when the robot is pushing against an object or trying to move when it is stuck.
This can be used to compare movements or check if the drivetrain is struggling. If power stays high, the drivetrain may get warmer or use energy less efficiently.
Usage:
drivetrain.power()
Parameters |
Description |
|---|---|
This method has no parameters. |
torque#
Torque shows how hard the drivetrain can push or pull while the wheels spin.
torque returns how much torque the drivetrain is using, measured in Newton-meters (Nm) or inch-pounds (InLb).
A higher torque value means the drivetrain is pushing or pulling harder. This can happen when the robot is pushing against an object or trying to move when it is stuck.
This can be used to check if the drivetrain is struggling or to compare how much push different movements need.
Usage:
drivetrain.torque(units)
Parameters |
Description |
|---|---|
|
Optional. The unit to return torque in: |
efficiency#
efficiency returns how efficiently the drivetrain is using power, as a percentage from 0% to 100%.
Efficiency shows how much of the drivetrain’s power is being used for movement. A higher efficiency value means more of the drivetrain’s power is being used to move. A lower efficiency value can happen when the drivetrain is working hard but not moving much, like when the robot is stuck or pushing against an object.
This can be used to compare movements or check if the drivetrain is wasting power instead of using it for movement.
Usage:
drivetrain.efficiency()
Parameters |
Description |
|---|---|
This method has no parameters. |
temperature#
temperature returns the average temperature of the drivetrain in the selected temperature unit.
Temperature shows how warm the drivetrain motors are. A higher temperature means the motors are getting warmer while they work. The motors should stay below 55°C to keep working at full performance.
If the motors get too hot, they will lower their maximum current to protect themselves. At 70°C, the motors will stop running until they cool down.
This can be used to check if the drivetrain is getting too hot during repeated movements, long runs, or when it is pushing against an object.
Usage:
drivetrain.temperature(units)
Parameters |
Description |
|---|---|
|
Optional. The unit to return temperature in: |
Constructors#
Constructors are used to create DriveTrain and SmartDrive objects in code. This is useful when configuring a drivetrain without using the Devices window.
At least two Motor or MotorGroup objects must be created before creating a drivetrain.
DriveTrain#
DriveTrain creates a drivetrain without a Gyro Sensor or Inertial Sensor.
Usage:
DriveTrain(lm, rm, wheelTravel, trackWidth, wheelBase, units, externalGearRatio)
Parameter |
Description |
|---|---|
|
The left motor or motor group. |
|
The right motor or motor group. |
|
Optional. The circumference of the drivetrain wheels. The default is 300 millimeters. |
|
Optional. The width between the left and right wheels. The default is 320 millimeters. |
|
Optional. The distance between the front and back wheels. The default is 320 millimeters. |
|
Optional. The unit for |
|
Optional. The gear ratio used to adjust drive distances if gears are used. This can be an |
# Create left and right drive motor objects
left_drive_smart = Motor(Ports.PORT1, 1.0, False)
right_drive_smart = Motor(Ports.PORT2, 1.0, True)
'''
Create a Drivetrain with the following values:
wheelTravel: 200
trackWidth: 173
wheelBase: 76
units: MM
externalGearRatio: 1
'''
drivetrain = DriveTrain(left_drive_smart, right_drive_smart, 200, 173, 76, MM, 1)
# Create two motor group objects for left and right 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)
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)
'''
Create a Drivetrain with the following values:
wheelTravel: 200
trackWidth: 173
wheelBase: 76
units: MM
externalGearRatio: 1
'''
drivetrain = DriveTrain(left_drive_smart, right_drive_smart, 200, 173, 76, MM, 1)
SmartDrive#
SmartDrive creates a drivetrain with a Gyro Sensor or Inertial Sensor.
Usage:
SmartDrive(lm, rm, g, wheelTravel, trackWidth, wheelBase, units, externalGearRatio)
Parameter |
Description |
|---|---|
|
The left motor or motor group. |
|
The right motor or motor group. |
|
The Gyro Sensor or Inertial Sensor used by the drivetrain. |
|
Optional. The circumference of the drivetrain wheels. The default is 300 millimeters. |
|
Optional. The width between the left and right wheels. The default is 320 millimeters. |
|
Optional. The distance between the front and back wheels. The default is 320 millimeters. |
|
Optional. The unit for |
|
Optional. The gear ratio used to adjust drive distances if gears are used. This can be an |
# Create the Inertial Sensor
brain_inertial = Inertial()
# Make left and right drive motors
left_drive_smart = Motor(Ports.PORT1, 1.0, False)
right_drive_smart = Motor(Ports.PORT6, 1.0, True)
# Create a SmartDrive object with wheelTravel 200
drivetrain = SmartDrive(left_drive_smart, right_drive_smart, brain_inertial, 200)
If making a smart drivetrain with multiple motors, you need to create the motors separately before grouping them into a motor group.
# Create the Inertial Sensor
brain_inertial = Inertial()
# Create two motor groups for left and right 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)
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)
# Create a SmartDrive object with wheelTravel 200 mm
drivetrain = SmartDrive(left_drive_smart, right_drive_smart, brain_inertial, 200)