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@0:98a3b6fbd242, 2015-04-14 (annotated)
- Committer:
- jebradshaw
- Date:
- Tue Apr 14 20:05:13 2015 +0000
- Revision:
- 0:98a3b6fbd242
- Child:
- 1:b7cc6da72d09
ServoIn - First class library 20150414; Joe Bradshaw
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jebradshaw | 0:98a3b6fbd242 | 1 | // Uses Timer to read servo pulse on single input pin |
jebradshaw | 0:98a3b6fbd242 | 2 | // J. Bradshaw 20140925 |
jebradshaw | 0:98a3b6fbd242 | 3 | /* Copyright (c) 2015, jbradshaw (http://mbed.org) |
jebradshaw | 0:98a3b6fbd242 | 4 | * |
jebradshaw | 0:98a3b6fbd242 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
jebradshaw | 0:98a3b6fbd242 | 6 | * of this software and associated documentation files (the "Software"), to deal |
jebradshaw | 0:98a3b6fbd242 | 7 | * in the Software without restriction, including without limitation the rights |
jebradshaw | 0:98a3b6fbd242 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
jebradshaw | 0:98a3b6fbd242 | 9 | * copies of the Software, and to permit persons to whom the Software is |
jebradshaw | 0:98a3b6fbd242 | 10 | * furnished to do so, subject to the following conditions: |
jebradshaw | 0:98a3b6fbd242 | 11 | * |
jebradshaw | 0:98a3b6fbd242 | 12 | * The above copyright notice and this permission notice shall be included in |
jebradshaw | 0:98a3b6fbd242 | 13 | * all copies or substantial portions of the Software. |
jebradshaw | 0:98a3b6fbd242 | 14 | * |
jebradshaw | 0:98a3b6fbd242 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
jebradshaw | 0:98a3b6fbd242 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
jebradshaw | 0:98a3b6fbd242 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
jebradshaw | 0:98a3b6fbd242 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
jebradshaw | 0:98a3b6fbd242 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jebradshaw | 0:98a3b6fbd242 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
jebradshaw | 0:98a3b6fbd242 | 21 | * THE SOFTWARE. |
jebradshaw | 0:98a3b6fbd242 | 22 | * |
jebradshaw | 0:98a3b6fbd242 | 23 | */ |
jebradshaw | 0:98a3b6fbd242 | 24 | #include "mbed.h" |
jebradshaw | 0:98a3b6fbd242 | 25 | #include "ServoIn.h" |
jebradshaw | 0:98a3b6fbd242 | 26 | |
jebradshaw | 0:98a3b6fbd242 | 27 | ServoIn::ServoIn(PinName pin): _interrupt(pin){ // create the InterruptIn on the pin specified to ServoIn |
jebradshaw | 0:98a3b6fbd242 | 28 | _interrupt.rise(this, &ServoIn::PulseRead); // attach PulseRead function of this ServoIn instance |
jebradshaw | 0:98a3b6fbd242 | 29 | |
jebradshaw | 0:98a3b6fbd242 | 30 | pulseMeasure.start(); |
jebradshaw | 0:98a3b6fbd242 | 31 | _pulseFlag = 0; |
jebradshaw | 0:98a3b6fbd242 | 32 | servoPulse = 0; |
jebradshaw | 0:98a3b6fbd242 | 33 | } |
jebradshaw | 0:98a3b6fbd242 | 34 | |
jebradshaw | 0:98a3b6fbd242 | 35 | //function to read the servo pulse duration |
jebradshaw | 0:98a3b6fbd242 | 36 | void ServoIn::PulseRead() { |
jebradshaw | 0:98a3b6fbd242 | 37 | if(!_pulseFlag){ //if the flag is low, was waiting for high input interrupt |
jebradshaw | 0:98a3b6fbd242 | 38 | this->pulseMeasure.reset(); //reset the timer |
jebradshaw | 0:98a3b6fbd242 | 39 | _interrupt.fall(this, &ServoIn::PulseRead); |
jebradshaw | 0:98a3b6fbd242 | 40 | _pulseFlag = 1; //set the pulse input flag |
jebradshaw | 0:98a3b6fbd242 | 41 | } |
jebradshaw | 0:98a3b6fbd242 | 42 | else{ |
jebradshaw | 0:98a3b6fbd242 | 43 | servoPulse = pulseMeasure.read_us(); |
jebradshaw | 0:98a3b6fbd242 | 44 | _interrupt.rise(this, &ServoIn::PulseRead); |
jebradshaw | 0:98a3b6fbd242 | 45 | _pulseFlag = 0; |
jebradshaw | 0:98a3b6fbd242 | 46 | } |
jebradshaw | 0:98a3b6fbd242 | 47 | } |
jebradshaw | 0:98a3b6fbd242 | 48 | |
jebradshaw | 0:98a3b6fbd242 | 49 | |
jebradshaw | 0:98a3b6fbd242 | 50 | int ServoIn::read(void){ |
jebradshaw | 0:98a3b6fbd242 | 51 | return servoPulse; |
jebradshaw | 0:98a3b6fbd242 | 52 | } |