![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A polled Quadrature encoder Class
PolledQEI.h@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 | #ifndef _POLLED_QEI_H |
jgnoss | 0:7c16b1d2f01e | 2 | #define _POLLED_QEI_H |
jgnoss | 0:7c16b1d2f01e | 3 | |
jgnoss | 0:7c16b1d2f01e | 4 | #define CW 0 |
jgnoss | 0:7c16b1d2f01e | 5 | #define CCW 1 |
jgnoss | 0:7c16b1d2f01e | 6 | |
jgnoss | 0:7c16b1d2f01e | 7 | |
jgnoss | 0:7c16b1d2f01e | 8 | class PolledQEI { |
jgnoss | 0:7c16b1d2f01e | 9 | public: |
jgnoss | 0:7c16b1d2f01e | 10 | |
jgnoss | 0:7c16b1d2f01e | 11 | |
jgnoss | 0:7c16b1d2f01e | 12 | |
jgnoss | 0:7c16b1d2f01e | 13 | PolledQEI() { |
jgnoss | 0:7c16b1d2f01e | 14 | error("Constructor need 2 parameters"); |
jgnoss | 0:7c16b1d2f01e | 15 | }; |
jgnoss | 0:7c16b1d2f01e | 16 | |
jgnoss | 0:7c16b1d2f01e | 17 | /** PolledQEI constructor |
jgnoss | 0:7c16b1d2f01e | 18 | * |
jgnoss | 0:7c16b1d2f01e | 19 | * @param PinName PhaseA and PinName PhaseB |
jgnoss | 0:7c16b1d2f01e | 20 | * are a valid pin that supports DigitalIn |
jgnoss | 0:7c16b1d2f01e | 21 | */ |
jgnoss | 0:7c16b1d2f01e | 22 | PolledQEI(PinName a, PinName b) { |
jgnoss | 0:7c16b1d2f01e | 23 | init( a, b, NC ); |
jgnoss | 0:7c16b1d2f01e | 24 | } |
jgnoss | 0:7c16b1d2f01e | 25 | |
jgnoss | 0:7c16b1d2f01e | 26 | /** PolledQEI constructor |
jgnoss | 0:7c16b1d2f01e | 27 | * |
jgnoss | 0:7c16b1d2f01e | 28 | * @param PinName PhaseA and PinName PhaseB and PinName PhaseZ |
jgnoss | 0:7c16b1d2f01e | 29 | * are a valid pin that supports DigitalIn |
jgnoss | 0:7c16b1d2f01e | 30 | */ |
jgnoss | 0:7c16b1d2f01e | 31 | PolledQEI(PinName a, PinName b, PinName z) { |
jgnoss | 0:7c16b1d2f01e | 32 | init( a, b, z ); |
jgnoss | 0:7c16b1d2f01e | 33 | } |
jgnoss | 0:7c16b1d2f01e | 34 | |
jgnoss | 0:7c16b1d2f01e | 35 | /** PolledQEI destructor |
jgnoss | 0:7c16b1d2f01e | 36 | */ |
jgnoss | 0:7c16b1d2f01e | 37 | ~PolledQEI() { |
jgnoss | 0:7c16b1d2f01e | 38 | if ( _ticker ) delete( _ticker ); |
jgnoss | 0:7c16b1d2f01e | 39 | if ( _phaseA ) delete( _phaseA ); |
jgnoss | 0:7c16b1d2f01e | 40 | if ( _phaseB ) delete( _phaseB ); |
jgnoss | 0:7c16b1d2f01e | 41 | if ( _phaseZ ) delete( _phaseZ ); |
jgnoss | 0:7c16b1d2f01e | 42 | } |
jgnoss | 0:7c16b1d2f01e | 43 | |
jgnoss | 0:7c16b1d2f01e | 44 | /** Set the sampling time in microseconds. |
jgnoss | 0:7c16b1d2f01e | 45 | * |
jgnoss | 0:7c16b1d2f01e | 46 | * @param int The time between pin samples in microseconds. |
jgnoss | 0:7c16b1d2f01e | 47 | */ |
jgnoss | 0:7c16b1d2f01e | 48 | void setSampleFrequency(int i); |
jgnoss | 0:7c16b1d2f01e | 49 | int pos(void); |
jgnoss | 0:7c16b1d2f01e | 50 | int dir(void); |
jgnoss | 0:7c16b1d2f01e | 51 | int rev(void); |
jgnoss | 0:7c16b1d2f01e | 52 | |
jgnoss | 0:7c16b1d2f01e | 53 | |
jgnoss | 0:7c16b1d2f01e | 54 | private: |
jgnoss | 0:7c16b1d2f01e | 55 | |
jgnoss | 0:7c16b1d2f01e | 56 | |
jgnoss | 0:7c16b1d2f01e | 57 | protected: |
jgnoss | 0:7c16b1d2f01e | 58 | DigitalIn *_phaseA; |
jgnoss | 0:7c16b1d2f01e | 59 | DigitalIn *_phaseB; |
jgnoss | 0:7c16b1d2f01e | 60 | InterruptIn *_phaseZ; |
jgnoss | 0:7c16b1d2f01e | 61 | Ticker *_ticker; |
jgnoss | 0:7c16b1d2f01e | 62 | int _sample_uSec; |
jgnoss | 0:7c16b1d2f01e | 63 | unsigned int _actEncPos; |
jgnoss | 0:7c16b1d2f01e | 64 | unsigned int _actEncRev; |
jgnoss | 0:7c16b1d2f01e | 65 | int _dir; |
jgnoss | 0:7c16b1d2f01e | 66 | unsigned int last; |
jgnoss | 0:7c16b1d2f01e | 67 | |
jgnoss | 0:7c16b1d2f01e | 68 | void init(PinName a, PinName b, PinName z); |
jgnoss | 0:7c16b1d2f01e | 69 | void check (void); |
jgnoss | 0:7c16b1d2f01e | 70 | void countRev(void); |
jgnoss | 0:7c16b1d2f01e | 71 | |
jgnoss | 0:7c16b1d2f01e | 72 | }; |
jgnoss | 0:7c16b1d2f01e | 73 | |
jgnoss | 0:7c16b1d2f01e | 74 | #endif |