Tachometer library
Dependents: ES202_FinalProject_workingExample DREAMTEAM
Fork of Tach by
Diff: Tach.h
- Revision:
- 0:c165325c9e3f
- Child:
- 1:b17c9f1d9d5c
diff -r 000000000000 -r c165325c9e3f Tach.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Tach.h Fri Mar 02 16:52:58 2018 +0000 @@ -0,0 +1,75 @@ + +/* Single Channel Encoder as Speedometer + John Donnal 2018 +*/ + +#ifndef TACH_H +#define TACH_H + +/** + * Includes + */ +#include "mbed.h" + +/** + * Defines + */ +#define NSAMP 3 + +/** + * Tach Interface. + */ +class Tach { + +public: + + /** + * Constructor. + * + * Tracks rising and falling edges with interrupts, provides + * rotations per second (Hz) output + * + * @param input mbed pin for input channel. + * @param pulsesPerRev Number of pulses in one revolution. + * @param encoding The encoding to use. Uses X2 encoding by default. X2 + */ + Tach(PinName input, int pulsesPerRev); + + /** + * Read the state of the encoder. + * + * @return The current state of the encoder as a 2-bit number, where: + * bit 1 = The reading from channel B + * bit 2 = The reading from channel A + */ + float getSpeed(void); + int getCount(void); + +private: + + /** + * Update the pulse count. + * + * Called on every rising/falling edge of input + * + * update current speed estimate + */ + void update(void); + + /** + * Called on every rising edge of channel index to update revolution + * count by one. + */ + void index(void); + + InterruptIn input_; + Timer timer_; + + int pulsesPerRev_; + float speed_; + int count_; + int dt_; + float speedBuffer_[NSAMP]; +}; + +#endif /* TACH_H */