Servo control routines

Fork of Servo by Oleg Evsegneev

Committer:
Decimus
Date:
Mon May 30 08:09:19 2016 +0000
Revision:
0:b3bed6121155
[+]

Who changed what in which revision?

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