Servo pt. 2

Dependents:   FollowMeBot FollowMeBot FollowMeBot3 FollowMeBot

Fork of Servo by Simon Ford

Committer:
dhamilton31
Date:
Mon Nov 11 19:22:01 2013 +0000
Revision:
4:d67ccd284081
Parent:
3:36b69a7ced07
Child:
5:dec99beeafee
Created iRobot class for controlling the follow me bot

Who changed what in which revision?

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