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
Tach.cpp@0:c165325c9e3f, 2018-03-02 (annotated)
- Committer:
- jdonnal
- Date:
- Fri Mar 02 16:52:58 2018 +0000
- Revision:
- 0:c165325c9e3f
Initial Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jdonnal | 0:c165325c9e3f | 1 | #include "Tach.h" |
jdonnal | 0:c165325c9e3f | 2 | |
jdonnal | 0:c165325c9e3f | 3 | //64 counts per rev |
jdonnal | 0:c165325c9e3f | 4 | |
jdonnal | 0:c165325c9e3f | 5 | Tach::Tach(PinName input, |
jdonnal | 0:c165325c9e3f | 6 | int pulsesPerRev): input_(input){ |
jdonnal | 0:c165325c9e3f | 7 | speed_ = 0.0; |
jdonnal | 0:c165325c9e3f | 8 | pulsesPerRev_ = pulsesPerRev; |
jdonnal | 0:c165325c9e3f | 9 | input_.mode(PullUp); |
jdonnal | 0:c165325c9e3f | 10 | input_.rise(this, &Tach::update); |
jdonnal | 0:c165325c9e3f | 11 | //input_.fall(this, &Tach::update); |
jdonnal | 0:c165325c9e3f | 12 | timer_.start(); |
jdonnal | 0:c165325c9e3f | 13 | for(int i=0;i<NSAMP;i++){ |
jdonnal | 0:c165325c9e3f | 14 | speedBuffer_[i]=0.0; |
jdonnal | 0:c165325c9e3f | 15 | } |
jdonnal | 0:c165325c9e3f | 16 | } |
jdonnal | 0:c165325c9e3f | 17 | int Tach::getCount(void){ |
jdonnal | 0:c165325c9e3f | 18 | return count_; |
jdonnal | 0:c165325c9e3f | 19 | } |
jdonnal | 0:c165325c9e3f | 20 | float Tach::getSpeed(void) { |
jdonnal | 0:c165325c9e3f | 21 | |
jdonnal | 0:c165325c9e3f | 22 | float acc=0; |
jdonnal | 0:c165325c9e3f | 23 | for(int i=0;i<NSAMP;i++){ |
jdonnal | 0:c165325c9e3f | 24 | acc+=speedBuffer_[i]; |
jdonnal | 0:c165325c9e3f | 25 | } |
jdonnal | 0:c165325c9e3f | 26 | return acc/NSAMP; |
jdonnal | 0:c165325c9e3f | 27 | |
jdonnal | 0:c165325c9e3f | 28 | } |
jdonnal | 0:c165325c9e3f | 29 | |
jdonnal | 0:c165325c9e3f | 30 | void Tach::update(void) { |
jdonnal | 0:c165325c9e3f | 31 | float speed; |
jdonnal | 0:c165325c9e3f | 32 | static int i=0; |
jdonnal | 0:c165325c9e3f | 33 | count_++; |
jdonnal | 0:c165325c9e3f | 34 | speed = 1.0/(timer_.read()*pulsesPerRev_); |
jdonnal | 0:c165325c9e3f | 35 | speedBuffer_[i%NSAMP]=speed; |
jdonnal | 0:c165325c9e3f | 36 | i++; |
jdonnal | 0:c165325c9e3f | 37 | timer_.reset(); |
jdonnal | 0:c165325c9e3f | 38 | } |