Servo
Fork of Servo by
Servo.h@4:32985ec2fe2d, 2015-09-11 (annotated)
- Committer:
- pierro42100
- Date:
- Fri Sep 11 11:02:32 2015 +0000
- Revision:
- 4:32985ec2fe2d
- Parent:
- 3:36b69a7ced07
test
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 | */ |
pierro42100 | 4:32985ec2fe2d | 52 | |
pierro42100 | 4:32985ec2fe2d | 53 | //Définition de la classe Servo |
simon | 0:24148c673250 | 54 | class Servo { |
simon | 0:24148c673250 | 55 | |
simon | 0:24148c673250 | 56 | public: |
pierro42100 | 4:32985ec2fe2d | 57 | /** Création d'un objet servo associé à la pin (broche) indiquée |
simon | 0:24148c673250 | 58 | * |
simon | 3:36b69a7ced07 | 59 | * @param pin PwmOut pin to connect to |
simon | 0:24148c673250 | 60 | */ |
simon | 0:24148c673250 | 61 | Servo(PinName pin); |
simon | 0:24148c673250 | 62 | |
pierro42100 | 4:32985ec2fe2d | 63 | /** Imposer au servo une position. Cette position est normalisée (pourcentage), par rapport à son amplitude max. |
simon | 0:24148c673250 | 64 | * |
simon | 3:36b69a7ced07 | 65 | * @param percent A normalised number 0.0-1.0 to represent the full range. |
simon | 0:24148c673250 | 66 | */ |
simon | 0:24148c673250 | 67 | void write(float percent); |
simon | 0:24148c673250 | 68 | |
pierro42100 | 4:32985ec2fe2d | 69 | /** Renvoit la position courante du servo |
simon | 0:24148c673250 | 70 | * |
simon | 3:36b69a7ced07 | 71 | * @param returns A normalised number 0.0-1.0 representing the full range. |
simon | 0:24148c673250 | 72 | */ |
simon | 0:24148c673250 | 73 | float read(); |
simon | 0:24148c673250 | 74 | |
pierro42100 | 4:32985ec2fe2d | 75 | /** Impose au servo une position en degrés |
simon | 0:24148c673250 | 76 | * |
simon | 3:36b69a7ced07 | 77 | * @param degrees Servo position in degrees |
simon | 0:24148c673250 | 78 | */ |
simon | 0:24148c673250 | 79 | void position(float degrees); |
simon | 0:24148c673250 | 80 | |
simon | 3:36b69a7ced07 | 81 | /** Allows calibration of the range and angles for a particular servo |
simon | 0:24148c673250 | 82 | * |
simon | 3:36b69a7ced07 | 83 | * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds |
simon | 3:36b69a7ced07 | 84 | * @param degrees Angle from centre to maximum/minimum position in degrees |
simon | 0:24148c673250 | 85 | */ |
simon | 0:24148c673250 | 86 | void calibrate(float range = 0.0005, float degrees = 45.0); |
simon | 0:24148c673250 | 87 | |
simon | 3:36b69a7ced07 | 88 | /** Shorthand for the write and read functions */ |
simon | 0:24148c673250 | 89 | Servo& operator= (float percent); |
simon | 0:24148c673250 | 90 | Servo& operator= (Servo& rhs); |
simon | 0:24148c673250 | 91 | operator float(); |
simon | 0:24148c673250 | 92 | |
pierro42100 | 4:32985ec2fe2d | 93 | //Attributs |
pierro42100 | 4:32985ec2fe2d | 94 | |
simon | 0:24148c673250 | 95 | protected: |
pierro42100 | 4:32985ec2fe2d | 96 | PwmOut _pwm;//la broche (pin) |
pierro42100 | 4:32985ec2fe2d | 97 | float _range;//l'amplitude |
pierro42100 | 4:32985ec2fe2d | 98 | float _degrees;//la position en degrés |
pierro42100 | 4:32985ec2fe2d | 99 | float _p; //la position relative |
simon | 0:24148c673250 | 100 | }; |
simon | 0:24148c673250 | 101 | |
simon | 0:24148c673250 | 102 | #endif |