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