Used to read incoming PWM signals from RC channels
PulseIn.cpp@1:eaf70ff4df07, 2012-12-23 (annotated)
- Committer:
- KarimAzzouz
- Date:
- Sun Dec 23 23:49:18 2012 +0000
- Revision:
- 1:eaf70ff4df07
- Parent:
- 0:1a57c7f88422
- Child:
- 2:4abac72addb7
No problems Here
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
KarimAzzouz | 0:1a57c7f88422 | 1 | #include "PulseIn.h" |
KarimAzzouz | 0:1a57c7f88422 | 2 | #include "mbed.h" |
KarimAzzouz | 0:1a57c7f88422 | 3 | |
KarimAzzouz | 0:1a57c7f88422 | 4 | PulseIn::PulseIn(PinName p) : _k(p) |
KarimAzzouz | 0:1a57c7f88422 | 5 | { |
KarimAzzouz | 0:1a57c7f88422 | 6 | _k.rise(this, &PulseIn::rise); |
KarimAzzouz | 0:1a57c7f88422 | 7 | _k.fall(this, &PulseIn::fall); |
KarimAzzouz | 0:1a57c7f88422 | 8 | _duration=0; |
KarimAzzouz | 0:1a57c7f88422 | 9 | |
KarimAzzouz | 0:1a57c7f88422 | 10 | } |
KarimAzzouz | 1:eaf70ff4df07 | 11 | |
KarimAzzouz | 1:eaf70ff4df07 | 12 | |
KarimAzzouz | 0:1a57c7f88422 | 13 | int PulseIn::read() |
KarimAzzouz | 0:1a57c7f88422 | 14 | { |
KarimAzzouz | 0:1a57c7f88422 | 15 | return _duration; |
KarimAzzouz | 0:1a57c7f88422 | 16 | } |
KarimAzzouz | 0:1a57c7f88422 | 17 | |
KarimAzzouz | 1:eaf70ff4df07 | 18 | int PulseIn::getAngle(){ |
KarimAzzouz | 1:eaf70ff4df07 | 19 | |
KarimAzzouz | 1:eaf70ff4df07 | 20 | return 30*(1500-_duration)/500; // Scale (1000--->1500 and 1500--->2000) to +/- 30 degrees range |
KarimAzzouz | 1:eaf70ff4df07 | 21 | |
KarimAzzouz | 1:eaf70ff4df07 | 22 | } |
KarimAzzouz | 1:eaf70ff4df07 | 23 | |
KarimAzzouz | 0:1a57c7f88422 | 24 | void PulseIn::rise() |
KarimAzzouz | 1:eaf70ff4df07 | 25 | { |
KarimAzzouz | 0:1a57c7f88422 | 26 | t.start(); |
KarimAzzouz | 0:1a57c7f88422 | 27 | } |
KarimAzzouz | 0:1a57c7f88422 | 28 | |
KarimAzzouz | 0:1a57c7f88422 | 29 | void PulseIn::fall() |
KarimAzzouz | 0:1a57c7f88422 | 30 | { |
KarimAzzouz | 0:1a57c7f88422 | 31 | t.stop(); |
KarimAzzouz | 1:eaf70ff4df07 | 32 | uint16_t test = t.read_us(); |
KarimAzzouz | 1:eaf70ff4df07 | 33 | if(test >= 1000 && test <=2000){ |
KarimAzzouz | 1:eaf70ff4df07 | 34 | _prev=_duration; |
KarimAzzouz | 1:eaf70ff4df07 | 35 | _duration = (_prev * (1.0 - 0.3) + (t.read_us() * 0.3)); // Filter Data |
KarimAzzouz | 1:eaf70ff4df07 | 36 | } |
KarimAzzouz | 1:eaf70ff4df07 | 37 | t.reset(); |
KarimAzzouz | 0:1a57c7f88422 | 38 | } |