5.4. Quadrature encoders
Each drive motor on the 3pi+ 32U4 has a corresponding quadrature encoder system consisting of a magnetic disc attached to the extended motor shaft and a pair of Hall effect sensors mounted on the control board. Other than the sensor orientation, these encoders work similarly to our magnetic encoder kits for Micro Metal Gearmotors. They can be used to track the rotational speed and direction of the robot’s wheels.
The encoders provide a resolution of 12 counts per revolution of the motor shaft when counting both edges of both channels. To compute the counts per revolution of the wheels, multiply the gearboxes’ gear ratios by 12. For example, if 30:1 motors (which have gear ratios more accurately specified as 29.86:1) are used, the encoders provide 29.86 × 12 ≈ 358.3 CPR. The exact gear ratios of our Micro Metal Gearmotors are specified on their product pages.
Quadrature encoder transitions are often detected by monitoring both encoder channels directly. However, since transitions on the 3pi+’s encoders can occur at high frequencies (several thousand per second) when its motors are running, it is necessary to use the AVR’s pin change interrupts or external interrupts to read the encoders. To reduce the required number of interrupt pins, the 3pi+ 32U4 control board XORs together both channels of each encoder and connects the resulting signal to an interrupt pin, while channel B of each encoder is connected to a non-interrupt pin:
- Digital pin 7, or PE6, reads the right encoder XORed signal using external interrupt INT6.
- Digital pin 8, or PB4, reads the left encoder XORed signal using pin change interrupt PCINT4.
- Digital pin 23 (analog pin 5), or PF0, reads the right encoder channel B.
- Pin PE2 reads the left encoder channel B.
The XORed signal and the channel B signal can be used to reconstruct the channel A signal by simply XORing them again: (A XOR B) XOR B = A. For both encoders on the Turtle Edition and Standard Edition, channel B leads channel A when the motor is rotating in the forward direction; that is, B rises before A rises and B falls before A falls. (The waveforms in the diagram above would be produced by forward rotation.)
Since the motor directions on the Hyper Edition are reversed (see Section 5.3), the behavior described above corresponds to backward rotation instead on that version.
The Pololu3piPlus32U4 library provides appropriate interrupt service routines and functions for reading the encoders and keeping track of their counts (see Section 7).