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