A program using forked servo.h that allows controlling the speed of multiple individual servos. Each servo runs at a different speed if required. ANY output port can be used. (PWM not required)

Dependencies:   VarSpeedServo mbed

Committer:
sierrasmith71
Date:
Wed Oct 14 02:11:24 2015 +0000
Revision:
2:95cabad9292b
Parent:
0:0f740c5bbb7a
fixed initialization of NewPostion in Enable main.cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sierrasmith71 0:0f740c5bbb7a 1 /**
sierrasmith71 0:0f740c5bbb7a 2 * This programm demonstrates the changes and additions I made to :mbed Servo Library without using PWM pins
sierrasmith71 0:0f740c5bbb7a 3 * Copyright (c) 2010 Jasper Denkers
sierrasmith71 0:0f740c5bbb7a 4 * that allows independent cotrol of many servo's speed. The original Servo.h cleverly uses a mix of MBED Ticker and Timeout functions
sierrasmith71 0:0f740c5bbb7a 5 * to control the servos. Both of these functions are interrupt driven and so operate in the background. I added some more of these functions
sierrasmith71 0:0f740c5bbb7a 6 * to add the Variable Speed function.
sierrasmith71 0:0f740c5bbb7a 7 *
sierrasmith71 0:0f740c5bbb7a 8 * This program was tested on a Nucleo 152RE with a 40MHz clock and a ST DISCO F726NG running at 216MHz.
sierrasmith71 0:0f740c5bbb7a 9 *
sierrasmith71 0:0f740c5bbb7a 10 * ANY pin on the MBED board, that can be configured as an output, may be used to drive a servo
sierrasmith71 0:0f740c5bbb7a 11 *
sierrasmith71 0:0f740c5bbb7a 12 * Use of the library is both simple and straight forward;
sierrasmith71 0:0f740c5bbb7a 13 * create a new instance of the Servo object
sierrasmith71 0:0f740c5bbb7a 14 * enable that object setting the servo starting position, refesh rate, and desired servo speed
sierrasmith71 0:0f740c5bbb7a 15 *
sierrasmith71 0:0f740c5bbb7a 16 * write a program and use the statement --for example: Servo1.SetPosition(2300); and the servo will move, at the speed you specified.
sierrasmith71 0:0f740c5bbb7a 17 * the wait() statements in the example program are there to cause a pause to allow the servos to make a full transit, before returning, as they are set to a rather slow speed.
sierrasmith71 0:0f740c5bbb7a 18 * The slow rate was chosen to demonstrate the library's ability to fully control a servo's speed.
sierrasmith71 0:0f740c5bbb7a 19 * notes:
sierrasmith71 0:0f740c5bbb7a 20 * The refresh rate for Analog servos is almost always 50 Hz or every 20mS. If you are using digital servos than a faster refresh rate can be used, up to 300 Hz
sierrasmith71 0:0f740c5bbb7a 21 * Servos draw a lot of power so do not attempt to use the MBED +5V to power more than, say , one 9g small servo. A separate power supply is required for the servos.
sierrasmith71 0:0f740c5bbb7a 22 * Make sure to combine the board ground and servo power supply ground.
sierrasmith71 0:0f740c5bbb7a 23 *
sierrasmith71 0:0f740c5bbb7a 24 */
sierrasmith71 0:0f740c5bbb7a 25
sierrasmith71 0:0f740c5bbb7a 26
sierrasmith71 0:0f740c5bbb7a 27 // Keep sweeping 2 servos from left to right at a slow rate.
sierrasmith71 0:0f740c5bbb7a 28
sierrasmith71 0:0f740c5bbb7a 29 #include "mbed.h"
sierrasmith71 0:0f740c5bbb7a 30 #include "Servo.h"
sierrasmith71 0:0f740c5bbb7a 31
sierrasmith71 0:0f740c5bbb7a 32
sierrasmith71 0:0f740c5bbb7a 33 int main()
sierrasmith71 0:0f740c5bbb7a 34 {
sierrasmith71 0:0f740c5bbb7a 35
sierrasmith71 0:0f740c5bbb7a 36 Servo Servo1(D12); // create new instance of servo with output pin number
sierrasmith71 0:0f740c5bbb7a 37 Servo Servo2(D10);
sierrasmith71 0:0f740c5bbb7a 38 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 0:0f740c5bbb7a 39 Servo2.Enable(1500,50,10);
sierrasmith71 2:95cabad9292b 40
sierrasmith71 2:95cabad9292b 41 wait(5.0);
sierrasmith71 0:0f740c5bbb7a 42 while(1) {
sierrasmith71 0:0f740c5bbb7a 43
sierrasmith71 0:0f740c5bbb7a 44 Servo1.SetPosition(700);
sierrasmith71 0:0f740c5bbb7a 45 Servo2.SetPosition(700);
sierrasmith71 0:0f740c5bbb7a 46 wait(2.5);
sierrasmith71 2:95cabad9292b 47
sierrasmith71 2:95cabad9292b 48 Servo1.SetPosition(2300);
sierrasmith71 2:95cabad9292b 49 Servo2.SetPosition(2300);
sierrasmith71 2:95cabad9292b 50 wait(2.5);
sierrasmith71 0:0f740c5bbb7a 51 }
sierrasmith71 0:0f740c5bbb7a 52
sierrasmith71 0:0f740c5bbb7a 53 }
sierrasmith71 0:0f740c5bbb7a 54
sierrasmith71 0:0f740c5bbb7a 55
sierrasmith71 0:0f740c5bbb7a 56
sierrasmith71 0:0f740c5bbb7a 57