another clone of servo library

Committer:
yungsung
Date:
Thu Jan 09 19:18:56 2020 +0000
Revision:
0:c24d2ead5c71
done

Who changed what in which revision?

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