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:07:00 2015 +0000
Revision:
5:74d2f30b4dd6
Child:
6:6a59017c4f62
ServoOut - library for generating servo output pulses using the Ticker class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jebradshaw 5:74d2f30b4dd6 1 // Uses attach_us to generate servo pulse on single output pin
jebradshaw 5:74d2f30b4dd6 2 // J. Bradshaw 20140925
jebradshaw 5:74d2f30b4dd6 3 /* Copyright (c) 2014, jbradshaw (http://mbed.org)
jebradshaw 5:74d2f30b4dd6 4 *
jebradshaw 5:74d2f30b4dd6 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
jebradshaw 5:74d2f30b4dd6 6 * of this software and associated documentation files (the "Software"), to deal
jebradshaw 5:74d2f30b4dd6 7 * in the Software without restriction, including without limitation the rights
jebradshaw 5:74d2f30b4dd6 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jebradshaw 5:74d2f30b4dd6 9 * copies of the Software, and to permit persons to whom the Software is
jebradshaw 5:74d2f30b4dd6 10 * furnished to do so, subject to the following conditions:
jebradshaw 5:74d2f30b4dd6 11 *
jebradshaw 5:74d2f30b4dd6 12 * The above copyright notice and this permission notice shall be included in
jebradshaw 5:74d2f30b4dd6 13 * all copies or substantial portions of the Software.
jebradshaw 5:74d2f30b4dd6 14 *
jebradshaw 5:74d2f30b4dd6 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jebradshaw 5:74d2f30b4dd6 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jebradshaw 5:74d2f30b4dd6 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jebradshaw 5:74d2f30b4dd6 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jebradshaw 5:74d2f30b4dd6 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jebradshaw 5:74d2f30b4dd6 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jebradshaw 5:74d2f30b4dd6 21 * THE SOFTWARE.
jebradshaw 5:74d2f30b4dd6 22 *
jebradshaw 5:74d2f30b4dd6 23 */
jebradshaw 5:74d2f30b4dd6 24 #include "mbed.h"
jebradshaw 5:74d2f30b4dd6 25 #include "ServoOut.h"
jebradshaw 5:74d2f30b4dd6 26
jebradshaw 5:74d2f30b4dd6 27 ServoOut::ServoOut(PinName pin) : _pin(pin) {
jebradshaw 5:74d2f30b4dd6 28 _pin = 0; // Initialize output pin as off
jebradshaw 5:74d2f30b4dd6 29 _sCycle = 0; // initialize servo cycle as 0
jebradshaw 5:74d2f30b4dd6 30 pulseMin=900; // default minimum (except 0) servo pulse width
jebradshaw 5:74d2f30b4dd6 31 pulseMax=2100; // maximum servo pulse width
jebradshaw 5:74d2f30b4dd6 32 pulse_us=0; // Initialize servo pulse width as 0
jebradshaw 5:74d2f30b4dd6 33 ticker = new Ticker(); // instantiate new Ticker object
jebradshaw 5:74d2f30b4dd6 34 ticker->attach_us(this,&ServoOut::tickFunct,20000); //begin tickFunction in 20ms
jebradshaw 5:74d2f30b4dd6 35 }
jebradshaw 5:74d2f30b4dd6 36
jebradshaw 5:74d2f30b4dd6 37 void ServoOut::tickFunct(void){
jebradshaw 5:74d2f30b4dd6 38
jebradshaw 5:74d2f30b4dd6 39 if(pulse_us == 0){ //first check to see if the pulse width is 0 (OFF, servo not powered)
jebradshaw 5:74d2f30b4dd6 40 _pin=0; // disable servo pulse output
jebradshaw 5:74d2f30b4dd6 41 ticker->attach_us(this,&ServoOut::tickFunct,20000); // recall this function in 20ms
jebradshaw 5:74d2f30b4dd6 42 return;
jebradshaw 5:74d2f30b4dd6 43 }
jebradshaw 5:74d2f30b4dd6 44
jebradshaw 5:74d2f30b4dd6 45 else{
jebradshaw 5:74d2f30b4dd6 46 if(pulse_us < pulseMin) //keep within bounds
jebradshaw 5:74d2f30b4dd6 47 pulse_us = pulseMin;
jebradshaw 5:74d2f30b4dd6 48 if(pulse_us > pulseMax)
jebradshaw 5:74d2f30b4dd6 49 pulse_us = pulseMax;
jebradshaw 5:74d2f30b4dd6 50
jebradshaw 5:74d2f30b4dd6 51 if(_sCycle){ //mode == 1
jebradshaw 5:74d2f30b4dd6 52 //pulse was high, now drop low for remaining time
jebradshaw 5:74d2f30b4dd6 53 ticker->attach_us(this,&ServoOut::tickFunct,20000-pulse_us);; // recall this function in 20ms - servo pulse width (servo pulse low)
jebradshaw 5:74d2f30b4dd6 54 _pin=0;
jebradshaw 5:74d2f30b4dd6 55 _sCycle=0;
jebradshaw 5:74d2f30b4dd6 56 }
jebradshaw 5:74d2f30b4dd6 57 else{ //mode == 0
jebradshaw 5:74d2f30b4dd6 58 ticker->attach_us(this,&ServoOut::tickFunct,pulse_us);; // recall this function in 20ms - pulse width (servo pulse high)
jebradshaw 5:74d2f30b4dd6 59 _pin=1;
jebradshaw 5:74d2f30b4dd6 60 _sCycle=1;
jebradshaw 5:74d2f30b4dd6 61 }
jebradshaw 5:74d2f30b4dd6 62 } //pulse != 0 (OFF)
jebradshaw 5:74d2f30b4dd6 63 }
jebradshaw 5:74d2f30b4dd6 64
jebradshaw 5:74d2f30b4dd6 65 void ServoOut::write(int pulse){
jebradshaw 5:74d2f30b4dd6 66 pulse_us = pulse;
jebradshaw 5:74d2f30b4dd6 67 }
jebradshaw 5:74d2f30b4dd6 68
jebradshaw 5:74d2f30b4dd6 69 int ServoOut::read(){
jebradshaw 5:74d2f30b4dd6 70 return pulse_us;
jebradshaw 5:74d2f30b4dd6 71 }
jebradshaw 5:74d2f30b4dd6 72
jebradshaw 5:74d2f30b4dd6 73 ServoOut& ServoOut::operator= (int pulse_us) {
jebradshaw 5:74d2f30b4dd6 74 write(pulse_us);
jebradshaw 5:74d2f30b4dd6 75 return *this;
jebradshaw 5:74d2f30b4dd6 76 }
jebradshaw 5:74d2f30b4dd6 77
jebradshaw 5:74d2f30b4dd6 78 ServoOut& ServoOut::operator= (ServoOut& rhs) {
jebradshaw 5:74d2f30b4dd6 79 write(rhs.read());
jebradshaw 5:74d2f30b4dd6 80 return *this;
jebradshaw 5:74d2f30b4dd6 81 }
jebradshaw 5:74d2f30b4dd6 82
jebradshaw 5:74d2f30b4dd6 83 ServoOut::operator int() {
jebradshaw 5:74d2f30b4dd6 84 return read();
jebradshaw 5:74d2f30b4dd6 85 }