Used to read incoming PWM signals from RC channels

Dependents:   A-Quad

PulseIn.cpp

Committer:
KarimAzzouz
Date:
2012-12-23
Revision:
1:eaf70ff4df07
Parent:
0:1a57c7f88422
Child:
2:4abac72addb7

File content as of revision 1:eaf70ff4df07:

#include "PulseIn.h"
#include "mbed.h"
 
PulseIn::PulseIn(PinName p) : _k(p)
{
    _k.rise(this, &PulseIn::rise);
    _k.fall(this, &PulseIn::fall);
    _duration=0;
    
}


int PulseIn::read()
{
    return _duration;
}
 
 int PulseIn::getAngle(){
 
  return 30*(1500-_duration)/500; // Scale (1000--->1500 and 1500--->2000) to +/- 30 degrees range 
 
 }
 
void PulseIn::rise()
{   
    t.start();
}
 
void PulseIn::fall()
{
    t.stop();
    uint16_t test = t.read_us();
    if(test >= 1000 && test <=2000){
    _prev=_duration;
    _duration = (_prev * (1.0 - 0.3) + (t.read_us() * 0.3)); // Filter Data 
    }
    t.reset(); 
}