Lesson 18: Intro to Encoders

Encoders measure rotation. In FRC, they are commonly used to measure how far a wheel has turned, how fast a shooter is spinning, or how far an elevator has moved.

How Encoders Work

An encoder sends pulses as it rotates. The roboRIO counts those pulses. If you know how many pulses equal one rotation, and how far the mechanism moves per rotation, you can convert encoder counts into useful units.

Creating an Encoder

Quadrature encoders usually use two digital input ports.

Encoder leftEncoder;

public void robotInit() {
    leftEncoder = new Encoder(0, 1);
    leftEncoder.reset();
}

The numbers in the constructor are the digital input ports. As always, these should eventually live in your Wiring class.

Reading Counts

int counts = leftEncoder.get();
System.out.println("Left encoder: " + counts);

Raw counts are useful for debugging, but they are not very meaningful to a driver. You usually want distance or speed.

Distance Per Pulse

If your wheel travels 18.85 inches per rotation and your encoder reports 360 counts per rotation, each count is about 0.052 inches. Once you set distance per pulse, the encoder can report distance directly.

leftEncoder.setDistancePerPulse(18.85 / 360.0);
double inches = leftEncoder.getDistance();

Practice

Find the counts per revolution for an encoder on your robot. Then calculate the distance per pulse for one drive wheel. Print both raw counts and converted distance while slowly pushing the robot forward.