Encoder Guidance Needed

Hello, my team is planning on using encoders, specifically the ones that were in FIRST Choice. I’ve looked though all the Java threads on them, but I can’t make sense of how the Encoder class really works.

My team plans on using the encoder to move to desired set positions in autonomous and to prevent from going too fast.

For instance, I really don’t know how the getDistancePerPulse() and how to use the other methods accordingly to get distance calculations. Samples code on setting up an encoder would be awesome!

Encoders work by detecting little sprockets on a wheel inside the encoder usually using optic sensors or anything along those lines. The encoders you have 360 of these bumps or sprockets meaning that every revolution of the shaft the encoder is mounted on, the encoder pulses 360 times. Using the libraries provided you can set the distance per pulse. Therefore by calculating how far, say your robot drives per revolution of the output shaft your encoder is mounted on, you can get how far your robot has travelled and how fast its moving.

Here is some sample code on calculating distance per pulse and setting up an encoder. You would just need to place them in the proper locations of your code.

The code should be self explanatory, but if you have any questions, reply back.



        //DIO Port of Encoder
	public static final int rightDriveEncoderChannelA = 3;
	public static final int rightDriveEncoderChannelB = 4;


	//Wheel Radius
	public static final int driveWheelRadius=2;//wheel radius in inches
	public static final int pulsePerRotation = 360; //encoder pulse per rotation
	public static final double gearRatio = 1/1; //ratio between wheel and encoder
	public static final double driveEncoderPulsePerRot= pulsePerRotation*gearRatio; //pulse per rotation * gear ratio
	public static final double driveEncoderDistPerTick=(Math.PI*2*driveWheelRadius)/driveEncoderPulsePerRot;
	public static final int driveEncoderMinRate=10; 
	public static final int driveEncoderMinPeriod=10;
        public static final boolean rightDriveTrainEncoderReverse=false;



	//declare sensors
	Encoder rightEncoder;

         //instantiate encoder
        rightEncoder = new Encoder(rightDriveEncoderChannelA, rightDriveEncoderChannelB, rightDriveTrainEncoderReverse, CounterBase.EncodingType.k4X);
    	rightEncoder.setDistancePerPulse(driveEencoderDistPerTick);
    	rightEncoder.setMaxPeriod(driveEncoderMinPeriod);//min period before reported stopped
    	rightEncoder.setMinRate(driveEncoderMinRate);//min rate before reported stopped
    	rightEncoder.start();


Hope this helps,
Kev