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

Generic/quadrature.h

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 have find your are missing ticks because you have a high resolution encoder then you may want to consider using the fastquad.h routines 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 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.
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 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

 


MAKE_GENERIC_QUADRATURE(const IOPin* channelA,const IOPin* channelB,boolean inverted,uint16_t numStripes,boolean interpolate)

Create a new quadrature encoder sensor giving 2 ticks per encoder stripe.
The channelA parameter specifies the IOPin to use for channel A and must support hardware pin change interrupts - see pinChange.h.
The channelB parameters 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 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/quadrature.h"
QUADRATURE quad1 = MAKE_GENERIC_QUADRATURE(B4,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_QUADRATUREx2(const IOPin* channelA,const IOPin* channelB,boolean inverted,uint16_t numStripes,boolean interpolate)

Create a new quadrature encoder sensor giving 4 ticks per encoder stripe.
The channelA parameter specifies the IOPin to use for channel A and must support hardware pin change interrupts. See pinChange.h
The channelB parameter specifies the IOPin to use for channel B and must support hardware pin change interrupts. See pinChange.h
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/quadrature.h"
QUADRATURE quad1 = MAKE_GENERIC_QUADRATUREx2(B4,B5,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