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.
Diff: Servo.cpp
- Revision:
- 15:55221f9de707
- Parent:
- 14:0eff26aa0d17
--- 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);