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#

smartdrive(
    motor_group     &l,
    motor_group     &r,
    guido           &g,
    double          wheelTravel = 320,
    double          trackWidth  = 320,
    double          wheelBase   = 130,
    distanceUnits   unit = mm,
    double          externalGearRatio = 1.0 );
This constructor constructs a smartdrive using references to existing motor_group instances for the left (&l) and right (&r) sides.
smartdrive(
    motor           &l,
    motor           &r,
    guido           &g,
    double          wheelTravel = 320,
    double          trackWidth  = 320,
    double          wheelBase   = 130,
    distanceUnits   unit = mm,
    double          externalGearRatio = 1.0 );
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

&l / &r

motor_group / motor

References to existing motor or motor_group instances. Both parameters must be of the same type: either two motor_group instances or two motor instances. The &l parameter refers to the left motor or motor group, and &r refers to the right motor or motor group.

&g

guido

A reference to an existing gps, gyro, or inertial instance.

wheelTravel

double

The wheel’s circumference. Default is 320, expressed in millimeters by default or in the units specified by the unit parameter.

trackWidth

double

The distance between the left and right wheels. Default is 320, expressed in millimeters by default or in the units specified by the unit parameter.

wheelBase

double

The width of the wheel base. Default is 130, expressed in millimeters by default or in the units specified by the unit parameter.

unit

distanceUnits

Units for the wheelTravel, trackWidth, and wheelBase parameters:

  • mm (default) — millimeters
  • inches

externalGearRatio

double

The gear ration compensation factor. Default is 1.0.

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:

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
bool turnToHeading( 
  double          angle, 
  rotationUnits   units,  
  bool            waitForCompletion=true );
2
bool turnToHeading( 
  double          angle, 
  rotationUnits   units, 
  double          velocity, 
  velocityUnits   units_v, 
  bool            waitForCompletion=true );
  • Uses the velocity provided in the function call
Parameters

Parameter

Type

Description

angle

double

The heading the drivetrain will turn to.

units

rotationUnits

The units representing the angle:

  • deg — degrees
  • rev — revolutions
  • raw — raw units

velocity

double

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

units_v

velocityUnits

The unit to represent the velocity:

  • velocityUnits::pct — percent
  • rpm — rotations per minute
  • dps — degrees per second

waitForCompletion

bool

Optional.

  • true (default) — The project waits until the function finishes before executing the next line of code.
  • false — The project starts the action and moves on to the next line of code right away, without waiting for the function to finish.

Return Values

Returns a Boolean indicating whether the drivetrain reached the target heading parameter value:

  • true — The drivetrain successfully reaches the target heading parameter value.
  • false — The drivetrain does not complete the movement or if the waitForCompletion parameter is set to false.

Notes
  • Executing another drivetrain movement function (such as turn or drive) while turnToHeading is in progress will interrupt the current movement.

  • Because turnToHeading is target-based (uses an angle parameter), functions such as isDone and isMoving work with turnToHeading.

Examples
// 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
bool turnToRotation( 
  double angle, 
  rotationUnits units,  
  bool waitForCompletion=true );
2
bool turnToRotation( 
  double angle, 
  rotationUnits units, 
  double velocity, 
  velocityUnits units_v, 
  bool waitForCompletion=true );
  • Uses the velocity provided in the function call
Parameters

Parameter

Type

Description

angle

double

The rotational value the drivetrain will turn to.

units

rotationUnits

The units representing the angle:

  • deg — degrees
  • rev — revolutions
  • raw — raw units

velocity

double

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

units_v

velocityUnits

The unit to represent the velocity:

  • velocityUnits::pct — percent
  • rpm — rotations per minute
  • dps — degrees per second

waitForCompletion

bool

Optional.

  • true (default) — The project waits until the function finishes before executing the next line of code.
  • false — The project starts the action and moves on to the next line of code right away, without waiting for the function to finish.

Return Values

Returns a Boolean indicating whether the drivetrain reached the target rotation parameter value:

  • true — The drivetrain successfully reaches the target rotation parameter value.
  • false — The drivetrain does not complete the movement or if the waitForCompletion parameter is set to false.

Notes
  • Executing another drivetrain movement function (such as turn or drive) while turnToRotation is in progress will interrupt the current movement.

  • Because turnToRotation is target-based (uses an angle parameter), functions such as isDone and isMoving work with turnToRotation.

Examples
// 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.

Available Functions
calibrateDrivetrain();

Parameters

This function does not accept any parameters.

Return Values

This function does not return a value.

Notes
  • The robot calibrates automatically at the start of each project.

  • During calibration, ensure the robot is on a flat surface and remains motionless.

Function Implementation

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 Functions
void  setHeading( 
  double value, 
  rotationUnits units);

Parameters

Parameter

Type

Description

value

double

The heading the drivetrain is currently facing.

units

rotationUnits

The units representing the heading:

  • deg — degrees
  • rev — revolutions
  • raw — raw units

Return Values

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 Functions
void  setRotation( 
  double value, 
  rotationUnits units);

Parameters

Parameter

Type

Description

value

double

The rotation the drivetrain is currently facing.

units

rotationUnits

The units representing the heading:

  • deg — degrees
  • rev — revolutions
  • raw — raw units

Return Values

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 Functions
double heading( rotationUnits units = rotationUnits::deg );

Parameters

Parameter

Type

Description

units

rotationUnits

Optional. The units representing the heading:

  • degrees / deg (default) — degrees
  • rev — revolutions
  • raw — raw units

Return Values

Returns a double indicating the heading of the drivetrain in the units specified by the units parameter.

Examples
// 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 Functions
double rotation( rotationUnits units = rotationUnits::deg );

Parameters

Parameter

Type

Description

units

rotationUnits

Optional. The units representing the heading:

  • degrees / deg (default) — degrees
  • rev — revolutions
  • raw — raw units

Return Values

Returns a double indicating the rotation of the drivetrain in the units specified by the units parameter.

Examples
// 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();