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

Revision:
6:2ad5029f7b1a
Parent:
5:451dd1189b0f
Child:
7:752b8065ce2d
--- a/Tarco.cpp	Tue Nov 19 12:44:26 2013 +0000
+++ b/Tarco.cpp	Wed Nov 20 13:29:29 2013 +0000
@@ -42,9 +42,20 @@
 {
 static unsigned int TarcoHead; 
 //measure pulstime
+ static bool MistedPeriod; //dette var en lang periode
+ int thisperiode;
+ float periodechange;
     t.stop();
     if (TarcoHead>=TarcoRunMean) TarcoHead=0;
-    AdvPeriode[TarcoHead++]=t.read_us();
+    thisperiode=t.read_us();
+    if (lastperiode!=0) 
+      periodechange=(float) thisperiode/lastperiode;
+    if((periodechange<1.2) || (lastperiode==0) || MistedPeriod) {
+      AdvPeriode[TarcoHead++]=thisperiode;
+      MistedPeriod=false;
+    }
+    else
+      MistedPeriod=true;  
     t.reset();
     t.start();
 }
@@ -55,7 +66,8 @@
  unsigned int periodetid;
    periodetid=0;
    for (int i=0; i<TarcoRunMean; i++) periodetid+=AdvPeriode[i];
-   lastfrekvens=(1e6/periodetid)*(TarcoRunMean/24.3);
+   lastperiode=periodetid/TarcoRunMean;
+   lastfrekvens=(1e6/periodetid)*(TarcoRunMean/25);
    return (float) lastfrekvens;
 }