Interface to a standard tarco sensor. Measure the periode and the cal the speed
Diff: Tarco.cpp
- Revision:
- 4:35024179a736
- Parent:
- 3:6d3f2034e5f1
- Child:
- 5:451dd1189b0f
--- a/Tarco.cpp Thu Nov 07 12:02:47 2013 +0000 +++ b/Tarco.cpp Tue Nov 19 09:19:01 2013 +0000 @@ -1,26 +1,71 @@ #include "Tarco.h" +#include "mbed.h" + +DigitalOut debug(LED2); -Tarco::Tarco(PinName tarcosignal) : - _tarcosensor(tarcosignal) { - t.start(); - AdvPeriode=99999; +Tarco::Tarco(PinName tarcosignal, char mode) : + _tarcosensor(tarcosignal) +{ + tarcomode=mode; + p1=0; + p2=0; + pulsvariation=100; + AdvPeriode=0; + if (mode==0) _tarcosensor.fall(this, &Tarco::click); + if (mode==1) + _tarcosensor.rise(this, &Tarco::fsk); } -void Tarco::click() { -//måler pulstiden - t.stop(); - AdvPeriode=t.read_us(); - t.reset(); t.start(); +void Tarco::fsk() +{ +//detect frekvensshift and start/Stop time to measure terco time + int pulstime; + fsktime.stop(); + pulstime=fsktime.read_us(); + fsktime.reset(); + fsktime.start(); + + if (pulstime>(lastpuls+pulsvariation)) { + t.stop(); + AdvPeriode=t.read_us(); + debug=!debug; + t.reset(); + t.start(); + } + else + lastpuls=pulstime; } -float Tarco::Speed(){ -//Giver puls/sek [hz] - float frek=1e6/AdvPeriode; - return (float) frek; + +void Tarco::click() +{ +/* +//measure pulstime + t.stop(); + AdvPeriode=t.read_us(); + t.reset(); + t.start();*/ + count++; } -float Tarco::RPM() { -float rpm=60e6/AdvPeriode; -return (float) rpm; +float Tarco::Speed() +{ +//returns puls/sek [hz] + t.stop(); + AdvPeriode=t.read_us()/count; + t.reset(); count=0; + t.start(); + float frek=(1e6/AdvPeriode)/24.3; + return (float) frek; } + +float Tarco::RPM() +{ + if (AdvPeriode) { + float rpm=(60e6/AdvPeriode)/24.3; + return (float) rpm; + } else return 0; +} + +