simple QEI library. no return speed, only return angle.
Dependents: WRS_mechanamu_test
QEI.cpp@0:bffc97496048, 2018-08-20 (annotated)
- Committer:
- sgrsn
- Date:
- Mon Aug 20 04:54:24 2018 +0000
- Revision:
- 0:bffc97496048
- Child:
- 1:6fa863d09d45
- Child:
- 2:62821df48957
simple QEI
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sgrsn | 0:bffc97496048 | 1 | #include "QEI.h" |
sgrsn | 0:bffc97496048 | 2 | |
sgrsn | 0:bffc97496048 | 3 | const int8_t encodeTable[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 }; |
sgrsn | 0:bffc97496048 | 4 | |
sgrsn | 0:bffc97496048 | 5 | QEI::QEI(PinName A, PinName B, int ppr) : channelA(A), channelB(B) |
sgrsn | 0:bffc97496048 | 6 | { |
sgrsn | 0:bffc97496048 | 7 | channelA.rise(this, &QEI::encode); |
sgrsn | 0:bffc97496048 | 8 | channelB.rise(this, &QEI::encode); |
sgrsn | 0:bffc97496048 | 9 | channelA.fall(this, &QEI::encode); |
sgrsn | 0:bffc97496048 | 10 | channelB.fall(this, &QEI::encode); |
sgrsn | 0:bffc97496048 | 11 | _ppr = ppr; |
sgrsn | 0:bffc97496048 | 12 | currState = 0; |
sgrsn | 0:bffc97496048 | 13 | prevState = 0; |
sgrsn | 0:bffc97496048 | 14 | position = 0; |
sgrsn | 0:bffc97496048 | 15 | } |
sgrsn | 0:bffc97496048 | 16 | |
sgrsn | 0:bffc97496048 | 17 | float QEI::getAngle() |
sgrsn | 0:bffc97496048 | 18 | { |
sgrsn | 0:bffc97496048 | 19 | return float(position) * 360.0/(_ppr*4.0); |
sgrsn | 0:bffc97496048 | 20 | } |
sgrsn | 0:bffc97496048 | 21 | |
sgrsn | 0:bffc97496048 | 22 | void QEI::encode(void) |
sgrsn | 0:bffc97496048 | 23 | { |
sgrsn | 0:bffc97496048 | 24 | int8_t chanA = channelA.read(); |
sgrsn | 0:bffc97496048 | 25 | int8_t chanB = channelB.read(); |
sgrsn | 0:bffc97496048 | 26 | currState = chanA | (chanB << 1); |
sgrsn | 0:bffc97496048 | 27 | |
sgrsn | 0:bffc97496048 | 28 | if (prevState != currState) { |
sgrsn | 0:bffc97496048 | 29 | position += encodeTable[currState | (prevState<<2)]; |
sgrsn | 0:bffc97496048 | 30 | prevState = currState; |
sgrsn | 0:bffc97496048 | 31 | } |
sgrsn | 0:bffc97496048 | 32 | } |