Drivetrain#
Introduction#
The drivetrain class provides control over a drivetrain using separate left and right motors or motor groups.
Derived Classes#
The drivetrain class serves as a base class for the following derived classes:
smartdrive— This class adds Inertial Sensor, GPS Sensor, or Gyro Sensor support to enable heading-based and rotation-based turns.
Class Constructors#
|
This constructor constructs a drivetrain using references to existing motor_group instances for the left (&l) and right (&r) sides.
|
|
This constructor constructs a drivetrain using references to existing motor instances for the left (&l) and right (&r) sides.
|
Class Destructor#
Destroys the drivetrain object and releases associated resources.
virtual ~drivetrain();
参数#
Parameter |
Type |
Description |
|---|---|---|
|
|
References to existing |
|
|
The wheel’s circumference. Default is |
|
|
The distance between the left and right wheels. Default is |
|
|
The width of the wheel base. Default is |
|
|
Units for the
|
|
|
The gear ration compensation factor. Default is |
例子#
// Create the left and right motor instances
motor leftMotor = motor(PORT1, false);
motor rightMotor = motor(PORT9, true);
// Create the drivetrain instance
drivetrain myDrivetrain = drivetrain(
leftMotor, // left motor instance
rightMotor, // right motor instance
259.34, // wheelTravel
320, // trackWidth
40, // wheelBase
mm, // unit
1.0 ); // externalGearRatio
Member Functions#
The drivetrain class includes the following member functions:
drive— Drives the robot continuously forward or in reverse using the current drive velocity.driveFor— Drives the robot forward or in reverse for a set distance.turn— Rotates the robot continuously left or right using the current turn velocity.turnFor— Rotates the robot left or right for a specified angle.stop— Stops the drivetrain using the configured stopping mode.setDriveVelocity— Sets the default drive velocity used by drive methods.setTurnVelocity— Sets the default turn velocity used by turn methods.setStopping— Sets how the drivetrain behaves when it stops.setTimeout— Sets how long drive and turn methods try to reach their target before timing out.isDone— Returns whether a drivetrain is not currently moving.isMoving— Returns whether a drivetrain is currently moving.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.voltage— Returns the voltage currently applied to the drivetrain.
Before calling any drivetrain member functions, a drivetrain instance must be created, as shown below:
/* This constructor is required when using VS Code.
Drivetrain configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */
// Create the left and right motor instances
motor leftMotor = motor(PORT1, false);
motor rightMotor = motor(PORT9, true);
// Create the drivetrain instance
drivetrain myDrivetrain = drivetrain(
leftMotor, // left motor instance
rightMotor, // right motor instance
259.34, // wheelTravel
320, // trackWidth
40, // wheelBase
mm, // unit
1.0 ); // externalGearRatio
drive#
Moves the drivetrain in a specified direction continuously.
Available Functions| 1 |
|
|
| 2 |
|
|
Parameter |
Type |
Description |
|---|---|---|
|
|
The direction in which the robot drives:
|
|
|
The velocity at which the drivetrain will move as a double. If the velocity is not specified or previously set, the default velocity is 50%. |
|
|
The unit to represent the velocity:
|
This function does not return a value.
Notesdriveis non-waiting; the project will continue executing the next line of code immediately after the call.The drivetrain will continue moving until
stopis called or another drivetrain movement function (such asdriveFororturn) is executed.Functions such as
isDoneandisMovingare not applicable todrive, since it does not use target-based movement (doesn’t have adistanceparameter).
// Drive forward and back
myDrivetrain.drive(forward);
wait(2, seconds);
myDrivetrain.drive(reverse, 25, rpm);
wait(2, seconds);
myDrivetrain.stop();
driveFor#
Moves the drivetrain in a specified direction for a specified distance in the specified units.
Available Functions| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
Parameters
Parameter |
Type |
Description |
|---|---|---|
|
|
The direction in which the robot drives:
|
|
|
The distance the drivetrain will move. |
|
|
The units representing the distance:
|
|
|
The velocity at which the drivetrain will move as a double. If the velocity is not specified or previously set, the default velocity is 50%. |
|
|
The unit to represent the velocity:
|
|
|
Optional.
|
Returns a Boolean indicating whether the drivetrain reached the target distance parameter value:
true— The drivetrain successfully reaches the targetdistanceparameter value.false— The drivetrain does not complete the movement or if thewaitForCompletionparameter is set tofalse.
Executing another drivetrain movement function (such as
driveorturnFor) whiledriveForis in progress will interrupt the current movement.Because
driveForis target-based (uses adistanceparameter), functions such asisDoneandisMovingwork withdriveFor.
// Drive 200 mm, then back 200 mm
myDrivetrain.driveFor(forward, 200, mm);
myDrivetrain.driveFor(reverse, 200, mm);
turn#
Rotates the drivetrain in a specified direction continuously.
Available Functions| 1 |
|
|
| 2 |
|
|
Parameter |
Type |
Description |
|---|---|---|
|
|
The direction in which the robot turns:
|
|
|
The velocity at which the drivetrain will turn as a double. If the velocity is not specified or previously set, the default velocity is 50%. |
|
|
The unit to represent the velocity:
|
turnis non-waiting; the project will continue executing the next line of code immediately after the call.The drivetrain will continue turning until
stopis called or another drivetrain movement function (such asturnForordrive) is executed.Functions such as
isDoneandisMovingare not applicable toturn, since it does not use target-based movement (doesn’t have anangleparameter).
// Turn right, then left, then stop
myDrivetrain.turn(right);
wait(2, seconds);
myDrivetrain.turn(left);
wait(2, seconds);
myDrivetrain.stop();
turnFor#
Rotates the drivetrain in a specified direction for a specified angle in the specified units.
Available Functions| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
Parameter |
Type |
Description |
|---|---|---|
|
|
The direction in which the robot turns:
|
|
|
The angle the drivetrain will turn. |
|
|
The units representing the angle:
|
|
|
The velocity at which the drivetrain will turn as a double. If the velocity is not specified or previously set, the default velocity is 50%. |
|
|
The unit to represent the velocity:
|
|
|
Optional.
|
Returns a Boolean indicating whether the drivetrain reached the target angle parameter value:
true— The drivetrain successfully reaches the targetangleparameter value.false— The drivetrain does not complete the movement or if thewaitForCompletionparameter is set tofalse.
Executing another drivetrain movement function (such as
turnordrive) whileturnForis in progress will interrupt the current movement.Because
turnForis target-based (uses anangleparameter), functions such asisDoneandisMovingwork withturnFor.
// Turn right then left
myDrivetrain.turnFor(right, 90, degrees);
wait(1, seconds);
myDrivetrain.turnFor(left, 90, degrees);
stop#
Stops the drivetrain.
Available Functions| 1 |
|
|
| 2 |
|
|
Parameter |
Type |
Description |
|---|---|---|
|
|
The brake type to use when stopping the drivetrain:
|
This function does not return a value.
Examples// Drive forward, then coast to a stop
myDrivetrain.setDriveVelocity(100, percent);
myDrivetrain.drive(forward);
wait(2, seconds);
myDrivetrain.stop(coast);
setDriveVelocity#
Sets the default drive velocity for the drivetrain.
Available Functionsvoid setDriveVelocity(
double velocity,
velocityUnits units );
Parameter |
Type |
Description |
|---|---|---|
|
|
The velocity at which the drivetrain will move. |
|
|
The unit to represent the velocity:
|
This function does not return a value.
NotesThe drive velocity applies to all subsequent drivetrain movement functions (such as
driveanddriveFor) unless a specific velocity is provided in the function call.
// Drive forward, then coast to a stop
myDrivetrain.setDriveVelocity(100, percent);
myDrivetrain.drive(forward);
wait(2, seconds);
myDrivetrain.stop(coast);
setTurnVelocity#
Sets the default turn velocity for the drivetrain.
Available Functionsvoid setTurnVelocity(
double velocity,
velocityUnits units
);
Parameter |
Type |
Description |
|---|---|---|
|
|
The velocity at which the drivetrain will turn. |
|
|
The units representing the velocity:
|
This function does not return a value.
NotesThe turn velocity applies to all subsequent drivetrain turning functions (such as
turnandturnFor) unless a specific velocity is provided in the function call.
// Try default, slow, then fast
myDrivetrain.turnFor(right, 360, degrees);
wait(1, seconds);
myDrivetrain.setTurnVelocity(20, percent);
myDrivetrain.turnFor(right, 360, degrees);
wait(1, seconds);
myDrivetrain.setTurnVelocity(100, percent);
myDrivetrain.turnFor(right, 360, degrees);
setStopping#
Sets the default stopping mode for the drivetrain.
Available Functionsvoid setStopping( brakeType mode );
Parameter |
Type |
Description |
|---|---|---|
|
|
The brake type to use when stopping the drivetrain:
|
This function does not return a value.
NotesThe stopping mode applies to all subsequent
stopcalls unless a specific stopping mode is provided in the function call.
setTimeout#
Sets a time limit for how long the drivetrain will attempt to reach a target distance or angle. If the drivetrain cannot complete the movement within the set time, it will stop automatically and continue with the next function.
Available Functionsvoid setTimeout(
int32_t time,
timeUnits units );
Parameter |
Type |
Description |
|---|---|---|
|
|
The maximum number of seconds a |
|
|
The unit that represents the time:
|
This function does not return a value.
NotesThe timeout only applies to target-based drivetrain movement functions (such as
driveForandturnFor).
// Start turning if driving takes too long
myDrivetrain.setTimeout(1, seconds);
myDrivetrain.driveFor(forward, 25, inches);
myDrivetrain.turnFor(right, 90, degrees);
isDone#
Returns a Boolean indicating whether no target-based drivetrain movement (such as driveFor or turnFor) is currently in progress.
bool isDone( void );
This function does not accept any parameters.
Return ValuesReturns a Boolean indicating whether the drivetrain is not moving:
-
true— The drivetrain is not moving. -
false— The drivetrain is moving.
isMoving#
Returns a Boolean indicating whether the drivetrain is currently executing a target-based movement (such as driveFor or turnFor).
virtual bool isMoving( void );
This function does not accept any parameters.
Return ValuesReturns a Boolean indicating whether the drivetrain is moving:
-
true— The drivetrain is moving. -
false— The drivetrain is not moving.
velocity#
Returns the velocity of the drivetrain.
Available Functionsdouble velocity( velocityUnits units );
Parameter |
Type |
Description |
|---|---|---|
|
|
The units that represent the velocity:
|
Returns a double indicating the velocity of the drivetrain in the units specified by the units parameter.
// Show velocity before and during motion
Brain.Screen.print("Resting: %f", myDrivetrain.velocity(percent));
myDrivetrain.drive(forward, 100, velocityUnits::pct);
wait(1, seconds);
Brain.Screen.newLine();
Brain.Screen.print("Moving: %f", myDrivetrain.velocity(percent));
myDrivetrain.stop();
current#
Returns the amount of electrical current the drivetrain is using.
Available Functions| 1 |
|
Accept a currentUnits parameter |
| 2 |
|
Accept a percentUnits parameter |
Parameter |
Type |
Description |
|---|---|---|
|
|
The units that represent the current:
|
|
|
The units that represent the current:
|
Returns a double indicating the amount of electrical current the drivetrain is using in the units specified by the units parameter.
power#
Returns the drivetrain’s power consumption.
Available Functionsdouble power( powerUnits units = powerUnits::watt );
Parameter |
Type |
Description |
|---|---|---|
|
|
Optional. The units that represent the power:
|
Returns a double representing the drivetrain’s power consumption in watts.
torque#
Returns the torque produced by the drivetrain.
Available Functionsdouble torque( torqueUnits units = torqueUnits::nm );
Parameter |
Type |
Description |
|---|---|---|
|
|
Optional. The units representing the torque:
|
Returns a double indicating the torque produced by the drivetrain in the units specified by the units parameter.
efficiency#
Returns the efficiency of the drivetrain.
Available Functionsdouble efficiency( percentUnits units = percentUnits::pct );
Parameter |
Type |
Description |
|---|---|---|
|
|
Optional. The units that represent the efficiency:
|
Returns a double representing the drivetrain’s efficiency as a percentage.
temperature#
Returns the average temperature of the motors used by the drivetrain.
Available Functionsdouble temperature( percentUnits units );
Parameter |
Type |
Description |
|---|---|---|
|
|
The units that represent the temperature:
|
Returns a double indicating the average temperature of the motors used by the drivetrain as a percentage.
NotesA returned value of
50%indicates that the average motor temperature is approximately45°C(113°F).The typical operating temperature range for drivetrain motors is approximately
20°C(68°F) to70°C(158°F).
voltage#
Returns the electrical voltage of the drivetrain.
Available Functionsdouble voltage( voltageUnits units = voltageUnits::volt );
Parameter |
Type |
Description |
|---|---|---|
|
|
Optional. The units that represent the voltage:
|
Returns a double indicating the voltage of the drivetrain in the units specified by the units parameter.