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.h@3:3944952c9eab, 2013-03-25 (annotated)
- Committer:
- humlet
- Date:
- Mon Mar 25 21:47:38 2013 +0000
- Revision:
- 3:3944952c9eab
- Parent:
- 2:1bf9f7b6bf29
Added NeedfulThings lib dependency (hopefully)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
humlet | 0:8cb1142a88d5 | 1 | #ifndef FTENCODER_H |
humlet | 0:8cb1142a88d5 | 2 | #define FTENCODER_H |
humlet | 0:8cb1142a88d5 | 3 | |
humlet | 0:8cb1142a88d5 | 4 | #include "InterruptIn.h" |
humlet | 0:8cb1142a88d5 | 5 | #include "us_ticker_api.h" |
humlet | 0:8cb1142a88d5 | 6 | #include "FunctionPointer.h" |
humlet | 2:1bf9f7b6bf29 | 7 | #include "TimeoutTweaked.h" |
humlet | 0:8cb1142a88d5 | 8 | |
humlet | 0:8cb1142a88d5 | 9 | using namespace mbed; |
humlet | 0:8cb1142a88d5 | 10 | |
humlet | 1:9e595056c3da | 11 | /// Simple Interface for the simple encoder of the red fischertechnik motors. |
humlet | 1:9e595056c3da | 12 | /// The interface counts both, rising and falling edges, resulting in 150 counts per revolution. |
humlet | 1:9e595056c3da | 13 | /// The interface also provides a speed measurement updated with each edge. |
humlet | 0:8cb1142a88d5 | 14 | /// @note Connect the green wire to GND, the red one to +3.3V and the |
humlet | 0:8cb1142a88d5 | 15 | /// black signal line to any of mbed numbered pins. Additionally connect the signal line |
humlet | 0:8cb1142a88d5 | 16 | /// via a pullup resitor to +3.3V. A 10K resistor works fine. |
humlet | 0:8cb1142a88d5 | 17 | /// |
humlet | 0:8cb1142a88d5 | 18 | class FtEncoder |
humlet | 0:8cb1142a88d5 | 19 | { |
humlet | 1:9e595056c3da | 20 | static const float c_speedFactor = 1e6/75.0; /// 1/(µs * 150/2) edges per revolution) |
humlet | 1:9e595056c3da | 21 | static const int c_nTmStmps = 1<<2; /// size of ringbuffer: Has to be 2^x |
humlet | 0:8cb1142a88d5 | 22 | |
humlet | 0:8cb1142a88d5 | 23 | InterruptIn m_encIRQ; |
humlet | 2:1bf9f7b6bf29 | 24 | TimeoutTweaked m_tmOut; |
humlet | 0:8cb1142a88d5 | 25 | |
humlet | 1:9e595056c3da | 26 | volatile unsigned int m_cnt; /// edge counter |
humlet | 1:9e595056c3da | 27 | unsigned int m_standStillTimeout; |
humlet | 1:9e595056c3da | 28 | volatile unsigned int m_cursor; /// ringbuffer cursor |
humlet | 2:1bf9f7b6bf29 | 29 | volatile uint32_t m_timeStamps[c_nTmStmps]; /// ringbuffer of edge timestamps |
humlet | 0:8cb1142a88d5 | 30 | |
humlet | 0:8cb1142a88d5 | 31 | FunctionPointer m_callBack; |
humlet | 0:8cb1142a88d5 | 32 | |
humlet | 0:8cb1142a88d5 | 33 | /// ISR called on rising and falling encoder edges |
humlet | 0:8cb1142a88d5 | 34 | void encoderISR(); |
humlet | 1:9e595056c3da | 35 | /// ISR called on edge timeout i.e. standstill |
humlet | 1:9e595056c3da | 36 | void timeoutISR(); |
humlet | 0:8cb1142a88d5 | 37 | |
humlet | 0:8cb1142a88d5 | 38 | public: |
humlet | 0:8cb1142a88d5 | 39 | |
humlet | 0:8cb1142a88d5 | 40 | /// create a simple interface to the encoder connected to the given pin |
humlet | 1:9e595056c3da | 41 | /// @param name of the pin the black encoder signal wire is connected to |
humlet | 0:8cb1142a88d5 | 42 | /// @param standStillTimeout After this time [µs] without any encoder edge getSpeed() returns zero |
humlet | 0:8cb1142a88d5 | 43 | FtEncoder(PinName pwm, unsigned int standStillTimeout=50000); |
humlet | 0:8cb1142a88d5 | 44 | |
humlet | 0:8cb1142a88d5 | 45 | /// get number of falling and rising encoder edges |
humlet | 0:8cb1142a88d5 | 46 | inline unsigned int getCounter() const { |
humlet | 0:8cb1142a88d5 | 47 | return m_cnt; |
humlet | 0:8cb1142a88d5 | 48 | }; |
humlet | 0:8cb1142a88d5 | 49 | |
humlet | 0:8cb1142a88d5 | 50 | /// reset the encoder edge counter to the given value (default is 0) |
humlet | 0:8cb1142a88d5 | 51 | inline void resetCounter(unsigned int cnt=0) { |
humlet | 0:8cb1142a88d5 | 52 | m_cnt=cnt; |
humlet | 0:8cb1142a88d5 | 53 | }; |
humlet | 0:8cb1142a88d5 | 54 | |
humlet | 0:8cb1142a88d5 | 55 | /// get period [µs] between the two latest edges of same direction (rising or falling) |
humlet | 0:8cb1142a88d5 | 56 | /// @note Returns standStillTimeout if motor stands still or no edges have been detected yet |
humlet | 0:8cb1142a88d5 | 57 | unsigned int getLastPeriod() const; |
humlet | 0:8cb1142a88d5 | 58 | |
humlet | 0:8cb1142a88d5 | 59 | /// get time stamp [µs] of last update i.e. encoder edge or timeout |
humlet | 0:8cb1142a88d5 | 60 | unsigned int getTimeStampOfLastUpdate() const { |
humlet | 0:8cb1142a88d5 | 61 | return m_timeStamps[m_cursor]; |
humlet | 0:8cb1142a88d5 | 62 | }; |
humlet | 0:8cb1142a88d5 | 63 | |
humlet | 0:8cb1142a88d5 | 64 | /// returns current time stamp [µs] |
humlet | 0:8cb1142a88d5 | 65 | /// @note simply calls us_ticker_read() from mbed's C-API (capi/us_ticker_api.h) |
humlet | 0:8cb1142a88d5 | 66 | inline unsigned int getCurrentTimeStamp() const { |
humlet | 0:8cb1142a88d5 | 67 | return us_ticker_read(); |
humlet | 0:8cb1142a88d5 | 68 | } |
humlet | 0:8cb1142a88d5 | 69 | |
humlet | 0:8cb1142a88d5 | 70 | /// get speed in revolutions per second (Hz) |
humlet | 0:8cb1142a88d5 | 71 | float getSpeed() const; |
humlet | 0:8cb1142a88d5 | 72 | |
humlet | 0:8cb1142a88d5 | 73 | /// hook in a call back to the encoder ISR e.g. for sending a RTOS signal |
humlet | 0:8cb1142a88d5 | 74 | void setCallBack(FunctionPointer callBack) { |
humlet | 0:8cb1142a88d5 | 75 | m_callBack=callBack; |
humlet | 0:8cb1142a88d5 | 76 | }; |
humlet | 0:8cb1142a88d5 | 77 | }; |
humlet | 0:8cb1142a88d5 | 78 | |
humlet | 0:8cb1142a88d5 | 79 | #endif |