Library to control servos
Servo.cpp@1:940dffb14ff6, 2017-10-21 (annotated)
- Committer:
- Cornelius Bezuidenhout
- Date:
- Sat Oct 21 21:32:00 2017 +0200
- Revision:
- 1:940dffb14ff6
- Parent:
- 0:6c0e56235914
- Child:
- 2:738b08cb03ea
Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cornelius Bezuidenhout |
1:940dffb14ff6 | 1 | #ifndef _SERVO_ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 2 | #define _SERVO_ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 3 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 4 | #include "mbed.h" |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 5 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 6 | /** |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 7 | * A Servo controller library :)s |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 8 | * |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 9 | * @author CA Bezuidenhout |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 10 | */ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 11 | class Servo { |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 12 | public: |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 13 | /** |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 14 | * @param pwmPin : PWM pin of servo / orange wire |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 15 | * @param zeroAngle : The zero angle of the servo |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 16 | * @param minAngle : The minimum angle allowed for the servo |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 17 | * @param maxAngle : The maximum angle allowed for the servo |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 18 | */ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 19 | Servo(PinName pwmPin, float zeroAngle = 0, float minAngle = -90.0f, float maxAngle = 90.0f); |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 20 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 21 | /** |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 22 | * Saves the current position as the zero position. |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 23 | */ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 24 | void SaveZero(); |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 25 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 26 | /** |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 27 | * Sets the zero position without moving the servo |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 28 | * |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 29 | * @param angle : The angle to set as the zero position |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 30 | */ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 31 | void SetZero(float angle); |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 32 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 33 | /** |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 34 | * Calibrate the servo |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 35 | * @param actualAngle : The actual angle from zero, of the current position in degrees. |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 36 | */ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 37 | void Calibrate(float actualAngle); |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 38 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 39 | /** |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 40 | * Sets the angular position of the servo |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 41 | * @param angle : Desired angle from the zero position in degrees |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 42 | */ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 43 | void Set(float angle); |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 44 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 45 | /** |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 46 | * Moves to an relative angular position from the current position |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 47 | * @param relAngle : The relative angle to move in degrees |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 48 | */ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 49 | void Move(float relAngle); |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 50 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 51 | /** |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 52 | * Disables control signal to servo |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 53 | */ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 54 | void Off(); |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 55 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 56 | /** |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 57 | * Gets the current angle of the servo |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 58 | * @returns Current angle of the servo |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 59 | */ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 60 | float GetAngle(); |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 61 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 62 | /** |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 63 | * Sets the minimum angle allowed for the servo |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 64 | * @params minAngle : minimum angle allowed |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 65 | */ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 66 | void SetMin(float minAngle); |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 67 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 68 | /** |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 69 | * Sets the maximum angle allowed for the servo |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 70 | * @params maxAngle : maximum angle allowed |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 71 | */ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 72 | void SetMax(float maxAngle); |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 73 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 74 | /** |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 75 | * Sets the servo to minimum position |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 76 | */ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 77 | void GotoMin(); |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 78 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 79 | /** |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 80 | * Sets the servo to maximum position |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 81 | */ |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 82 | void GotoMax(); |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 83 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 84 | private: |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 85 | PwmOut _servo; |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 86 | float _angle; |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 87 | float _ref; |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 88 | float _minAngle; |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 89 | float _maxAngle; |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 90 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 91 | float PW_PER_DEG; |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 92 | float ZEROPW; |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 93 | static const float MIN_PW = 0.0005; |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 94 | static const float MAX_PW = 0.0025f; |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 95 | |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 96 | void SetServo(); |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 97 | }; |
Cornelius Bezuidenhout |
1:940dffb14ff6 | 98 | #endif |