Class Library for reading hobby servos and detecting invalid or disconnected channels
Dependents: MadPulseCntrl Emaxx_Navigation Emaxx_Navigation_Dynamic_HIL Madpulse_Speed_Control_temp ... more
ServoIn Class Library
This class library uses external interrupts with timer functions to read servos pulses on mbed input pins.
ServoIn Library usage example
#include "mbed.h" #include "ServoIn.h" DigitalOut led1(LED1); ServoIn servoIn1(p15); ServoIn servoIn2(p14); Serial pc(USBTX, USBRX); // tx, rx int main() { pc.baud(921600); //Fast baud rate while(1) { led1 = 1; wait(0.05); led1 = 0; wait(0.05); pc.printf("servo pulse: CH1=%5dus CH2=%5dus\r\n", servoIn1.read(), servoIn2.read()); } }
Diff: ServoIn.cpp
- Revision:
- 1:b7cc6da72d09
- Parent:
- 0:98a3b6fbd242
- Child:
- 3:19c8eaf905e9
--- a/ServoIn.cpp Tue Apr 14 20:05:13 2015 +0000 +++ b/ServoIn.cpp Wed Apr 15 19:36:40 2015 +0000 @@ -27,9 +27,11 @@ ServoIn::ServoIn(PinName pin): _interrupt(pin){ // create the InterruptIn on the pin specified to ServoIn _interrupt.rise(this, &ServoIn::PulseRead); // attach PulseRead function of this ServoIn instance + _validPulseTimeout.attach(this, &ServoIn::timeoutTest, 0.015); // the member function, and interval (15ms) pulseMeasure.start(); _pulseFlag = 0; servoPulse = 0; + _t_state=0; } //function to read the servo pulse duration @@ -43,10 +45,26 @@ servoPulse = pulseMeasure.read_us(); _interrupt.rise(this, &ServoIn::PulseRead); _pulseFlag = 0; - } + } + _validPulseTimeout.attach(this, &ServoIn::timeoutTest, 0.015); //setup interrupt for 15ms + _t_state=0; //re-zero t-state } int ServoIn::read(void){ return servoPulse; -} \ No newline at end of file +} + +void ServoIn::timeoutTest(void){ + if(!_t_state){ + _validPulseTimeout.attach(this, &ServoIn::timeoutTest, 0.015); + _t_state=1; //toggle t_sate (15ms has elapsed without servo edge + } + else{ //30 ms has elapsed without a valid servo pulse + _pulseFlag = 0; + _t_state=0; + servoPulse = 0; + _validPulseTimeout.attach(this, &ServoIn::timeoutTest, 0.015); + } +} +