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

Revision:
7:752b8065ce2d
Parent:
6:2ad5029f7b1a
Child:
8:2e6b17f1da02
--- a/Tarco.cpp	Wed Nov 20 13:29:29 2013 +0000
+++ b/Tarco.cpp	Fri Mar 07 08:01:26 2014 +0000
@@ -7,55 +7,46 @@
     _tarcosensor(tarcosignal)
 {
     tarcomode=mode;
-    p1=0;
-    p2=0;
-    pulsvariation=100;
- //   AdvPeriode=0;
+//   AdvPeriode=0;
     if (mode==0)
-        _tarcosensor.fall(this, &Tarco::click);
+        _tarcosensor.fall(this, &Tarco::RunMeanMeasure);
     if (mode==1)
-        _tarcosensor.rise(this, &Tarco::fsk);
+        _tarcosensor.fall(this, &Tarco::Rotationtimer);
 }
 
-void Tarco::fsk()
+void Tarco::Rotationtimer()
 {
-//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)) {
+//Da switchAuto motoren kommer ud med 24 pulser/rotation laver vi en prescale på 24
+//
+    static int pulscounter;
+    if (pulscounter++>=PulsPrRotation-1) {
         t.stop();
-//        AdvPeriode=t.read_us();
-        debug=!debug;
+        AdvPeriode[0]=t.read_us();
         t.reset();
         t.start();
+        pulscounter=0;
     }
-    else
-    lastpuls=pulstime;
 }
 
 
-void Tarco::click()
+void Tarco::RunMeanMeasure()
 {
-static unsigned int TarcoHead; 
-//measure pulstime
- static bool MistedPeriod; //dette var en lang periode
- int thisperiode;
- float periodechange;
+    static unsigned int TarcoHead;
+//Måler tarco ved at lave en runing mean på periodetiderne, hvis der en variation
+//på over 20% bliver den pågældende periode skippet
+    static bool MistedPeriod; //dette var en lang periode
+    int thisperiode;
+    float periodechange=0;
     t.stop();
     if (TarcoHead>=TarcoRunMean) TarcoHead=0;
     thisperiode=t.read_us();
-    if (lastperiode!=0) 
-      periodechange=(float) thisperiode/lastperiode;
+    if (lastperiode!=0)
+        periodechange=(float) thisperiode/lastperiode;
     if((periodechange<1.2) || (lastperiode==0) || MistedPeriod) {
-      AdvPeriode[TarcoHead++]=thisperiode;
-      MistedPeriod=false;
-    }
-    else
-      MistedPeriod=true;  
+        AdvPeriode[TarcoHead++]=thisperiode;
+        MistedPeriod=false;
+    } else
+        MistedPeriod=true;
     t.reset();
     t.start();
 }
@@ -63,20 +54,27 @@
 float Tarco::Speed()
 {
 //returns puls/sek [hz]
- unsigned int periodetid;
-   periodetid=0;
-   for (int i=0; i<TarcoRunMean; i++) periodetid+=AdvPeriode[i];
-   lastperiode=periodetid/TarcoRunMean;
-   lastfrekvens=(1e6/periodetid)*(TarcoRunMean/25);
-   return (float) lastfrekvens;
+    unsigned int periodetid;
+    lastfrekvens=0;
+    switch (tarcomode) {
+        case 0: {
+            periodetid=0;
+            for (int i=0; i<TarcoRunMean; i++) periodetid+=AdvPeriode[i];
+            lastperiode=periodetid/TarcoRunMean;
+            lastfrekvens=(1e6/periodetid)*(TarcoRunMean/25);
+        } break;
+        case 1: {
+            lastfrekvens=(1e6/AdvPeriode[0]);
+        } break;
+        default: break;
+    }
+    return (float) lastfrekvens;
 }
 
 float Tarco::RPM()
 {
-    if (AdvPeriode) {
-        float rpm=lastfrekvens*60;
-        return (float) rpm;
-    } else return 0;
+    float rpm=lastfrekvens*60;
+    return (float) rpm;
 }