fish

Dependencies:   mbed

Committer:
tzxl10000
Date:
Wed Jun 14 20:22:45 2017 +0000
Revision:
0:561f7672eaad
fish;

Who changed what in which revision?

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