ART v1.0-alpha
A Robot Template that raises the floor for VRC teams
Loading...
Searching...
No Matches
art::SmartDrive Class Reference

A Smarter Drive Class. More...

#include <SmartDrive.h>

Inheritance diagram for art::SmartDrive:
art::TankDrive

Classes

struct  HorizontalTracker
 A struct for tracking the robot's lateral displacement. More...
 

Public Member Functions

 SmartDrive (TankDrive drive, vex::inertial inert)
 Construct a new Smart Drive object.
 
SmartDrivewithWheelSize (Length size)
 Sets the wheel size of the Smart Drive.
 
SmartDrivewithGearRatio (double ratio)
 Sets the gear ratio of the drivetrain.
 
SmartDrivewithHorizontalTracker (vex::rotation rotation, Length wheelSize, double gearRatio)
 Add Horizontal Tracker to the SmartDrive.
 
SmartDrivewithHorizontalTracker (vex::rotation rotation, Length wheelSize, double gearRatio, Length wheelOffset)
 Add a Horizontal Tracker to the SmartDrive with a wheel offset.
 
int track ()
 Continuously tracks and updates the position of the SmartDrive.
 
void driveFor (Length target, double speed)
 Drives for a specified distance.
 
void driveForPID (Length target)
 Drives for a specified distance (and stops)
 
SmartDrivewithDriveForPID (PID pid)
 Sets the PID object for the driveForPID Method.
 
void turnFor (Angle target, double speed)
 Turns for a specified Angle.
 
void turnForPID (Angle target)
 Turns for a specified angle (and stops)
 
SmartDrivewithTurnForPID (PID pid)
 Sets the PID object for the turnForPID Method.
 
void turnTo (Angle target, double speed)
 Turns to a specified field-centric Angle.
 
void turnToPID (Angle target)
 Turns tp a specified angle (and stops)
 
SmartDrivewithTurnToPID (PID pid)
 Sets the PID object for the turnToPID Method.
 
Length getWheelTravel ()
 Get the Travel of the wheels for one revolution of the motor.
 
- Public Member Functions inherited from art::TankDrive
 TankDrive (vex::motor_group left, vex::motor_group right)
 Construct a new Tank Drive object.
 
void arcade (double x, double y, double rot)
 Commands the TankDrive using arcade inputs.
 
void arcade (double drive, double rot)
 Commands the TankDrive using Tank-only arcade inputs.
 
void tank (double left, double right)
 Commands the TankDrive using tank inputs.
 
void LeftSplitArcade (const vex::controller &cont)
 Commands the TankDrive using arcade inputs directly from a controller.
 
void LeftSplitArcadeCurved (const vex::controller &cont)
 Commands the TankDrive using arcade inputs directly from a controller.
 
void update ()
 Uses the stored commands to command the motors.
 

Public Attributes

PID m_driveForPID
 The PID object for the driveFor Method.
 
PID m_turnForPID
 The PID object for the turnForPID Method.
 
PID m_turnToPID
 The PID object for the turnToPID Method.
 

Private Member Functions

Angle getLeftTravel ()
 Gets the Angle traveled by the left motors.
 
Angle getRightTravel ()
 Gets the Angle traveled by the left motors.
 

Private Attributes

HorizontalTracker m_tracker
 Stores the Horizontal Tracker.
 
Vec2 m_pos
 Stores the Position of the robot.
 
Vec2 m_centerPos
 Stores the Center Position of the robot.
 
Angle m_dir
 Stores the current heading of the robot.
 
vex::inertial m_inert
 Stores the inertial sensor object.
 
Length m_wheelSize {Inches(3.25)}
 A Length representing the diameter of the wheel.
 
double m_gearRatio {1.0}
 A double representing the gear ratio of the drivetrain.
 
Angle m_LastLeftPos
 The last angle read from the Left motors.
 
Angle m_LastRightPos
 The last angle read from the Right motors.
 

Additional Inherited Members

- Protected Attributes inherited from art::TankDrive
SimpleMotorGroup m_left
 a SimpleMotorGroup for the left side
 
SimpleMotorGroup m_right
 a SimpleMotorGroup for the right side
 
double m_cmdX {0}
 The Stored command for the X-direction.
 
double m_cmdY {0}
 The Stored command for the Y-direction.
 
double m_cmdRot {0}
 The Stored command for rotation.
 

Detailed Description

A Smarter Drive Class.

This class is a wrapper around the TankDrive class, adding more complex functionality like odometry, and a few simple(ish) controlled drive functions. All the functionality of the TankDrive is retained in the SmartDrive class.

See also
TankDrive

The SmartDrive class requires an Inertial sensor and prefers to have information on gear ratio and wheel size. However, the latter is optional, although the distances the robot travel will be inaccurate. Using just this, the SmartDrive can also calculate its position over time using odometry. The SmartDrive class can also be configured with a HorizontalTracker, which can be used to track the robot's lateral displacement in the case that it has the ability to slide sideways(ie. running an all omni tank-drive). Once again, this is not a requirement, but it can be useful in some cases and often increases tracking resolution.

Definition at line 47 of file SmartDrive.h.

Constructor & Destructor Documentation

◆ SmartDrive()

art::SmartDrive::SmartDrive ( TankDrive drive,
vex::inertial inert )

Construct a new Smart Drive object.

Parameters
drivea tankDrive object representing the drivetrain
inerta vex inertial sensor object

The SmartDrive constructor only takes and initializes the features and member variables that are absolutely necessary for use, those being the drivetrain and the inertial sensor. Everything else(odometry and/or horizontal tracker) can be added as optional additions to the constructed SmartDrive.

Definition at line 21 of file SmartDrive.cpp.

Member Function Documentation

◆ driveFor()

void art::SmartDrive::driveFor ( Length target,
double speed )

Drives for a specified distance.

Parameters
targetthe distance to drive for
speedthe speed to drive at

Utilizes the motor encoders as well as information about the wheelSize and gear ratio to travel a specified distance. Use a Length object to specify the distance in any unit. The speed is a number from -100 to 100 as a percentage of max voltage(12). Setting the speed to 100 and -100 commands the motors at 12 and -12 volts respectively.

The SmartDrive will drive at this speed until it passes the target distance threshold, immediately exiting. This method is blocking, so the function where this is called will not continue until this is finished running. Keep in mind that if the speed makes the robot travel in the incorrect direction, it will never reach its target.

The SmartDrive will not slow down as it approaches the target either. If you want to to stop precisely at a target distance, it may be a good idea to divide the distance into smaller chunks and slow down the latter chunks so it slows as it approaches the overall target.

Example:

smartDrive.driveFor(art::Length::tiles(2), 75);
double tiles()
Returns the Length in tiles.
Definition Units.cpp:50

Definition at line 91 of file SmartDrive.cpp.

◆ driveForPID()

void art::SmartDrive::driveForPID ( Length target)

Drives for a specified distance (and stops)

Parameters
targetthe distance to drive for

Performs a similar function to driveFor(), but utilizes a PID loop to better control the SmartDrive's motion. Assuming the PID is tuned correctly, the SmartDrive will slow down and stop at the target distance. Both the speed and direction will be calculated to reach this distance.

To see how the PID runs, take a look at the PID class.

The PID object for this method can be configured by modifying driveForPID.

Definition at line 110 of file SmartDrive.cpp.

◆ getLeftTravel()

Angle art::SmartDrive::getLeftTravel ( )
private

Gets the Angle traveled by the left motors.

Returns
angle the angle the left motors have traveled through

Only used internally, inaccessible externally. Used for odometry and some drive functions.

Definition at line 239 of file SmartDrive.cpp.

◆ getRightTravel()

Angle art::SmartDrive::getRightTravel ( )
private

Gets the Angle traveled by the left motors.

Returns
angle the angle the left motors have traveled through

Only used internally, inaccessible externally. Used for odometry and some drive functions.

Definition at line 247 of file SmartDrive.cpp.

◆ getWheelTravel()

Length art::SmartDrive::getWheelTravel ( )

Get the Travel of the wheels for one revolution of the motor.

Returns
Distance that would be traveled if the base motor were turned onces

Calculates and returns the distance that would be traveled if the base motor were turned once. This is used to calculate the odometry and some drive functions.

Definition at line 217 of file SmartDrive.cpp.

◆ track()

int art::SmartDrive::track ( )

Continuously tracks and updates the position of the SmartDrive.

Returns
int runs infinitely and will only return 0 when the task ends

This method exists to run in the background and update the position of the SmartDrive while other things are happening. In order to do this, a vex::thread should be created when you want tracking to start.

Warning: Calling this somewhere else will cause the code to become stuck within this method and not run anything else. Do not run this outside of a separate thread from the rest of your code.

Definition at line 50 of file SmartDrive.cpp.

◆ turnFor()

void art::SmartDrive::turnFor ( Angle target,
double speed )

Turns for a specified Angle.

Parameters
targetthe angle to turn for
speedthe speed to turn at

Utilizes the inertial sensor's gyro to turn a specified Angle. Use an Angle object to specify the angler in any unit. The speed is a number from -100 to 100 as a percentage of max voltage(12). Setting the speed to 100 commands the left and right motors at 12 and -12 volts respectively and -100 flips those values.

The SmartDrive will turn at this speed until it passes the target angle threshold, immediately exiting. This method is blocking, so the function where this is called will not continue until this is finished running. Keep in mind that if the speed makes the robot travel in the incorrect direction, it will never reach its target.

Todo
Check if the robot will wrap around and stop.

The SmartDrive will not slow down as it approaches the target either. If you want to to stop precisely at a target angle, it may be a good idea to divide the distance into smaller chunks and slow down the latter chunks so it slows as it approaches the overall target.

Example:

smartDrive.turnFor(art::Angle::Rotations(2), 75);

Definition at line 134 of file SmartDrive.cpp.

◆ turnForPID()

void art::SmartDrive::turnForPID ( Angle target)

Turns for a specified angle (and stops)

Parameters
targetthe angle to turn for

Performs a similar function to turnFor(), but utilizes a PID loop to better control the SmartDrive's motion. Assuming the PID is tuned correctly, the SmartDrive will slow down and stop at the target Angle. Both the speed and direction will be calculated to reach this angle.

To see how the PID runs, take a look at the PID class.

The PID object for this method can be configured by modifying turnForPID.

Definition at line 150 of file SmartDrive.cpp.

◆ turnTo()

void art::SmartDrive::turnTo ( Angle target,
double speed )

Turns to a specified field-centric Angle.

Parameters
targetthe angle to turn to
speedthe speed to turn at

Utilizes the inertial sensor's gyro to turn to a specified Angle on the field. Use an Angle object to specify the angler in any unit. The speed is a number from -100 to 100 as a percentage of max voltage(12). Setting the speed to 100 commands the left and right motors at 12 and -12 volts respectively and -100 flips those values.

The SmartDrive will turn at this speed until it falls within the target angle threshold, immediately exiting. This method is blocking, so the function where this is called will not continue until this is finished running. Keep in mind that depending the speed, the robot may rotate and reach the angle using the longer route.

The Angle will be wrapped around and constrained to a circle. Therefore, even if the angle resides outside a circle, like 540 degrees, it will be wrapped around to 180 degrees instead. Unless the gyro is reset or re-calibrated, the angle specified will always be relative to the initial gyro reading. Therefore, all angles will be relative to the position when the robot is powered on. It is possible to power the robot on and move it to the starting position, but if you do this, keep in mind where the starting location is set in the code and ensure the odometry is not confused by this process.

The SmartDrive will not slow down as it approaches the target, so if you want to to stop precisely at a target angle, it may be a good idea to divide the turn into smaller chunks and slow down the latter chunks so it slows as it approaches the overall target.

Example:

smartDrive.turnTo(art::Angle::Degrees(90), 75);
friend Angle Degrees(double degrees)
Constructs an Angle from Degrees.
Definition Units.cpp:120

Definition at line 174 of file SmartDrive.cpp.

◆ turnToPID()

void art::SmartDrive::turnToPID ( Angle target)

Turns tp a specified angle (and stops)

Parameters
targetthe angle to turn to

Performs a similar function to turnTo(), but utilizes a PID loop to better control the SmartDrive's motion. Assuming the PID is tuned correctly, the SmartDrive will slow down and stop at the target Angle. Both the speed and direction will be calculated to reach this angle.

To see how the PID runs, take a look at the PID class.

The PID object for this method can be configured by modifying turnToPID.

Definition at line 195 of file SmartDrive.cpp.

◆ withDriveForPID()

SmartDrive & art::SmartDrive::withDriveForPID ( PID pid)

Sets the PID object for the driveForPID Method.

Parameters
pidthe PID object to copy
Returns
SmartDrive& a reference to the SmartDrive

This method allows you to set the PID object for the driveForPID method. It can be set using another PID object or one that is constructed inline like so:

smartDrive.withDriveForPID(
art::PID().withConstants(3.0, 0.2, 1.0)
);
A general use PID class.
Definition PID.h:59

Take a look at the documentation for the PID class for more information on how to configure the PID object.

Definition at line 129 of file SmartDrive.cpp.

◆ withGearRatio()

SmartDrive & art::SmartDrive::withGearRatio ( double ratio)

Sets the gear ratio of the drivetrain.

Parameters
ratioa double representing the ratio of output to input gears(output/input)
Returns
SmartDrive& a reference to the SmartDrive

Sets the gear ratio which can be calculated using (output teeth#)/(input teeth#). Because it returns a reference to the SmartDrive object, other methods can be chained to the end of this one, or the result can be stored into a SmartDrive object. Look at withWheelSize() for an example of this.

It is advised to simply type out both tooth counts in the constructor, like 60.0/36.0. Note the ".0" added to each number to ensure that the result is a double and not an integer. If it converts to an integer, then all the decimal places will be truncated and the odometry will not work.

The gear ratio (m_gearRatio) is only used for odometry, so it isn't necessary if you don't care for odometry. However, it is also one of 2 requirements for basic odometry (withWheelSize()), so it can be useful, as the data on the position will be at least somewhat accurate and useful.

Definition at line 32 of file SmartDrive.cpp.

◆ withHorizontalTracker() [1/2]

SmartDrive & art::SmartDrive::withHorizontalTracker ( vex::rotation rotation,
Length wheelSize,
double gearRatio )

Add Horizontal Tracker to the SmartDrive.

Parameters
rotationa rotation sensor to use for tracking
wheelSizea Length object representing the diameter of the wheel
gearRatioa double representing the ratio of output to input gears(output/input)
Returns
SmartDrive& a reference to the SmartDrive

While the HorizontalTracker is store privately in the SmartDrive class, one can be added to the SmartDrive by calling this method. It is recommended to attach this to the end of the constructor, like so:

art::SmartDrive smart = art::SmartDrive( drive, inert )
vex::rotation(vex::PORT2, true),
art::Inches(2.75),
3.f/5.f
);
A Smarter Drive Class.
Definition SmartDrive.h:48
SmartDrive & withWheelSize(Length size)
Sets the wheel size of the Smart Drive.
SmartDrive & withHorizontalTracker(vex::rotation rotation, Length wheelSize, double gearRatio)
Add Horizontal Tracker to the SmartDrive.
Length Inches(double inches)
Constructs a Length from Inches.
Definition Units.cpp:78
See also
HorizontalTracker(vex::rotation rotation, Length wheelSize, double gearRatio) HorizontalTracker(vex::rotation rotation, Length wheelSize, double gearRatio, Length wheelOffset)

Definition at line 38 of file SmartDrive.cpp.

◆ withHorizontalTracker() [2/2]

SmartDrive & art::SmartDrive::withHorizontalTracker ( vex::rotation rotation,
Length wheelSize,
double gearRatio,
Length wheelOffset )

Add a Horizontal Tracker to the SmartDrive with a wheel offset.

Parameters
rotationa rotation sensor to use for tracking
wheelSizea Length object representing the diameter of the wheel
gearRatioa double representing the ratio of output to input gears(output/input)
wheelOffseta Length object representing the vertical distance from horizontal tracker to the tracking center (positive is toward the front of the robot)
Returns
SmartDrive& a reference to the SmartDrive

While the HorizontalTracker is store privately in the SmartDrive class, one can be added to the SmartDrive by calling this method. It is recommended to attach this to the end of the constructor, like so:

art::SmartDrive smart = art::SmartDrive( drive, inert )
vex::rotation(vex::PORT2, true),
art::Inches(2.75),
3.f/5.f,
);
See also
HorizontalTracker(vex::rotation rotation, Length wheelSize, double gearRatio, Length wheelOffset)

Definition at line 44 of file SmartDrive.cpp.

◆ withTurnForPID()

SmartDrive & art::SmartDrive::withTurnForPID ( PID pid)

Sets the PID object for the turnForPID Method.

Parameters
pidthe PID object to copy
Returns
SmartDrive& a reference to the SmartDrive

This method allows you to set the PID object for the turnForPID method. It can be set using another PID object or one that is constructed inline like so:

smartDrive.withTurnForPID(
art::PID().withConstants(3.0, 0.2, 1.0)
);

Take a look at the documentation for the PID class for more information on how to configure the PID object.

Definition at line 169 of file SmartDrive.cpp.

◆ withTurnToPID()

SmartDrive & art::SmartDrive::withTurnToPID ( PID pid)

Sets the PID object for the turnToPID Method.

Parameters
pidthe PID object to copy
Returns
SmartDrive& a reference to the SmartDrive

This method allows you to set the PID object for the turnToPID method. It can be set using another PID object or one that is constructed inline like so:

smartDrive.withTurnToPID(
art::PID().withConstants(3.0, 0.2, 1.0)
);

Take a look at the documentation for the PID class for more information on how to configure the PID object.

Definition at line 212 of file SmartDrive.cpp.

◆ withWheelSize()

SmartDrive & art::SmartDrive::withWheelSize ( Length size)

Sets the wheel size of the Smart Drive.

Parameters
sizea Length object representing the diameter of the wheel
Returns
SmartDrive& a reference to the SmartDrive

Sets the wheel diameter of the wheel using a Distance object, allowing any unit to be used. Because it returns a reference to the SmartDrive object, other methods can be chained to the end of this one, or the result can be stored into a SmartDrive object.

The wheel size (m_wheelSize) is only used for odometry, so it isn't necessary if you don't care for odometry. However, it is also one of 2 requirements for basic odometry (withGearRatio()), so it can be useful, as the data on the position will be at least somewhat accurate and useful.

For example:

art::SmartDrive smart = art::SmartDrive( drive, inert )

Or

art::SmartDrive smart2 = art::SmartDrive( drive, inert )
.withGearRatio(3.0/4.0);
SmartDrive & withGearRatio(double ratio)
Sets the gear ratio of the drivetrain.

Definition at line 26 of file SmartDrive.cpp.

Member Data Documentation

◆ m_centerPos

Vec2 art::SmartDrive::m_centerPos
private

Stores the Center Position of the robot.

Uses the HorizontalTracker's offset to calculate and store where the center of the robot would be. If there isn't a tracker, this will be the same as m_pos, since that will store the center of the robot if there isn't a tracker.

Definition at line 610 of file SmartDrive.h.

◆ m_dir

Angle art::SmartDrive::m_dir
private

Stores the current heading of the robot.

An Angle representing the direction the robot is currently facing. Taken directly from the inertial sensor and used to calculate odometry. This value is still used outside the odometry tracking including the turning functions and some drive functions.

This value is mostly internal, the getDir() method returns a value directly from the inertial sensor and doesn't necessarily return this value, although they are often the same.

Definition at line 621 of file SmartDrive.h.

◆ m_driveForPID

PID art::SmartDrive::m_driveForPID

The PID object for the driveFor Method.

Modifying this PID object will change how driveFor works.

Wherever you configure your robot, it is recommended to configure this as well. You can simply set this PID object equal to another defined PID object, but this code will need to run in the pre_auton or main functions. To put it with the rest of the Robot Configuration, use the withDriveForPID() method.

See also
PID

Definition at line 246 of file SmartDrive.h.

◆ m_gearRatio

double art::SmartDrive::m_gearRatio {1.0}
private

A double representing the gear ratio of the drivetrain.

If the Base has a gear ratio between the motors and the wheels, a gear ratio will need to be entered to convert the motor's encoder values to wheel travel. The gear ratio is a number that can be calculated using (output teeth#)/(input teeth#).

It is advised to simply type out both tooth counts in the constructor, like 60.0/36.0. Note the ".0" added to each number to ensure that the result is a double and not an integer. If it converts to an integer, then all the decimal places will be truncated and the odometry will not work.

Definition at line 658 of file SmartDrive.h.

◆ m_inert

vex::inertial art::SmartDrive::m_inert
private

Stores the inertial sensor object.

Used to observe the heading of the robot. The inertial sensor added to the SmartDrive isn't removed or deleted(this is basically just a copy), so it can still be used outside the class. However, because this inertial sensor instance is private, it can't be accessed from outside the SmartDrive class.

Definition at line 631 of file SmartDrive.h.

◆ m_LastLeftPos

Angle art::SmartDrive::m_LastLeftPos
private

The last angle read from the Left motors.

Used internally to calculate the travel of the motors and the base

Definition at line 675 of file SmartDrive.h.

◆ m_LastRightPos

Angle art::SmartDrive::m_LastRightPos
private

The last angle read from the Right motors.

Used internally to calculate the travel of the motors and the base

Definition at line 692 of file SmartDrive.h.

◆ m_pos

Vec2 art::SmartDrive::m_pos
private

Stores the Position of the robot.

Stores a Vec2(2-dimensional vector) storing the position of the robot. This is the position that is used and incremented constantly by the tracking function. On the robot, this position is found in the center of the robot, or where the HorizontalTracker is, if you have one.

Definition at line 602 of file SmartDrive.h.

◆ m_tracker

HorizontalTracker art::SmartDrive::m_tracker
private

Stores the Horizontal Tracker.

Only used internally, as it is a private instance. This handles the calculations for the lateral displacement of the robot.

Definition at line 592 of file SmartDrive.h.

◆ m_turnForPID

PID art::SmartDrive::m_turnForPID

The PID object for the turnForPID Method.

Modifying this PID object will change how turnForPID works.

Wherever you configure your robot, it is recommended to configure this as well. You can simply set this PID object equal to another defined PID object, but this code will need to run in the pre_auton or main functions. To put it with the rest of the Robot Configuration, use the withTurnForPID() method.

See also
PID

Definition at line 326 of file SmartDrive.h.

◆ m_turnToPID

PID art::SmartDrive::m_turnToPID

The PID object for the turnToPID Method.

Modifying this PID object will change how turnToPID works.

Wherever you configure your robot, it is recommended to configure this as well. You can simply set this PID object equal to another defined PID object, but this code will need to run in the pre_auton or main functions. To put it with the rest of the Robot Configuration, use the withTurnToPID() method.

See also
PID

Definition at line 412 of file SmartDrive.h.

◆ m_wheelSize

Length art::SmartDrive::m_wheelSize {Inches(3.25)}
private

A Length representing the diameter of the wheel.

The standard Vex Wheel sizes are 2.75, 3,25 and 4 inches. As with any other Length, you are free to use any unit you wish, but keep in mind that this is the Diameter and not the travel of the wheel.

Defaults to 3.25 inches, but you can(and should) attach withWheelSize to the constructor and set the wheel size yourself.

See also
withWheelSize

Definition at line 645 of file SmartDrive.h.


The documentation for this class was generated from the following files: