A class to control a model R/C servo, using a PwmOut
Fork of Servo by
Revision 0:24148c673250, committed 2009-11-16
- Comitter:
- simon
- Date:
- Mon Nov 16 18:24:16 2009 +0000
- Child:
- 1:92f46d402550
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.cpp Mon Nov 16 18:24:16 2009 +0000 @@ -0,0 +1,56 @@ +/* mbed R/C Servo + * Copyright (c) 2007-2009 sford, cstyles + * Released under the MIT License: http://mbed.org/license/mit + */ + +#include "Servo.h" +#include "mbed.h" + +static float clamp(float value, float min, float max) { + if(value < min) { + return min; + } else if(value > max) { + return max; + } else { + return value; + } +} + +Servo::Servo(PinName pin) : _pwm(pin) { + calibrate(); + write(0.5); +} + +void Servo::write(float percent) { + float offset = _range * 2.0 * (percent - 0.5); + _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range)); + _p = clamp(percent, 0.0, 1.0); +} + +void Servo::position(float degrees) { + float offset = _range * (degrees / _degrees); + _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range)); +} + +void Servo::calibrate(float range, float degrees) { + _range = range; + _degrees = degrees; +} + +float Servo::read() { + return _p; +} + +Servo& Servo::operator= (float percent) { + write(percent); + return *this; +} + +Servo& Servo::operator= (Servo& rhs) { + write(rhs.read()); + return *this; +} + +Servo::operator float() { + return read(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.h Mon Nov 16 18:24:16 2009 +0000 @@ -0,0 +1,95 @@ +/* mbed R/C Servo + * Copyright (c) 2007-2009 sford, cstyles + * Released under the MIT License: http://mbed.org/license/mit + */ + +#ifndef MBED_SERVO_H +#define MBED_SERVO_H + +#include "mbed.h" + +/* Class: Servo + * Abstraction on top of PwmOut to control the position of a servo motor + * + * Example: + * > // Continuously sweep the servo through it's full range + * > #include "mbed.h" + * > #include "Servo.h" + * > + * > Servo myservo(p21); + * > + * > int main() { + * > while(1) { + * > for(int i=0; i<100; i++) { + * > myservo = i/100.0; + * > wait(0.01); + * > } + * > for(int i=100; i>0; i--) { + * > myservo = i/100.0; + * > wait(0.01); + * > } + * > } + * > } + */ +class Servo { + +public: + /* Constructor: Servo + * Create a servo object connected to the specified PwmOut pin + * + * Variables: + * pin - PwmOut pin to connect to + */ + Servo(PinName pin); + + /* Function: write + * Set the servo position, normalised to it's full range + * + * Variables: + * percent - A normalised number 0.0-1.0 to represent the full range. + */ + void write(float percent); + + /* Function: read + * Read the servo motors current position + * + * Variables: + * returns - A normalised number 0.0-1.0 representing the full range. + */ + float read(); + + /* Function: position + * Set the servo position + * + * Variables: + * degrees - Servo position in degrees + */ + void position(float degrees); + + /* Function: calibrate + * Allows calibration of the range and angles for a particular servo + * + * + * Variables: + * range - Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds + * degrees - Angle from centre to maximum/minimum position in degrees + */ + void calibrate(float range = 0.0005, float degrees = 45.0); + + /* Function: operator= + * Shorthand for the write and read functions + */ + Servo& operator= (float percent); + Servo& operator= (Servo& rhs); + operator float(); + +protected: + PwmOut _pwm; + float _range; + float _degrees; + float _p; +}; + + + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Nov 16 18:24:16 2009 +0000 @@ -0,0 +1,27 @@ +#include "mbed.h" +#include "Servo.h" + +Servo myservo(p21); +Serial pc(USBTX, USBRX); + +int main() { + printf("Servo Calibration Controls:\n"); + printf("1,2,3 - Position Servo (full left, middle, full right)\n"); + printf("4,5 - Decrease or Increase range\n"); + + float range = 0.0005; + float position = 0.5; + + while(1) { + switch(pc.getc()) { + case '1': position = 0.0; break; + case '2': position = 0.5; break; + case '3': position = 1.0; break; + case '4': range += 0.0001; break; + case '5': range -= 0.0001; break; + } + printf("position = %.1f, range = +/-%0.4f\n", position, range); + myservo.calibrate(range, 45.0); + myservo = position; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Nov 16 18:24:16 2009 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/737756e0b479