Joseph Bradshaw / SERVOGEN

Dependents:   mbed_ES200_Tester_SERVOGEN mbed_ES20X_Thread_Test

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SERVOGEN.h Source File

SERVOGEN.h

00001 // J. Bradshaw 20140929
00002 /** SERVOGEN Class for Generating servo pulses on single output pin
00003  * Copyright (c) 2014, jbradshaw (http://mbed.org)
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in
00013  * all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021  * THE SOFTWARE.
00022  *
00023 *
00024 * Example:
00025 * @code
00026 *#include "mbed.h"
00027 *#include "SERVOGEN.h" 
00028 * 
00029 *SERVOGEN servo1(p21);
00030 *SERVOGEN servo2(p24);
00031 *DigitalOut led1(LED1);
00032 * 
00033 *int main() {
00034 *    servo1.pulse_us = 0;        
00035 *    // spin in a main loop. flipper will interrupt it to call flip
00036 *    while(1) {
00037 *        for(float cycle=0.0;cycle<2.0*3.14159;cycle+=.003){
00038 *            //small amplitude sine wave on servo1
00039 *            servo1.pulse_us = 300 * sin(cycle) + 1500;            
00040 *            //ramp up the second servo2 channel relative to cycle
00041 *            servo2.pulse_us = 1000 + cycle*159; // .001/(2*PI)
00042 *            
00043 *            wait(.001); //short delay
00044 *        }
00045 *        led1 = !led1;   //toggle led1 to indicate activity
00046 *    }//while(1)
00047 *}//main
00048 *
00049 *@endcode
00050 */
00051 
00052 #ifndef MBED_SERVOGEN_H
00053 #define MBED_SERVOGEN_H
00054  
00055 #include "mbed.h"
00056 
00057 /**
00058  * SERVOGEN Class.
00059  */
00060 
00061 class SERVOGEN {
00062 public:
00063     /**
00064      * Constructor.
00065      *
00066      * @param pin - servo pulse output pin
00067      */         
00068      SERVOGEN(PinName pin);
00069     
00070     int pulse_us;       //determines the pulse width in microseconds (default is 0 = OFF)
00071     int pulseMin;       //minimum pulse width (default is 900 microseconds)
00072     int pulseMax;       //maximum pulse width (default is 2100 microseconds)
00073     
00074     void write(int pulse);  //
00075     int read(void);
00076     
00077         /**  Shorthand for the write and read functions */
00078     SERVOGEN& operator= (int pulse_us);
00079     SERVOGEN& operator= (SERVOGEN& rhs);
00080     operator int();
00081   
00082 private:
00083     void tickFunct(void);   //Function that takes care of the servo pulse generation
00084     Ticker *ticker;         //pointer to ticker object
00085     DigitalOut _pin;        //make assigned pin a digital output
00086     int _sCycle;            //flag to toggle for keeping track of servo cycle
00087 };
00088  
00089 #endif