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
Committer:
jebradshaw
Date:
Mon Sep 29 16:47:31 2014 +0000
Revision:
3:edc18509ff11
Parent:
2:44766ee77ce0
Added operator accessible read/writes to simplify use;  J Bradshaw 20140929

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jebradshaw 0:67dd4036010a 1 // Uses attach_us to generate servo pulse on single output pin
jebradshaw 0:67dd4036010a 2 // J. Bradshaw 20140925
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 2:44766ee77ce0 23 */
jebradshaw 0:67dd4036010a 24 #include "mbed.h"
jebradshaw 0:67dd4036010a 25 #include "SERVOGEN.h"
jebradshaw 0:67dd4036010a 26
jebradshaw 0:67dd4036010a 27 SERVOGEN::SERVOGEN(PinName pin) : _pin(pin) {
jebradshaw 0:67dd4036010a 28 _pin = 0; // Initialize output pin as off
jebradshaw 0:67dd4036010a 29 _sCycle = 0; // initialize servo cycle as 0
jebradshaw 0:67dd4036010a 30 pulseMin=900; // default minimum (except 0) servo pulse width
jebradshaw 0:67dd4036010a 31 pulseMax=2100; // maximum servo pulse width
jebradshaw 0:67dd4036010a 32 pulse_us=0; // Initialize servo pulse width as 0
jebradshaw 0:67dd4036010a 33 ticker = new Ticker(); // instantiate new Ticker object
jebradshaw 0:67dd4036010a 34 ticker->attach_us(this,&SERVOGEN::tickFunct,20000); //begin tickFunction in 20ms
jebradshaw 0:67dd4036010a 35 }
jebradshaw 0:67dd4036010a 36
jebradshaw 0:67dd4036010a 37 void SERVOGEN::tickFunct(void){
jebradshaw 0:67dd4036010a 38
jebradshaw 0:67dd4036010a 39 if(pulse_us == 0){ //first check to see if the pulse width is 0 (OFF, servo not powered)
jebradshaw 0:67dd4036010a 40 _pin=0; // disable servo pulse output
jebradshaw 0:67dd4036010a 41 ticker->attach_us(this,&SERVOGEN::tickFunct,20000); // recall this function in 20ms
jebradshaw 0:67dd4036010a 42 return;
jebradshaw 0:67dd4036010a 43 }
jebradshaw 0:67dd4036010a 44
jebradshaw 0:67dd4036010a 45 else{
jebradshaw 0:67dd4036010a 46 if(pulse_us < pulseMin) //keep within bounds
jebradshaw 0:67dd4036010a 47 pulse_us = pulseMin;
jebradshaw 0:67dd4036010a 48 if(pulse_us > pulseMax)
jebradshaw 0:67dd4036010a 49 pulse_us = pulseMax;
jebradshaw 0:67dd4036010a 50
jebradshaw 0:67dd4036010a 51 if(_sCycle){ //mode == 1
jebradshaw 0:67dd4036010a 52 //pulse was high, now drop low for remaining time
jebradshaw 0:67dd4036010a 53 ticker->attach_us(this,&SERVOGEN::tickFunct,20000-pulse_us);; // recall this function in 20ms - servo pulse width (servo pulse low)
jebradshaw 0:67dd4036010a 54 _pin=0;
jebradshaw 0:67dd4036010a 55 _sCycle=0;
jebradshaw 0:67dd4036010a 56 }
jebradshaw 0:67dd4036010a 57 else{ //mode == 0
jebradshaw 0:67dd4036010a 58 ticker->attach_us(this,&SERVOGEN::tickFunct,pulse_us);; // recall this function in 20ms - pulse width (servo pulse high)
jebradshaw 0:67dd4036010a 59 _pin=1;
jebradshaw 0:67dd4036010a 60 _sCycle=1;
jebradshaw 0:67dd4036010a 61 }
jebradshaw 0:67dd4036010a 62 } //pulse != 0 (OFF)
jebradshaw 3:edc18509ff11 63 }
jebradshaw 3:edc18509ff11 64
jebradshaw 3:edc18509ff11 65 void SERVOGEN::write(int pulse){
jebradshaw 3:edc18509ff11 66 pulse_us = pulse;
jebradshaw 3:edc18509ff11 67 }
jebradshaw 3:edc18509ff11 68
jebradshaw 3:edc18509ff11 69 int SERVOGEN::read(){
jebradshaw 3:edc18509ff11 70 return pulse_us;
jebradshaw 3:edc18509ff11 71 }
jebradshaw 3:edc18509ff11 72
jebradshaw 3:edc18509ff11 73 SERVOGEN& SERVOGEN::operator= (int pulse_us) {
jebradshaw 3:edc18509ff11 74 write(pulse_us);
jebradshaw 3:edc18509ff11 75 return *this;
jebradshaw 3:edc18509ff11 76 }
jebradshaw 3:edc18509ff11 77
jebradshaw 3:edc18509ff11 78 SERVOGEN& SERVOGEN::operator= (SERVOGEN& rhs) {
jebradshaw 3:edc18509ff11 79 write(rhs.read());
jebradshaw 3:edc18509ff11 80 return *this;
jebradshaw 3:edc18509ff11 81 }
jebradshaw 3:edc18509ff11 82
jebradshaw 3:edc18509ff11 83 SERVOGEN::operator int() {
jebradshaw 3:edc18509ff11 84 return read();
jebradshaw 0:67dd4036010a 85 }