Made changes to individually control servo speeds
Dependents: VariableSpeedServo
Fork of Servo by
Servo.h@3:774dd54867f2, 2015-10-12 (annotated)
- Committer:
- sierrasmith71
- Date:
- Mon Oct 12 14:56:58 2015 +0000
- Revision:
- 3:774dd54867f2
- Parent:
- 2:466f005a4dd5
- Child:
- 4:8bdb46e629b9
cleaned up opmments
Who changed what in which revision?
User | Revision | Line number | New 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 | * |
sierrasmith71 | 3:774dd54867f2 | 4 | * Changes and additions made by David Garrison, October 2015 to add ability to independently control each servo's speed |
sierrasmith71 | 3:774dd54867f2 | 5 | * |
jdenkers | 1:352133517ccc | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
jdenkers | 1:352133517ccc | 7 | * of this software and associated documentation files (the "Software"), to deal |
jdenkers | 1:352133517ccc | 8 | * in the Software without restriction, including without limitation the rights |
jdenkers | 1:352133517ccc | 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
jdenkers | 1:352133517ccc | 10 | * copies of the Software, and to permit persons to whom the Software is |
jdenkers | 1:352133517ccc | 11 | * furnished to do so, subject to the following conditions: |
jdenkers | 1:352133517ccc | 12 | * |
jdenkers | 1:352133517ccc | 13 | * The above copyright notice and this permission notice shall be included in |
jdenkers | 1:352133517ccc | 14 | * all copies or substantial portions of the Software. |
jdenkers | 1:352133517ccc | 15 | * |
jdenkers | 1:352133517ccc | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
jdenkers | 1:352133517ccc | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
jdenkers | 1:352133517ccc | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
jdenkers | 1:352133517ccc | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
jdenkers | 1:352133517ccc | 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jdenkers | 1:352133517ccc | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
jdenkers | 1:352133517ccc | 22 | * THE SOFTWARE. |
jdenkers | 1:352133517ccc | 23 | */ |
jdenkers | 1:352133517ccc | 24 | |
jdenkers | 0:30b972d2dcec | 25 | #ifndef MBED_SERVO_H |
jdenkers | 0:30b972d2dcec | 26 | #define MBED_SERVO_H |
jdenkers | 0:30b972d2dcec | 27 | |
jdenkers | 0:30b972d2dcec | 28 | #include "mbed.h" |
jdenkers | 0:30b972d2dcec | 29 | |
sierrasmith71 | 3:774dd54867f2 | 30 | /** Class to control servos on any pin, without using pwm |
sierrasmith71 | 3:774dd54867f2 | 31 | * Tested on Nucleo -L152RE @40MHz MCU clock and the |
sierrasmith71 | 3:774dd54867f2 | 32 | * st disco -f746ng RUNNING AT 216mhZ. |
jdenkers | 1:352133517ccc | 33 | * |
jdenkers | 1:352133517ccc | 34 | * Example: |
jdenkers | 1:352133517ccc | 35 | * @code |
sierrasmith71 | 3:774dd54867f2 | 36 | * |
sierrasmith71 | 3:774dd54867f2 | 37 | * // Keep sweeping two servos from left to right |
jdenkers | 1:352133517ccc | 38 | * #include "mbed.h" |
jdenkers | 1:352133517ccc | 39 | * #include "Servo.h" |
jdenkers | 1:352133517ccc | 40 | * |
sierrasmith71 | 3:774dd54867f2 | 41 | *int main() |
sierrasmith71 | 3:774dd54867f2 | 42 | *{ |
jdenkers | 1:352133517ccc | 43 | * |
sierrasmith71 | 3:774dd54867f2 | 44 | * Servo Servo1(D12); // create new instance of servo with output pin number |
sierrasmith71 | 3:774dd54867f2 | 45 | * Servo Servo2(D10); |
sierrasmith71 | 3:774dd54867f2 | 46 | * 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 |
sierrasmith71 | 3:774dd54867f2 | 47 | * Servo2.Enable(1500,50,10); |
sierrasmith71 | 3:774dd54867f2 | 48 | * while(1) { |
jdenkers | 1:352133517ccc | 49 | * |
sierrasmith71 | 3:774dd54867f2 | 50 | * Servo1.SetPosition(2300); |
sierrasmith71 | 3:774dd54867f2 | 51 | * Servo2.SetPosition(2300); |
sierrasmith71 | 3:774dd54867f2 | 52 | * wait(2.5); |
sierrasmith71 | 3:774dd54867f2 | 53 | * |
sierrasmith71 | 3:774dd54867f2 | 54 | * |
sierrasmith71 | 3:774dd54867f2 | 55 | * Servo1.SetPosition(700); |
sierrasmith71 | 3:774dd54867f2 | 56 | * Servo2.SetPosition(700); |
sierrasmith71 | 3:774dd54867f2 | 57 | * wait(2.5); |
sierrasmith71 | 3:774dd54867f2 | 58 | * } |
sierrasmith71 | 3:774dd54867f2 | 59 | * |
sierrasmith71 | 3:774dd54867f2 | 60 | *} |
jdenkers | 1:352133517ccc | 61 | * @endcode |
jdenkers | 1:352133517ccc | 62 | */ |
jdenkers | 1:352133517ccc | 63 | |
jdenkers | 0:30b972d2dcec | 64 | class Servo { |
jdenkers | 1:352133517ccc | 65 | |
jdenkers | 0:30b972d2dcec | 66 | public: |
jdenkers | 1:352133517ccc | 67 | /** Create a new Servo object on any mbed pin |
jdenkers | 1:352133517ccc | 68 | * |
jdenkers | 1:352133517ccc | 69 | * @param Pin Pin on mbed to connect servo to |
jdenkers | 1:352133517ccc | 70 | */ |
jdenkers | 0:30b972d2dcec | 71 | Servo(PinName Pin); |
jdenkers | 1:352133517ccc | 72 | |
jdenkers | 1:352133517ccc | 73 | /** Change the position of the servo. Position in us |
jdenkers | 1:352133517ccc | 74 | * |
jdenkers | 1:352133517ccc | 75 | * @param NewPos The new value of the servos position (us) |
jdenkers | 1:352133517ccc | 76 | */ |
jdenkers | 0:30b972d2dcec | 77 | void SetPosition(int NewPos); |
jdenkers | 1:352133517ccc | 78 | |
sierrasmith71 | 3:774dd54867f2 | 79 | /** speed oonversion from arbitary range of 1-50 to us to set Ticker Speed rate. |
sierrasmith71 | 3:774dd54867f2 | 80 | * used in Enable function |
sierrasmith71 | 2:466f005a4dd5 | 81 | */ |
sierrasmith71 | 2:466f005a4dd5 | 82 | int SpeedConvert (int _speed); |
sierrasmith71 | 2:466f005a4dd5 | 83 | |
jdenkers | 1:352133517ccc | 84 | /** Enable the servo. Without enabling the servo won't be running. Startposition and period both in us. |
jdenkers | 1:352133517ccc | 85 | * |
jdenkers | 1:352133517ccc | 86 | * @param StartPos The position of the servo to start (us) |
sierrasmith71 | 2:466f005a4dd5 | 87 | * @param RefreshRate.set in Hz The time between every servo pulse. 20000 us = 50 Hz(standard) (us) |
sierrasmith71 | 2:466f005a4dd5 | 88 | Analog servos typically use a 50Hz refresh rate, while digital servos can use up to a 300Hz refresh rate. |
sierrasmith71 | 2:466f005a4dd5 | 89 | * @parm _speed The time between updating the pulse width of the servo pulses --controls speed of servo movement |
jdenkers | 1:352133517ccc | 90 | */ |
sierrasmith71 | 2:466f005a4dd5 | 91 | void Enable(int StartPos, int RefreshRate, int _speed); |
jdenkers | 1:352133517ccc | 92 | |
jdenkers | 1:352133517ccc | 93 | /** Disable the servo. After disabling the servo won't get any signal anymore |
jdenkers | 1:352133517ccc | 94 | * |
sierrasmith71 | 3:774dd54867f2 | 95 | */ |
sierrasmith71 | 3:774dd54867f2 | 96 | void Disable(); |
sierrasmith71 | 3:774dd54867f2 | 97 | |
sierrasmith71 | 3:774dd54867f2 | 98 | /** determines direction of servo's move, called by Ticker Speed |
jdenkers | 1:352133517ccc | 99 | */ |
sierrasmith71 | 2:466f005a4dd5 | 100 | void Update(); |
sierrasmith71 | 3:774dd54867f2 | 101 | |
sierrasmith71 | 2:466f005a4dd5 | 102 | int speed; |
sierrasmith71 | 2:466f005a4dd5 | 103 | int CurrentPosition; |
sierrasmith71 | 3:774dd54867f2 | 104 | |
jdenkers | 0:30b972d2dcec | 105 | |
jdenkers | 0:30b972d2dcec | 106 | private: |
jdenkers | 0:30b972d2dcec | 107 | void StartPulse(); |
jdenkers | 0:30b972d2dcec | 108 | void EndPulse(); |
jdenkers | 0:30b972d2dcec | 109 | int Position; |
sierrasmith71 | 2:466f005a4dd5 | 110 | int Period; |
sierrasmith71 | 2:466f005a4dd5 | 111 | int NewPosition; |
jdenkers | 0:30b972d2dcec | 112 | DigitalOut ServoPin; |
jdenkers | 0:30b972d2dcec | 113 | Ticker Pulse; |
sierrasmith71 | 2:466f005a4dd5 | 114 | Ticker Speed; |
jdenkers | 0:30b972d2dcec | 115 | Timeout PulseStop; |
jdenkers | 0:30b972d2dcec | 116 | }; |
jdenkers | 0:30b972d2dcec | 117 | |
jdenkers | 0:30b972d2dcec | 118 | #endif |