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