A class to control a model R/C servo, using a PwmOut
Dependents: Brute_TS_Controller_2018_11
Revision 6:917d115add54, committed 2018-05-09
- Comitter:
- JonFreeman
- Date:
- Wed May 09 17:11:54 2018 +0000
- Parent:
- 5:c7143247a1e7
- Commit message:
- changed all floats to doubles in servo to kill compiler warnings
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 |
diff -r c7143247a1e7 -r 917d115add54 Servo.cpp --- a/Servo.cpp Wed May 09 15:42:15 2018 +0000 +++ b/Servo.cpp Wed May 09 17:11:54 2018 +0000 @@ -24,7 +24,7 @@ #include "Servo.h" #include "mbed.h" -static float clamp(float value, float min, float max) { +static double clamp(double value, double min, double max) { if(value < min) { return min; } else if(value > max) { @@ -39,27 +39,27 @@ // write(0.5); } -void Servo::write(float percent) { - float offset = _range * 2.0 * (percent - 0.5); +void Servo::write(double percent) { + double 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); +void Servo::position(double degrees) { + double offset = _range * (degrees / _degrees); _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range)); } -void Servo::calibrate(float range, float degrees) { +void Servo::calibrate(double range, double degrees) { _range = range; _degrees = degrees; } -float Servo::read() { +double Servo::read() { return _p; } -Servo& Servo::operator= (float percent) { +Servo& Servo::operator= (double percent) { write(percent); return *this; } @@ -69,6 +69,6 @@ return *this; } -Servo::operator float() { +Servo::operator double() { return read(); }
diff -r c7143247a1e7 -r 917d115add54 Servo.h --- a/Servo.h Wed May 09 15:42:15 2018 +0000 +++ b/Servo.h Wed May 09 17:11:54 2018 +0000 @@ -62,37 +62,37 @@ * * @param percent A normalised number 0.0-1.0 to represent the full range. */ - void write(float percent); + void write(double percent); /** Read the servo motors current position * * @param returns A normalised number 0.0-1.0 representing the full range. */ - float read(); + double read(); /** Set the servo position * * @param degrees Servo position in degrees */ - void position(float degrees); + void position(double degrees); /** Allows calibration of the range and angles for a particular servo * * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds * @param degrees Angle from centre to maximum/minimum position in degrees */ - void calibrate(float range = 0.0005, float degrees = 45.0); + void calibrate(double range = 0.0005, double degrees = 45.0); /** Shorthand for the write and read functions */ - Servo& operator= (float percent); + Servo& operator= (double percent); Servo& operator= (Servo& rhs); - operator float(); + operator double(); protected: PwmOut _pwm; - float _range; - float _degrees; - float _p; + double _range; + double _degrees; + double _p; }; #endif