Library for generating servo pulses from mbed output pins

Dependents:   mbed_ES20X_Tester MadPulseCntrl ES413Integration Emaxx_Navigation ... more

Fork of SERVOGEN by Joseph Bradshaw

ServoOut Library

Assign the ServoOut object (output pin) a value in microseconds to change the servo pulse. Note that the default range is between 900 and 2100 microseconds.

Library for generating servo pulses using the Ticker class

/media/uploads/jebradshaw/mbedwsesbc_servos.jpg

/media/uploads/jebradshaw/mbed_servo_pulses.jpg

include the mbed library with this snippet

// ServoOut Class test program
// J. Bradshaw 20140925
#include "mbed.h"
#include "ServoOut.h" 
 
ServoOut servo1(p21);
ServoOut servo2(p24);
DigitalOut led1(LED1);
 
int main() {
    servo1.pulse_us = 0;        
    // spin in a main loop. flipper will interrupt it to call flip
    while(1) {
        for(float cycle=0.0;cycle<2.0*3.14159;cycle+=.003){
            //small amplitude sine wave on servo1
            servo1 = 300 * sin(cycle) + 1500;            
            //ramp up the second servo2 channel relative to cycle
            servo2 = 1000 + cycle*159; // .001/(2*PI)
            
            wait(.001); //short delay
        }
        led1 = !led1;   //toggle led1 to indicate activity
    }//while(1)
}//main
Committer:
jebradshaw
Date:
Fri Apr 10 17:08:12 2015 +0000
Revision:
6:6a59017c4f62
Parent:
5:74d2f30b4dd6
ServoOut - First revision (renamed from SERVOGEN) of ServoOut library to generate servo pulses from mbed output pins.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jebradshaw 4:7043ab96dd4a 1 // J. Bradshaw 20150409
jebradshaw 4:7043ab96dd4a 2 /** ServoGen Class for Generating servo pulses on single output pin
jebradshaw 2:44766ee77ce0 3 * Copyright (c) 2014, jbradshaw (http://mbed.org)
jebradshaw 2:44766ee77ce0 4 *
jebradshaw 2:44766ee77ce0 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
jebradshaw 2:44766ee77ce0 6 * of this software and associated documentation files (the "Software"), to deal
jebradshaw 2:44766ee77ce0 7 * in the Software without restriction, including without limitation the rights
jebradshaw 2:44766ee77ce0 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jebradshaw 2:44766ee77ce0 9 * copies of the Software, and to permit persons to whom the Software is
jebradshaw 2:44766ee77ce0 10 * furnished to do so, subject to the following conditions:
jebradshaw 2:44766ee77ce0 11 *
jebradshaw 2:44766ee77ce0 12 * The above copyright notice and this permission notice shall be included in
jebradshaw 2:44766ee77ce0 13 * all copies or substantial portions of the Software.
jebradshaw 2:44766ee77ce0 14 *
jebradshaw 2:44766ee77ce0 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jebradshaw 2:44766ee77ce0 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jebradshaw 2:44766ee77ce0 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jebradshaw 2:44766ee77ce0 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jebradshaw 2:44766ee77ce0 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jebradshaw 2:44766ee77ce0 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jebradshaw 2:44766ee77ce0 21 * THE SOFTWARE.
jebradshaw 2:44766ee77ce0 22 *
jebradshaw 1:7d669a801bb0 23 *
jebradshaw 1:7d669a801bb0 24 * Example:
jebradshaw 1:7d669a801bb0 25 * @code
jebradshaw 1:7d669a801bb0 26 *#include "mbed.h"
jebradshaw 5:74d2f30b4dd6 27 *#include "ServoOut.h"
jebradshaw 1:7d669a801bb0 28 *
jebradshaw 5:74d2f30b4dd6 29 *ServoOut servo1(p21);
jebradshaw 5:74d2f30b4dd6 30 *ServoOut servo2(p24);
jebradshaw 1:7d669a801bb0 31 *DigitalOut led1(LED1);
jebradshaw 1:7d669a801bb0 32 *
jebradshaw 1:7d669a801bb0 33 *int main() {
jebradshaw 1:7d669a801bb0 34 * servo1.pulse_us = 0;
jebradshaw 1:7d669a801bb0 35 * // spin in a main loop. flipper will interrupt it to call flip
jebradshaw 1:7d669a801bb0 36 * while(1) {
jebradshaw 1:7d669a801bb0 37 * for(float cycle=0.0;cycle<2.0*3.14159;cycle+=.003){
jebradshaw 1:7d669a801bb0 38 * //small amplitude sine wave on servo1
jebradshaw 1:7d669a801bb0 39 * servo1.pulse_us = 300 * sin(cycle) + 1500;
jebradshaw 1:7d669a801bb0 40 * //ramp up the second servo2 channel relative to cycle
jebradshaw 1:7d669a801bb0 41 * servo2.pulse_us = 1000 + cycle*159; // .001/(2*PI)
jebradshaw 1:7d669a801bb0 42 *
jebradshaw 1:7d669a801bb0 43 * wait(.001); //short delay
jebradshaw 1:7d669a801bb0 44 * }
jebradshaw 1:7d669a801bb0 45 * led1 = !led1; //toggle led1 to indicate activity
jebradshaw 1:7d669a801bb0 46 * }//while(1)
jebradshaw 1:7d669a801bb0 47 *}//main
jebradshaw 1:7d669a801bb0 48 *
jebradshaw 1:7d669a801bb0 49 *@endcode
jebradshaw 1:7d669a801bb0 50 */
jebradshaw 1:7d669a801bb0 51
jebradshaw 5:74d2f30b4dd6 52 #ifndef MBED_SERVOOUT_H
jebradshaw 5:74d2f30b4dd6 53 #define MBED_SERVOOUT_H
jebradshaw 0:67dd4036010a 54
jebradshaw 0:67dd4036010a 55 #include "mbed.h"
jebradshaw 0:67dd4036010a 56
jebradshaw 0:67dd4036010a 57 /**
jebradshaw 5:74d2f30b4dd6 58 * ServoOut Class.
jebradshaw 0:67dd4036010a 59 */
jebradshaw 0:67dd4036010a 60
jebradshaw 5:74d2f30b4dd6 61 class ServoOut {
jebradshaw 0:67dd4036010a 62 public:
jebradshaw 0:67dd4036010a 63 /**
jebradshaw 0:67dd4036010a 64 * Constructor.
jebradshaw 0:67dd4036010a 65 *
jebradshaw 0:67dd4036010a 66 * @param pin - servo pulse output pin
jebradshaw 0:67dd4036010a 67 */
jebradshaw 5:74d2f30b4dd6 68 ServoOut(PinName pin);
jebradshaw 0:67dd4036010a 69
jebradshaw 0:67dd4036010a 70 int pulse_us; //determines the pulse width in microseconds (default is 0 = OFF)
jebradshaw 0:67dd4036010a 71 int pulseMin; //minimum pulse width (default is 900 microseconds)
jebradshaw 0:67dd4036010a 72 int pulseMax; //maximum pulse width (default is 2100 microseconds)
jebradshaw 3:edc18509ff11 73
jebradshaw 3:edc18509ff11 74 void write(int pulse); //
jebradshaw 3:edc18509ff11 75 int read(void);
jebradshaw 3:edc18509ff11 76
jebradshaw 3:edc18509ff11 77 /** Shorthand for the write and read functions */
jebradshaw 5:74d2f30b4dd6 78 ServoOut& operator= (int pulse_us);
jebradshaw 5:74d2f30b4dd6 79 ServoOut& operator= (ServoOut& rhs);
jebradshaw 3:edc18509ff11 80 operator int();
jebradshaw 0:67dd4036010a 81
jebradshaw 0:67dd4036010a 82 private:
jebradshaw 0:67dd4036010a 83 void tickFunct(void); //Function that takes care of the servo pulse generation
jebradshaw 0:67dd4036010a 84 Ticker *ticker; //pointer to ticker object
jebradshaw 0:67dd4036010a 85 DigitalOut _pin; //make assigned pin a digital output
jebradshaw 0:67dd4036010a 86 int _sCycle; //flag to toggle for keeping track of servo cycle
jebradshaw 0:67dd4036010a 87 };
jebradshaw 0:67dd4036010a 88
jebradshaw 0:67dd4036010a 89 #endif