ECE 4180 Final Project

Dependencies:   mbed PinDetect

Committer:
amitchell41
Date:
Wed Dec 12 17:11:24 2018 +0000
Revision:
3:660252b9fa29
Parent:
0:eff94d72c9f4
Final version

Who changed what in which revision?

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