3.4. Quadrature encoders
Balboa encoder, showing the magnetic disc and sensors (with one motor removed).
Each drive motor on the Balboa 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, calculate the total gear ratio of the gearmotors and the Balboa chassis’s gearboxes combined by multiplying their individual gear ratios, then multiply the result by 12. For example, if 50:1 motors (which have gear ratios more accurately specified as 51.45:1) are used along with the plastic gears that give an additional 2.88:1 reduction, the encoders provide 51.45 × 2.88 × 12 ≈ 1778 CPR.
Quadrature encoder transitions are often detected by monitoring both encoder channels directly. However, since transitions on the Balboa’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 Balboa 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, 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.) Note that “forward” refers to the rotation direction that would cause a balancing Balboa to move in the direction its battery cover is facing.
The Balboa32U4 library provides appropriate interrupt service routines and functions for reading the encoders and keeping track of their counts (see Section 6).