![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
PES2_mbed_os_6
Servo.h@2:87ca9b0c9992, 2021-03-13 (annotated)
- Committer:
- boro
- Date:
- Sat Mar 13 11:06:23 2021 +0100
- Revision:
- 2:87ca9b0c9992
updated to be compatible with mbed-os 6.8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
boro | 2:87ca9b0c9992 | 1 | /* mbed Servo Library without using PWM pins |
boro | 2:87ca9b0c9992 | 2 | * Copyright (c) 2010 Jasper Denkers |
boro | 2:87ca9b0c9992 | 3 | * |
boro | 2:87ca9b0c9992 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
boro | 2:87ca9b0c9992 | 5 | * of this software and associated documentation files (the "Software"), to deal |
boro | 2:87ca9b0c9992 | 6 | * in the Software without restriction, including without limitation the rights |
boro | 2:87ca9b0c9992 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
boro | 2:87ca9b0c9992 | 8 | * copies of the Software, and to permit persons to whom the Software is |
boro | 2:87ca9b0c9992 | 9 | * furnished to do so, subject to the following conditions: |
boro | 2:87ca9b0c9992 | 10 | * |
boro | 2:87ca9b0c9992 | 11 | * The above copyright notice and this permission notice shall be included in |
boro | 2:87ca9b0c9992 | 12 | * all copies or substantial portions of the Software. |
boro | 2:87ca9b0c9992 | 13 | * |
boro | 2:87ca9b0c9992 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
boro | 2:87ca9b0c9992 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
boro | 2:87ca9b0c9992 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
boro | 2:87ca9b0c9992 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
boro | 2:87ca9b0c9992 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
boro | 2:87ca9b0c9992 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
boro | 2:87ca9b0c9992 | 20 | * THE SOFTWARE. |
boro | 2:87ca9b0c9992 | 21 | */ |
boro | 2:87ca9b0c9992 | 22 | |
boro | 2:87ca9b0c9992 | 23 | #ifndef MBED_SERVO_H |
boro | 2:87ca9b0c9992 | 24 | #define MBED_SERVO_H |
boro | 2:87ca9b0c9992 | 25 | |
boro | 2:87ca9b0c9992 | 26 | #include "mbed.h" |
boro | 2:87ca9b0c9992 | 27 | |
boro | 2:87ca9b0c9992 | 28 | /** Class to control a servo on any pin, without using pwm |
boro | 2:87ca9b0c9992 | 29 | * |
boro | 2:87ca9b0c9992 | 30 | * Example: |
boro | 2:87ca9b0c9992 | 31 | * @code |
boro | 2:87ca9b0c9992 | 32 | * // Keep sweeping servo from left to right |
boro | 2:87ca9b0c9992 | 33 | * #include "mbed.h" |
boro | 2:87ca9b0c9992 | 34 | * #include "Servo.h" |
boro | 2:87ca9b0c9992 | 35 | * |
boro | 2:87ca9b0c9992 | 36 | * Servo Servo1(p20); |
boro | 2:87ca9b0c9992 | 37 | * |
boro | 2:87ca9b0c9992 | 38 | * Servo1.Enable(1500,20000); |
boro | 2:87ca9b0c9992 | 39 | * |
boro | 2:87ca9b0c9992 | 40 | * while(1) { |
boro | 2:87ca9b0c9992 | 41 | * for (int pos = 1000; pos < 2000; pos += 25) { |
boro | 2:87ca9b0c9992 | 42 | * Servo1.SetPosition(pos); |
boro | 2:87ca9b0c9992 | 43 | * wait_ms(20); |
boro | 2:87ca9b0c9992 | 44 | * } |
boro | 2:87ca9b0c9992 | 45 | * for (int pos = 2000; pos > 1000; pos -= 25) { |
boro | 2:87ca9b0c9992 | 46 | * Servo1.SetPosition(pos); |
boro | 2:87ca9b0c9992 | 47 | * wait_ms(20); |
boro | 2:87ca9b0c9992 | 48 | * } |
boro | 2:87ca9b0c9992 | 49 | * } |
boro | 2:87ca9b0c9992 | 50 | * @endcode |
boro | 2:87ca9b0c9992 | 51 | */ |
boro | 2:87ca9b0c9992 | 52 | |
boro | 2:87ca9b0c9992 | 53 | class Servo { |
boro | 2:87ca9b0c9992 | 54 | |
boro | 2:87ca9b0c9992 | 55 | public: |
boro | 2:87ca9b0c9992 | 56 | /** Create a new Servo object on any mbed pin |
boro | 2:87ca9b0c9992 | 57 | * |
boro | 2:87ca9b0c9992 | 58 | * @param Pin Pin on mbed to connect servo to |
boro | 2:87ca9b0c9992 | 59 | */ |
boro | 2:87ca9b0c9992 | 60 | Servo(PinName Pin); |
boro | 2:87ca9b0c9992 | 61 | |
boro | 2:87ca9b0c9992 | 62 | /** Change the position of the servo. Position in us |
boro | 2:87ca9b0c9992 | 63 | * |
boro | 2:87ca9b0c9992 | 64 | * @param NewPos The new value of the servos position (us) |
boro | 2:87ca9b0c9992 | 65 | */ |
boro | 2:87ca9b0c9992 | 66 | void SetPosition(int NewPos); |
boro | 2:87ca9b0c9992 | 67 | |
boro | 2:87ca9b0c9992 | 68 | /** Enable the servo. Without enabling the servo won't be running. Startposition and period both in us. |
boro | 2:87ca9b0c9992 | 69 | * |
boro | 2:87ca9b0c9992 | 70 | * @param StartPos The position of the servo to start (us) |
boro | 2:87ca9b0c9992 | 71 | * @param Period The time between every pulse. 20000 us = 50 Hz(standard) (us) |
boro | 2:87ca9b0c9992 | 72 | */ |
boro | 2:87ca9b0c9992 | 73 | void Enable(int StartPos, int Period); |
boro | 2:87ca9b0c9992 | 74 | |
boro | 2:87ca9b0c9992 | 75 | /** Disable the servo. After disabling the servo won't get any signal anymore |
boro | 2:87ca9b0c9992 | 76 | * |
boro | 2:87ca9b0c9992 | 77 | */ |
boro | 2:87ca9b0c9992 | 78 | void Disable(); |
boro | 2:87ca9b0c9992 | 79 | |
boro | 2:87ca9b0c9992 | 80 | private: |
boro | 2:87ca9b0c9992 | 81 | void StartPulse(); |
boro | 2:87ca9b0c9992 | 82 | void EndPulse(); |
boro | 2:87ca9b0c9992 | 83 | |
boro | 2:87ca9b0c9992 | 84 | int Position; |
boro | 2:87ca9b0c9992 | 85 | DigitalOut ServoPin; |
boro | 2:87ca9b0c9992 | 86 | Ticker Pulse; |
boro | 2:87ca9b0c9992 | 87 | Timeout PulseStop; |
boro | 2:87ca9b0c9992 | 88 | }; |
boro | 2:87ca9b0c9992 | 89 | |
boro | 2:87ca9b0c9992 | 90 | #endif |