WebbotLib AVR library
WebbotLib It just does it
C++documentationCdocumentation

Fast Quadrature

Support for a generic quadrature encoder using external interrupts.
Most modern ATmel micro controllers have some external interrupts (between 2 and 8) but some of these are put on pins used by UARTs, I2C, PWM pins etc so often there are only a few of them available for use. However they are fast to process making them suitable for high resolution encoders.
A quadrature encoder provides two signals called Channel A and Channel B which are 90 degrees out of phase. When channel A changes state then by reading channel B we can tell whether the device is rotating clockwise or anti-clockwise.
So channel A provides a pulse and channel B tells us whether to increment or decrement a counter so that we know how far the device has rotated.
Assuming an encoder disk with 32 stripes is attached to a drive motor then we will get 64 pulses per revolution. Obviously the distance travelled will depend on the diameter of the wheel that is attached to the motor drive shaft.
If you have enough INT pins then you can double the output of the encoder by ticking the 'Double the ticks per revolution' setting in Project Designer. This requires two INT pins per encoder - ie for 32 stripes then this will generate 128 pulses per revolution.
Like all encoders we can read the encoder using the read() function to store the current value of the counter. We can then access this value through the getTicks() function.
Assuming that the wheel is just rotating continuously then the counter will eventually overflow back to 0 and the frequency of this will depend on how many stripes the encoder has and on how fast the motor is rotating.
Note that you cannot set the counter to a given value - instead you must use the subtract(ENCODER_TYPE count) function to reduce the value. The reason being that otherwise we may 'miss' some pulses. Assuming your encoder is called 'myEncoder' then performing:
// read the quadrature encoder
myEncoder.read();
// get the value from the last read
ENCODER_TYPE ticks = myEncoder.getTicks();
// reduce the value by the same amount
myEncoder.subtract(ticks);
will change the counter to 0 if the motor is not rotating.
Metrics
If encoder interpolation is switched off then the library can cope with up to about 9,000 ticks per second per MHz of processor speed. ie for an Axon running at 16MHz then: 16*9,000 = 144,000 ticks per second.
If encoder interpolation is switched on then the library can cope with up to about 2,900 ticks per second per MHz of processor speed. ie for an Axon running at 16MHz then: 16*2,900 = 46,400 ticks per second.
These numbers are 'per encoder' - so if you have 4 encoders then divide the above figures by 4.
Example: assume we have a 16MHz processor and 4 encoders each generating up to 3,000 ticks per second and interpolation is turned off for all of them. Then: 4 encoders x 3,000 ticks = 12,000 ticks per second. 100% cpu = 144,000 ticks, so 12,000 ticks=8.3%. So your program will be spending 8.3% of its time just processing the encoder ticks. Obviously as the figure approaches 100% then your main code will run more slowly. Exceeding 100% means that everything will break!
Function Summary

Valid XHTML 1.0 Transitional