A simple library for driving RC servos without using the mbed's PWM functions. This allows the mbed to drive as many servos as there are DigitalOut pins, and additionally allows for the PWM functions to be used at a different frequency than the 50Hz used for servos.

Files at this revision

API Documentation at this revision

Comitter:
pclary
Date:
Thu Jan 17 18:32:18 2013 +0000
Parent:
14:0eff26aa0d17
Commit message:
Added ability to disable servos and select whether they start enabled or disabled.

Changed in this revision

Servo.cpp Show annotated file Show diff for this revision Revisions of this file
Servo.h Show annotated file Show diff for this revision Revisions of this file
--- a/Servo.cpp	Wed Jan 09 21:08:53 2013 +0000
+++ b/Servo.cpp	Thu Jan 17 18:32:18 2013 +0000
@@ -17,7 +17,7 @@
 // the initial pulse width can be omitted, giving a default of 
 // 1500 us. 
 // This should be at about half the range of most servos.
-Servo::Servo(PinName pin) : signalPin(pin)
+Servo::Servo(PinName pin, bool start) : signalPin(pin)
 {
     // Set default calibration
     calibrate(2000, 1000, 60.f, -60.f);
@@ -33,6 +33,8 @@
     }
     
     servos[numServos++] = this;
+    
+    enabled = start;
 }
 
 
@@ -88,6 +90,20 @@
 
 
 
+void Servo::disable()
+{
+    enabled = false;
+}
+
+
+
+void Servo::enable()
+{
+    enabled = true;
+}
+
+
+
 void Servo::operator=(float degrees)
 {
     write(degrees);
@@ -114,7 +130,7 @@
     // Start all of the individual servo width timeouts and write a logical 1 to their signal pins
     for (unsigned int i = 0; i < numServos; i++)
     {
-        if (servos[i]->pulseWidth > 0)
+        if (servos[i]->enabled)
         {
             servos[i]->servoTimeout.attach_us(servos[i], &Servo::timeout, servos[i]->pulseWidth);
             Servo::servos[i]->signalPin.write(1);
--- a/Servo.h	Wed Jan 09 21:08:53 2013 +0000
+++ b/Servo.h	Thu Jan 17 18:32:18 2013 +0000
@@ -8,12 +8,14 @@
 class Servo
 {
 public:
-    Servo(PinName pin);
+    Servo(PinName pin, bool start = true);
     bool calibrate(unsigned int plus45, unsigned int minus45, float upperLimit, float lowerLimit);
     void write(float degrees);
     void writeWidth(unsigned int width);
     float read();
     int readWidth();
+    void disable();
+    void enable();
     void operator=(float degrees);
     operator float();
     operator int();
@@ -31,6 +33,7 @@
     Timeout servoTimeout;
     unsigned int pulseWidth;
     DigitalOut signalPin;
+    bool enabled;