My modifications/additions to the code
Dependencies: ADXL345 ADXL345_I2C IMUfilter ITG3200 Servo fishgait mbed-rtos mbed pixy_cam
Fork of robotic_fish_ver_4_8 by
Diff: PwmReader.cpp
- Revision:
- 5:090ef6275773
- Child:
- 6:a4d6f3e4bf28
diff -r f081a97ca000 -r 090ef6275773 PwmReader.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PwmReader.cpp Tue Jan 28 23:59:21 2014 +0000 @@ -0,0 +1,44 @@ +#include "PwmReader.h" + +//Constructors +PwmReader::PwmReader(PinName pwmInPort) +{ + di = new InterruptIn(pwmInPort); + + lastRise = 0; + period = 0; + duty = 0.0; + di->rise(this,&PwmReader::pwmRise); // attach the address of the flip function to the rising edge + di->fall(this,&PwmReader::pwmFall); +} + +PwmReader::~PwmReader() +{ + delete di; +} + +// public methods +float PwmReader::getDuty() +{ + return duty; +} + +// private methods +void PwmReader::pwmRise() +{ + int rise = t.read_us(); + if( (lastRise > 0) && (rise > lastRise) ) { + period = rise - lastRise; + } + lastRise = rise; + +} + +void PwmReader::pwmFall() +{ + int fallTime = t.read_us(); + if(period > 0 ) { + int delta = fallTime - lastRise; + duty = float(delta)/float(period); + } +} \ No newline at end of file