Quadrature Encoder Interface for motion control with resistance to jitter and chatter on AB signals and motor vibrations

Dependents:   QEIx4_Example realtimeMM_V3 realtimeMM_V3

Embed: (wiki syntax)

« Back to documentation index

QEIx4 Class Reference

QEIx4 Class Reference

Quadrature Encoder Interface for Motion Control. More...

#include <QEIx4.h>

Public Member Functions

 QEIx4 (PinName pinA, PinName pinB, PinName pinI=NC, EMODE eMode=IRQ)
 constructor of QEIx4 object
 ~QEIx4 ()
 destructor of QEIx4 object
int read ()
 Gets the actual counter value.
 operator int ()
 Gets the actual counter value as int operator.
void write (int counter)
 Sets the counter value at actual encoder position to given value.
int operator= (int counter)
 Sets the counter value at actual encoder position to given value as assign operator.
void poll ()
 Polls the state machine manually and updates the counter value.
void setIndexTrigger (bool bIndexTrigger)
 Sets the flag for zeroing on next high on index pin while AB lines triggers next counting.
void attachCounterChange (void(*function)(int)=0)
 attach - Overloaded attachment function.
template<class T >
void attachCounterChange (T *item, void(T::*method)(int))
 attachCounterChange - Overloaded attachment function.
void attachDirectionChange (void(*function)(int)=0)
 attachDirectionChange - Overloaded attachment function.
template<class T >
void attachDirectionChange (T *item, void(T::*method)(int))
 attachDirectionChange - Overloaded attachment function.
void attachIndexTrigger (void(*function)(int)=0)
 attachIndexTrigger - Overloaded attachment function.
template<class T >
void attachIndexTrigger (T *item, void(T::*method)(int))
 attachIndexTrigger - Overloaded attachment function.
void setSpeedFactor (float fSpeedFactor)
 Sets the factor for the getter-functions to convert in another unit (1.0=Hz, 1/(4*CPR)=rps, 1/(60*4*CPR)=rpm, 360/(4*CPR)=°/s, ...)
float getSpeed ()
 Gets the actual speed as float value.
void setPositionFactor (float fPositionFactor)
 Sets the factor for the getter-functions to convert in another unit (e.g.
float getPosition ()
 Gets the actual counter value as float value.

Detailed Description

Quadrature Encoder Interface for Motion Control.

A class to decode pulses on a rotary encoder with AB signals (quadrature encoder). It uses all 4 edges of the AB signals to increase the counter resolution 4 times of cycles per rotation/revolution (CPR) (e.g. an encoder with 500 CPR get 2000 counts per rotation)

In opposite to most common QEI implementation this is resistant to jitter and chatter on AB signals and motor vibrations. When using interrupts (IRQ_NO_JAMMING-mode) only the needed edge and pin is activated to prevent jamming CPU time with unnecessary interrupts. Whes reaching the next position the edge that triggerd this position (state) is ignored to aboid oscillating up/down counts.

It can also be used in polling mode i.g. in idle routines if interrupts are not desired. At this mode be sure that the sampling frequency is heigher than the maximum rotation speed (expeced counts per second)

The internal state machine is based on a look up table (LUT) to minimize interrupt retention time and get all necessary flags at once.

Additional the rotation speed of the encoder can be measured. The algorithm is based on the measuring time between the edges to get a very precise speed at very slow rotation.

The library is designed to support closed loop speed- and motion-controller for also slow and smooth motions like movie camera motion control.

Example:

#include "mbed.h"
#include "QEIx4.h"

DigitalOut LEDalive(LED1);
DigitalOut LEDzero(LED2);
DigitalOut LEDup(LED4);
DigitalOut LEDdown(LED3);

Timer t;   // timer for polling

// ports for nxp LPC 1768
QEIx4 qei1(p30, p29, p28, (QEIx4::EMODE)(QEIx4::IRQ | QEIx4::SPEED));   // QEI with index signal for zeroing
QEIx4 qei2(p21, p22, NC,  QEIx4::IRQ_NO_JAMMING);                       // QEI with AB signals only
QEIx4 qei3(p25, p24, NC,  QEIx4::POLLING);                              // QEI without interrups in polling mode

// The callback functions
void myCounterChangeCallback(int value)
{
    static int valueLast=-1;

    if ( value > valueLast ) {
        LEDup = !LEDup;
        LEDdown = 0;
    } else {
        LEDdown = !LEDdown;
        LEDup = 0;
    }
    valueLast = value;
}

void myIndexTriggerCallback(int value)
{
    qei1 = 0;   // reset counter
    LEDzero = 1;
}

int main()
{
    t.start();

    qei1.setIndexTrigger(true);     // set the flag to zero counter on next index signal rises
    qei1.setSpeedFactor(1.0f);      // factor to scale from Hz (edges pe second = 4 * CPS) to user units (1.0=Hz, 1/(4*CPR)=rps, 1/(60*4*CPR)=rpm, 360/(4*CPR)=°/s, ...)
    qei3.attachIndexTrigger(myIndexTriggerCallback);
    
    qei3.attachCounterChange(myCounterChangeCallback);

    while(1) {
        qei3.poll();   // poll manually without interrupt - sampling in this loop with about 2kHz

        if ( t.read_ms() > 250 ) { // every quater second (4 Hz)
            t.reset();
            t.start();
            LEDalive = !LEDalive;

            printf ( "\r\n%6d  %6d  %6d  %10.3f", (int)qei1, (int)qei2, (int)qei3, (float)qei1.getSpeed() );   // print counter values
        }

        wait_us(20);   // for about 50kHz polling
    }
}

Definition at line 119 of file QEIx4.h.


Constructor & Destructor Documentation

QEIx4 ( PinName  pinA,
PinName  pinB,
PinName  pinI = NC,
EMODE  eMode = IRQ 
)

constructor of QEIx4 object

Parameters:
pinAPin number of input/interrupt pin for encoder line A. All port pins are possible except p19 and p20
pinBPin number of input/interrupt pin for encoder line B. All port pins are possible except p19 and p20
pinIPin number of input pin for optional encoder index or reference switch.
eModeFlag to use interrups to detect changes on line A and B. For none interrupt use mode POLLING and call the function poll() frequently. For optional speed calculation the mode SPEED can be ored

Definition at line 71 of file QEIx4.cpp.

~QEIx4 (  )

destructor of QEIx4 object

Definition at line 115 of file QEIx4.cpp.


Member Function Documentation

void attachCounterChange ( void(*)(int)  function = 0 )

attach - Overloaded attachment function.

Attach a C type function pointer as the callback.

Note, the callback function prototype must be:-

 void myCallbackFunction(int);
Parameters:
AC function pointer to call.

Definition at line 200 of file QEIx4.h.

void attachCounterChange ( T *  item,
void(T::*)(int)  method 
)

attachCounterChange - Overloaded attachment function.

Attach a C++ type object/method pointer as the callback.

Note, the callback method prototype must be:-

     public:
         static void myCallbackFunction(int);
Parameters:
AC++ object pointer.
AC++ method within the object to call.

Definition at line 217 of file QEIx4.h.

void attachDirectionChange ( void(*)(int)  function = 0 )

attachDirectionChange - Overloaded attachment function.

Attach a C type function pointer as the callback.

Note, the callback function prototype must be:-

 void myCallbackFunction(int);
Parameters:
AC function pointer to call.

Definition at line 231 of file QEIx4.h.

void attachDirectionChange ( T *  item,
void(T::*)(int)  method 
)

attachDirectionChange - Overloaded attachment function.

Attach a C++ type object/method pointer as the callback.

Note, the callback method prototype must be:-

     public:
         static void myCallbackFunction(int);
Parameters:
AC++ object pointer.
AC++ method within the object to call.

Definition at line 248 of file QEIx4.h.

void attachIndexTrigger ( void(*)(int)  function = 0 )

attachIndexTrigger - Overloaded attachment function.

Attach a C type function pointer as the callback.

Note, the callback function prototype must be:-

 void myCallbackFunction(int);
Parameters:
AC function pointer to call.

Definition at line 262 of file QEIx4.h.

void attachIndexTrigger ( T *  item,
void(T::*)(int)  method 
)

attachIndexTrigger - Overloaded attachment function.

Attach a C++ type object/method pointer as the callback.

Note, the callback method prototype must be:-

     public:
         static void myCallbackFunction(int);
Parameters:
AC++ object pointer.
AC++ method within the object to call.

Definition at line 279 of file QEIx4.h.

float getPosition (  )

Gets the actual counter value as float value.

The value is scales by the facor set by setSpeedFactor()

Returns:
Actual encoder position as float

Definition at line 309 of file QEIx4.h.

float getSpeed (  )

Gets the actual speed as float value.

The value is scales by the facor set by setPositionFactor()

Returns:
Actual encoder speed as float

Definition at line 125 of file QEIx4.cpp.

operator int (  )

Gets the actual counter value as int operator.

Returns:
Actual counter value as int operator

Definition at line 155 of file QEIx4.h.

int operator= ( int  counter )

Sets the counter value at actual encoder position to given value as assign operator.

Parameters:
Countervalue

Definition at line 171 of file QEIx4.h.

void poll (  )

Polls the state machine manually and updates the counter value.

Definition at line 178 of file QEIx4.h.

int read (  )

Gets the actual counter value.

Returns:
Actual counter value

Definition at line 147 of file QEIx4.h.

void setIndexTrigger ( bool  bIndexTrigger )

Sets the flag for zeroing on next high on index pin while AB lines triggers next counting.

The trigger calls tha callback function in which the counter can be set to zero or the actual counter can be latched in for later offset calculation

Parameters:
Flagfor triggering. Set to 1 for call the attached callback. It is reseted after this call

Definition at line 186 of file QEIx4.h.

void setPositionFactor ( float  fPositionFactor )

Sets the factor for the getter-functions to convert in another unit (e.g.

CPR (cycles per rotation) * 4.0 to get 1.0 for a full rotation)

Parameters:
fPositionFactorFactor to scale from counts to user unit

Definition at line 301 of file QEIx4.h.

void setSpeedFactor ( float  fSpeedFactor )

Sets the factor for the getter-functions to convert in another unit (1.0=Hz, 1/(4*CPR)=rps, 1/(60*4*CPR)=rpm, 360/(4*CPR)=°/s, ...)

Parameters:
fSpeedFactor- factor to scale from Hz (edges per second = 4 * CPS) to user units

Definition at line 287 of file QEIx4.h.

void write ( int  counter )

Sets the counter value at actual encoder position to given value.

Parameters:
Countervalue

Definition at line 163 of file QEIx4.h.