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
PwmReader.cpp@5:090ef6275773, 2014-01-28 (annotated)
- Committer:
- sandwich
- Date:
- Tue Jan 28 23:59:21 2014 +0000
- Revision:
- 5:090ef6275773
- Child:
- 6:a4d6f3e4bf28
added interrupts
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sandwich | 5:090ef6275773 | 1 | #include "PwmReader.h" |
sandwich | 5:090ef6275773 | 2 | |
sandwich | 5:090ef6275773 | 3 | //Constructors |
sandwich | 5:090ef6275773 | 4 | PwmReader::PwmReader(PinName pwmInPort) |
sandwich | 5:090ef6275773 | 5 | { |
sandwich | 5:090ef6275773 | 6 | di = new InterruptIn(pwmInPort); |
sandwich | 5:090ef6275773 | 7 | |
sandwich | 5:090ef6275773 | 8 | lastRise = 0; |
sandwich | 5:090ef6275773 | 9 | period = 0; |
sandwich | 5:090ef6275773 | 10 | duty = 0.0; |
sandwich | 5:090ef6275773 | 11 | di->rise(this,&PwmReader::pwmRise); // attach the address of the flip function to the rising edge |
sandwich | 5:090ef6275773 | 12 | di->fall(this,&PwmReader::pwmFall); |
sandwich | 5:090ef6275773 | 13 | } |
sandwich | 5:090ef6275773 | 14 | |
sandwich | 5:090ef6275773 | 15 | PwmReader::~PwmReader() |
sandwich | 5:090ef6275773 | 16 | { |
sandwich | 5:090ef6275773 | 17 | delete di; |
sandwich | 5:090ef6275773 | 18 | } |
sandwich | 5:090ef6275773 | 19 | |
sandwich | 5:090ef6275773 | 20 | // public methods |
sandwich | 5:090ef6275773 | 21 | float PwmReader::getDuty() |
sandwich | 5:090ef6275773 | 22 | { |
sandwich | 5:090ef6275773 | 23 | return duty; |
sandwich | 5:090ef6275773 | 24 | } |
sandwich | 5:090ef6275773 | 25 | |
sandwich | 5:090ef6275773 | 26 | // private methods |
sandwich | 5:090ef6275773 | 27 | void PwmReader::pwmRise() |
sandwich | 5:090ef6275773 | 28 | { |
sandwich | 5:090ef6275773 | 29 | int rise = t.read_us(); |
sandwich | 5:090ef6275773 | 30 | if( (lastRise > 0) && (rise > lastRise) ) { |
sandwich | 5:090ef6275773 | 31 | period = rise - lastRise; |
sandwich | 5:090ef6275773 | 32 | } |
sandwich | 5:090ef6275773 | 33 | lastRise = rise; |
sandwich | 5:090ef6275773 | 34 | |
sandwich | 5:090ef6275773 | 35 | } |
sandwich | 5:090ef6275773 | 36 | |
sandwich | 5:090ef6275773 | 37 | void PwmReader::pwmFall() |
sandwich | 5:090ef6275773 | 38 | { |
sandwich | 5:090ef6275773 | 39 | int fallTime = t.read_us(); |
sandwich | 5:090ef6275773 | 40 | if(period > 0 ) { |
sandwich | 5:090ef6275773 | 41 | int delta = fallTime - lastRise; |
sandwich | 5:090ef6275773 | 42 | duty = float(delta)/float(period); |
sandwich | 5:090ef6275773 | 43 | } |
sandwich | 5:090ef6275773 | 44 | } |