SERVO PARA QUADCOPTER

Dependents:   quadcopter servo1 Elevator

Committer:
cr0n0s20
Date:
Tue Feb 22 22:07:28 2011 +0000
Revision:
0:5490c22f727d
1.0

Who changed what in which revision?

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