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.

Committer:
pclary
Date:
Thu Jan 17 18:32:18 2013 +0000
Revision:
15:55221f9de707
Parent:
14:0eff26aa0d17
Added ability to disable servos and select whether they start enabled or disabled.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
UCSBRobotics 7:ff85ac12e11b 1 #ifndef SERVO_H
UCSBRobotics 7:ff85ac12e11b 2 #define SERVO_H
UCSBRobotics 7:ff85ac12e11b 3
UCSBRobotics 7:ff85ac12e11b 4 #include "mbed.h"
UCSBRobotics 7:ff85ac12e11b 5
UCSBRobotics 7:ff85ac12e11b 6
UCSBRobotics 7:ff85ac12e11b 7
UCSBRobotics 7:ff85ac12e11b 8 class Servo
UCSBRobotics 7:ff85ac12e11b 9 {
UCSBRobotics 7:ff85ac12e11b 10 public:
pclary 15:55221f9de707 11 Servo(PinName pin, bool start = true);
pclary 13:896410a1b594 12 bool calibrate(unsigned int plus45, unsigned int minus45, float upperLimit, float lowerLimit);
pclary 9:6bfea9af4dcb 13 void write(float degrees);
pclary 9:6bfea9af4dcb 14 void writeWidth(unsigned int width);
pclary 9:6bfea9af4dcb 15 float read();
pclary 9:6bfea9af4dcb 16 int readWidth();
pclary 15:55221f9de707 17 void disable();
pclary 15:55221f9de707 18 void enable();
pclary 9:6bfea9af4dcb 19 void operator=(float degrees);
pclary 9:6bfea9af4dcb 20 operator float();
UCSBRobotics 7:ff85ac12e11b 21 operator int();
UCSBRobotics 7:ff85ac12e11b 22
pclary 11:79862bf306c1 23 unsigned int center;
pclary 11:79862bf306c1 24 float usPerDegree;
pclary 11:79862bf306c1 25 float upperLimit;
pclary 11:79862bf306c1 26 float lowerLimit;
pclary 11:79862bf306c1 27
UCSBRobotics 7:ff85ac12e11b 28 protected:
UCSBRobotics 7:ff85ac12e11b 29 static unsigned int numServos;
UCSBRobotics 7:ff85ac12e11b 30 static Servo *servos[];
UCSBRobotics 7:ff85ac12e11b 31 static const unsigned int period = 20000;
UCSBRobotics 7:ff85ac12e11b 32 static Ticker* refreshTicker;
UCSBRobotics 7:ff85ac12e11b 33 Timeout servoTimeout;
UCSBRobotics 7:ff85ac12e11b 34 unsigned int pulseWidth;
UCSBRobotics 7:ff85ac12e11b 35 DigitalOut signalPin;
pclary 15:55221f9de707 36 bool enabled;
UCSBRobotics 7:ff85ac12e11b 37
pclary 11:79862bf306c1 38
pclary 9:6bfea9af4dcb 39
UCSBRobotics 7:ff85ac12e11b 40 static void refresh();
UCSBRobotics 7:ff85ac12e11b 41 void timeout();
UCSBRobotics 7:ff85ac12e11b 42 };
UCSBRobotics 7:ff85ac12e11b 43
UCSBRobotics 2:19f995979c6a 44 #endif // SERVO_H