Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: Servo.cpp
- Revision:
- 0:1fc280fa2177
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.cpp Wed Jun 02 21:40:10 2010 +0000
@@ -0,0 +1,79 @@
+#include "Servo.h"
+
+Servo::Servo(PinName port) : PwmOut(port)
+{
+ period = 20000;
+ maximum = 2400;
+ minimum = 600;
+ center = 1500;
+ position = 0;
+ //set default values
+ pulsewidth_us(0);
+ period_us(period);
+}
+
+void Servo::setposition(int input)
+{
+ position = input;
+ if(position > 255)
+ {
+ position = 255;
+ }
+ else if (position < -255)
+ {
+ position = -255;
+ }
+ //saturate
+ if(position >= 0)
+ {
+ pulsewidth_us(position * (maximum - center) / 255 + center);
+ }
+ else
+ {
+ pulsewidth_us(position * (center - minimum) / 255 + center);
+ }
+ //set position normalized to 255
+}
+
+void Servo::setperiod(int input)
+{
+ period = input;
+ period_us(period);
+}
+
+void Servo::setcenter(int input)
+{
+ center = input;
+}
+
+void Servo::setmaximum(int input)
+{
+ maximum = input;
+}
+
+void Servo::setminimum(int input)
+{
+ minimum = input;
+}
+
+Servo& Servo::operator =(int assignment)
+{
+ Servo::setposition(assignment);
+ //shorthand for setposition
+ *this = assignment;
+ return *this;
+ //allow multiple assignment
+}
+
+Servo::operator int()
+{
+ int measurement = read() * period;
+ if(measurement >= center)
+ {
+ return 255 * (measurement - center) / (maximum - center);
+ }
+ else
+ {
+ return 255 * (measurement - position) / (center - minimum);
+ }
+}
\ No newline at end of file