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