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.
Revision 0:39cd1e782d1e, committed 2010-10-18
- Comitter:
- jcrx
- Date:
- Mon Oct 18 13:17:22 2010 +0000
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.cpp Mon Oct 18 13:17:22 2010 +0000
@@ -0,0 +1,74 @@
+/* mbed R/C Servo Library
+ *
+ * Copyright (c) 2007-2010 sford, cstyles
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#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 Oct 18 13:17:22 2010 +0000
@@ -0,0 +1,98 @@
+/* mbed R/C Servo Library
+ * Copyright (c) 2007-2010 sford, cstyles
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MBED_SERVO_H
+#define MBED_SERVO_H
+
+#include "mbed.h"
+
+/** Servo control class, based on a PwmOut
+ *
+ * Example:
+ * @code
+ * // 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);
+ * }
+ * }
+ * }
+ * @endcode
+ */
+class Servo {
+
+public:
+ /** Create a servo object connected to the specified PwmOut pin
+ *
+ * @param pin PwmOut pin to connect to
+ */
+ Servo(PinName pin);
+
+ /** Set the servo position, normalised to it's full range
+ *
+ * @param percent A normalised number 0.0-1.0 to represent the full range.
+ */
+ void write(float percent);
+
+ /** Read the servo motors current position
+ *
+ * @param returns A normalised number 0.0-1.0 representing the full range.
+ */
+ float read();
+
+ /** Set the servo position
+ *
+ * @param degrees Servo position in degrees
+ */
+ void position(float 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);
+
+ /** 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 Oct 18 13:17:22 2010 +0000
@@ -0,0 +1,66 @@
+/* just a test for "mbed R/C Servo Library"
+ * send from PC to mbed a string like ":XXYY"<RC>
+ * where XX is the hexa value [00..FF ] (0..255) of Servo1
+ * and YY is the hexa value [00..FF ] (0..255) of Servo2
+ * also "V"<RC> gives the version.
+ */
+
+#include "mbed.h"
+#include "Servo.h"
+
+PwmOut led_1(LED1);
+PwmOut led_2(LED2);
+
+Servo servo_1(p21);
+Servo servo_2(p22);
+
+Serial rs_pc(USBTX, USBRX); // virtual com (tx, rx)
+
+// convert 2 ascii chars hex format ("00".."FF") -> 1 unsigned char (0..255)
+unsigned char hex_bin (unsigned char *ptr )
+{
+ unsigned char temp;
+
+ if (*ptr>='0' && *ptr<='9') temp = *ptr & 0x0F;
+ else temp = (*ptr & 0x0F) + 9;
+ temp <<= 4;
+ if (*(ptr+1)>='0' && *(ptr+1)<='9') temp |= *(ptr+1) & 0x0F;
+ else temp |= (*(ptr+1) & 0x0F) + 9;
+ return temp;
+}
+
+int main() {
+ unsigned char i=0;
+ float p;
+ unsigned char c;
+ unsigned char cmd[6];
+ while(1) {
+ if(rs_pc.readable()) { // char on rs_pc
+ c = rs_pc.getc();
+ if (c==13){ // end of tram
+ if (cmd[0]== ':'){ // cmd servos
+ rs_pc.putc(13); // ok
+ // servo 1
+ p = hex_bin(&cmd[1])/255.0; // 0.00 (min) -> 1.00 (max)
+ servo_1 = p;
+ led_1 = p; // just for eyes
+ // servo 2
+ p = hex_bin(&cmd[3])/255.0; // 0.00 (min) -> 1.00 (max)
+ servo_2 = p;
+ led_2 = p;
+ }
+ if (cmd[0]== 'V'){ // version
+ rs_pc.printf("Servos test V1.01.a\r");
+ }
+ i=0;
+ }else{
+ if (c==':') i=0; // start of tram
+ cmd[i++]=c;
+ if (i>5){
+ rs_pc.printf("Wrong command\r");
+ i=0;
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Oct 18 13:17:22 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e