Interface with a single channel encoder (eg IR emitter/detector on a slotted wheel). Requires a digital input and counts per revolution. Provides speed in Hz
Dependents: ES202FinalProject ES202_straight1 ES202_Final_Turn ES202_Turn ... more
Diff: Tach.cpp
- Revision:
- 0:c165325c9e3f
diff -r 000000000000 -r c165325c9e3f Tach.cpp --- /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