![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A polled Quadrature encoder Class
Revision 1:72e6e124a7e6, committed 2011-08-30
- Comitter:
- jgnoss
- Date:
- Tue Aug 30 20:03:36 2011 +0000
- Parent:
- 0:7c16b1d2f01e
- Commit message:
- Added Phase Z and Rev Counter
Changed in this revision
--- a/PolledQEI.cpp Tue Aug 30 19:52:34 2011 +0000 +++ b/PolledQEI.cpp Tue Aug 30 20:03:36 2011 +0000 @@ -1,73 +1,73 @@ -#include "mbed.h" -#include "PolledQEI.h" - - - - -void PolledQEI::setSampleFrequency(int i = 10000) { - _sample_uSec = i; - _actEncPos = 0; - _ticker->attach_us( this, &PolledQEI::check, _sample_uSec ); -} - -int PolledQEI::pos(void) { - return _actEncPos; -} - -int PolledQEI::dir(void) { - return _dir; -} - -int PolledQEI::rev(void) { - return _actEncRev; -} - -void PolledQEI::countRev(void) { - if (_dir) { - _actEncRev++; - } else { - _actEncRev--; - } -} - -void PolledQEI::init(PinName a, PinName b, PinName z = NC) { - - _phaseA = new DigitalIn( a ); - _phaseA->mode( PullUp ); - _phaseB = new DigitalIn( b ); - _phaseB->mode( PullUp ); - if (z != NC) { - _phaseZ = new InterruptIn( z ); - _phaseZ->mode( PullUp ); - _phaseZ->fall(this, &PolledQEI::countRev); - } - _actEncRev = 0; - _actEncPos = 0; - _dir = 0; - - _ticker = new Ticker; -} - -void PolledQEI::check (void) { - unsigned int pA = _phaseA->read(); - unsigned int pB = _phaseB->read(); - unsigned int newV = 0, diffV = 0; - - - // convert gray to binary - if ( pA ) { - newV = 3; - } - if ( pB ) { - newV ^= 1; - } - - diffV = last - newV; // difference last - new - if ( diffV & 1 ) { // bit 0 = value (1) - last = newV; // store new as next last - _actEncPos -= (diffV & 2) - 1; // bit 1 = direction (+/-) - _dir = (diffV & 2)?CW:CCW; - } -} - - +#include "mbed.h" +#include "PolledQEI.h" + + + + +void PolledQEI::setSampleFrequency(int i = 10000) { + _sample_uSec = i; + _actEncPos = 0; + _ticker->attach_us( this, &PolledQEI::check, _sample_uSec ); +} + +int PolledQEI::pos(void) { + return _actEncPos; +} + +int PolledQEI::dir(void) { + return _dir; +} + +int PolledQEI::rev(void) { + return _actEncRev; +} + +void PolledQEI::countRev(void) { + if (_dir) { + _actEncRev++; + } else { + _actEncRev--; + } +} + +void PolledQEI::init(PinName a, PinName b, PinName z = NC) { + + _phaseA = new DigitalIn( a ); + _phaseA->mode( PullUp ); + _phaseB = new DigitalIn( b ); + _phaseB->mode( PullUp ); + if (z != NC) { + _phaseZ = new InterruptIn( z ); + _phaseZ->mode( PullUp ); + _phaseZ->fall(this, &PolledQEI::countRev); + } + _actEncRev = 0; + _actEncPos = 0; + _dir = 0; + + _ticker = new Ticker; +} + +void PolledQEI::check (void) { + unsigned int pA = _phaseA->read(); + unsigned int pB = _phaseB->read(); + unsigned int newV = 0, diffV = 0; + + + // convert gray to binary + if ( pA ) { + newV = 3; + } + if ( pB ) { + newV ^= 1; + } + + diffV = last - newV; // difference last - new + if ( diffV & 1 ) { // bit 0 = value (1) + last = newV; // store new as next last + _actEncPos -= (diffV & 2) - 1; // bit 1 = direction (+/-) + _dir = (diffV & 2)?CW:CCW; + } +} + +
--- a/PolledQEI.h Tue Aug 30 19:52:34 2011 +0000 +++ b/PolledQEI.h Tue Aug 30 20:03:36 2011 +0000 @@ -1,74 +1,74 @@ -#ifndef _POLLED_QEI_H -#define _POLLED_QEI_H - -#define CW 0 -#define CCW 1 - - -class PolledQEI { -public: - - - - PolledQEI() { - error("Constructor need 2 parameters"); - }; - - /** PolledQEI constructor - * - * @param PinName PhaseA and PinName PhaseB - * are a valid pin that supports DigitalIn - */ - PolledQEI(PinName a, PinName b) { - init( a, b, NC ); - } - - /** PolledQEI constructor - * - * @param PinName PhaseA and PinName PhaseB and PinName PhaseZ - * are a valid pin that supports DigitalIn - */ - PolledQEI(PinName a, PinName b, PinName z) { - init( a, b, z ); - } - - /** PolledQEI destructor - */ - ~PolledQEI() { - if ( _ticker ) delete( _ticker ); - if ( _phaseA ) delete( _phaseA ); - if ( _phaseB ) delete( _phaseB ); - if ( _phaseZ ) delete( _phaseZ ); - } - - /** Set the sampling time in microseconds. - * - * @param int The time between pin samples in microseconds. - */ - void setSampleFrequency(int i); - int pos(void); - int dir(void); - int rev(void); - - -private: - - -protected: - DigitalIn *_phaseA; - DigitalIn *_phaseB; - InterruptIn *_phaseZ; - Ticker *_ticker; - int _sample_uSec; - unsigned int _actEncPos; - unsigned int _actEncRev; - int _dir; - unsigned int last; - - void init(PinName a, PinName b, PinName z); - void check (void); - void countRev(void); - -}; - +#ifndef _POLLED_QEI_H +#define _POLLED_QEI_H + +#define CW 0 +#define CCW 1 + + +class PolledQEI { +public: + + + + PolledQEI() { + error("Constructor needs 2(3) Pin as parameters"); + }; + + /** PolledQEI constructor + * + * @param PinName PhaseA and PinName PhaseB + * are a valid pin that supports DigitalIn + */ + PolledQEI(PinName a, PinName b) { + init( a, b, NC ); + } + + /** PolledQEI constructor + * + * @param PinName PhaseA and PinName PhaseB and PinName PhaseZ + * are a valid pin that supports DigitalIn + */ + PolledQEI(PinName a, PinName b, PinName z) { + init( a, b, z ); + } + + /** PolledQEI destructor + */ + ~PolledQEI() { + if ( _ticker ) delete( _ticker ); + if ( _phaseA ) delete( _phaseA ); + if ( _phaseB ) delete( _phaseB ); + if ( _phaseZ ) delete( _phaseZ ); + } + + /** Set the sampling time in microseconds. + * + * @param int The time between pin samples in microseconds. + */ + void setSampleFrequency(int i); + int pos(void); + int dir(void); + int rev(void); + + +private: + + +protected: + DigitalIn *_phaseA; + DigitalIn *_phaseB; + InterruptIn *_phaseZ; + Ticker *_ticker; + int _sample_uSec; + unsigned int _actEncPos; + unsigned int _actEncRev; + int _dir; + unsigned int last; + + void init(PinName a, PinName b, PinName z); + void check (void); + void countRev(void); + +}; + #endif \ No newline at end of file
--- a/main.cpp Tue Aug 30 19:52:34 2011 +0000 +++ b/main.cpp Tue Aug 30 20:03:36 2011 +0000 @@ -7,10 +7,13 @@ int main() { int oldEncPos = 0; int newEncPos = 0; + int oldEncRev = 0; + int newEncRev = 0; DigitalOut led1( LED1 ); + DigitalOut led2( LED2 ); - PolledQEI enc(p5, p6); + PolledQEI enc(p5, p6, p7); enc.setSampleFrequency(10000); led1 = 1; @@ -23,6 +26,11 @@ led1 = !led1; } + newEncRev = enc.rev(); + if (oldEncRev != newEncRev ) { + oldEncRev = newEncRev; + led2 = !led2; + } wait(0.01); } }