PM2_Lib

Dependencies:   LSM9DS1 RangeFinder FastPWM

Committer:
pmic
Date:
Tue Mar 30 12:21:00 2021 +0000
Revision:
0:86129f1b4a93
Child:
4:9c003c402033
First commit.

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 0:86129f1b4a93 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 0:86129f1b4a93 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 0:86129f1b4a93 46 * Servo1.SetPosition(pos);
pmic 0:86129f1b4a93 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 0:86129f1b4a93 53 class Servo {
pmic 0:86129f1b4a93 54
pmic 0:86129f1b4a93 55 public:
pmic 0:86129f1b4a93 56 /** Create a new Servo object on any mbed pin
pmic 0:86129f1b4a93 57 *
pmic 0:86129f1b4a93 58 * @param Pin Pin on mbed to connect servo to
pmic 0:86129f1b4a93 59 */
pmic 0:86129f1b4a93 60 Servo(PinName Pin);
pmic 0:86129f1b4a93 61
pmic 0:86129f1b4a93 62 /** Change the position of the servo. Position in us
pmic 0:86129f1b4a93 63 *
pmic 0:86129f1b4a93 64 * @param NewPos The new value of the servos position (us)
pmic 0:86129f1b4a93 65 */
pmic 0:86129f1b4a93 66 void SetPosition(int NewPos);
pmic 0:86129f1b4a93 67
pmic 0:86129f1b4a93 68 /** Enable the servo. Without enabling the servo won't be running. Startposition and period both in us.
pmic 0:86129f1b4a93 69 *
pmic 0:86129f1b4a93 70 * @param StartPos The position of the servo to start (us)
pmic 0:86129f1b4a93 71 * @param Period The time between every pulse. 20000 us = 50 Hz(standard) (us)
pmic 0:86129f1b4a93 72 */
pmic 0:86129f1b4a93 73 void Enable(int StartPos, int Period);
pmic 0:86129f1b4a93 74
pmic 0:86129f1b4a93 75 /** Disable the servo. After disabling the servo won't get any signal anymore
pmic 0:86129f1b4a93 76 *
pmic 0:86129f1b4a93 77 */
pmic 0:86129f1b4a93 78 void Disable();
pmic 0:86129f1b4a93 79
pmic 0:86129f1b4a93 80 private:
pmic 0:86129f1b4a93 81 void StartPulse();
pmic 0:86129f1b4a93 82 void EndPulse();
pmic 0:86129f1b4a93 83
pmic 0:86129f1b4a93 84 int Position;
pmic 0:86129f1b4a93 85 DigitalOut ServoPin;
pmic 0:86129f1b4a93 86 Ticker Pulse;
pmic 0:86129f1b4a93 87 Timeout PulseStop;
pmic 0:86129f1b4a93 88 };
pmic 0:86129f1b4a93 89
pmic 0:86129f1b4a93 90 #endif