Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mbed-rtos SPI_MX25R
Control/Servo/Servo.h@0:d6402bcd58f7, 2018-12-01 (annotated)
- Committer:
- shimogamo
- Date:
- Sat Dec 01 15:10:42 2018 +0000
- Revision:
- 0:d6402bcd58f7
BIRD2018????????????
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| shimogamo | 0:d6402bcd58f7 | 1 | /* mbed R/C Servo Library |
| shimogamo | 0:d6402bcd58f7 | 2 | * Copyright (c) 2007-2010 sford, cstyles |
| shimogamo | 0:d6402bcd58f7 | 3 | * |
| shimogamo | 0:d6402bcd58f7 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| shimogamo | 0:d6402bcd58f7 | 5 | * of this software and associated documentation files (the "Software"), to deal |
| shimogamo | 0:d6402bcd58f7 | 6 | * in the Software without restriction, including without limitation the rights |
| shimogamo | 0:d6402bcd58f7 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| shimogamo | 0:d6402bcd58f7 | 8 | * copies of the Software, and to permit persons to whom the Software is |
| shimogamo | 0:d6402bcd58f7 | 9 | * furnished to do so, subject to the following conditions: |
| shimogamo | 0:d6402bcd58f7 | 10 | * |
| shimogamo | 0:d6402bcd58f7 | 11 | * The above copyright notice and this permission notice shall be included in |
| shimogamo | 0:d6402bcd58f7 | 12 | * all copies or substantial portions of the Software. |
| shimogamo | 0:d6402bcd58f7 | 13 | * |
| shimogamo | 0:d6402bcd58f7 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| shimogamo | 0:d6402bcd58f7 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| shimogamo | 0:d6402bcd58f7 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| shimogamo | 0:d6402bcd58f7 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| shimogamo | 0:d6402bcd58f7 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| shimogamo | 0:d6402bcd58f7 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| shimogamo | 0:d6402bcd58f7 | 20 | * THE SOFTWARE. |
| shimogamo | 0:d6402bcd58f7 | 21 | */ |
| shimogamo | 0:d6402bcd58f7 | 22 | |
| shimogamo | 0:d6402bcd58f7 | 23 | #ifndef MBED_SERVO_H |
| shimogamo | 0:d6402bcd58f7 | 24 | #define MBED_SERVO_H |
| shimogamo | 0:d6402bcd58f7 | 25 | |
| shimogamo | 0:d6402bcd58f7 | 26 | #include "mbed.h" |
| shimogamo | 0:d6402bcd58f7 | 27 | |
| shimogamo | 0:d6402bcd58f7 | 28 | /** Servo control class, based on a PwmOut |
| shimogamo | 0:d6402bcd58f7 | 29 | * |
| shimogamo | 0:d6402bcd58f7 | 30 | * Example: |
| shimogamo | 0:d6402bcd58f7 | 31 | * @code |
| shimogamo | 0:d6402bcd58f7 | 32 | * // Continuously sweep the servo through it's full range |
| shimogamo | 0:d6402bcd58f7 | 33 | * #include "mbed.h" |
| shimogamo | 0:d6402bcd58f7 | 34 | * #include "Servo.h" |
| shimogamo | 0:d6402bcd58f7 | 35 | * |
| shimogamo | 0:d6402bcd58f7 | 36 | * Servo myservo(p21); |
| shimogamo | 0:d6402bcd58f7 | 37 | * |
| shimogamo | 0:d6402bcd58f7 | 38 | * int main() { |
| shimogamo | 0:d6402bcd58f7 | 39 | * while(1) { |
| shimogamo | 0:d6402bcd58f7 | 40 | * for(int i=0; i<100; i++) { |
| shimogamo | 0:d6402bcd58f7 | 41 | * myservo = i/100.0; |
| shimogamo | 0:d6402bcd58f7 | 42 | * wait(0.01); |
| shimogamo | 0:d6402bcd58f7 | 43 | * } |
| shimogamo | 0:d6402bcd58f7 | 44 | * for(int i=100; i>0; i--) { |
| shimogamo | 0:d6402bcd58f7 | 45 | * myservo = i/100.0; |
| shimogamo | 0:d6402bcd58f7 | 46 | * wait(0.01); |
| shimogamo | 0:d6402bcd58f7 | 47 | * } |
| shimogamo | 0:d6402bcd58f7 | 48 | * } |
| shimogamo | 0:d6402bcd58f7 | 49 | * } |
| shimogamo | 0:d6402bcd58f7 | 50 | * @endcode |
| shimogamo | 0:d6402bcd58f7 | 51 | */ |
| shimogamo | 0:d6402bcd58f7 | 52 | class Servo { |
| shimogamo | 0:d6402bcd58f7 | 53 | |
| shimogamo | 0:d6402bcd58f7 | 54 | public: |
| shimogamo | 0:d6402bcd58f7 | 55 | /** Create a servo object connected to the specified PwmOut pin |
| shimogamo | 0:d6402bcd58f7 | 56 | * |
| shimogamo | 0:d6402bcd58f7 | 57 | * @param pin PwmOut pin to connect to |
| shimogamo | 0:d6402bcd58f7 | 58 | */ |
| shimogamo | 0:d6402bcd58f7 | 59 | Servo(PinName pin); |
| shimogamo | 0:d6402bcd58f7 | 60 | |
| shimogamo | 0:d6402bcd58f7 | 61 | /** Set the servo position, normalised to it's full range |
| shimogamo | 0:d6402bcd58f7 | 62 | * |
| shimogamo | 0:d6402bcd58f7 | 63 | * @param percent A normalised number 0.0-1.0 to represent the full range. |
| shimogamo | 0:d6402bcd58f7 | 64 | */ |
| shimogamo | 0:d6402bcd58f7 | 65 | void write(float percent); |
| shimogamo | 0:d6402bcd58f7 | 66 | |
| shimogamo | 0:d6402bcd58f7 | 67 | /** Read the servo motors current position |
| shimogamo | 0:d6402bcd58f7 | 68 | * |
| shimogamo | 0:d6402bcd58f7 | 69 | * @param returns A normalised number 0.0-1.0 representing the full range. |
| shimogamo | 0:d6402bcd58f7 | 70 | */ |
| shimogamo | 0:d6402bcd58f7 | 71 | float read(); |
| shimogamo | 0:d6402bcd58f7 | 72 | |
| shimogamo | 0:d6402bcd58f7 | 73 | /** Set the servo position |
| shimogamo | 0:d6402bcd58f7 | 74 | * |
| shimogamo | 0:d6402bcd58f7 | 75 | * @param degrees Servo position in degrees |
| shimogamo | 0:d6402bcd58f7 | 76 | */ |
| shimogamo | 0:d6402bcd58f7 | 77 | void position(float degrees); |
| shimogamo | 0:d6402bcd58f7 | 78 | |
| shimogamo | 0:d6402bcd58f7 | 79 | /** Allows calibration of the range and angles for a particular servo |
| shimogamo | 0:d6402bcd58f7 | 80 | * |
| shimogamo | 0:d6402bcd58f7 | 81 | * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds |
| shimogamo | 0:d6402bcd58f7 | 82 | * @param degrees Angle from centre to maximum/minimum position in degrees |
| shimogamo | 0:d6402bcd58f7 | 83 | */ |
| shimogamo | 0:d6402bcd58f7 | 84 | //NX8921(JR PROPO)の180度モードに合わせたパラメータ(デフォルトは120度モード)。他のサーボに変えた時、変更する必要あり。 |
| shimogamo | 0:d6402bcd58f7 | 85 | void calibrate(float range_us = 600.0, float degrees = 90.0, float center_us = 1500.0, int period_ms_ = 5); |
| shimogamo | 0:d6402bcd58f7 | 86 | |
| shimogamo | 0:d6402bcd58f7 | 87 | |
| shimogamo | 0:d6402bcd58f7 | 88 | void rawwrite(int width_us); |
| shimogamo | 0:d6402bcd58f7 | 89 | |
| shimogamo | 0:d6402bcd58f7 | 90 | float readangle(); |
| shimogamo | 0:d6402bcd58f7 | 91 | |
| shimogamo | 0:d6402bcd58f7 | 92 | |
| shimogamo | 0:d6402bcd58f7 | 93 | /** Shorthand for the write and read functions */ |
| shimogamo | 0:d6402bcd58f7 | 94 | Servo& operator= (float percent); |
| shimogamo | 0:d6402bcd58f7 | 95 | Servo& operator= (Servo& rhs); |
| shimogamo | 0:d6402bcd58f7 | 96 | operator float(); |
| shimogamo | 0:d6402bcd58f7 | 97 | |
| shimogamo | 0:d6402bcd58f7 | 98 | protected: |
| shimogamo | 0:d6402bcd58f7 | 99 | PwmOut _pwm; |
| shimogamo | 0:d6402bcd58f7 | 100 | float _range; |
| shimogamo | 0:d6402bcd58f7 | 101 | float _degrees; |
| shimogamo | 0:d6402bcd58f7 | 102 | float _p; |
| shimogamo | 0:d6402bcd58f7 | 103 | float _angle; |
| shimogamo | 0:d6402bcd58f7 | 104 | float _center; |
| shimogamo | 0:d6402bcd58f7 | 105 | int _period_ms; |
| shimogamo | 0:d6402bcd58f7 | 106 | float p2angle(float p); |
| shimogamo | 0:d6402bcd58f7 | 107 | }; |
| shimogamo | 0:d6402bcd58f7 | 108 | |
| shimogamo | 0:d6402bcd58f7 | 109 | #endif |