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

Generic/fastquad.h

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 using the MAKE_GENERIC_FAST_QUADRATUREx2 constructor which 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 encoderRead function to store the current value of the counter. We can then access this value through the encoder.value property.
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 encoderSubtract function to reduce the value. The reason being that otherwise we may 'miss' some pulses. Performing:
// read the quadrature encoder
encoderRead(quadrature);
// get the value from the last read
ENCODER_TYPE ticks = quadrature.encoder.value;
// reduce the value by the same amount
encoderSubtract(quadrature, 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

 


MAKE_GENERIC_FAST_QUADRATURE(INTaaa,const IOPin* channelB,boolean inverted,uint16_t numStripes,boolean interpolate)

Create a new fast quadrature encoder sensor giving two ticks per stripe.
The INTaaa parameter specifies the INT pin to use for channel A eg INT0, INT1 etc. The use of an invalid value will result in a compile/link error.
The channelB parameter specifies the IOPin to use for channel B and may be any IOPin.
The inverted parameter is similar in concept to the idea of inverted motors or sensors. If you have encoders on the motors of a differential drive robot then, when going forward, one encoder would count up and the other would count down (as the wheels are rotating in different directions). This parameter allows you to 'flip' one of the encoders so that they both count upward.
The numStripes parameter allows you to specify how many stripes there are on the encoder. This is purely specified for use by the encoderTicksPerRevolution function so that your main code can convert the counter value into an angular rotation.
The final parameter specifies whether interpolation should be turned on or off by default.
Example:
#include "Sensors/Encoder/Generic/fastquad.h"
FAST_QUADRATURE quad1 = MAKE_GENERIC_FAST_QUADRATURE(INT0,B5,FALSE,32, FALSE);
// then in your main code you can read the encoder as follows:-
encoderRead(quad1);
ENCODER_TYPE ticks = quad1.encoder.value;

MAKE_GENERIC_FAST_QUADRATUREx2(INTaaa,INTbbb,boolean inverted,uint16_t numStripes,boolean interpolate)

Create a new fast quadrature encoder sensor giving 4 ticks per stripe.
The INTaaa parameter specifies the INT pin to use for Channel A eg INT0, INT1 etc. The use of an invalid value will result in a compile/link error.
The INTbbb parameter specifies the INT pin to use for Channel B eg INT0, INT1 etc. The use of an invalid value will result in a compile/link error.
The inverted parameter is similar in concept to the idea of inverted motors or sensors. If you have encoders on the motors of a differential drive robot then, when going forward, one encoder would count up and the other would count down (as the wheels are rotating in different directions). This parameter allows you to 'flip' one of the encoders so that they both count upwards.
The numStripes parameter allows you to specify how many stripes there are on the encoder. This is purely specified for use by the encoderTicksPerRevolution function so that your main code can convert the counter value into an angular rotation.
The final parameter specifies whether interpolation should be turned on or off by default.
Example:
#include "Sensors/Encoder/Generic/fastquad.h"
FAST_QUADRATUREx2 quad1 = MAKE_GENERIC_FAST_QUADRATUREx2(INT0,INT1,FALSE,32,FALSE);
// then in your main code you can read the encoder as follows:-
encoderRead(quad1);
ENCODER_TYPE ticks = quad1.encoder.value;

encoderSubtract(ENCODER* encoder, ENCODER_TYPE count)

Subtract a given value from the encoder counter.

uint16_t encoderTicksPerRevolution(const QUADRATURE* encoder)

Returns the number of counter ticks generated for each 360 degree revolution.

Valid XHTML 1.0 Transitional