smartdrive#
Introduction#
The smartdrive class is derived from the drivetrain base class and incorporates an Inertial Sensor, Gyro Sensor, or GPS Sensor for heading and rotation-based functions.
Class Constructors#
|
This constructor constructs a smartdrive using references to existing motor_group instances for the left (&l) and right (&r) sides.
|
|
This constructor constructs a smartdrive using references to existing motor instances for the left (&l) and right (&r) sides.
|
Class Destructor#
Destroys the smartdrive object and releases associated resources.
~smartdrive();
Parameters#
Parameter |
Type |
Description |
|---|---|---|
|
|
References to existing |
|
|
A reference to an 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 |
Example#
/* 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 inertial instance
inertial myInertial = inertial(PORT3);
// Create the smartdrive instance
smartdrive myDrivetrain = smartdrive(
leftMotor, // left motor object
rightMotor, // right motor object
myInertial, // inertial object
259.34, // wheelTravel
320, // trackWidth
40, // wheelBase
mm, // unit
1.0 ); // externalGearRatio
Member Functions#
The smartdrive class includes the following member functions:
turnToHeading— Rotates the robot to face a specific absolute heading.turnToRotation— Rotates the robot to reach a specific cumulative rotation.calibrateDrivetrain— Calibrates the drivetrain’s configured Inertial Sensor, GPS Sensor, or Gyro Sensor.setHeading— Manually sets the robot’s heading value.setRotation— Manually sets the robot’s cumulative rotation value.heading— Returns the drivetrain’s heading angle.rotation— Returns how much the drivetrain has turned since the project started.
Before calling any smartdrive member functions, a smartdrive 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 inertial instance
inertial myInertial = inertial(PORT3);
// Create the smartdrive instance
smartdrive myDrivetrain = smartdrive(
leftMotor, // left motor object
rightMotor, // right motor object
myInertial, // inertial object
259.34, // wheelTravel
320, // trackWidth
40, // wheelBase
mm, // unit
1.0 ); // externalGearRatio
turnToHeading#
Rotates the drivetrain to a specified heading.
Available Functions| 1 |
|
|
| 2 |
|
|
Parameter |
Type |
Description |
|---|---|---|
|
|
The heading the drivetrain will turn to. |
|
|
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 heading parameter value:
true— The drivetrain successfully reaches the targetheadingparameter value.false— The drivetrain does not complete the movement or if thewaitForCompletionparameter is set tofalse.
Executing another drivetrain movement function (such as
turnordrive) whileturnToHeadingis in progress will interrupt the current movement.Because
turnToHeadingis target-based (uses anangleparameter), functions such asisDoneandisMovingwork withturnToHeading.
// Face the new 0 degrees
myDrivetrain.setHeading(90, degrees);
myDrivetrain.turnToHeading(0, degrees);
turnToRotation#
Rotates the drivetrain to a specified rotational value.
Available Functions| 1 |
|
|
| 2 |
|
|
Parameter |
Type |
Description |
|---|---|---|
|
|
The rotational value the drivetrain will turn to. |
|
|
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 rotation parameter value:
true— The drivetrain successfully reaches the targetrotationparameter value.false— The drivetrain does not complete the movement or if thewaitForCompletionparameter is set tofalse.
Executing another drivetrain movement function (such as
turnordrive) whileturnToRotationis in progress will interrupt the current movement.Because
turnToRotationis target-based (uses anangleparameter), functions such asisDoneandisMovingwork withturnToRotation.
// Spin counterclockwise for 2 turns, then face forward
myDrivetrain.setRotation(720, degrees);
myDrivetrain.turnToRotation(0, degrees);
calibrateDrivetrain#
Calibrates the drivetrain.
calibrateDrivetrain is a helper function generated automatically by VEXcode when a drivetrain is configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor. It is provided as part of the project source and is not part of the VEX V5 C++ API.
calibrateDrivetrain();
This function does not accept any parameters.
Return ValuesThis function does not return a value.
NotesThe robot calibrates automatically at the start of each project.
During calibration, ensure the robot is on a flat surface and remains motionless.
The following is the auto-generated implementation of calibrateDrivetrain created by VEXcode:
bool vexcode_initial_drivetrain_calibration_completed = false;
void calibrateDrivetrain() {
wait(200, msec);
Brain.Screen.print("Calibrating");
Brain.Screen.newLine();
Brain.Screen.print("Inertial");
DrivetrainInertial.calibrate();
while (DrivetrainInertial.isCalibrating()) {
wait(25, msec);
}
vexcode_initial_drivetrain_calibration_completed = true;
// Clears the screen and returns the cursor to row 1, column 1.
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
}
setHeading#
Sets the heading for the drivetrain.
Available Functionsvoid setHeading(
double value,
rotationUnits units);
Parameter |
Type |
Description |
|---|---|---|
|
|
The heading the drivetrain is currently facing. |
|
|
The units representing the heading:
|
This function does not return a value.
Examples// Face the new 0 degrees
myDrivetrain.setHeading(90, degrees);
DrivemyDrivetraintrain.turnToHeading(0, degrees);
setRotation#
Sets the rotation for the drivetrain.
Available Functionsvoid setRotation(
double value,
rotationUnits units);
Parameter |
Type |
Description |
|---|---|---|
|
|
The rotation the drivetrain is currently facing. |
|
|
The units representing the heading:
|
This function does not return a value.
Examples// Spin counterclockwise for 2 turns, then face forward
myDrivetrain.setRotation(720, degrees);
myDrivetrain.turnToRotation(0, degrees);
heading#
Returns the heading of the drivetrain.
Available Functionsdouble heading( rotationUnits units = rotationUnits::deg );
Parameter |
Type |
Description |
|---|---|---|
|
|
Optional. The units representing the heading:
|
Returns a double indicating the heading of the drivetrain in the units specified by the units parameter.
// Show heading before and after turning
Brain.Screen.print("Before: %f", myDrivetrain.heading(degrees));
myDrivetrain.turnFor(right, 90, degrees);
wait(1, seconds);
Brain.Screen.newLine();
Brain.Screen.print("After: %f", myDrivetrain.heading(degrees));
myDrivetrain.stop();
rotation#
Returns the rotation of the drivetrain.
Available Functionsdouble rotation( rotationUnits units = rotationUnits::deg );
Parameter |
Type |
Description |
|---|---|---|
|
|
Optional. The units representing the heading:
|
Returns a double indicating the rotation of the drivetrain in the units specified by the units parameter.
// Show rotation before and after turning
Brain.Screen.print("Before: %f", myDrivetrain.rotation(degrees));
myDrivetrain.turnFor(right, 90, degrees);
wait(1, seconds);
Brain.Screen.newLine();
Brain.Screen.print("After: %f", myDrivetrain.rotation(degrees));
myDrivetrain.stop();