Tachometer library

Dependents:   ES202_FinalProject_workingExample DREAMTEAM

Fork of Tach by John Donnal

Tach.cpp

Committer:
jdonnal
Date:
2018-03-02
Revision:
0:c165325c9e3f
Child:
1:b17c9f1d9d5c

File content as of revision 0:c165325c9e3f:

 #include "Tach.h"
 
 //64 counts per rev
 
 Tach::Tach(PinName input, 
            int pulsesPerRev): input_(input){
    speed_       = 0.0;
    pulsesPerRev_ = pulsesPerRev;
    input_.mode(PullUp);
    input_.rise(this, &Tach::update);
    //input_.fall(this, &Tach::update);
    timer_.start();
    for(int i=0;i<NSAMP;i++){
        speedBuffer_[i]=0.0;
    }
}
int Tach::getCount(void){
    return count_;
}
float Tach::getSpeed(void) {

    float acc=0;
    for(int i=0;i<NSAMP;i++){
        acc+=speedBuffer_[i];
    }
    return acc/NSAMP;

}

void Tach::update(void) {
    float speed;
    static int i=0;
    count_++;
    speed =  1.0/(timer_.read()*pulsesPerRev_);
    speedBuffer_[i%NSAMP]=speed;
    i++;
    timer_.reset();
}