Interface to a standard tarco sensor. Measure the periode and the cal the speed

Committer:
gert_lauritsen
Date:
Tue Nov 19 12:44:26 2013 +0000
Revision:
5:451dd1189b0f
Parent:
4:35024179a736
Child:
6:2ad5029f7b1a
?ndret midling af periode

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gert_lauritsen 2:c50da089786a 1 #include "Tarco.h"
gert_lauritsen 4:35024179a736 2 #include "mbed.h"
gert_lauritsen 4:35024179a736 3
gert_lauritsen 4:35024179a736 4 DigitalOut debug(LED2);
gert_lauritsen 0:8421e11df32a 5
gert_lauritsen 4:35024179a736 6 Tarco::Tarco(PinName tarcosignal, char mode) :
gert_lauritsen 4:35024179a736 7 _tarcosensor(tarcosignal)
gert_lauritsen 4:35024179a736 8 {
gert_lauritsen 4:35024179a736 9 tarcomode=mode;
gert_lauritsen 4:35024179a736 10 p1=0;
gert_lauritsen 4:35024179a736 11 p2=0;
gert_lauritsen 4:35024179a736 12 pulsvariation=100;
gert_lauritsen 5:451dd1189b0f 13 // AdvPeriode=0;
gert_lauritsen 4:35024179a736 14 if (mode==0)
gert_lauritsen 2:c50da089786a 15 _tarcosensor.fall(this, &Tarco::click);
gert_lauritsen 4:35024179a736 16 if (mode==1)
gert_lauritsen 4:35024179a736 17 _tarcosensor.rise(this, &Tarco::fsk);
gert_lauritsen 0:8421e11df32a 18 }
gert_lauritsen 0:8421e11df32a 19
gert_lauritsen 4:35024179a736 20 void Tarco::fsk()
gert_lauritsen 4:35024179a736 21 {
gert_lauritsen 4:35024179a736 22 //detect frekvensshift and start/Stop time to measure terco time
gert_lauritsen 4:35024179a736 23 int pulstime;
gert_lauritsen 4:35024179a736 24 fsktime.stop();
gert_lauritsen 4:35024179a736 25 pulstime=fsktime.read_us();
gert_lauritsen 4:35024179a736 26 fsktime.reset();
gert_lauritsen 4:35024179a736 27 fsktime.start();
gert_lauritsen 4:35024179a736 28
gert_lauritsen 4:35024179a736 29 if (pulstime>(lastpuls+pulsvariation)) {
gert_lauritsen 4:35024179a736 30 t.stop();
gert_lauritsen 5:451dd1189b0f 31 // AdvPeriode=t.read_us();
gert_lauritsen 4:35024179a736 32 debug=!debug;
gert_lauritsen 4:35024179a736 33 t.reset();
gert_lauritsen 4:35024179a736 34 t.start();
gert_lauritsen 4:35024179a736 35 }
gert_lauritsen 4:35024179a736 36 else
gert_lauritsen 4:35024179a736 37 lastpuls=pulstime;
gert_lauritsen 0:8421e11df32a 38 }
gert_lauritsen 0:8421e11df32a 39
gert_lauritsen 4:35024179a736 40
gert_lauritsen 4:35024179a736 41 void Tarco::click()
gert_lauritsen 4:35024179a736 42 {
gert_lauritsen 5:451dd1189b0f 43 static unsigned int TarcoHead;
gert_lauritsen 4:35024179a736 44 //measure pulstime
gert_lauritsen 4:35024179a736 45 t.stop();
gert_lauritsen 5:451dd1189b0f 46 if (TarcoHead>=TarcoRunMean) TarcoHead=0;
gert_lauritsen 5:451dd1189b0f 47 AdvPeriode[TarcoHead++]=t.read_us();
gert_lauritsen 4:35024179a736 48 t.reset();
gert_lauritsen 5:451dd1189b0f 49 t.start();
gert_lauritsen 0:8421e11df32a 50 }
gert_lauritsen 0:8421e11df32a 51
gert_lauritsen 4:35024179a736 52 float Tarco::Speed()
gert_lauritsen 4:35024179a736 53 {
gert_lauritsen 4:35024179a736 54 //returns puls/sek [hz]
gert_lauritsen 5:451dd1189b0f 55 unsigned int periodetid;
gert_lauritsen 5:451dd1189b0f 56 periodetid=0;
gert_lauritsen 5:451dd1189b0f 57 for (int i=0; i<TarcoRunMean; i++) periodetid+=AdvPeriode[i];
gert_lauritsen 5:451dd1189b0f 58 lastfrekvens=(1e6/periodetid)*(TarcoRunMean/24.3);
gert_lauritsen 5:451dd1189b0f 59 return (float) lastfrekvens;
gert_lauritsen 2:c50da089786a 60 }
gert_lauritsen 4:35024179a736 61
gert_lauritsen 4:35024179a736 62 float Tarco::RPM()
gert_lauritsen 4:35024179a736 63 {
gert_lauritsen 4:35024179a736 64 if (AdvPeriode) {
gert_lauritsen 5:451dd1189b0f 65 float rpm=lastfrekvens*60;
gert_lauritsen 4:35024179a736 66 return (float) rpm;
gert_lauritsen 4:35024179a736 67 } else return 0;
gert_lauritsen 4:35024179a736 68 }
gert_lauritsen 4:35024179a736 69
gert_lauritsen 4:35024179a736 70