hiroki kanda / Mbed 2 deprecated Nucleo_test2

Dependencies:   mbed

Committer:
hirokikanda
Date:
Mon Feb 14 13:16:09 2022 +0000
Revision:
1:038eca93a8f4
Parent:
0:09287ca178ff
test

Who changed what in which revision?

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