# Wheel Encoders

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.

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. Since the encoders are really part of the DriveSubsystem, we will be adding these to that class. Consulting the Encoder class, we see that the constructor take a single integer parameter which specifies the encoder number (i.e. 0 or 1). For this robot the left encoder is connected to port 0, while the right encoder is on port 1. Open the DriveSubsystem.java file and add the following at top of the class.

Once again, we are using constants for the port numbers which must be declared as follows:

Now the way these encoders are wired we will discover that for the right encoder when the wheel moves in the forward direction, the encoder will count up. However the left encoder will count down when the wheel moves forward. Hence we want to reverse the direction of the left encoder. We do this by creating a constructor for the DriveSubsystem class and inverting the left encoder as follows:

Now we are going to need access to these encoders from our Command classes, so we will also create functions to retrieve the left and right encoders:

Your DriveSubsystem.java file should now look like:

Now it is time to create our new command which we will call DriverForDistanceCommand. 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 replace all instances of DriverForTimeCommand with DriverForDistanceCommand. This will give us a better starting point. Your new file should look like:

Since we will not be using the timer this time, let’s remove all references to the Timer variable m_timer.

Next we want to change our constructor to take a distance instead of a time. This will also involve in replacing the stored m_time with a stored m_distance.

In the initialize we need to reset the encoders. We can use one or both encoders here, but for now we will use the left encoder to measure the distance

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:

Now we need to create some way to run this command. What we are going to do is to change the OI.java file so that this command is executed when you press button 2 which is on the side of the joystick handle. To do this we need to define a new button which we will call m_testButton.

And then in the constructor, OI(), we will configure the DriveForDistance command to be executed when this new button is pressed.

Note that we have arbitrarily chosen to drive for 2000 units. The units that the encoder uses is a function of the type of encoder. For these encoders, the encoder will register a little more than 1000 units per revolution of the wheel.

Your OI.java file should now look like:

Build and deploy your program and verify that the robot moves forward a short distance and then stops when you press button 2 on the joystick. 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.

At some point you could make a conversion from encoder units to say, inches, by measuring the distance the robot moves in a fixed number of units and then computing the scale factor that will convert units to inches. This will be left as an exercise for the reader.

Next: Speed Control

Pierre Pierre, who replica handbag 185cm tall, gucci replica handbags the "Diamond King" in his hermes replica handbags . He has been selected as one of the handbag replica most beautiful 50 people in the "People" magazine. It is also known as replica handbags most elegant and quiet in the world. Unforgettable prince.