Encoders

Hello, I would like to know how to do various things with encoders. If you would be so kind as to help, I would very much appreciate it!
Here are the things that I must learn to do.

1.) Get the encoders to work
2.) Be able to use them for autonomous and to go a set amount of distance per rotation

Thank You so much for your help!

The first step is to wire up your encoders and make sure that you can access the information they provide in your code.

The following code snippet provides an example of how to construct an quadrature Encoder object and register it with the LiveWindow system. Once your robot program is compiled and deployed, you should then be able to select Test mode instead of Teleop on the driver station and see the values being reported by the encoder.

You will need to adjust the DIO port constants to match where you plugged in your encoder cables.

The following assumes that you have a two channel (quadrature encoder) that has two PWM cables that you plugged into DIO ports on the roboRIO (you did not specify much information about your robot - so this was a guess).

NOTE: I don’t have access to a robot to try out this sample code below, so you may need to make some adjustments.


    // The digital I/O ports you plug your cables into on the roboRIO
    // IMPORTANT: You MUST CHANGE these values to match the digital input
    // ports you plugged your encoder into on the robotRIO!
    private static final int DIO_LEFT_ENCODER_A_CHAN = 0;
    private static final int DIO_LEFT_ENCODER_B_CHAN = 1;
    
    // Once you are able to see counts, you can push your robot
    // a fixed distance and enter the distance you pushed it below
    // and the number of counts your encoder reported for the distance
    private double DIST_MEASURED = 1.0;
    private double COUNTS_MEASURED = 1.0;
    
    // Computes the distance per count value for the encoder from
    // the measure values
    private double DIST_PER_COUNT = DIST_MEASURED / COUNTS_MEASURED;
    
    // Encoder attached to the left side motors
    Encoder leftDriveEncoder;
    
    public void robotInit() {

    	// Construct a new instance of the encoder for left side of drive
    	leftDriveEncoder = new Encoder(DIO_LEFT_ENCODER_A_CHAN, DIO_LEFT_ENCODER_B_CHAN);
    	leftDriveEncoder.setDistancePerPulse(DIST_PER_COUNT);

    	// Add it to LiveWindow monitoring for "Test" mode viewing
    	LiveWindow.addSensor("Drive", "Left Encoder", leftDriveEncoder);


       // Rest of your robotInit() code
    }

Hope that helps get you started enough that you can move on to the next step of driving a fixed distance.