servo

Committer:
wisnup
Date:
Thu Feb 20 04:14:03 2014 +0000
Revision:
0:7fd570dc03ee
servo

Who changed what in which revision?

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