khairul irwan fikri mohammad / Mbed 2 deprecated projecttest

Dependencies:   mbed

Committer:
irwanfikri
Date:
Tue Jun 12 08:36:32 2018 +0000
Revision:
0:48846c0a796e
robot

Who changed what in which revision?

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