David Garrsion / VarSpeedServo

Dependents:   VariableSpeedServo

Fork of Servo by Jasper Denkers

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  * Changes and additions made by David Garrison, October 2015 to add ability to independently control each servo's speed
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a copy
00007  * of this software and associated documentation files (the "Software"), to deal
00008  * in the Software without restriction, including without limitation the rights
00009  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  * copies of the Software, and to permit persons to whom the Software is
00011  * furnished to do so, subject to the following conditions:
00012  *
00013  * The above copyright notice and this permission notice shall be included in
00014  * all copies or substantial portions of the Software.
00015  *
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022  * THE SOFTWARE.
00023  */
00024 
00025 #ifndef MBED_SERVO_H
00026 #define MBED_SERVO_H
00027 
00028 #include "mbed.h"
00029 
00030 /** Class to control servos on any pin, without using pwm 
00031  * Tested on Nucleo -L152RE @40MHz MCU clock and the 
00032  *  st disco -f746ng RUNNING AT 216mhZ.
00033  *
00034  * Example:
00035  * @code
00036  *
00037  * // Keep sweeping two servos from left to right
00038  * #include "mbed.h"
00039  * #include "Servo.h"
00040  * 
00041  *int main()
00042  *{
00043  *
00044  *   Servo Servo1(D12);            // create new instance of servo with output pin number
00045  *   Servo Servo2(D10);
00046  *   Servo1.Enable(1500,50,10);    // Start position ; in us (1500 = center), servo refresh rate in Hz (analog servos = 50 Hz), servo movement speed range from 1-50, 1 slowest, about 20 seconds/180 degrees
00047  *   Servo2.Enable(1500,50,10);
00048  *   while(1) {
00049  *
00050  *       Servo1.SetPosition(2300);
00051  *       Servo2.SetPosition(2300);
00052  *       wait(2.5);
00053  *
00054  *
00055  *       Servo1.SetPosition(700);
00056  *       Servo2.SetPosition(700);
00057  *       wait(2.5);
00058  *   }
00059  *
00060  *}
00061  * @endcode
00062  */
00063 
00064 class Servo {
00065 
00066 public:
00067     /** Create a new Servo object on any mbed pin
00068      *
00069      * @param Pin Pin on mbed to connect servo to
00070      */
00071     Servo(PinName Pin);
00072     
00073     /** Change the position of the servo. Position in us
00074      *
00075      * @param NewPos The new value of the servos position (us)
00076      */
00077     void SetPosition(int NewPos);
00078     
00079     /** speed oonversion from arbitary range of 1-50 to us to set Ticker Speed rate.
00080     * used in Enable function
00081     */
00082     int SpeedConvert (int _speed);  
00083     
00084     /** Enable the servo. Without enabling the servo won't be running. Startposition and period both in us.
00085      *
00086      * @param StartPos The position of the servo to start (us) 
00087      * @param RefreshRate.set in Hz  The time between every servo pulse. 20000 us = 50 Hz(standard) (us) 
00088                 Analog servos typically use a 50Hz refresh rate, while digital servos can use up to a 300Hz refresh rate. 
00089      * @parm _speed The time between updating the pulse width of the servo pulses --controls speed of servo movement
00090      */
00091     void Enable(int StartPos, int RefreshRate, int _speed);
00092     
00093     /** Disable the servo. After disabling the servo won't get any signal anymore
00094      *
00095      */     
00096      void Disable();
00097      
00098      /** determines direction of servo's move, called by Ticker Speed
00099      */
00100      void Update();
00101      //int Position;
00102      int speed;
00103      int CurrentPosition;
00104     
00105 
00106 private:
00107     void StartPulse();
00108     void EndPulse();
00109     int Position;
00110     int Period; 
00111     int NewPosition;
00112     DigitalOut ServoPin;
00113     Ticker Pulse;
00114     Ticker Speed;
00115     Timeout PulseStop;
00116 };
00117 
00118 #endif