Description

Dependents:   RoboPanorama

Fork of Servo by Simon Ford

Committer:
lndv3
Date:
Fri Oct 12 16:41:39 2012 +0000
Revision:
4:2176f8595b34
Parent:
3:36b69a7ced07
Commit.

Who changed what in which revision?

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