Joseph Bradshaw / SERVOGEN

Dependents:   mbed_ES200_Tester_SERVOGEN mbed_ES20X_Thread_Test

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SERVOGEN.cpp Source File

SERVOGEN.cpp

00001 // Uses attach_us to generate servo pulse on single output pin
00002 // J. Bradshaw 20140925
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 #include "mbed.h"
00025 #include "SERVOGEN.h"
00026 
00027 SERVOGEN::SERVOGEN(PinName pin) : _pin(pin) {
00028     _pin = 0;           // Initialize output pin as off
00029     _sCycle = 0;        // initialize servo cycle as 0
00030     pulseMin=900;       // default minimum (except 0) servo pulse width
00031     pulseMax=2100;      // maximum servo pulse width
00032     pulse_us=0;         // Initialize servo pulse width as 0
00033     ticker = new Ticker();  // instantiate new Ticker object
00034     ticker->attach_us(this,&SERVOGEN::tickFunct,20000); //begin tickFunction in 20ms
00035 }
00036 
00037 void SERVOGEN::tickFunct(void){
00038     
00039     if(pulse_us == 0){  //first check to see if the pulse width is 0 (OFF, servo not powered)
00040         _pin=0;         // disable servo pulse output
00041         ticker->attach_us(this,&SERVOGEN::tickFunct,20000); // recall this function in 20ms
00042         return;
00043     }
00044     
00045     else{        
00046         if(pulse_us < pulseMin)     //keep within bounds
00047             pulse_us = pulseMin;
00048         if(pulse_us > pulseMax)
00049             pulse_us = pulseMax;
00050         
00051         if(_sCycle){   //mode == 1
00052             //pulse was high, now drop low for remaining time
00053             ticker->attach_us(this,&SERVOGEN::tickFunct,20000-pulse_us);; // recall this function in 20ms - servo pulse width (servo pulse low)
00054             _pin=0;    
00055             _sCycle=0;
00056         }
00057         else{   //mode == 0
00058             ticker->attach_us(this,&SERVOGEN::tickFunct,pulse_us);; // recall this function in 20ms - pulse width (servo pulse high)
00059             _pin=1;        
00060             _sCycle=1;
00061         }
00062     } //pulse != 0 (OFF)
00063 }
00064 
00065 void SERVOGEN::write(int pulse){
00066     pulse_us = pulse;    
00067 }
00068 
00069 int SERVOGEN::read(){
00070     return pulse_us;    
00071 }
00072 
00073 SERVOGEN& SERVOGEN::operator= (int pulse_us) { 
00074     write(pulse_us);
00075     return *this;
00076 }
00077  
00078 SERVOGEN& SERVOGEN::operator= (SERVOGEN& rhs) {
00079     write(rhs.read());
00080     return *this;
00081 }
00082  
00083 SERVOGEN::operator int() {
00084     return read();
00085 }