Servo control routines

Dependents:   Q2_Stabi

Committer:
Decimus
Date:
Mon May 30 08:09:19 2016 +0000
Revision:
0:b3bed6121155
[+]

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Decimus 0:b3bed6121155 1 /* mbed R/C Servo Library
Decimus 0:b3bed6121155 2 *
Decimus 0:b3bed6121155 3 * Copyright (c) 2007-2010 sford, cstyles
Decimus 0:b3bed6121155 4 *
Decimus 0:b3bed6121155 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
Decimus 0:b3bed6121155 6 * of this software and associated documentation files (the "Software"), to deal
Decimus 0:b3bed6121155 7 * in the Software without restriction, including without limitation the rights
Decimus 0:b3bed6121155 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Decimus 0:b3bed6121155 9 * copies of the Software, and to permit persons to whom the Software is
Decimus 0:b3bed6121155 10 * furnished to do so, subject to the following conditions:
Decimus 0:b3bed6121155 11 *
Decimus 0:b3bed6121155 12 * The above copyright notice and this permission notice shall be included in
Decimus 0:b3bed6121155 13 * all copies or substantial portions of the Software.
Decimus 0:b3bed6121155 14 *
Decimus 0:b3bed6121155 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Decimus 0:b3bed6121155 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Decimus 0:b3bed6121155 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Decimus 0:b3bed6121155 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Decimus 0:b3bed6121155 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Decimus 0:b3bed6121155 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Decimus 0:b3bed6121155 21 * THE SOFTWARE.
Decimus 0:b3bed6121155 22 */
Decimus 0:b3bed6121155 23
Decimus 0:b3bed6121155 24 #include "Servo.h"
Decimus 0:b3bed6121155 25 #include "mbed.h"
Decimus 0:b3bed6121155 26
Decimus 0:b3bed6121155 27 static float clamp(float value, float min, float max) {
Decimus 0:b3bed6121155 28 if(value < min) {
Decimus 0:b3bed6121155 29 return min;
Decimus 0:b3bed6121155 30 } else if(value > max) {
Decimus 0:b3bed6121155 31 return max;
Decimus 0:b3bed6121155 32 } else {
Decimus 0:b3bed6121155 33 return value;
Decimus 0:b3bed6121155 34 }
Decimus 0:b3bed6121155 35 }
Decimus 0:b3bed6121155 36
Decimus 0:b3bed6121155 37 Servo::Servo(PinName pin) : _pwm(pin) {
Decimus 0:b3bed6121155 38 //calibrate();
Decimus 0:b3bed6121155 39 //write(0.5);
Decimus 0:b3bed6121155 40 }
Decimus 0:b3bed6121155 41
Decimus 0:b3bed6121155 42 void Servo::write(float percent) {
Decimus 0:b3bed6121155 43 float offset = _range * 2.0 * (percent - 0.5);
Decimus 0:b3bed6121155 44 _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range));
Decimus 0:b3bed6121155 45 _p = clamp(percent, 0.0, 1.0);
Decimus 0:b3bed6121155 46 }
Decimus 0:b3bed6121155 47
Decimus 0:b3bed6121155 48 void Servo::position(float degrees) {
Decimus 0:b3bed6121155 49 float offset = _range * (degrees / _degrees);
Decimus 0:b3bed6121155 50 _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range));
Decimus 0:b3bed6121155 51 }
Decimus 0:b3bed6121155 52
Decimus 0:b3bed6121155 53 void Servo::calibrate(float range, float degrees, float period) {
Decimus 0:b3bed6121155 54 _range = range;
Decimus 0:b3bed6121155 55 _degrees = degrees;
Decimus 0:b3bed6121155 56 _pwm.period(period);
Decimus 0:b3bed6121155 57 }
Decimus 0:b3bed6121155 58
Decimus 0:b3bed6121155 59 float Servo::read() {
Decimus 0:b3bed6121155 60 return _p;
Decimus 0:b3bed6121155 61 }
Decimus 0:b3bed6121155 62
Decimus 0:b3bed6121155 63 Servo& Servo::operator= (float percent) {
Decimus 0:b3bed6121155 64 write(percent);
Decimus 0:b3bed6121155 65 return *this;
Decimus 0:b3bed6121155 66 }
Decimus 0:b3bed6121155 67
Decimus 0:b3bed6121155 68 Servo& Servo::operator= (Servo& rhs) {
Decimus 0:b3bed6121155 69 write(rhs.read());
Decimus 0:b3bed6121155 70 return *this;
Decimus 0:b3bed6121155 71 }
Decimus 0:b3bed6121155 72
Decimus 0:b3bed6121155 73 Servo::operator float() {
Decimus 0:b3bed6121155 74 return read();
Decimus 0:b3bed6121155 75 }