# 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(); } } 

## 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); 

### 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); 

### Encoder Accessor Method¶

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

Important

The returned velocities must be in meters! Because we configured the distance per pulse on the encoders above, calling getRate() will automatically apply the conversion factor from encoder units to meters. If you are not using WPILib’s Encoder class, you must perform this conversion either through the respective vendor’s API or by manually multiplying by a conversion factor.

 82 83 84 85 86 87 88 89  /** * 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()); } 

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

### 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(); } 

## 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; 

### 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()); } 

### 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(); } 

## 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:

 110 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(); } 

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.