You are viewing an older revision! See the latest version

QEI

A rotary encoder converts angular position to an analog or digital code.

This page will deal a specific type of incremental rotary encoder - an optical quadrature encoder, which is very popular tool for determining how much a wheel has rotated and thus how far something, such as a robot, has moved.

An optical encoder consists of a disc with alternating areas of reflection and non-reflection. An emitter and receiver such as an LED and photodiode read the resulting optical pattern from the position of the optical encoder.

An incremental encoder has no knowledge of its absolute position and is simply able to count pulses which can be used to update a position in external electronics.

For the rest of this page, the phrase "quadrature encoder" will be used to mean "optical quadrature encoder".

Quadrature Encoder

A quadrature encoder consists of a disc with two tracks, containing alternating areas of reflection and non reflection, 90 degrees out of phase.

http://mbed.org/media/uploads/aberk/encoderwheel.png

As it rotates in front of an emitter/receiver pair for each track (which we will call channel A, and channel B), it will produce the following results.

http://mbed.org/media/uploads/aberk/quadraturephase.png

There are four distinct states that can be achieved with this disc, which are gray codes. Each time there is a valid state change, the pulse count is incremented, or decremented depending on the direction. A state change is only valid if only one of the tracks has changed. If both tracks change at the same time the state change is invalid.

Clockwise rotations

PhaseChannel AChannel B
100
201
311
410

Counter clockwise rotations

PhaseChannel AChannel B
110
211
301
400

Software

The QEI library use X4 encoding, which looks at the state every time a rising or falling edge occurs on either track, and updates a pulse count appropriately, depending on the direction of rotation.

It uses channel A and channel B as InterruptIn pins to ensure as few pulses as possible are missed which might cause invalid state readings or an incorrect count if polling was used.

An optional index channel is available which is essentially a third track on the disc which has one pulse per revolution. Thus it can keep track of the number of revolutions of the disc.

After creation of a QEI object, any change in the position of the encoder wheel will cause an update of the internal pulse count. The following method can be used to find out what the current pulse count is, and with supporting code can be used to determine absolute position.

    /**
     * Read the number of pulses recorded by the encoder.
     *
     * @return Number of pulses which have occured.
     */
    int getPulses(void);

The pulse, and revolution count can also be reset.

    /**
     * Reset the encoder.
     *
     * Sets the pulses and revolutions count to zero.
     */
    void reset(void);

It should be noted that the internal revolution count is only updated by the index channel. If no index channel is used, it is still easy to work out the current revolution count by dividing the pulse count by number of pulses per revolution for the disc, but this must be done in external code.

Interrupt Latency

As this library uses to interrupts to determine the pulse count, there is an upper limit on the number of pulses per second it can detect, which is dependent on the time it takes to service the interrupt.

Preliminary tests show the library should be able to deal with approximately 1000 counts per second, but this figure is likely to increase in the future as the interrupt method is further refined.

It has been tested on an encoder with 624 counts per revolution running at greater than 60rpm with no problems.

Hardware

Although this page has dealt primarily with optical quadrature encoders, the interface will deal with any encoder that provides quadrature outputs. These include the Pololu Encoder and this DC Gearhead Robot Motor which has a built in quadrature encoder.

Alternatively you can make your own with a pair of QRD1114 IR sensors, and an encoder disc which you can generate and then print using this useful online tool.

By lining each QRD1114 up with each track of the encoder disc, you have a very cheap and reliable quadrature encoder which can be used to accurately determine the position [from a set starting point] of a wheeled object.

Library

Reference


All wikipages