Interface for the encoder of the red fischertechnik motors
Simple Interface for the simple encoder of the red fischertechnik motors.
The interface counts both, rising and falling edges, resulting in 150 counts per revolution. The interface also provides a speed measurement updated with each edge.
Connect the green wire to GND, the red one to +3.3V and the black signal line to any of mbed numbered pins. Additionally connect the signal line via a pullup resitor to +3.3V. A 10K resistor works fine.
FtEncoder.cpp@1:9e595056c3da, 2013-03-20 (annotated)
- Committer:
- humlet
- Date:
- Wed Mar 20 21:42:39 2013 +0000
- Revision:
- 1:9e595056c3da
- Parent:
- 0:8cb1142a88d5
- Child:
- 2:1bf9f7b6bf29
works as expected ... hopefully
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
humlet | 0:8cb1142a88d5 | 1 | #include "FtEncoder.h" |
humlet | 0:8cb1142a88d5 | 2 | |
humlet | 0:8cb1142a88d5 | 3 | FtEncoder::FtEncoder(PinName pwm, unsigned int standStillTimeout) |
humlet | 1:9e595056c3da | 4 | :m_encIRQ(pwm), |
humlet | 1:9e595056c3da | 5 | m_cnt(0), |
humlet | 1:9e595056c3da | 6 | m_standStillTimeout(standStillTimeout), |
humlet | 1:9e595056c3da | 7 | m_cursor(0) |
humlet | 0:8cb1142a88d5 | 8 | { |
humlet | 1:9e595056c3da | 9 | // initialize the edge timestamp ringbuffer |
humlet | 0:8cb1142a88d5 | 10 | unsigned int tmStmp = getCurrentTimeStamp() - c_nTmStmps * m_standStillTimeout; |
humlet | 1:9e595056c3da | 11 | for(int i=0; i<c_nTmStmps; ++i) |
humlet | 0:8cb1142a88d5 | 12 | m_timeStamps[i] = tmStmp + i*m_standStillTimeout; |
humlet | 0:8cb1142a88d5 | 13 | m_cursor = c_nTmStmps-1; |
humlet | 0:8cb1142a88d5 | 14 | |
humlet | 1:9e595056c3da | 15 | m_encIRQ.mode(PullUp); // set it to pullup, but an external pullup is needed, the internal isn't sufficient |
humlet | 0:8cb1142a88d5 | 16 | m_encIRQ.rise(this, &FtEncoder::encoderISR); |
humlet | 0:8cb1142a88d5 | 17 | m_encIRQ.fall(this, &FtEncoder::encoderISR); |
humlet | 1:9e595056c3da | 18 | m_tmOut.attach_us(this, &FtEncoder::timeoutISR, m_standStillTimeout); |
humlet | 0:8cb1142a88d5 | 19 | } |
humlet | 0:8cb1142a88d5 | 20 | |
humlet | 0:8cb1142a88d5 | 21 | unsigned int FtEncoder::getLastPeriod() const |
humlet | 0:8cb1142a88d5 | 22 | { |
humlet | 0:8cb1142a88d5 | 23 | unsigned int period = 0; |
humlet | 0:8cb1142a88d5 | 24 | unsigned int cursor, cnt; |
humlet | 0:8cb1142a88d5 | 25 | do { |
humlet | 0:8cb1142a88d5 | 26 | cursor = m_cursor; |
humlet | 0:8cb1142a88d5 | 27 | cnt = m_cnt; |
humlet | 0:8cb1142a88d5 | 28 | period = m_timeStamps[m_cursor] - m_timeStamps[(m_cursor-2)&(c_nTmStmps-1)]; |
humlet | 1:9e595056c3da | 29 | } while(cursor!=m_cursor || cnt!=m_cnt); // stay in loop until we have a non intrrupted reading |
humlet | 0:8cb1142a88d5 | 30 | return period; |
humlet | 0:8cb1142a88d5 | 31 | } |
humlet | 0:8cb1142a88d5 | 32 | |
humlet | 0:8cb1142a88d5 | 33 | float FtEncoder::getSpeed() const |
humlet | 0:8cb1142a88d5 | 34 | { |
humlet | 0:8cb1142a88d5 | 35 | unsigned int period = getLastPeriod(); |
humlet | 0:8cb1142a88d5 | 36 | return (period!=0 && period<m_standStillTimeout) ? |
humlet | 0:8cb1142a88d5 | 37 | c_speedFactor/period : 0.0; |
humlet | 0:8cb1142a88d5 | 38 | } |
humlet | 0:8cb1142a88d5 | 39 | |
humlet | 0:8cb1142a88d5 | 40 | void FtEncoder::encoderISR() |
humlet | 0:8cb1142a88d5 | 41 | { |
humlet | 0:8cb1142a88d5 | 42 | ++m_cursor; |
humlet | 0:8cb1142a88d5 | 43 | m_cursor &= c_nTmStmps-1; |
humlet | 0:8cb1142a88d5 | 44 | m_timeStamps[m_cursor] = getCurrentTimeStamp(); |
humlet | 1:9e595056c3da | 45 | // restart the timeout |
humlet | 0:8cb1142a88d5 | 46 | m_tmOut.detach(); |
humlet | 1:9e595056c3da | 47 | m_tmOut.attach_us(this, &FtEncoder::timeoutISR, m_standStillTimeout); |
humlet | 1:9e595056c3da | 48 | ++m_cnt; |
humlet | 0:8cb1142a88d5 | 49 | m_callBack.call(); |
humlet | 1:9e595056c3da | 50 | } |
humlet | 1:9e595056c3da | 51 | |
humlet | 1:9e595056c3da | 52 | void FtEncoder::timeoutISR(){ |
humlet | 1:9e595056c3da | 53 | --m_cnt; |
humlet | 1:9e595056c3da | 54 | encoderISR(); |
humlet | 0:8cb1142a88d5 | 55 | } |