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.cpp
- Committer:
- jebradshaw
- Date:
- 2015-04-14
- Revision:
- 0:98a3b6fbd242
- Child:
- 1:b7cc6da72d09
File content as of revision 0:98a3b6fbd242:
// Uses Timer to read servo pulse on single input pin // J. Bradshaw 20140925 /* Copyright (c) 2015, 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. * */ #include "mbed.h" #include "ServoIn.h" 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 pulseMeasure.start(); _pulseFlag = 0; servoPulse = 0; } //function to read the servo pulse duration void ServoIn::PulseRead() { if(!_pulseFlag){ //if the flag is low, was waiting for high input interrupt this->pulseMeasure.reset(); //reset the timer _interrupt.fall(this, &ServoIn::PulseRead); _pulseFlag = 1; //set the pulse input flag } else{ servoPulse = pulseMeasure.read_us(); _interrupt.rise(this, &ServoIn::PulseRead); _pulseFlag = 0; } } int ServoIn::read(void){ return servoPulse; }