C++ documentation C documentation

Supports measurement of encoders in ticks.

All readings from this library are in 'ticks'.

They measure the clockwise and counter-clockwise rotation of an axle. For our purposes they are normally mounted onto motors or wheels.

Each encoder emits a given number of 'ticks' per revolution with the ticks increasing if the axle is turned one way and decreased if turning the other way.

If we know how many ticks the axle has moved, and the number of ticks the encoder produces per 360 degrees, then we can work out how many degrees the axle has turned. Then, by knowing the wheel circumference, we can convert this into a distance travelled. But beware of slippage: if the robot is on ice, or a steep hill, then the wheel may turn and generate the ticks but the robot may not have moved. Slippage can be minimised by changing acceleration gradually (ie avoid wheel spin unlike those drag racing cars!), by having 'gripping' wheels etc etc.

There are other uses for encoders - including non-robot stuff. I bet you've had one of those old scratchy radios where every time you twiddle the volume you hear a horrible scratching noise. That is because they use a potentiometer and that noise is caused because it involves mechanical contacts which eventually wear out the carbon track on the potentiometer. More modern designs use encoders for volume control as there is no mechanical contact - so less noise and nothing to 'wear out'.

Assuming you have added an encoder within Project Designer and called in 'myEncoder' then you can access the current number of ticks as follows:-

// Read and store the current value

myEncoder.read();

// You can now access the returned value

ENCODER_TYPE ticks = myEncoder.getTicks();

// Or dump it the standard output stream

cout << myEncoder;

This library allows you to add some additional functionality to your encoders called interpolation.

If 'interpolation' is enabled then two additional pieces of information can be accessed following the read().

TICK_COUNT t1 = myEncoder.getTimeSinceLastTick();

TICK_COUNT t2 = myEncoder.getDurationOfLastTick();

The 'timeSinceLastTick' is the number of µS since we last a received a tick; and durationOfLastTick is the number of µS between the previous two ticks.

This means that, as a rough approximation, the 'durationOfLastTick' can be used to guess the current speed of the motor. ie if it has a value of 1000µS and the encoder is giving 200 ticks per revolution. Then a full revolution would currently require 1000 x 200 = 200,000µS = 0.2 seconds. Hence the motor is rotating at 300 rpm. Given that we know the wheel circumference then we can turn this into a ground speed.

Obviously this assumes that the motor speed is fairly constant as it is just calculated from the last 'tick'.

The 'timeSinceLastTick' is useful if your encoder only has a small number of stripes but the wheel circumference is quite high. In this case a single 'tick' can represent a significant distance. So this parameter allows you to estimate the current fraction of a tick.

For example: if 'durationOfLastTick' is 1000µS and the 'timeSinceLastTick' is 500µS then we can estimate that the wheel has turned by a further 0.5 of a 'tick'.

The 'interpolation' can be turned on and off within Project Designer and also at runtime by calling the 'setInterpolating()' function.

When interpolation is turned on then there is extra processing required for each tick and so the maximum number of ticks per second is reduced (see later for metrics). However: since interpolation is only generally useful for low resolution encoders then this should not be an issue.

Project Designer supports the following devices

- Fast Quadrature - Support for a generic quadrature encoder using external interrupts.
- Quadrature - Support for a generic quadrature encoder using pin change interrupts.