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

Files at this revision

API Documentation at this revision

Comitter:
jebradshaw
Date:
Mon Sep 29 16:47:31 2014 +0000
Parent:
2:44766ee77ce0
Commit message:
Added operator accessible read/writes to simplify use; J Bradshaw 20140929

Changed in this revision

SERVOGEN.cpp Show annotated file Show diff for this revision Revisions of this file
SERVOGEN.h Show annotated file Show diff for this revision Revisions of this file
diff -r 44766ee77ce0 -r edc18509ff11 SERVOGEN.cpp
--- a/SERVOGEN.cpp	Sun Sep 28 18:57:11 2014 +0000
+++ b/SERVOGEN.cpp	Mon Sep 29 16:47:31 2014 +0000
@@ -60,4 +60,26 @@
             _sCycle=1;
         }
     } //pulse != 0 (OFF)
+}
+
+void SERVOGEN::write(int pulse){
+    pulse_us = pulse;    
+}
+
+int SERVOGEN::read(){
+    return pulse_us;    
+}
+
+SERVOGEN& SERVOGEN::operator= (int pulse_us) { 
+    write(pulse_us);
+    return *this;
+}
+ 
+SERVOGEN& SERVOGEN::operator= (SERVOGEN& rhs) {
+    write(rhs.read());
+    return *this;
+}
+ 
+SERVOGEN::operator int() {
+    return read();
 }
\ No newline at end of file
diff -r 44766ee77ce0 -r edc18509ff11 SERVOGEN.h
--- a/SERVOGEN.h	Sun Sep 28 18:57:11 2014 +0000
+++ b/SERVOGEN.h	Mon Sep 29 16:47:31 2014 +0000
@@ -1,4 +1,4 @@
-// J. Bradshaw 20140925
+// J. Bradshaw 20140929
 /** SERVOGEN Class for Generating servo pulses on single output pin
  * Copyright (c) 2014, jbradshaw (http://mbed.org)
  *
@@ -70,6 +70,14 @@
     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)
+    
+    void write(int pulse);  //
+    int read(void);
+    
+        /**  Shorthand for the write and read functions */
+    SERVOGEN& operator= (int pulse_us);
+    SERVOGEN& operator= (SERVOGEN& rhs);
+    operator int();
   
 private:
     void tickFunct(void);   //Function that takes care of the servo pulse generation