# Step 3: Creating a Drive Subsystem¶

Now that our drive is characterized, it is time to start writing our robot code proper. As mentioned before, we will use the command-based framework for our robot code. Accordingly, our first step is to write a suitable drive subsystem class.

The full drive class from the RamseteCommand Example Project (Java, C++) can be seen below. The rest of the article will describe the steps involved in writing this class.

  8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 package edu.wpi.first.wpilibj.examples.ramsetecommand.subsystems; import edu.wpi.first.wpilibj.ADXRS450_Gyro; import edu.wpi.first.wpilibj.Encoder; import edu.wpi.first.wpilibj.PWMVictorSPX; import edu.wpi.first.wpilibj.SpeedControllerGroup; import edu.wpi.first.wpilibj.drive.DifferentialDrive; import edu.wpi.first.wpilibj.geometry.Pose2d; import edu.wpi.first.wpilibj.interfaces.Gyro; import edu.wpi.first.wpilibj.kinematics.DifferentialDriveOdometry; import edu.wpi.first.wpilibj.kinematics.DifferentialDriveWheelSpeeds; import edu.wpi.first.wpilibj2.command.SubsystemBase; import edu.wpi.first.wpilibj.examples.ramsetecommand.Constants.DriveConstants; public class DriveSubsystem extends SubsystemBase { // The motors on the left side of the drive. private final SpeedControllerGroup m_leftMotors = new SpeedControllerGroup(new PWMVictorSPX(DriveConstants.kLeftMotor1Port), new PWMVictorSPX(DriveConstants.kLeftMotor2Port)); // The motors on the right side of the drive. private final SpeedControllerGroup m_rightMotors = new SpeedControllerGroup(new PWMVictorSPX(DriveConstants.kRightMotor1Port), new PWMVictorSPX(DriveConstants.kRightMotor2Port)); // The robot's drive private final DifferentialDrive m_drive = new DifferentialDrive(m_leftMotors, m_rightMotors); // The left-side drive encoder private final Encoder m_leftEncoder = new Encoder(DriveConstants.kLeftEncoderPorts[0], DriveConstants.kLeftEncoderPorts[1], DriveConstants.kLeftEncoderReversed); // The right-side drive encoder private final Encoder m_rightEncoder = new Encoder(DriveConstants.kRightEncoderPorts[0], DriveConstants.kRightEncoderPorts[1], DriveConstants.kRightEncoderReversed); // The gyro sensor private final Gyro m_gyro = new ADXRS450_Gyro(); // Odometry class for tracking robot pose private final DifferentialDriveOdometry m_odometry; /** * Creates a new DriveSubsystem. */ public DriveSubsystem() { // Sets the distance per pulse for the encoders m_leftEncoder.setDistancePerPulse(DriveConstants.kEncoderDistancePerPulse); m_rightEncoder.setDistancePerPulse(DriveConstants.kEncoderDistancePerPulse); resetEncoders(); m_odometry = new DifferentialDriveOdometry(m_gyro.getRotation2d()); } @Override public void periodic() { // Update the odometry in the periodic block m_odometry.update(m_gyro.getRotation2d(), m_leftEncoder.getDistance(), m_rightEncoder.getDistance()); } /** * Returns the currently-estimated pose of the robot. * * @return The pose. */ public Pose2d getPose() { return m_odometry.getPoseMeters(); } /** * Returns the current wheel speeds of the robot. * * @return The current wheel speeds. */ public DifferentialDriveWheelSpeeds getWheelSpeeds() { return new DifferentialDriveWheelSpeeds(m_leftEncoder.getRate(), m_rightEncoder.getRate()); } /** * Resets the odometry to the specified pose. * * @param pose The pose to which to set the odometry. */ public void resetOdometry(Pose2d pose) { resetEncoders(); m_odometry.resetPosition(pose, m_gyro.getRotation2d()); } /** * Drives the robot using arcade controls. * * @param fwd the commanded forward movement * @param rot the commanded rotation */ public void arcadeDrive(double fwd, double rot) { m_drive.arcadeDrive(fwd, rot); } /** * Controls the left and right sides of the drive directly with voltages. * * @param leftVolts the commanded left output * @param rightVolts the commanded right output */ public void tankDriveVolts(double leftVolts, double rightVolts) { m_leftMotors.setVoltage(leftVolts); m_rightMotors.setVoltage(-rightVolts); m_drive.feed(); } /** * Resets the drive encoders to currently read a position of 0. */ public void resetEncoders() { m_leftEncoder.reset(); m_rightEncoder.reset(); } /** * Gets the average distance of the two encoders. * * @return the average of the two encoder readings */ public double getAverageEncoderDistance() { return (m_leftEncoder.getDistance() + m_rightEncoder.getDistance()) / 2.0; } /** * Gets the left drive encoder. * * @return the left drive encoder */ public Encoder getLeftEncoder() { return m_leftEncoder; } /** * Gets the right drive encoder. * * @return the right drive encoder */ public Encoder getRightEncoder() { return m_rightEncoder; } /** * Sets the max output of the drive. Useful for scaling the drive to drive more slowly. * * @param maxOutput the maximum output to which the drive will be constrained */ public void setMaxOutput(double maxOutput) { m_drive.setMaxOutput(maxOutput); } /** * Zeroes the heading of the robot. */ public void zeroHeading() { m_gyro.reset(); } /** * Returns the heading of the robot. * * @return the robot's heading in degrees, from -180 to 180 */ public double getHeading() { return m_gyro.getRotation2d().getDegrees(); } /** * Returns the turn rate of the robot. * * @return The turn rate of the robot, in degrees per second */ public double getTurnRate() { return -m_gyro.getRate(); } } 
  8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 #pragma once #include #include #include #include #include #include #include #include #include #include "Constants.h" class DriveSubsystem : public frc2::SubsystemBase { public: DriveSubsystem(); /** * Will be called periodically whenever the CommandScheduler runs. */ void Periodic() override; // Subsystem methods go here. /** * Drives the robot using arcade controls. * * @param fwd the commanded forward movement * @param rot the commanded rotation */ void ArcadeDrive(double fwd, double rot); /** * Controls each side of the drive directly with a voltage. * * @param left the commanded left output * @param right the commanded right output */ void TankDriveVolts(units::volt_t left, units::volt_t right); /** * Resets the drive encoders to currently read a position of 0. */ void ResetEncoders(); /** * Gets the average distance of the TWO encoders. * * @return the average of the TWO encoder readings */ double GetAverageEncoderDistance(); /** * Gets the left drive encoder. * * @return the left drive encoder */ frc::Encoder& GetLeftEncoder(); /** * Gets the right drive encoder. * * @return the right drive encoder */ frc::Encoder& GetRightEncoder(); /** * Sets the max output of the drive. Useful for scaling the drive to drive * more slowly. * * @param maxOutput the maximum output to which the drive will be constrained */ void SetMaxOutput(double maxOutput); /** * Returns the heading of the robot. * * @return the robot's heading in degrees, from -180 to 180 */ units::degree_t GetHeading() const; /** * Returns the turn rate of the robot. * * @return The turn rate of the robot, in degrees per second */ double GetTurnRate(); /** * Returns the currently-estimated pose of the robot. * * @return The pose. */ frc::Pose2d GetPose(); /** * Returns the current wheel speeds of the robot. * * @return The current wheel speeds. */ frc::DifferentialDriveWheelSpeeds GetWheelSpeeds(); /** * Resets the odometry to the specified pose. * * @param pose The pose to which to set the odometry. */ void ResetOdometry(frc::Pose2d pose); private: // Components (e.g. motor controllers and sensors) should generally be // declared private and exposed only through public methods. // The motor controllers frc::PWMVictorSPX m_left1; frc::PWMVictorSPX m_left2; frc::PWMVictorSPX m_right1; frc::PWMVictorSPX m_right2; // The motors on the left side of the drive frc::SpeedControllerGroup m_leftMotors{m_left1, m_left2}; // The motors on the right side of the drive frc::SpeedControllerGroup m_rightMotors{m_right1, m_right2}; // The robot's drive frc::DifferentialDrive m_drive{m_leftMotors, m_rightMotors}; // The left-side drive encoder frc::Encoder m_leftEncoder; // The right-side drive encoder frc::Encoder m_rightEncoder; // The gyro sensor frc::ADXRS450_Gyro m_gyro; // Odometry class for tracking robot pose frc::DifferentialDriveOdometry m_odometry; }; 
  8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 #include "subsystems/DriveSubsystem.h" #include #include using namespace DriveConstants; DriveSubsystem::DriveSubsystem() : m_left1{kLeftMotor1Port}, m_left2{kLeftMotor2Port}, m_right1{kRightMotor1Port}, m_right2{kRightMotor2Port}, m_leftEncoder{kLeftEncoderPorts[0], kLeftEncoderPorts[1]}, m_rightEncoder{kRightEncoderPorts[0], kRightEncoderPorts[1]}, m_odometry{m_gyro.GetRotation2d()} { // Set the distance per pulse for the encoders m_leftEncoder.SetDistancePerPulse(kEncoderDistancePerPulse); m_rightEncoder.SetDistancePerPulse(kEncoderDistancePerPulse); ResetEncoders(); } void DriveSubsystem::Periodic() { // Implementation of subsystem periodic method goes here. m_odometry.Update(m_gyro.GetRotation2d(), units::meter_t(m_leftEncoder.GetDistance()), units::meter_t(m_rightEncoder.GetDistance())); } void DriveSubsystem::ArcadeDrive(double fwd, double rot) { m_drive.ArcadeDrive(fwd, rot); } void DriveSubsystem::TankDriveVolts(units::volt_t left, units::volt_t right) { m_leftMotors.SetVoltage(left); m_rightMotors.SetVoltage(-right); m_drive.Feed(); } void DriveSubsystem::ResetEncoders() { m_leftEncoder.Reset(); m_rightEncoder.Reset(); } double DriveSubsystem::GetAverageEncoderDistance() { return (m_leftEncoder.GetDistance() + m_rightEncoder.GetDistance()) / 2.0; } frc::Encoder& DriveSubsystem::GetLeftEncoder() { return m_leftEncoder; } frc::Encoder& DriveSubsystem::GetRightEncoder() { return m_rightEncoder; } void DriveSubsystem::SetMaxOutput(double maxOutput) { m_drive.SetMaxOutput(maxOutput); } units::degree_t DriveSubsystem::GetHeading() const { return m_gyro.GetRotation2d().Degrees(); } double DriveSubsystem::GetTurnRate() { return -m_gyro.GetRate(); } frc::Pose2d DriveSubsystem::GetPose() { return m_odometry.GetPose(); } frc::DifferentialDriveWheelSpeeds DriveSubsystem::GetWheelSpeeds() { return {units::meters_per_second_t(m_leftEncoder.GetRate()), units::meters_per_second_t(m_rightEncoder.GetRate())}; } void DriveSubsystem::ResetOdometry(frc::Pose2d pose) { ResetEncoders(); m_odometry.ResetPosition(pose, m_gyro.GetRotation2d()); } 

## Configuring the Drive Encoders¶

The drive encoders measure the rotation of the wheels on each side of the drive. To properly configure the encoders, we need to specify two things: the ports the encoders are plugged into, and the distance per encoder pulse. Then, we need to write methods allowing access to the encoder values from code that uses the subsystem.

### Encoder Ports¶

The encoder ports are specified in the encoder’s constructor, like so:

 38 39 40 41 42 43 44 45  private final Encoder m_leftEncoder = new Encoder(DriveConstants.kLeftEncoderPorts[0], DriveConstants.kLeftEncoderPorts[1], DriveConstants.kLeftEncoderReversed); // The right-side drive encoder private final Encoder m_rightEncoder = new Encoder(DriveConstants.kRightEncoderPorts[0], DriveConstants.kRightEncoderPorts[1], DriveConstants.kRightEncoderReversed); 
 20 21  m_leftEncoder{kLeftEncoderPorts[0], kLeftEncoderPorts[1]}, m_rightEncoder{kRightEncoderPorts[0], kRightEncoderPorts[1]}, 

### Encoder Distance per Pulse¶

The distance per pulse is specified by calling the encoder’s setDistancePerPulse method. Note that for the WPILib Encoder class, “pulse” refers to a full encoder cycle (i.e. four edges), and thus will be 1/4 the value that was specified in the FRC-Characterization config. Remember, as well, that the distance should be measured in meters!

 58 59  m_leftEncoder.setDistancePerPulse(DriveConstants.kEncoderDistancePerPulse); m_rightEncoder.setDistancePerPulse(DriveConstants.kEncoderDistancePerPulse); 
 23 24 25  // Set the distance per pulse for the encoders m_leftEncoder.SetDistancePerPulse(kEncoderDistancePerPulse); m_rightEncoder.SetDistancePerPulse(kEncoderDistancePerPulse); 

### Encoder Accessor Method¶

To access the values measured by the encoders, we include the following method:

 82 83 84 85 86 87 88  * Returns the current wheel speeds of the robot. * * @return The current wheel speeds. */ public DifferentialDriveWheelSpeeds getWheelSpeeds() { return new DifferentialDriveWheelSpeeds(m_leftEncoder.getRate(), m_rightEncoder.getRate()); } 
 74 75 76 77  units::meters_per_second_t(m_rightEncoder.GetRate())}; } void DriveSubsystem::ResetOdometry(frc::Pose2d pose) { 

We wrap the measured encoder values in a DifferentialDriveWheelSpeeds object for easier integration with the RamseteCommand class later on.

## Configuring the Gyroscope¶

The gyroscope measures the rate of change of the robot’s heading (which can then be integrated to provide a measurement of the robot’s heading relative to when it first turned on). In our example, we use the Analog Devices ADXRS450 FRC Gyro Board, which has been included in the kit of parts for several years:

 48  private final Gyro m_gyro = new ADXRS450_Gyro(); 
 143 144  // The gyro sensor frc::ADXRS450_Gyro m_gyro; 

### Gyroscope Accessor Method¶

To access the current heading measured by the gyroscope, we include the following method:

 173 174 175 176 177 178 179 180  /** * Returns the heading of the robot. * * @return the robot's heading in degrees, from -180 to 180 */ public double getHeading() { return m_gyro.getRotation2d().getDegrees(); } 
 64 65 66 units::degree_t DriveSubsystem::GetHeading() const { return m_gyro.GetRotation2d().Degrees(); } 

## Configuring the Odometry¶

Now that we have our encoders and gyroscope configured, it is time to set up our drive subsystem to automatically compute its position from the encoder and gyroscope readings.

First, we create a member instance of the DifferentialDriveOdometry class:

 51  private final DifferentialDriveOdometry m_odometry; 
 146 147  // Odometry class for tracking robot pose frc::DifferentialDriveOdometry m_odometry; 

### Updating the Odometry¶

The odometry class must be regularly updated to incorporate new readings from the encoder and gyroscope. We accomplish this inside the subsystem’s periodic method, which is automatically called once per main loop iteration:

 66 67 68 69 70  public void periodic() { // Update the odometry in the periodic block m_odometry.update(m_gyro.getRotation2d(), m_leftEncoder.getDistance(), m_rightEncoder.getDistance()); } 
 30 31 32 33 34 35 void DriveSubsystem::Periodic() { // Implementation of subsystem periodic method goes here. m_odometry.Update(m_gyro.GetRotation2d(), units::meter_t(m_leftEncoder.GetDistance()), units::meter_t(m_rightEncoder.GetDistance())); } 

### Odometry Accessor Method¶

To access the robot’s current computed pose, we include the following method:

 72 73 74 75 76 77 78 79  /** * Returns the currently-estimated pose of the robot. * * @return The pose. */ public Pose2d getPose() { return m_odometry.getPoseMeters(); } 
 72 frc::DifferentialDriveWheelSpeeds DriveSubsystem::GetWheelSpeeds() { 

## Voltage-Based Drive Method¶

Finally, we must include one additional method - a method that allows us to set the voltage to each side of the drive using the setVoltage() method of the SpeedController interface. The default WPILib drive class does not include this functionality, so we must write it ourselves:

 111 112 113 114 115 116 117 118 119 120  * Controls the left and right sides of the drive directly with voltages. * * @param leftVolts the commanded left output * @param rightVolts the commanded right output */ public void tankDriveVolts(double leftVolts, double rightVolts) { m_leftMotors.setVoltage(leftVolts); m_rightMotors.setVoltage(-rightVolts); m_drive.feed(); } 
 41 42 43 44 void DriveSubsystem::TankDriveVolts(units::volt_t left, units::volt_t right) { m_leftMotors.SetVoltage(left); m_rightMotors.SetVoltage(-right); m_drive.Feed(); 

It is very important to use the setVoltage() method rather than the ordinary set() method, as this will automatically compensate for battery “voltage sag” during operation. Since our feedforward voltages are physically-meaningful (as they are based on measured characterization data), this is essential to ensuring their accuracy.