Quadrature encoders are similar in purpose to gear tooth sensors but are different in functionality. A quadrature encoder has an additional channel that indicates the direction the encoder is being turned.
In Kevin Watson’s FRC code the functions like Get_Encoder_1_Count() return the net number of pulses sent by the encoder plugged into the port as declared in the encoder.h file. The numbers are used to distinguish between encoders, I believe Kevin’s code allows for 6 to be used at a time.
The value returned by Get_Encoder_1_Count() can be converted into distance traveled since the encoder was last reset by multiplying the value by the ratio of encoder turns to wheel turns then by multiplying by the wheel circumference.
For encoder 1 phase A goes to dig_in 1 phase b to dig_in 11
encoder 2 uses 2 and 12, 3 uses 3 and 13, ans so on.
There is not more than one option, the encoder or gear tooth sensor just pulses once for every set fraction of a rotation, ie when ever the gear tooth sensor senses a gear tooth. This pulse triggers an interrupt in Kevin’s code and that tallies the number of pulses, adding one for each forward pulse and subtracting one for each reverse pulse. This tally is returned by get_encoder_count.
The word “quadrature” refers to two signals which are out of phase with each other by one quarter of a cycle. The signals on a quadrature encoder are digital, and there are four possible combinations: 00, 01, 11, and 10. Notice that they don’t “count” in binary; instead, only one of the signals changes for a transition between states. A change from 00 to 01 indicates the encoder is turning in one direction; a change fom 01 to 00 indicates it is turning in the other. Here’s the full list of possibilities:
from to direction
00 01 forward
00 10 reverse
01 11 forward
01 00 reverse
11 10 forward
11 01 reverse
10 00 forward
10 11 reverse
A simple way to use quadrature encoders is to use interrupts to detect a transition from 0 to 1 on one signal (usually called Phase A), and look at the state of the other signal (Phase B) at that time to decide which direction the sensor is turning. That only uses two of the possible eight changes, but it’s sufficient: 01 to 11 is forward, and 00 to 10 is reverse.
Kevin Watson has written exactly what you need, and makes it available to all. Here it is, complete with detailed instructions for adding it to your project in MPLab.
Do as above, but note that the gear tooth sensors in the kit have only a single phase. This means that the encoder code will only count either up or down.
If you want direction information (i.e. a count that goes up when rotating one way, and down when rotating the other), you need to estimate it based on other factors (such as commanded motor direction, etc.).