servo

Committer:
wisnup
Date:
Thu Feb 20 04:14:03 2014 +0000
Revision:
0:7fd570dc03ee
servo

Who changed what in which revision?

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