the fish that looks like a jet

Dependencies:   ADXL345 ADXL345_I2C IMUfilter ITG3200 mbed Servo

Committer:
rkk
Date:
Wed Jan 29 05:04:50 2014 +0000
Revision:
6:a4d6f3e4bf28
Parent:
5:090ef6275773
Child:
7:e005cfaff8d1
added pam ira

Who changed what in which revision?

UserRevisionLine numberNew 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 5:090ef6275773 17 di->rise(this,&PwmReader::pwmRise); // attach the address of the flip function to the rising edge
sandwich 5:090ef6275773 18 di->fall(this,&PwmReader::pwmFall);
sandwich 5:090ef6275773 19 }
sandwich 5:090ef6275773 20
sandwich 5:090ef6275773 21 PwmReader::~PwmReader()
sandwich 5:090ef6275773 22 {
sandwich 5:090ef6275773 23 delete di;
sandwich 5:090ef6275773 24 }
sandwich 5:090ef6275773 25
sandwich 5:090ef6275773 26 // public methods
sandwich 5:090ef6275773 27 float PwmReader::getDuty()
sandwich 5:090ef6275773 28 {
rkk 6:a4d6f3e4bf28 29 float smallDelta = (duty > pwmMin) ? (duty - pwmMin) : 0.0;
rkk 6:a4d6f3e4bf28 30 float dutyAdjusted = smallDelta / (pwmMax-pwmMin);
rkk 6:a4d6f3e4bf28 31 return dutyAdjusted;
sandwich 5:090ef6275773 32 }
sandwich 5:090ef6275773 33
sandwich 5:090ef6275773 34 // private methods
sandwich 5:090ef6275773 35 void PwmReader::pwmRise()
sandwich 5:090ef6275773 36 {
sandwich 5:090ef6275773 37 int rise = t.read_us();
sandwich 5:090ef6275773 38 if( (lastRise > 0) && (rise > lastRise) ) {
sandwich 5:090ef6275773 39 period = rise - lastRise;
sandwich 5:090ef6275773 40 }
sandwich 5:090ef6275773 41 lastRise = rise;
sandwich 5:090ef6275773 42
sandwich 5:090ef6275773 43 }
sandwich 5:090ef6275773 44
sandwich 5:090ef6275773 45 void PwmReader::pwmFall()
sandwich 5:090ef6275773 46 {
rkk 6:a4d6f3e4bf28 47 int fall = t.read_us();
sandwich 5:090ef6275773 48 if(period > 0 ) {
rkk 6:a4d6f3e4bf28 49 int delta = fall - lastRise;
rkk 6:a4d6f3e4bf28 50 duty = float(delta)/float(period);
sandwich 5:090ef6275773 51 }
sandwich 5:090ef6275773 52 }