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.

Revision:
0:8cb1142a88d5
Child:
1:9e595056c3da
diff -r 000000000000 -r 8cb1142a88d5 FtEncoder.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FtEncoder.cpp	Thu Mar 14 18:27:24 2013 +0000
@@ -0,0 +1,44 @@
+#include "FtEncoder.h"
+
+FtEncoder::FtEncoder(PinName pwm, unsigned int standStillTimeout)
+    :m_encIRQ(pwm), m_cnt(0), m_standStillTimeout(standStillTimeout), m_cursor(0), m_ready(false)
+{
+    unsigned int tmStmp = getCurrentTimeStamp() - c_nTmStmps * m_standStillTimeout;
+    for(int i=0; i<c_nTmStmps-1; ++i)
+        m_timeStamps[i] = tmStmp + i*m_standStillTimeout;
+    m_cursor = c_nTmStmps-1;
+
+    m_encIRQ.rise(this, &FtEncoder::encoderISR);
+    m_encIRQ.fall(this, &FtEncoder::encoderISR);
+    m_tmOut.attach_us(this, &FtEncoder::encoderISR, m_standStillTimeout);
+}
+
+unsigned int FtEncoder::getLastPeriod() const
+{
+    unsigned int period = 0;
+    unsigned int cursor, cnt;
+    do {
+        cursor = m_cursor;
+        cnt = m_cnt;
+        period = m_timeStamps[m_cursor] - m_timeStamps[(m_cursor-2)&(c_nTmStmps-1)];
+    } while(cursor!=m_cursor || cnt!=m_cnt); // stay in loop until we have an non intrrupted reading
+    return period;
+}
+
+float FtEncoder::getSpeed() const
+{
+    unsigned int period = getLastPeriod();
+    return (period!=0 && period<m_standStillTimeout) ?
+           c_speedFactor/period : 0.0;
+}
+
+void FtEncoder::encoderISR()
+{
+    ++m_cursor;
+    m_cursor &= c_nTmStmps-1;
+    m_timeStamps[m_cursor] = getCurrentTimeStamp();
+    m_tmOut.detach();
+    m_tmOut.attach_us(this, &FtEncoder::encoderISR, m_standStillTimeout);
+    m_callBack.call();
+    ++m_cnt;
+}
\ No newline at end of file