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

Tarco.cpp

Committer:
gert_lauritsen
Date:
2013-11-20
Revision:
6:2ad5029f7b1a
Parent:
5:451dd1189b0f
Child:
7:752b8065ce2d

File content as of revision 6:2ad5029f7b1a:

#include "Tarco.h"
#include "mbed.h"

DigitalOut debug(LED2);

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::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;
}


void Tarco::click()
{
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;
    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();
}

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;
}

float Tarco::RPM()
{
    if (AdvPeriode) {
        float rpm=lastfrekvens*60;
        return (float) rpm;
    } else return 0;
}