Test

Committer:
fermedicius
Date:
Mon Jan 11 11:02:01 2021 +0000
Revision:
0:2092473cf8c4
Test

Who changed what in which revision?

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