Interface for the encoder of the red fischertechnik motors

Dependencies:   NeedfulThings

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.

Committer:
humlet
Date:
Thu Mar 14 18:27:24 2013 +0000
Revision:
0:8cb1142a88d5
Child:
1:9e595056c3da
initial;

Who changed what in which revision?

UserRevisionLine numberNew 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 0:8cb1142a88d5 7 #include "Timeout.h"
humlet 0:8cb1142a88d5 8
humlet 0:8cb1142a88d5 9 using namespace mbed;
humlet 0:8cb1142a88d5 10
humlet 0:8cb1142a88d5 11 /// Simple Interface for the encoder of the red fischertechnik motors
humlet 0:8cb1142a88d5 12 /// @note Connect the green wire to GND, the red one to +3.3V and the
humlet 0:8cb1142a88d5 13 /// black signal line to any of mbed numbered pins. Additionally connect the signal line
humlet 0:8cb1142a88d5 14 /// via a pullup resitor to +3.3V. A 10K resistor works fine.
humlet 0:8cb1142a88d5 15 ///
humlet 0:8cb1142a88d5 16 class FtEncoder
humlet 0:8cb1142a88d5 17 {
humlet 0:8cb1142a88d5 18 static const float c_speedFactor = 1e6/75.0; // 1/(µs * 150/2) edges per revolution)
humlet 0:8cb1142a88d5 19 static const int c_nTmStmps = 1<<2;
humlet 0:8cb1142a88d5 20
humlet 0:8cb1142a88d5 21 InterruptIn m_encIRQ;
humlet 0:8cb1142a88d5 22 Timeout m_tmOut;
humlet 0:8cb1142a88d5 23
humlet 0:8cb1142a88d5 24 volatile unsigned int m_cnt;
humlet 0:8cb1142a88d5 25 unsigned int m_standStillTimeout;
humlet 0:8cb1142a88d5 26 volatile unsigned int m_cursor;
humlet 0:8cb1142a88d5 27 unsigned int m_timeStamps[c_nTmStmps];
humlet 0:8cb1142a88d5 28 bool m_ready;
humlet 0:8cb1142a88d5 29
humlet 0:8cb1142a88d5 30 FunctionPointer m_callBack;
humlet 0:8cb1142a88d5 31
humlet 0:8cb1142a88d5 32 /// ISR called on rising and falling encoder edges
humlet 0:8cb1142a88d5 33 void encoderISR();
humlet 0:8cb1142a88d5 34
humlet 0:8cb1142a88d5 35 public:
humlet 0:8cb1142a88d5 36
humlet 0:8cb1142a88d5 37 /// create a simple interface to the encoder connected to the given pin
humlet 0:8cb1142a88d5 38 /// @param name of the pin the black encoder signl wire is connected to
humlet 0:8cb1142a88d5 39 /// @param standStillTimeout After this time [µs] without any encoder edge getSpeed() returns zero
humlet 0:8cb1142a88d5 40 FtEncoder(PinName pwm, unsigned int standStillTimeout=50000);
humlet 0:8cb1142a88d5 41
humlet 0:8cb1142a88d5 42 /// get number of falling and rising encoder edges
humlet 0:8cb1142a88d5 43 inline unsigned int getCounter() const {
humlet 0:8cb1142a88d5 44 return m_cnt;
humlet 0:8cb1142a88d5 45 };
humlet 0:8cb1142a88d5 46
humlet 0:8cb1142a88d5 47 /// reset the encoder edge counter to the given value (default is 0)
humlet 0:8cb1142a88d5 48 inline void resetCounter(unsigned int cnt=0) {
humlet 0:8cb1142a88d5 49 m_cnt=cnt;
humlet 0:8cb1142a88d5 50 };
humlet 0:8cb1142a88d5 51
humlet 0:8cb1142a88d5 52 /// get period [µs] between the two latest edges of same direction (rising or falling)
humlet 0:8cb1142a88d5 53 /// @note Returns standStillTimeout if motor stands still or no edges have been detected yet
humlet 0:8cb1142a88d5 54 unsigned int getLastPeriod() const;
humlet 0:8cb1142a88d5 55
humlet 0:8cb1142a88d5 56 /// get time stamp [µs] of last update i.e. encoder edge or timeout
humlet 0:8cb1142a88d5 57 unsigned int getTimeStampOfLastUpdate() const {
humlet 0:8cb1142a88d5 58 return m_timeStamps[m_cursor];
humlet 0:8cb1142a88d5 59 };
humlet 0:8cb1142a88d5 60
humlet 0:8cb1142a88d5 61 /// returns current time stamp [µs]
humlet 0:8cb1142a88d5 62 /// @note simply calls us_ticker_read() from mbed's C-API (capi/us_ticker_api.h)
humlet 0:8cb1142a88d5 63 inline unsigned int getCurrentTimeStamp() const {
humlet 0:8cb1142a88d5 64 return us_ticker_read();
humlet 0:8cb1142a88d5 65 }
humlet 0:8cb1142a88d5 66
humlet 0:8cb1142a88d5 67 /// get speed in revolutions per second (Hz)
humlet 0:8cb1142a88d5 68 float getSpeed() const;
humlet 0:8cb1142a88d5 69
humlet 0:8cb1142a88d5 70 /// hook in a call back to the encoder ISR e.g. for sending a RTOS signal
humlet 0:8cb1142a88d5 71 void setCallBack(FunctionPointer callBack) {
humlet 0:8cb1142a88d5 72 m_callBack=callBack;
humlet 0:8cb1142a88d5 73 };
humlet 0:8cb1142a88d5 74 };
humlet 0:8cb1142a88d5 75
humlet 0:8cb1142a88d5 76 #endif