![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
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@25:4f2f441eceec, 2014-07-11 (annotated)
- Committer:
- sandwich
- Date:
- Fri Jul 11 14:30:36 2014 +0000
- Revision:
- 25:4f2f441eceec
- Parent:
- 7:e005cfaff8d1
latest revision
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 |
rkk | 6:a4d6f3e4bf28 | 4 | PwmReader::PwmReader(PinName pwmInPort, float min, float max) |
sandwich | 5:090ef6275773 | 5 | { |
rkk | 6:a4d6f3e4bf28 | 6 | if (min > max) |
rkk | 6:a4d6f3e4bf28 | 7 | { |
rkk | 6:a4d6f3e4bf28 | 8 | error("PwmReader min value greater than max value!"); |
rkk | 6:a4d6f3e4bf28 | 9 | } |
rkk | 6:a4d6f3e4bf28 | 10 | |
sandwich | 5:090ef6275773 | 11 | di = new InterruptIn(pwmInPort); |
rkk | 6:a4d6f3e4bf28 | 12 | pwmMin = min; |
rkk | 6:a4d6f3e4bf28 | 13 | pwmMax = max; |
sandwich | 5:090ef6275773 | 14 | lastRise = 0; |
sandwich | 5:090ef6275773 | 15 | period = 0; |
sandwich | 5:090ef6275773 | 16 | duty = 0.0; |
sandwich | 7:e005cfaff8d1 | 17 | di->mode(PullDown); |
sandwich | 5:090ef6275773 | 18 | di->rise(this,&PwmReader::pwmRise); // attach the address of the flip function to the rising edge |
sandwich | 5:090ef6275773 | 19 | di->fall(this,&PwmReader::pwmFall); |
sandwich | 7:e005cfaff8d1 | 20 | |
sandwich | 7:e005cfaff8d1 | 21 | t.start(); |
sandwich | 5:090ef6275773 | 22 | } |
sandwich | 5:090ef6275773 | 23 | |
sandwich | 5:090ef6275773 | 24 | PwmReader::~PwmReader() |
sandwich | 5:090ef6275773 | 25 | { |
sandwich | 5:090ef6275773 | 26 | delete di; |
sandwich | 7:e005cfaff8d1 | 27 | t.stop(); |
sandwich | 5:090ef6275773 | 28 | } |
sandwich | 5:090ef6275773 | 29 | |
sandwich | 5:090ef6275773 | 30 | // public methods |
sandwich | 5:090ef6275773 | 31 | float PwmReader::getDuty() |
sandwich | 5:090ef6275773 | 32 | { |
rkk | 6:a4d6f3e4bf28 | 33 | float smallDelta = (duty > pwmMin) ? (duty - pwmMin) : 0.0; |
rkk | 6:a4d6f3e4bf28 | 34 | float dutyAdjusted = smallDelta / (pwmMax-pwmMin); |
rkk | 6:a4d6f3e4bf28 | 35 | return dutyAdjusted; |
sandwich | 5:090ef6275773 | 36 | } |
sandwich | 5:090ef6275773 | 37 | |
sandwich | 5:090ef6275773 | 38 | // private methods |
sandwich | 5:090ef6275773 | 39 | void PwmReader::pwmRise() |
sandwich | 5:090ef6275773 | 40 | { |
sandwich | 5:090ef6275773 | 41 | int rise = t.read_us(); |
sandwich | 5:090ef6275773 | 42 | if( (lastRise > 0) && (rise > lastRise) ) { |
sandwich | 5:090ef6275773 | 43 | period = rise - lastRise; |
sandwich | 5:090ef6275773 | 44 | } |
sandwich | 5:090ef6275773 | 45 | lastRise = rise; |
sandwich | 5:090ef6275773 | 46 | |
sandwich | 5:090ef6275773 | 47 | } |
sandwich | 5:090ef6275773 | 48 | |
sandwich | 5:090ef6275773 | 49 | void PwmReader::pwmFall() |
sandwich | 5:090ef6275773 | 50 | { |
rkk | 6:a4d6f3e4bf28 | 51 | int fall = t.read_us(); |
sandwich | 5:090ef6275773 | 52 | if(period > 0 ) { |
rkk | 6:a4d6f3e4bf28 | 53 | int delta = fall - lastRise; |
rkk | 6:a4d6f3e4bf28 | 54 | duty = float(delta)/float(period); |
sandwich | 5:090ef6275773 | 55 | } |
sandwich | 5:090ef6275773 | 56 | } |