Class library for generating hobby servo pulses on any digital output pin

Dependents:   mbed_ES200_Tester_SERVOGEN mbed_ES20X_Thread_Test

SERVOGEN library

Library for generating servo pulses using the Ticker class

/media/uploads/jebradshaw/mbedwsesbc_servos.jpg

/media/uploads/jebradshaw/mbad_servo_pulses.jpg

include the mbed library with this snippet

// SERVOGEN Class test program
// J. Bradshaw 20140925
#include "mbed.h"
#include "SERVOGEN.h" 
 
SERVOGEN servo1(p21);
SERVOGEN 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

SERVOGEN.cpp

Committer:
jebradshaw
Date:
2014-09-28
Revision:
2:44766ee77ce0
Parent:
1:7d669a801bb0
Child:
3:edc18509ff11

File content as of revision 2:44766ee77ce0:

// Uses attach_us to generate servo pulse on single output pin
// J. Bradshaw 20140925
/* 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.
 *
*/ 
#include "mbed.h"
#include "SERVOGEN.h"

SERVOGEN::SERVOGEN(PinName pin) : _pin(pin) {
    _pin = 0;           // Initialize output pin as off
    _sCycle = 0;        // initialize servo cycle as 0
    pulseMin=900;       // default minimum (except 0) servo pulse width
    pulseMax=2100;      // maximum servo pulse width
    pulse_us=0;         // Initialize servo pulse width as 0
    ticker = new Ticker();  // instantiate new Ticker object
    ticker->attach_us(this,&SERVOGEN::tickFunct,20000); //begin tickFunction in 20ms
}

void SERVOGEN::tickFunct(void){
    
    if(pulse_us == 0){  //first check to see if the pulse width is 0 (OFF, servo not powered)
        _pin=0;         // disable servo pulse output
        ticker->attach_us(this,&SERVOGEN::tickFunct,20000); // recall this function in 20ms
        return;
    }
    
    else{        
        if(pulse_us < pulseMin)     //keep within bounds
            pulse_us = pulseMin;
        if(pulse_us > pulseMax)
            pulse_us = pulseMax;
        
        if(_sCycle){   //mode == 1
            //pulse was high, now drop low for remaining time
            ticker->attach_us(this,&SERVOGEN::tickFunct,20000-pulse_us);; // recall this function in 20ms - servo pulse width (servo pulse low)
            _pin=0;    
            _sCycle=0;
        }
        else{   //mode == 0
            ticker->attach_us(this,&SERVOGEN::tickFunct,pulse_us);; // recall this function in 20ms - pulse width (servo pulse high)
            _pin=1;        
            _sCycle=1;
        }
    } //pulse != 0 (OFF)
}