Jan Kamidra / Servo
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Servo.h Source File

Servo.h

00001 /* mbed Servo Library without using PWM pins
00002  * Copyright (c) 2010 Jasper Denkers
00003  * Copyright (c) 2022 Jan Kamidra
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in
00013  * all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021  * THE SOFTWARE.
00022  */
00023 
00024 #ifndef SERVO_H
00025 #define SERVO_H
00026 
00027 #include "mbed.h"
00028 
00029 /** Class to control a servo on any pin, without using pwm
00030  *
00031  * Example:
00032  * @code
00033  * // Keep sweeping servo from left to right
00034  * #include "mbed.h"
00035  * #include "Servo.h"
00036  * 
00037  * Servo Servo1(p20);
00038  *
00039  * Servo1.Enable(1500,20000);
00040  *
00041  * while(1) {
00042  *     for (int pos = 1000; pos < 2000; pos += 25) {
00043  *         Servo1.SetPosition(pos);  
00044  *         thread_sleep_for(20);
00045  *     }
00046  *     for (int pos = 2000; pos > 1000; pos -= 25) {
00047  *         Servo1.SetPosition(pos); 
00048  *         thread_sleep_for(20);
00049  *     }
00050  * }
00051  * @endcode
00052  */
00053 
00054 class Servo {
00055 
00056 public:
00057     /** Create a new Servo object on any mbed pin
00058      *
00059      * @param Pin Pin on mbed to connect servo to
00060      */
00061     Servo(PinName Pin);
00062     
00063     /** Change the position of the servo. Position in us
00064      *
00065      * @param NewPos The new value of the servos position (us)
00066      */
00067     void SetPosition(int NewPos);
00068     
00069     /** Enable the servo. Without enabling the servo won't be running. Startposition and period both in us.
00070      *
00071      * @param StartPos The position of the servo to start (us) 
00072      * @param Period The time between every pulse. 20000 us = 50 Hz(standard) (us)
00073      */
00074     void Enable(int StartPos, int Period);
00075     
00076     /** Disable the servo. After disabling the servo won't get any signal anymore
00077      *
00078      */
00079     void Disable();
00080 
00081 private:
00082     void StartPulse();
00083     void EndPulse();
00084 
00085     int Position;
00086     DigitalOut ServoPin;
00087     Ticker Pulse;
00088     Timeout PulseStop;
00089 };
00090 
00091 #endif