A servo library for controlling servos without using PWM. Can be used on any pin.

Dependents:   ServoTest ServoTest Nucleo_coffee NervousPuppySprintOne ... more

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