Workshop 2

Dependencies:   FastPWM

Committer:
pmic
Date:
Wed Apr 07 12:13:45 2021 +0000
Revision:
6:41dd03654c44
Parent:
4:9c003c402033
Last commit before first workshop 2.

Who changed what in which revision?

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