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
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.h
- Committer:
- jebradshaw
- Date:
- 2014-09-26
- Revision:
- 1:7d669a801bb0
- Parent:
- 0:67dd4036010a
- Child:
- 2:44766ee77ce0
File content as of revision 1:7d669a801bb0:
// J. Bradshaw 20140925 /** SERVOGEN Class for Generating servo pulses on single output pin * * Example: * @code *#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.pulse_us = 300 * sin(cycle) + 1500; * //ramp up the second servo2 channel relative to cycle * servo2.pulse_us = 1000 + cycle*159; // .001/(2*PI) * * wait(.001); //short delay * } * led1 = !led1; //toggle led1 to indicate activity * }//while(1) *}//main * *@endcode */ #ifndef MBED_SERVOGEN_H #define MBED_SERVOGEN_H #include "mbed.h" /** * SERVOGEN Class. */ class SERVOGEN { public: /** * Constructor. * * @param pin - servo pulse output pin */ SERVOGEN(PinName pin); int pulse_us; //determines the pulse width in microseconds (default is 0 = OFF) int pulseMin; //minimum pulse width (default is 900 microseconds) int pulseMax; //maximum pulse width (default is 2100 microseconds) private: void tickFunct(void); //Function that takes care of the servo pulse generation Ticker *ticker; //pointer to ticker object DigitalOut _pin; //make assigned pin a digital output int _sCycle; //flag to toggle for keeping track of servo cycle }; #endif