![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A polled Quadrature encoder Class
PolledQEI.cpp@0:7c16b1d2f01e, 2011-08-30 (annotated)
- Committer:
- jgnoss
- Date:
- Tue Aug 30 19:52:34 2011 +0000
- Revision:
- 0:7c16b1d2f01e
- Child:
- 1:72e6e124a7e6
Working example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jgnoss | 0:7c16b1d2f01e | 1 | #include "mbed.h" |
jgnoss | 0:7c16b1d2f01e | 2 | #include "PolledQEI.h" |
jgnoss | 0:7c16b1d2f01e | 3 | |
jgnoss | 0:7c16b1d2f01e | 4 | |
jgnoss | 0:7c16b1d2f01e | 5 | |
jgnoss | 0:7c16b1d2f01e | 6 | |
jgnoss | 0:7c16b1d2f01e | 7 | void PolledQEI::setSampleFrequency(int i = 10000) { |
jgnoss | 0:7c16b1d2f01e | 8 | _sample_uSec = i; |
jgnoss | 0:7c16b1d2f01e | 9 | _actEncPos = 0; |
jgnoss | 0:7c16b1d2f01e | 10 | _ticker->attach_us( this, &PolledQEI::check, _sample_uSec ); |
jgnoss | 0:7c16b1d2f01e | 11 | } |
jgnoss | 0:7c16b1d2f01e | 12 | |
jgnoss | 0:7c16b1d2f01e | 13 | int PolledQEI::pos(void) { |
jgnoss | 0:7c16b1d2f01e | 14 | return _actEncPos; |
jgnoss | 0:7c16b1d2f01e | 15 | } |
jgnoss | 0:7c16b1d2f01e | 16 | |
jgnoss | 0:7c16b1d2f01e | 17 | int PolledQEI::dir(void) { |
jgnoss | 0:7c16b1d2f01e | 18 | return _dir; |
jgnoss | 0:7c16b1d2f01e | 19 | } |
jgnoss | 0:7c16b1d2f01e | 20 | |
jgnoss | 0:7c16b1d2f01e | 21 | int PolledQEI::rev(void) { |
jgnoss | 0:7c16b1d2f01e | 22 | return _actEncRev; |
jgnoss | 0:7c16b1d2f01e | 23 | } |
jgnoss | 0:7c16b1d2f01e | 24 | |
jgnoss | 0:7c16b1d2f01e | 25 | void PolledQEI::countRev(void) { |
jgnoss | 0:7c16b1d2f01e | 26 | if (_dir) { |
jgnoss | 0:7c16b1d2f01e | 27 | _actEncRev++; |
jgnoss | 0:7c16b1d2f01e | 28 | } else { |
jgnoss | 0:7c16b1d2f01e | 29 | _actEncRev--; |
jgnoss | 0:7c16b1d2f01e | 30 | } |
jgnoss | 0:7c16b1d2f01e | 31 | } |
jgnoss | 0:7c16b1d2f01e | 32 | |
jgnoss | 0:7c16b1d2f01e | 33 | void PolledQEI::init(PinName a, PinName b, PinName z = NC) { |
jgnoss | 0:7c16b1d2f01e | 34 | |
jgnoss | 0:7c16b1d2f01e | 35 | _phaseA = new DigitalIn( a ); |
jgnoss | 0:7c16b1d2f01e | 36 | _phaseA->mode( PullUp ); |
jgnoss | 0:7c16b1d2f01e | 37 | _phaseB = new DigitalIn( b ); |
jgnoss | 0:7c16b1d2f01e | 38 | _phaseB->mode( PullUp ); |
jgnoss | 0:7c16b1d2f01e | 39 | if (z != NC) { |
jgnoss | 0:7c16b1d2f01e | 40 | _phaseZ = new InterruptIn( z ); |
jgnoss | 0:7c16b1d2f01e | 41 | _phaseZ->mode( PullUp ); |
jgnoss | 0:7c16b1d2f01e | 42 | _phaseZ->fall(this, &PolledQEI::countRev); |
jgnoss | 0:7c16b1d2f01e | 43 | } |
jgnoss | 0:7c16b1d2f01e | 44 | _actEncRev = 0; |
jgnoss | 0:7c16b1d2f01e | 45 | _actEncPos = 0; |
jgnoss | 0:7c16b1d2f01e | 46 | _dir = 0; |
jgnoss | 0:7c16b1d2f01e | 47 | |
jgnoss | 0:7c16b1d2f01e | 48 | _ticker = new Ticker; |
jgnoss | 0:7c16b1d2f01e | 49 | } |
jgnoss | 0:7c16b1d2f01e | 50 | |
jgnoss | 0:7c16b1d2f01e | 51 | void PolledQEI::check (void) { |
jgnoss | 0:7c16b1d2f01e | 52 | unsigned int pA = _phaseA->read(); |
jgnoss | 0:7c16b1d2f01e | 53 | unsigned int pB = _phaseB->read(); |
jgnoss | 0:7c16b1d2f01e | 54 | unsigned int newV = 0, diffV = 0; |
jgnoss | 0:7c16b1d2f01e | 55 | |
jgnoss | 0:7c16b1d2f01e | 56 | |
jgnoss | 0:7c16b1d2f01e | 57 | // convert gray to binary |
jgnoss | 0:7c16b1d2f01e | 58 | if ( pA ) { |
jgnoss | 0:7c16b1d2f01e | 59 | newV = 3; |
jgnoss | 0:7c16b1d2f01e | 60 | } |
jgnoss | 0:7c16b1d2f01e | 61 | if ( pB ) { |
jgnoss | 0:7c16b1d2f01e | 62 | newV ^= 1; |
jgnoss | 0:7c16b1d2f01e | 63 | } |
jgnoss | 0:7c16b1d2f01e | 64 | |
jgnoss | 0:7c16b1d2f01e | 65 | diffV = last - newV; // difference last - new |
jgnoss | 0:7c16b1d2f01e | 66 | if ( diffV & 1 ) { // bit 0 = value (1) |
jgnoss | 0:7c16b1d2f01e | 67 | last = newV; // store new as next last |
jgnoss | 0:7c16b1d2f01e | 68 | _actEncPos -= (diffV & 2) - 1; // bit 1 = direction (+/-) |
jgnoss | 0:7c16b1d2f01e | 69 | _dir = (diffV & 2)?CW:CCW; |
jgnoss | 0:7c16b1d2f01e | 70 | } |
jgnoss | 0:7c16b1d2f01e | 71 | } |
jgnoss | 0:7c16b1d2f01e | 72 | |
jgnoss | 0:7c16b1d2f01e | 73 |