Semi-Functional EtchSketch Smartbot code. Takes x/y values and draws with polar coords.

Committer:
theschrade54
Date:
Sat Dec 15 01:48:19 2012 +0000
Revision:
0:eae9712515a6
Semi-Functional EtchSketch Smartbot code. Takes x/y values and draws with polar coords.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
theschrade54 0:eae9712515a6 1 /* mbed R/C Servo Library
theschrade54 0:eae9712515a6 2 * Copyright (c) 2007-2010 sford, cstyles
theschrade54 0:eae9712515a6 3 *
theschrade54 0:eae9712515a6 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
theschrade54 0:eae9712515a6 5 * of this software and associated documentation files (the "Software"), to deal
theschrade54 0:eae9712515a6 6 * in the Software without restriction, including without limitation the rights
theschrade54 0:eae9712515a6 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
theschrade54 0:eae9712515a6 8 * copies of the Software, and to permit persons to whom the Software is
theschrade54 0:eae9712515a6 9 * furnished to do so, subject to the following conditions:
theschrade54 0:eae9712515a6 10 *
theschrade54 0:eae9712515a6 11 * The above copyright notice and this permission notice shall be included in
theschrade54 0:eae9712515a6 12 * all copies or substantial portions of the Software.
theschrade54 0:eae9712515a6 13 *
theschrade54 0:eae9712515a6 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
theschrade54 0:eae9712515a6 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
theschrade54 0:eae9712515a6 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
theschrade54 0:eae9712515a6 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
theschrade54 0:eae9712515a6 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
theschrade54 0:eae9712515a6 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
theschrade54 0:eae9712515a6 20 * THE SOFTWARE.
theschrade54 0:eae9712515a6 21 */
theschrade54 0:eae9712515a6 22
theschrade54 0:eae9712515a6 23 #ifndef MBED_SERVO_H
theschrade54 0:eae9712515a6 24 #define MBED_SERVO_H
theschrade54 0:eae9712515a6 25
theschrade54 0:eae9712515a6 26 #include "mbed.h"
theschrade54 0:eae9712515a6 27
theschrade54 0:eae9712515a6 28 /** Servo control class, based on a PwmOut
theschrade54 0:eae9712515a6 29 *
theschrade54 0:eae9712515a6 30 * Example:
theschrade54 0:eae9712515a6 31 * @code
theschrade54 0:eae9712515a6 32 * // Continuously sweep the servo through it's full range
theschrade54 0:eae9712515a6 33 * #include "mbed.h"
theschrade54 0:eae9712515a6 34 * #include "Servo.h"
theschrade54 0:eae9712515a6 35 *
theschrade54 0:eae9712515a6 36 * Servo myservo(p21);
theschrade54 0:eae9712515a6 37 *
theschrade54 0:eae9712515a6 38 * int main() {
theschrade54 0:eae9712515a6 39 * while(1) {
theschrade54 0:eae9712515a6 40 * for(int i=0; i<100; i++) {
theschrade54 0:eae9712515a6 41 * myservo = i/100.0;
theschrade54 0:eae9712515a6 42 * wait(0.01);
theschrade54 0:eae9712515a6 43 * }
theschrade54 0:eae9712515a6 44 * for(int i=100; i>0; i--) {
theschrade54 0:eae9712515a6 45 * myservo = i/100.0;
theschrade54 0:eae9712515a6 46 * wait(0.01);
theschrade54 0:eae9712515a6 47 * }
theschrade54 0:eae9712515a6 48 * }
theschrade54 0:eae9712515a6 49 * }
theschrade54 0:eae9712515a6 50 * @endcode
theschrade54 0:eae9712515a6 51 */
theschrade54 0:eae9712515a6 52 class Servo {
theschrade54 0:eae9712515a6 53
theschrade54 0:eae9712515a6 54 public:
theschrade54 0:eae9712515a6 55 /** Create a servo object connected to the specified PwmOut pin
theschrade54 0:eae9712515a6 56 *
theschrade54 0:eae9712515a6 57 * @param pin PwmOut pin to connect to
theschrade54 0:eae9712515a6 58 */
theschrade54 0:eae9712515a6 59 Servo(PinName pin);
theschrade54 0:eae9712515a6 60
theschrade54 0:eae9712515a6 61 /** Set the servo position, normalised to it's full range
theschrade54 0:eae9712515a6 62 *
theschrade54 0:eae9712515a6 63 * @param percent A normalised number 0.0-1.0 to represent the full range.
theschrade54 0:eae9712515a6 64 */
theschrade54 0:eae9712515a6 65 void write(float percent);
theschrade54 0:eae9712515a6 66
theschrade54 0:eae9712515a6 67 /** Read the servo motors current position
theschrade54 0:eae9712515a6 68 *
theschrade54 0:eae9712515a6 69 * @param returns A normalised number 0.0-1.0 representing the full range.
theschrade54 0:eae9712515a6 70 */
theschrade54 0:eae9712515a6 71 float read();
theschrade54 0:eae9712515a6 72
theschrade54 0:eae9712515a6 73 /** Set the servo position
theschrade54 0:eae9712515a6 74 *
theschrade54 0:eae9712515a6 75 * @param degrees Servo position in degrees
theschrade54 0:eae9712515a6 76 */
theschrade54 0:eae9712515a6 77 void position(float degrees);
theschrade54 0:eae9712515a6 78
theschrade54 0:eae9712515a6 79 /** Allows calibration of the range and angles for a particular servo
theschrade54 0:eae9712515a6 80 *
theschrade54 0:eae9712515a6 81 * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds
theschrade54 0:eae9712515a6 82 * @param degrees Angle from centre to maximum/minimum position in degrees
theschrade54 0:eae9712515a6 83 */
theschrade54 0:eae9712515a6 84 void calibrate(float range = 0.0005, float degrees = 45.0);
theschrade54 0:eae9712515a6 85
theschrade54 0:eae9712515a6 86 /** Shorthand for the write and read functions */
theschrade54 0:eae9712515a6 87 Servo& operator= (float percent);
theschrade54 0:eae9712515a6 88 Servo& operator= (Servo& rhs);
theschrade54 0:eae9712515a6 89 operator float();
theschrade54 0:eae9712515a6 90
theschrade54 0:eae9712515a6 91 protected:
theschrade54 0:eae9712515a6 92 PwmOut _pwm;
theschrade54 0:eae9712515a6 93 float _range;
theschrade54 0:eae9712515a6 94 float _degrees;
theschrade54 0:eae9712515a6 95 float _p;
theschrade54 0:eae9712515a6 96 };
theschrade54 0:eae9712515a6 97
theschrade54 0:eae9712515a6 98 #endif