Servo library based on Timer instead of PWM. Fork of original https://os.mbed.com/users/jdenkers/code/Servo/ and modified for compatibility with MbedOS6+

Committer:
JohnnyK
Date:
Fri Jun 10 04:22:15 2022 +0000
Revision:
2:2f6d7872d6f3
Parent:
1:352133517ccc
First release under MbedOS6+

Who changed what in which revision?

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