Tachometer library
Dependents: ES202_FinalProject_workingExample DREAMTEAM
Fork of Tach by
Diff: Tach.cpp
- Revision:
- 0:c165325c9e3f
- Child:
- 1:b17c9f1d9d5c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Tach.cpp Fri Mar 02 16:52:58 2018 +0000 @@ -0,0 +1,38 @@ + #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(); +} \ No newline at end of file