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());        
    }
}

ServoIn.h

Committer:
jebradshaw
Date:
2017-08-14
Revision:
5:598ccee73ed3
Parent:
4:379f9ab5cb4b

File content as of revision 5:598ccee73ed3:

// 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);
            
    int servoPulse;
    int servoPulseMax;
    int servoPulseMin;
    float servoPulseOffset;
    float servoPulseScale;
    
    float readCalPulse(void);
    int read(void);
    void PulseRead(void);
    void timeoutTest(void);
    
    ServoIn& operator= (ServoIn& rhs);
    operator int();
    
private:
    InterruptIn _interrupt;
    DigitalIn _pin;
    Timeout _validPulseTimeout;
    bool _pulseFlag;
    Timer   pulseMeasure;
    bool _t_state;
};

#endif