C++ documentation C documentation

Support for a generic quadrature encoder using pin change interrupts.

Most modern ATmel micro controllers have lots of pin change interrupts (up to around 24) and so they are more flexible however, due to the chip design, they are slow to process. So if you find your are missing ticks because you have a high resolution encoder then you may want to consider using the 'Fast Quadrature' device instead.

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 axle 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.

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.

If encoder interpolation is switched **off** then the library can cope with up to about 2,000 ticks per second per MHz of processor speed. ie for an Axon running at 16MHz then: 16*2,000 = 32,000 ticks per second.

If encoder interpolation is switched **on** then the library can cope with up to about 1,300 ticks per second per MHz of processor speed. ie for an Axon running at 16MHz then: 16*1,300 = 20,800 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 = 32,000 ticks, so 12,000 ticks=37.5%. So your program will be spending 37.5% 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

- dump - Dump the last read sensor values to the standard output device.
- dumpTo - Dump the contents of the sensor to the specified output stream.
- getDurationOfLastTick - Returns the duration, in μS, of the last received tick.
- getTicks - Returns the number of ticks, as an ENCODER_TYPE, at the time when you performed the last read(). This number will become more negative if the encoder is rotating one way, or more positive if it is rotating the other way.
- getTimeLastRead - Returns the value of the clock at the time when the sensor was last read.
- getTimeSinceLastTick - Returns the number of μS between your call to read() and when the last tick was received.
- isInterpolating - Returns whether or not the encoder is using interpolation.
- read - Read the sensor and store its current values. Returns TRUE if the sensor has been read successfully, or FALSE if the sensor is busy in which case the read values are unchanged.
- setInterpolating - Turn interpolation on or off.
- subtract - Subtract a given value from the encoder counter.
- ticksPerRevolution - Returns the number of counter ticks generated for each 360° revolution.