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.

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 no pulses 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.

Library

Reference


All wikipages