Servo HelloWorld
Dependents: Servo_Motor_Button
Fork of Servo by
Servo.h@0:24148c673250, 2009-11-16 (annotated)
- Committer:
- simon
- Date:
- Mon Nov 16 18:24:16 2009 +0000
- Revision:
- 0:24148c673250
- Child:
- 2:8995c167f399
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:24148c673250 | 1 | /* mbed R/C Servo |
simon | 0:24148c673250 | 2 | * Copyright (c) 2007-2009 sford, cstyles |
simon | 0:24148c673250 | 3 | * Released under the MIT License: http://mbed.org/license/mit |
simon | 0:24148c673250 | 4 | */ |
simon | 0:24148c673250 | 5 | |
simon | 0:24148c673250 | 6 | #ifndef MBED_SERVO_H |
simon | 0:24148c673250 | 7 | #define MBED_SERVO_H |
simon | 0:24148c673250 | 8 | |
simon | 0:24148c673250 | 9 | #include "mbed.h" |
simon | 0:24148c673250 | 10 | |
simon | 0:24148c673250 | 11 | /* Class: Servo |
simon | 0:24148c673250 | 12 | * Abstraction on top of PwmOut to control the position of a servo motor |
simon | 0:24148c673250 | 13 | * |
simon | 0:24148c673250 | 14 | * Example: |
simon | 0:24148c673250 | 15 | * > // Continuously sweep the servo through it's full range |
simon | 0:24148c673250 | 16 | * > #include "mbed.h" |
simon | 0:24148c673250 | 17 | * > #include "Servo.h" |
simon | 0:24148c673250 | 18 | * > |
simon | 0:24148c673250 | 19 | * > Servo myservo(p21); |
simon | 0:24148c673250 | 20 | * > |
simon | 0:24148c673250 | 21 | * > int main() { |
simon | 0:24148c673250 | 22 | * > while(1) { |
simon | 0:24148c673250 | 23 | * > for(int i=0; i<100; i++) { |
simon | 0:24148c673250 | 24 | * > myservo = i/100.0; |
simon | 0:24148c673250 | 25 | * > wait(0.01); |
simon | 0:24148c673250 | 26 | * > } |
simon | 0:24148c673250 | 27 | * > for(int i=100; i>0; i--) { |
simon | 0:24148c673250 | 28 | * > myservo = i/100.0; |
simon | 0:24148c673250 | 29 | * > wait(0.01); |
simon | 0:24148c673250 | 30 | * > } |
simon | 0:24148c673250 | 31 | * > } |
simon | 0:24148c673250 | 32 | * > } |
simon | 0:24148c673250 | 33 | */ |
simon | 0:24148c673250 | 34 | class Servo { |
simon | 0:24148c673250 | 35 | |
simon | 0:24148c673250 | 36 | public: |
simon | 0:24148c673250 | 37 | /* Constructor: Servo |
simon | 0:24148c673250 | 38 | * Create a servo object connected to the specified PwmOut pin |
simon | 0:24148c673250 | 39 | * |
simon | 0:24148c673250 | 40 | * Variables: |
simon | 0:24148c673250 | 41 | * pin - PwmOut pin to connect to |
simon | 0:24148c673250 | 42 | */ |
simon | 0:24148c673250 | 43 | Servo(PinName pin); |
simon | 0:24148c673250 | 44 | |
simon | 0:24148c673250 | 45 | /* Function: write |
simon | 0:24148c673250 | 46 | * Set the servo position, normalised to it's full range |
simon | 0:24148c673250 | 47 | * |
simon | 0:24148c673250 | 48 | * Variables: |
simon | 0:24148c673250 | 49 | * percent - A normalised number 0.0-1.0 to represent the full range. |
simon | 0:24148c673250 | 50 | */ |
simon | 0:24148c673250 | 51 | void write(float percent); |
simon | 0:24148c673250 | 52 | |
simon | 0:24148c673250 | 53 | /* Function: read |
simon | 0:24148c673250 | 54 | * Read the servo motors current position |
simon | 0:24148c673250 | 55 | * |
simon | 0:24148c673250 | 56 | * Variables: |
simon | 0:24148c673250 | 57 | * returns - A normalised number 0.0-1.0 representing the full range. |
simon | 0:24148c673250 | 58 | */ |
simon | 0:24148c673250 | 59 | float read(); |
simon | 0:24148c673250 | 60 | |
simon | 0:24148c673250 | 61 | /* Function: position |
simon | 0:24148c673250 | 62 | * Set the servo position |
simon | 0:24148c673250 | 63 | * |
simon | 0:24148c673250 | 64 | * Variables: |
simon | 0:24148c673250 | 65 | * degrees - Servo position in degrees |
simon | 0:24148c673250 | 66 | */ |
simon | 0:24148c673250 | 67 | void position(float degrees); |
simon | 0:24148c673250 | 68 | |
simon | 0:24148c673250 | 69 | /* Function: calibrate |
simon | 0:24148c673250 | 70 | * Allows calibration of the range and angles for a particular servo |
simon | 0:24148c673250 | 71 | * |
simon | 0:24148c673250 | 72 | * |
simon | 0:24148c673250 | 73 | * Variables: |
simon | 0:24148c673250 | 74 | * range - Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds |
simon | 0:24148c673250 | 75 | * degrees - Angle from centre to maximum/minimum position in degrees |
simon | 0:24148c673250 | 76 | */ |
simon | 0:24148c673250 | 77 | void calibrate(float range = 0.0005, float degrees = 45.0); |
simon | 0:24148c673250 | 78 | |
simon | 0:24148c673250 | 79 | /* Function: operator= |
simon | 0:24148c673250 | 80 | * Shorthand for the write and read functions |
simon | 0:24148c673250 | 81 | */ |
simon | 0:24148c673250 | 82 | Servo& operator= (float percent); |
simon | 0:24148c673250 | 83 | Servo& operator= (Servo& rhs); |
simon | 0:24148c673250 | 84 | operator float(); |
simon | 0:24148c673250 | 85 | |
simon | 0:24148c673250 | 86 | protected: |
simon | 0:24148c673250 | 87 | PwmOut _pwm; |
simon | 0:24148c673250 | 88 | float _range; |
simon | 0:24148c673250 | 89 | float _degrees; |
simon | 0:24148c673250 | 90 | float _p; |
simon | 0:24148c673250 | 91 | }; |
simon | 0:24148c673250 | 92 | |
simon | 0:24148c673250 | 93 | |
simon | 0:24148c673250 | 94 | |
simon | 0:24148c673250 | 95 | #endif |