ECE 4180 Final Project

Dependencies:   mbed PinDetect

Committer:
amitchell41
Date:
Wed Dec 12 17:11:24 2018 +0000
Revision:
3:660252b9fa29
Parent:
0:eff94d72c9f4
Final version

Who changed what in which revision?

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