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 #include "FtEncoder.h"
humlet 0:8cb1142a88d5 2
humlet 0:8cb1142a88d5 3 FtEncoder::FtEncoder(PinName pwm, unsigned int standStillTimeout)
humlet 0:8cb1142a88d5 4 :m_encIRQ(pwm), m_cnt(0), m_standStillTimeout(standStillTimeout), m_cursor(0), m_ready(false)
humlet 0:8cb1142a88d5 5 {
humlet 0:8cb1142a88d5 6 unsigned int tmStmp = getCurrentTimeStamp() - c_nTmStmps * m_standStillTimeout;
humlet 0:8cb1142a88d5 7 for(int i=0; i<c_nTmStmps-1; ++i)
humlet 0:8cb1142a88d5 8 m_timeStamps[i] = tmStmp + i*m_standStillTimeout;
humlet 0:8cb1142a88d5 9 m_cursor = c_nTmStmps-1;
humlet 0:8cb1142a88d5 10
humlet 0:8cb1142a88d5 11 m_encIRQ.rise(this, &FtEncoder::encoderISR);
humlet 0:8cb1142a88d5 12 m_encIRQ.fall(this, &FtEncoder::encoderISR);
humlet 0:8cb1142a88d5 13 m_tmOut.attach_us(this, &FtEncoder::encoderISR, m_standStillTimeout);
humlet 0:8cb1142a88d5 14 }
humlet 0:8cb1142a88d5 15
humlet 0:8cb1142a88d5 16 unsigned int FtEncoder::getLastPeriod() const
humlet 0:8cb1142a88d5 17 {
humlet 0:8cb1142a88d5 18 unsigned int period = 0;
humlet 0:8cb1142a88d5 19 unsigned int cursor, cnt;
humlet 0:8cb1142a88d5 20 do {
humlet 0:8cb1142a88d5 21 cursor = m_cursor;
humlet 0:8cb1142a88d5 22 cnt = m_cnt;
humlet 0:8cb1142a88d5 23 period = m_timeStamps[m_cursor] - m_timeStamps[(m_cursor-2)&(c_nTmStmps-1)];
humlet 0:8cb1142a88d5 24 } while(cursor!=m_cursor || cnt!=m_cnt); // stay in loop until we have an non intrrupted reading
humlet 0:8cb1142a88d5 25 return period;
humlet 0:8cb1142a88d5 26 }
humlet 0:8cb1142a88d5 27
humlet 0:8cb1142a88d5 28 float FtEncoder::getSpeed() const
humlet 0:8cb1142a88d5 29 {
humlet 0:8cb1142a88d5 30 unsigned int period = getLastPeriod();
humlet 0:8cb1142a88d5 31 return (period!=0 && period<m_standStillTimeout) ?
humlet 0:8cb1142a88d5 32 c_speedFactor/period : 0.0;
humlet 0:8cb1142a88d5 33 }
humlet 0:8cb1142a88d5 34
humlet 0:8cb1142a88d5 35 void FtEncoder::encoderISR()
humlet 0:8cb1142a88d5 36 {
humlet 0:8cb1142a88d5 37 ++m_cursor;
humlet 0:8cb1142a88d5 38 m_cursor &= c_nTmStmps-1;
humlet 0:8cb1142a88d5 39 m_timeStamps[m_cursor] = getCurrentTimeStamp();
humlet 0:8cb1142a88d5 40 m_tmOut.detach();
humlet 0:8cb1142a88d5 41 m_tmOut.attach_us(this, &FtEncoder::encoderISR, m_standStillTimeout);
humlet 0:8cb1142a88d5 42 m_callBack.call();
humlet 0:8cb1142a88d5 43 ++m_cnt;
humlet 0:8cb1142a88d5 44 }