Kallums Fork

Fork of SteeringServo by James Batchelar

Committer:
Kallum
Date:
Fri Sep 08 00:41:18 2017 +0000
Revision:
3:f81145ed8daa
Parent:
1:88fe796b4c2e
Fixed errors with the current pulse width

Who changed what in which revision?

UserRevisionLine numberNew contents of line
batchee7 0:5141c5e51f39 1 #ifndef SteeringServo_H
batchee7 0:5141c5e51f39 2 #define SteeringServo_H
batchee7 0:5141c5e51f39 3 #include "mbed.h"
batchee7 0:5141c5e51f39 4
batchee7 0:5141c5e51f39 5 /** NAME: SteeringServo
batchee7 0:5141c5e51f39 6 * AUTHOR: J.BATCHELAR
batchee7 0:5141c5e51f39 7 *
batchee7 0:5141c5e51f39 8 * @section LICENSE
batchee7 0:5141c5e51f39 9 *
batchee7 0:5141c5e51f39 10 * Permission is hereby granted, free of charge, to any person obtaining a copy
batchee7 0:5141c5e51f39 11 * of this software and associated documentation files (the "Software"), to deal
batchee7 0:5141c5e51f39 12 * in the Software without restriction, including without limitation the rights
batchee7 0:5141c5e51f39 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
batchee7 0:5141c5e51f39 14 * copies of the Software, and to permit persons to whom the Software is
batchee7 0:5141c5e51f39 15 * furnished to do so, subject to the following conditions:
batchee7 0:5141c5e51f39 16 *
batchee7 0:5141c5e51f39 17 * The above copyright notice and this permission notice shall be included in
batchee7 0:5141c5e51f39 18 * all copies or substantial portions of the Software.
batchee7 0:5141c5e51f39 19 *
batchee7 0:5141c5e51f39 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
batchee7 0:5141c5e51f39 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
batchee7 0:5141c5e51f39 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
batchee7 0:5141c5e51f39 23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
batchee7 0:5141c5e51f39 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
batchee7 0:5141c5e51f39 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
batchee7 0:5141c5e51f39 26 * THE SOFTWARE.
batchee7 0:5141c5e51f39 27 *
batchee7 0:5141c5e51f39 28 * @section DESCRIPTION
batchee7 0:5141c5e51f39 29 *
batchee7 0:5141c5e51f39 30 * Used with Tower Pro 9g Servo Motors (http://www.micropik.com/PDF/SG90Servo.pdf) to create a steering mechanism.
batchee7 0:5141c5e51f39 31 *
batchee7 0:5141c5e51f39 32 * Example:
batchee7 0:5141c5e51f39 33 * @code
batchee7 0:5141c5e51f39 34
batchee7 0:5141c5e51f39 35 // -- Code to move steering wheel between extreme limits
batchee7 0:5141c5e51f39 36 #include "mbed.h"
batchee7 0:5141c5e51f39 37 #include "SteeringServo.h"
batchee7 0:5141c5e51f39 38
batchee7 0:5141c5e51f39 39 DigitalIn bttn(PC_13);
batchee7 0:5141c5e51f39 40 DigitalOut led(LED1);
batchee7 0:5141c5e51f39 41
batchee7 0:5141c5e51f39 42 Serial pc(USBTX, USBRX);
batchee7 0:5141c5e51f39 43 SteeringServo steerMech(PA_10, 1650, 1000, 2300);
batchee7 0:5141c5e51f39 44
batchee7 0:5141c5e51f39 45 int main() {
batchee7 0:5141c5e51f39 46 led = 1;
batchee7 0:5141c5e51f39 47 pc.printf("\nServo Example\n");
batchee7 0:5141c5e51f39 48
batchee7 0:5141c5e51f39 49 while(1){
batchee7 0:5141c5e51f39 50 steerMech.goStraight(); //-- Don't call constantly(as you are writing into register need to allow time for value to set)!
batchee7 0:5141c5e51f39 51 //-- Wait for button to be pressed
batchee7 0:5141c5e51f39 52 while(bttn){};
batchee7 0:5141c5e51f39 53 steerMech.goToAngle(1000); //-- Go Left
batchee7 0:5141c5e51f39 54 wait(5);
batchee7 0:5141c5e51f39 55 steerMech.goToAngle(2300); //-- Go Left
batchee7 0:5141c5e51f39 56 wait(5);
batchee7 0:5141c5e51f39 57 }
batchee7 0:5141c5e51f39 58 }
batchee7 0:5141c5e51f39 59
batchee7 0:5141c5e51f39 60 * @endcode
batchee7 0:5141c5e51f39 61 */
batchee7 0:5141c5e51f39 62
batchee7 0:5141c5e51f39 63 class SteeringServo{
batchee7 0:5141c5e51f39 64 public:
batchee7 0:5141c5e51f39 65 /** Create a Clamp Servo Object
batchee7 0:5141c5e51f39 66 *
batchee7 0:5141c5e51f39 67 * @param pin The PWM Pin that the Servo Is Connected To
batchee7 0:5141c5e51f39 68 * @param centrePW (us) - Pulse Width to move to drive straight
batchee7 0:5141c5e51f39 69 * @param minPW (us) - Minimum pulse width that servo can move to (due to mechanical constraints)
batchee7 0:5141c5e51f39 70 * @param maxPW (us) - Maximum pulse width that servo can move to (due to mechanical constraints)
batchee7 0:5141c5e51f39 71 */
batchee7 0:5141c5e51f39 72 SteeringServo(PinName pin, int centrePW, int minPW, int maxPW);
batchee7 0:5141c5e51f39 73
batchee7 0:5141c5e51f39 74 /** Check if the Clamp is Open
batchee7 0:5141c5e51f39 75 * @param pulseWidth (us) - Pulse Width to move rotate to (is internally constrained by limits)
batchee7 0:5141c5e51f39 76 */
batchee7 0:5141c5e51f39 77 void goToAngle(int pulseWidth);
batchee7 0:5141c5e51f39 78
batchee7 0:5141c5e51f39 79
batchee7 0:5141c5e51f39 80 /** Goto the centre position. (Drive Straight)
batchee7 0:5141c5e51f39 81 *
batchee7 0:5141c5e51f39 82 */
Kallum 1:88fe796b4c2e 83 void goStraight(void);
Kallum 1:88fe796b4c2e 84
Kallum 1:88fe796b4c2e 85 /** Get the currentPulseWidth
Kallum 1:88fe796b4c2e 86 *
Kallum 1:88fe796b4c2e 87 */
Kallum 3:f81145ed8daa 88 int getPulseWidth(void);
batchee7 0:5141c5e51f39 89
batchee7 0:5141c5e51f39 90 private:
batchee7 0:5141c5e51f39 91 PwmOut servo_; // -- PWM Object
batchee7 0:5141c5e51f39 92 int minimumPulseWidth_; // -- (us) Minimum pulse width that servo can move to (due to mechanical constraints)
batchee7 0:5141c5e51f39 93 int maximumPulseWidth_; // -- (us) Maximum pulse width that servo can move to (due to mechanical constraints)
batchee7 0:5141c5e51f39 94 int centrePulseWidth_; // -- (us) Pulse Width to move to drive straight
Kallum 1:88fe796b4c2e 95 int currentPulseWidth_; // -- (us) Current Pulse Width of the servo
batchee7 0:5141c5e51f39 96 };
batchee7 0:5141c5e51f39 97
batchee7 0:5141c5e51f39 98 #endif