Complete library that let you control and read your servo's in all ways: position, degrees, step.You can define also min and max position. It is also compatibile with Simon Ford's Servo library calls

Dependents:   ServoTest

Fork of DuryServo by Fabio Durigon

Committer:
dury
Date:
Thu Jun 13 17:54:36 2013 +0000
Revision:
0:88fd1c2402c9
Servo library let you control: position, degrees, step.You can define also min and max position.; It is also compatibile with Simon Ford's Servo library calls

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dury 0:88fd1c2402c9 1 /* mbed R/C Servo Library
dury 0:88fd1c2402c9 2 *
dury 0:88fd1c2402c9 3 * Copyright (c) 2013 Fabio Durigon
dury 0:88fd1c2402c9 4 *
dury 0:88fd1c2402c9 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
dury 0:88fd1c2402c9 6 * of this software and associated documentation files (the "Software"), to deal
dury 0:88fd1c2402c9 7 * in the Software without restriction, including without limitation the rights
dury 0:88fd1c2402c9 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
dury 0:88fd1c2402c9 9 * copies of the Software, and to permit persons to whom the Software is
dury 0:88fd1c2402c9 10 * furnished to do so, subject to the following conditions:
dury 0:88fd1c2402c9 11 *
dury 0:88fd1c2402c9 12 * The above copyright notice and this permission notice shall be included in
dury 0:88fd1c2402c9 13 * all copies or substantial portions of the Software.
dury 0:88fd1c2402c9 14 *
dury 0:88fd1c2402c9 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dury 0:88fd1c2402c9 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dury 0:88fd1c2402c9 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
dury 0:88fd1c2402c9 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dury 0:88fd1c2402c9 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
dury 0:88fd1c2402c9 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
dury 0:88fd1c2402c9 21 * THE SOFTWARE.
dury 0:88fd1c2402c9 22 */
dury 0:88fd1c2402c9 23
dury 0:88fd1c2402c9 24 #ifndef MBED_SERVO_H
dury 0:88fd1c2402c9 25 #define MBED_SERVO_H
dury 0:88fd1c2402c9 26
dury 0:88fd1c2402c9 27 #include "mbed.h"
dury 0:88fd1c2402c9 28
dury 0:88fd1c2402c9 29 /** Library to control a Servo via pwm pin*/
dury 0:88fd1c2402c9 30 class Servo{
dury 0:88fd1c2402c9 31 public:
dury 0:88fd1c2402c9 32 /** Create a Servo object
dury 0:88fd1c2402c9 33 *
dury 0:88fd1c2402c9 34 * @param Pin Pwm output pin
dury 0:88fd1c2402c9 35 */
dury 0:88fd1c2402c9 36 Servo(PinName Pin);
dury 0:88fd1c2402c9 37
dury 0:88fd1c2402c9 38 /** Set servo position in range from 0.0 to 1.0
dury 0:88fd1c2402c9 39 *
dury 0:88fd1c2402c9 40 * @param range ablosute position from 0.0 to 1.0
dury 0:88fd1c2402c9 41 * 0.0 Full left position
dury 0:88fd1c2402c9 42 * 0.5 Centre position
dury 0:88fd1c2402c9 43 * 1.0 Full right position
dury 0:88fd1c2402c9 44 *
dury 0:88fd1c2402c9 45 * @return normalized position
dury 0:88fd1c2402c9 46 */
dury 0:88fd1c2402c9 47 float Write(float Pos);
dury 0:88fd1c2402c9 48 //! write() version for compatibility with Simon Ford's Servo Library
dury 0:88fd1c2402c9 49 float write(float Pos);
dury 0:88fd1c2402c9 50
dury 0:88fd1c2402c9 51 /** Set servo position in Degrees where 0 is the centre position
dury 0:88fd1c2402c9 52 * @param Degrees Degrees inside the "Degrees Range"
dury 0:88fd1c2402c9 53 *
dury 0:88fd1c2402c9 54 * @return normalized Degrees
dury 0:88fd1c2402c9 55 *
dury 0:88fd1c2402c9 56 * @code
dury 0:88fd1c2402c9 57 * servo.WriteDegrees(-45);
dury 0:88fd1c2402c9 58 * @endcode
dury 0:88fd1c2402c9 59 */
dury 0:88fd1c2402c9 60 float WriteDegrees(float Degrees);
dury 0:88fd1c2402c9 61
dury 0:88fd1c2402c9 62 /** Set the pulsewidth in secs and measured degrees for this servo to reach the minimum position (max escursion to the left)
dury 0:88fd1c2402c9 63 *
dury 0:88fd1c2402c9 64 * @param PulseSec Pulsewitdh in sec
dury 0:88fd1c2402c9 65 * @param Degrees Degrees associated to pulsewidth
dury 0:88fd1c2402c9 66 */
dury 0:88fd1c2402c9 67 void DefinePulseMin(float PulseSec, float Degrees);
dury 0:88fd1c2402c9 68
dury 0:88fd1c2402c9 69 /** Set the pulsewidth in secs and measured degrees for this servo to reach the maximum position (max escursion to the right)
dury 0:88fd1c2402c9 70 *
dury 0:88fd1c2402c9 71 * @param PulseSec Pulsewitdh in sec
dury 0:88fd1c2402c9 72 * @param Degrees Degrees associated to pulsewidth
dury 0:88fd1c2402c9 73 */
dury 0:88fd1c2402c9 74 void DefinePulseMax(float PulseSec, float Degrees);
dury 0:88fd1c2402c9 75
dury 0:88fd1c2402c9 76 /** Set total range in secs for the entire excursion(from pulsemin to pulsemax) associated to degrees excursion.
dury 0:88fd1c2402c9 77 * Central position of servo is PulseSec/2 or Degrres/2.
dury 0:88fd1c2402c9 78 *
dury 0:88fd1c2402c9 79 * @param TotPulseSec Pulsewitdh in sec from min position to max position
dury 0:88fd1c2402c9 80 * @param Degrees Total Degrees associated to TotPulseSec
dury 0:88fd1c2402c9 81 */
dury 0:88fd1c2402c9 82 void DefinePulseTotal(float TotPulseSec, float TotDegrees);
dury 0:88fd1c2402c9 83
dury 0:88fd1c2402c9 84 /** Read current position
dury 0:88fd1c2402c9 85 *
dury 0:88fd1c2402c9 86 * @return Current position within range from 0.0 to 1.0
dury 0:88fd1c2402c9 87 */
dury 0:88fd1c2402c9 88 float Read(void);
dury 0:88fd1c2402c9 89 //! read() version for compatibility with Simon Ford's Servo Library
dury 0:88fd1c2402c9 90 float read(void);
dury 0:88fd1c2402c9 91
dury 0:88fd1c2402c9 92 /** Read current position in BYTE number
dury 0:88fd1c2402c9 93 *
dury 0:88fd1c2402c9 94 * @return Current position within range from 0 to 255
dury 0:88fd1c2402c9 95 */
dury 0:88fd1c2402c9 96 unsigned char ReadByte(void);
dury 0:88fd1c2402c9 97
dury 0:88fd1c2402c9 98 /** Read current Degrees
dury 0:88fd1c2402c9 99 *
dury 0:88fd1c2402c9 100 * @return Current position in Degrees within range of Degrees
dury 0:88fd1c2402c9 101 */
dury 0:88fd1c2402c9 102 float ReadDegrees(void);
dury 0:88fd1c2402c9 103
dury 0:88fd1c2402c9 104 /** Read the current pulsemin
dury 0:88fd1c2402c9 105 *
dury 0:88fd1c2402c9 106 * @return Current pulsewith for min position
dury 0:88fd1c2402c9 107 */
dury 0:88fd1c2402c9 108 float ReadPulseMin(void);
dury 0:88fd1c2402c9 109
dury 0:88fd1c2402c9 110 /** Read the current pulsemax
dury 0:88fd1c2402c9 111 *
dury 0:88fd1c2402c9 112 * @return Current pulsewith for max position
dury 0:88fd1c2402c9 113 */
dury 0:88fd1c2402c9 114 float ReadPulseMax(void);
dury 0:88fd1c2402c9 115
dury 0:88fd1c2402c9 116 /** Read current degreemin
dury 0:88fd1c2402c9 117 *
dury 0:88fd1c2402c9 118 * @return Current Degrees associated to current pulsemin
dury 0:88fd1c2402c9 119 */
dury 0:88fd1c2402c9 120 float ReadDegreesMin(void);
dury 0:88fd1c2402c9 121
dury 0:88fd1c2402c9 122 /** Read current degreemax
dury 0:88fd1c2402c9 123 *
dury 0:88fd1c2402c9 124 * @return Current Degrees associated to current pulsemax
dury 0:88fd1c2402c9 125 */
dury 0:88fd1c2402c9 126 float ReadDegreesMax(void);
dury 0:88fd1c2402c9 127
dury 0:88fd1c2402c9 128 /** Shorthand for the write and read functions */
dury 0:88fd1c2402c9 129 Servo& operator= (float range);
dury 0:88fd1c2402c9 130 Servo& operator= (Servo& rhs);
dury 0:88fd1c2402c9 131 operator float();
dury 0:88fd1c2402c9 132
dury 0:88fd1c2402c9 133 void PrintValues(void);
dury 0:88fd1c2402c9 134
dury 0:88fd1c2402c9 135 private:
dury 0:88fd1c2402c9 136 PwmOut pwmout;
dury 0:88fd1c2402c9 137 float currpos;
dury 0:88fd1c2402c9 138 float currdegrees;
dury 0:88fd1c2402c9 139 float pulsemin;
dury 0:88fd1c2402c9 140 float pulsemax;
dury 0:88fd1c2402c9 141 float pulserange;
dury 0:88fd1c2402c9 142 float degreesmin;
dury 0:88fd1c2402c9 143 float degreesmax;
dury 0:88fd1c2402c9 144 float degreesrange;
dury 0:88fd1c2402c9 145 float degreeshrange;
dury 0:88fd1c2402c9 146 };
dury 0:88fd1c2402c9 147 #endif