Lab 3

Dependencies:   Motor Servo mbed

Committer:
m182376
Date:
Mon Oct 12 17:54:27 2015 +0000
Revision:
0:cb674e873f8e
Lab 3

Who changed what in which revision?

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