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