Wheel Encoders

So far we have been controlling the motors by setting their power. Unfortunately, this is not ideal because the power only indirectly controls the speed, which is what we are really interested in. As we have already seen, our robot does not exactly drive straight when we apply the same power to both wheels. This is because differences in the motors and the way they are mounted cause them to run at slightly different speeds when the same power is applied.

What we need is some way to tell how far the wheels have turned and how fast. Each of the wheels on this robot have an encoder which will do just that. We will use the encoder to control the distance the robot moves as well as it’s speed.

For our first exercise, we are going to create a new command, similar to our DriveForTimeCommand, that will drive the robot forward a fixed distance, rather than a fixed time. Before that, a word on encoders and how we use them in software.

The encoders that we are using are handled by the Encoder class. We will need to create a separate instance of this class for the left and right motors. The DriveSubsystem will eventually need the encoders so we will be adding these to that class. Consulting the Encoder class documentation, we see that the constructor take a encoder type and two integer parameters which specifies the Arduino pins to which they are connected. For this robot the encoder type is Quadrature. The left encoder is connected to pins Q1_INT and Q1_DIR, while the right encoder is connected to pins Q2_INT and Q2_DIR. Open the DriveSubsystem.java file and add the following at top of the class.

When you import the Encoder class be sure to select the one from RobotCore.

Encoder Import

Also note that once again we are declaring constants for the encoder pins rather than simply using the numbers directly in the call to the constructor.

The way the encoders are wired, the left encoder works as expected – when the left wheel moves forward (e.g., moves the robot forward), the encoder counts up. BUT, the right encoder counts down when the wheel moves forward. Rather than having to deal with this throughout the code, we can tell the encoder that it is “inverted” by adding the following to the DriveSubsystem constructor:

The call to setInverted(true) tells the encoder to report the reverse of its direction, so it becomes “positive means forward”, like the left encoder.

Now we need access to these encoders from our Command classes, so we will create functions to retrieve the left and right encoders. Add these to DriveSubsystem above the periodic() method.

Now it is time to create our new command which we will call DriveForDistanceCommand. Go ahead and create a new class under the commands folder. This time instead of using the ExampleCommand as our starting point, copy the contents of DriveForTimeCommand.java file into your newly created DriveForDistanceCommand.java file. Then rename the file and replace all instances of DriveForTimeCommand with DriveForDistanceCommand.

Since we will not be using a Timer for this class, remove all references to the Timer. This will involve removing the time parameter from the constructor and having isFinished() function return false.

This will give us a better starting point. Your new file should look like:

Next we want to change our constructor to take a distance instead of a time. We also need to get the encoders from the DriveSubsystem. To measure the distance we can use either of the encoders but for this command we will use the left encoder.

Steps:

  • Define class variable after the “class” line (m_distance, m_leftEncoder)
  • Add distance variable to the constructor parameters
  • In the constructor, save the incoming distance value to the m_distance class variable (so it’s available to other methods in this class).
  • In the constructor, get and save the initial value of the left encoder.
  • Import Encoder.

DriveForDistanceCommand.java should look like this:

Again, when you import the declaration for Encoder be sure to choose the one from robotCore.

In the initialize() function we need to reset the Encoder so that it counts from zero each time we run this command. Remember that the encoder we receive from the DriveSubsystem is our own copy so resetting it will not affect the DriveSubsystem or any other commands with use the encoders.

The last thing we need to do is change the isFinished() function to return true when the distance is greater than or equal to the target distance:

Your DriveForDistanceCommand.java file should now look like:

We now need configure joystick button 2 so when it is pressed we will run the DriveForDistanceCommand. Add one line to RobotContainer‘s configureButtonbindings() method:

Note that the distance that we are passing in are in somewhat arbitrary units. The Encoder counts are somewhat arbitrary as they depend on the particular motor/encoder and the size of the wheel. We have chosen 2000 here just to test this command. Later we will perform a conversion which will allow us to use real world units such as inches.

Your RobotContainer.java file should now look like:

>>>> KAT Update Progress Marker –Code block above uses m_power instead of m_speed. Changed later?

Now run your program and click the B2 button. You should find that your robot will drive a short distance and then stop. Also note that since we are now using distance rather than time, we can change the speed and the robot should still move the same distance. Try it out and see.

Next: Speed Control