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());        
    }
}
Revision:
0:98a3b6fbd242
Child:
1:b7cc6da72d09
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ServoIn.h	Tue Apr 14 20:05:13 2015 +0000
@@ -0,0 +1,56 @@
+// J. Bradshaw 20150409
+/** ServoIn Class for Reading servo pulses on single input pin
+ * Copyright (c) 2014, jbradshaw (http://mbed.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+*/
+
+#ifndef MBED_SERVOIN_H
+#define MBED_SERVOIN_H
+
+/**
+ * ServoIn Class.
+ */
+ 
+ class ServoIn {
+public:
+    /**
+    * Constructor.
+    *
+    * @param pin - servo pulse input pin, assigned as interrupt in
+    */         
+    
+    //ServoIn(PinName pin);
+    ServoIn(PinName pin);
+            
+    int servoPulse;
+    int read(void);
+    void PulseRead(void);
+    
+    ServoIn& operator= (ServoIn& rhs);
+    operator int();
+    
+private:
+    InterruptIn _interrupt;
+    bool _pulseFlag;
+    Timer   pulseMeasure;
+};
+
+#endif
\ No newline at end of file