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 Sep 26 19:22:18 2014 +0000
Revision:
0:67dd4036010a
Child:
1:7d669a801bb0
J. Bradshaw 20140926;  Working servo pulse generation library using Ticker class.

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 0:67dd4036010a 3 #ifndef MBED_SERVOGEN_H
jebradshaw 0:67dd4036010a 4 #define MBED_SERVOGEN_H
jebradshaw 0:67dd4036010a 5
jebradshaw 0:67dd4036010a 6 #include "mbed.h"
jebradshaw 0:67dd4036010a 7
jebradshaw 0:67dd4036010a 8 /**
jebradshaw 0:67dd4036010a 9 * SERVOGEN Class.
jebradshaw 0:67dd4036010a 10 */
jebradshaw 0:67dd4036010a 11
jebradshaw 0:67dd4036010a 12 class SERVOGEN {
jebradshaw 0:67dd4036010a 13 public:
jebradshaw 0:67dd4036010a 14 /**
jebradshaw 0:67dd4036010a 15 * Constructor.
jebradshaw 0:67dd4036010a 16 *
jebradshaw 0:67dd4036010a 17 * @param pin - servo pulse output pin
jebradshaw 0:67dd4036010a 18 */
jebradshaw 0:67dd4036010a 19 SERVOGEN(PinName pin);
jebradshaw 0:67dd4036010a 20
jebradshaw 0:67dd4036010a 21 int pulse_us; //determines the pulse width in microseconds (default is 0 = OFF)
jebradshaw 0:67dd4036010a 22 int pulseMin; //minimum pulse width (default is 900 microseconds)
jebradshaw 0:67dd4036010a 23 int pulseMax; //maximum pulse width (default is 2100 microseconds)
jebradshaw 0:67dd4036010a 24
jebradshaw 0:67dd4036010a 25 private:
jebradshaw 0:67dd4036010a 26 void tickFunct(void); //Function that takes care of the servo pulse generation
jebradshaw 0:67dd4036010a 27 Ticker *ticker; //pointer to ticker object
jebradshaw 0:67dd4036010a 28 DigitalOut _pin; //make assigned pin a digital output
jebradshaw 0:67dd4036010a 29 int _sCycle; //flag to toggle for keeping track of servo cycle
jebradshaw 0:67dd4036010a 30 };
jebradshaw 0:67dd4036010a 31
jebradshaw 0:67dd4036010a 32 #endif