Quadrature encoder interface library.

Dependents:   SimpleRover PIDRover IMURover PID_VelocityExample ... more

Embed: (wiki syntax)

« Back to documentation index

QEI Class Reference

QEI Class Reference

Quadrature Encoder Interface. More...

#include <QEI.h>

Public Member Functions

 QEI (PinName channelA, PinName channelB, PinName index, int pulsesPerRev, Encoding encoding=X2_ENCODING)
 Constructor.
void reset (void)
 Reset the encoder.
int getCurrentState (void)
 Read the state of the encoder.
int getPulses (void)
 Read the number of pulses recorded by the encoder.
int getRevolutions (void)
 Read the number of revolutions recorded by the encoder on the index channel.

Detailed Description

Quadrature Encoder Interface.

Definition at line 146 of file QEI.h.


Constructor & Destructor Documentation

QEI ( PinName  channelA,
PinName  channelB,
PinName  index,
int  pulsesPerRev,
Encoding  encoding = X2_ENCODING 
)

Constructor.

Includes.

Reads the current values on channel A and channel B to determine the initial state.

Attaches the encode function to the rise/fall interrupt edges of channels A and B to perform X4 encoding.

Attaches the index function to the rise interrupt edge of channel index (if it is used) to count revolutions.

Parameters:
channelAmbed pin for channel A input.
channelBmbed pin for channel B input.
indexmbed pin for optional index channel input, (pass NC if not needed).
pulsesPerRevNumber of pulses in one revolution.
encodingThe encoding to use. Uses X2 encoding by default. X2 encoding uses interrupts on the rising and falling edges of only channel A where as X4 uses them on both channels.
Author:
Aaron Berk

LICENSE

Copyright (c) 2010 ARM Limited

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

DESCRIPTION

Quadrature Encoder Interface.

A quadrature encoder consists of two code tracks on a disc which are 90 degrees out of phase. It can be used to determine how far a wheel has rotated, relative to a known starting position.

Only one code track changes at a time leading to a more robust system than a single track, because any jitter around any edge won't cause a state change as the other track will remain constant.

Encoders can be a homebrew affair, consisting of infrared emitters/receivers and paper code tracks consisting of alternating black and white sections; alternatively, complete disk and PCB emitter/receiver encoder systems can be bought, but the interface, regardless of implementation is the same.

+-----+ +-----+ +-----+ Channel A | ^ | | | | | ---+ ^ +-----+ +-----+ +----- ^ ^ ^ +-----+ +-----+ +-----+ Channel B ^ | | | | | | ------+ +-----+ +-----+ +----- ^ ^ ^ ^ 90deg

The interface uses X2 encoding by default which calculates the pulse count based on reading the current state after each rising and falling edge of channel A.

+-----+ +-----+ +-----+ Channel A | | | | | | ---+ +-----+ +-----+ +----- ^ ^ ^ ^ ^ ^ +-----+ ^ +-----+ ^ +-----+ Channel B ^ | ^ | ^ | ^ | ^ | | ------+ ^ +-----+ ^ +-----+ +-- ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ Pulse count 0 1 2 3 4 5 ...

This interface can also use X4 encoding which calculates the pulse count based on reading the current state after each rising and falling edge of either channel.

+-----+ +-----+ +-----+ Channel A | | | | | | ---+ +-----+ +-----+ +----- ^ ^ ^ ^ ^ ^ +-----+ ^ +-----+ ^ +-----+ Channel B ^ | ^ | ^ | ^ | ^ | | ------+ ^ +-----+ ^ +-----+ +-- ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ Pulse count 0 1 2 3 4 5 6 7 8 9 ...

It defaults

An optional index channel can be used which determines when a full revolution has occured.

If a 4 pules per revolution encoder was used, with X4 encoding, the following would be observed.

+-----+ +-----+ +-----+ Channel A | | | | | | ---+ +-----+ +-----+ +----- ^ ^ ^ ^ ^ ^ +-----+ ^ +-----+ ^ +-----+ Channel B ^ | ^ | ^ | ^ | ^ | | ------+ ^ +-----+ ^ +-----+ +-- ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ +--+ ^ ^ +--+ ^ ^ ^ ^ | | ^ ^ | | ^ Index ------------+ +--------+ +----------- ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ Pulse count 0 1 2 3 4 5 6 7 8 9 ... Rev. count 0 1 2

Rotational position in degrees can be calculated by:

(pulse count / X * N) * 360

Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number of pulses per revolution.

Linear position can be calculated by:

(pulse count / X * N) * (1 / PPI)

Where X is encoding type [e.g. X4 encoding => X=44], N is the number of pulses per revolution, and PPI is pulses per inch, or the equivalent for any other unit of displacement. PPI can be calculated by taking the circumference of the wheel or encoder disk and dividing it by the number of pulses per revolution.

Definition at line 131 of file QEI.cpp.


Member Function Documentation

int getCurrentState ( void   )

Read the state of the encoder.

Returns:
The current state of the encoder as a 2-bit number, where: bit 1 = The reading from channel B bit 2 = The reading from channel A

Definition at line 176 of file QEI.cpp.

int getPulses ( void   )

Read the number of pulses recorded by the encoder.

Returns:
Number of pulses which have occured.

Definition at line 182 of file QEI.cpp.

int getRevolutions ( void   )

Read the number of revolutions recorded by the encoder on the index channel.

Returns:
Number of revolutions which have occured on the index channel.

Definition at line 188 of file QEI.cpp.

void reset ( void   )

Reset the encoder.

Sets the pulses and revolutions count to zero.

Definition at line 169 of file QEI.cpp.