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:
jdenkers
Date:
Sun Oct 17 13:34:19 2010 +0000
Revision:
1:352133517ccc
Parent:
0:30b972d2dcec
Child:
2:2f6d7872d6f3

        

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