A simple API/Software interface to the LPC17xx PWM peripheral to provide control to up to six RC type servo controllers.

Committer:
AjK
Date:
Thu May 19 22:48:17 2011 +0000
Revision:
3:1232505e7206
Parent:
1:fbb8acf19e77
1.3 See ChangeLog.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 3:1232505e7206 1 /*
AjK 3:1232505e7206 2 Copyright (c) 2011 Andy Kirkham
AjK 3:1232505e7206 3
AjK 3:1232505e7206 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 3:1232505e7206 5 of this software and associated documentation files (the "Software"), to deal
AjK 3:1232505e7206 6 in the Software without restriction, including without limitation the rights
AjK 3:1232505e7206 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 3:1232505e7206 8 copies of the Software, and to permit persons to whom the Software is
AjK 3:1232505e7206 9 furnished to do so, subject to the following conditions:
AjK 3:1232505e7206 10
AjK 3:1232505e7206 11 The above copyright notice and this permission notice shall be included in
AjK 3:1232505e7206 12 all copies or substantial portions of the Software.
AjK 3:1232505e7206 13
AjK 3:1232505e7206 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 3:1232505e7206 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 3:1232505e7206 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 3:1232505e7206 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 3:1232505e7206 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 3:1232505e7206 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 3:1232505e7206 20 THE SOFTWARE.
AjK 3:1232505e7206 21 */
AjK 0:c680385286e3 22
AjK 0:c680385286e3 23 #include "mbed.h"
AjK 0:c680385286e3 24 #include "SimpleRCservos.h"
AjK 0:c680385286e3 25
AjK 0:c680385286e3 26 #define N_90 -90.0
AjK 0:c680385286e3 27 #define P_90 +90.0
AjK 0:c680385286e3 28
AjK 0:c680385286e3 29 SimpleRCservos servos;
AjK 0:c680385286e3 30
AjK 0:c680385286e3 31 int main() {
AjK 0:c680385286e3 32 double position = 0;
AjK 0:c680385286e3 33
AjK 0:c680385286e3 34 // The default scaling range is -1.0 to +1.0
AjK 0:c680385286e3 35 // Alter the scaling range to -90.0 to +90.0
AjK 0:c680385286e3 36 servos.setLimits(SimpleRCservos::Servo6, N_90, P_90); // Mbed p21
AjK 0:c680385286e3 37 servos.setLimits(SimpleRCservos::Servo5, N_90, P_90); // Mbed p22
AjK 0:c680385286e3 38 servos.setLimits(SimpleRCservos::Servo4, N_90, P_90); // Mbed p23
AjK 0:c680385286e3 39
AjK 0:c680385286e3 40 // The default range is 1ms to 2ms with 1.5ms being the neutral.
AjK 0:c680385286e3 41 // This line isn't actually needed as 1ms/2ms is the library's
AjK 0:c680385286e3 42 // default but is included here to show how altering it may be
AjK 0:c680385286e3 43 // achieved.
AjK 1:fbb8acf19e77 44 servos.setRange(SimpleRCservos::Servo6, 1000, 2000); // Servo6 uses p21
AjK 0:c680385286e3 45
AjK 0:c680385286e3 46 // Add a servo pulse range, 900us to 2.1ms, neutral 1.5ms.
AjK 1:fbb8acf19e77 47 servos.setRange(SimpleRCservos::Servo5, 900, 2100); // Servo5 uses p22
AjK 0:c680385286e3 48
AjK 0:c680385286e3 49 // Add a servo pulse range, 600us to 2.4ms, neutral 1.5ms.
AjK 0:c680385286e3 50 servos.setRange(SimpleRCservos::Servo4, 600, 2400); // Servo4 uses p23
AjK 0:c680385286e3 51
AjK 0:c680385286e3 52 // Enable the Servo (starts producing pulses).
AjK 0:c680385286e3 53 servos.enable(SimpleRCservos::Servo6);
AjK 0:c680385286e3 54 servos.enable(SimpleRCservos::Servo5);
AjK 0:c680385286e3 55 servos.enable(SimpleRCservos::Servo4);
AjK 0:c680385286e3 56
AjK 0:c680385286e3 57 while(1) {
AjK 0:c680385286e3 58
AjK 0:c680385286e3 59 // Slide up to max
AjK 0:c680385286e3 60 for(position = 0; position < P_90; position += 0.1) {
AjK 0:c680385286e3 61 servos.position(SimpleRCservos::Servo6, position);
AjK 0:c680385286e3 62 servos.position(SimpleRCservos::Servo5, position);
AjK 0:c680385286e3 63 servos.position(SimpleRCservos::Servo4, position);
AjK 0:c680385286e3 64 wait(0.005);
AjK 0:c680385286e3 65 }
AjK 0:c680385286e3 66 wait(2);
AjK 0:c680385286e3 67
AjK 0:c680385286e3 68 // Slide down to min
AjK 0:c680385286e3 69 for(position = P_90; position > N_90; position -= 0.1) {
AjK 0:c680385286e3 70 servos.position(SimpleRCservos::Servo6, position);
AjK 0:c680385286e3 71 servos.position(SimpleRCservos::Servo5, position);
AjK 0:c680385286e3 72 servos.position(SimpleRCservos::Servo4, position);
AjK 0:c680385286e3 73 wait(0.005);
AjK 0:c680385286e3 74 }
AjK 0:c680385286e3 75 wait(2);
AjK 0:c680385286e3 76
AjK 0:c680385286e3 77 // Slide back up to neutral
AjK 0:c680385286e3 78 for(position = N_90; position < 0; position += 0.1) {
AjK 0:c680385286e3 79 servos.position(SimpleRCservos::Servo6, position);
AjK 0:c680385286e3 80 servos.position(SimpleRCservos::Servo5, position);
AjK 0:c680385286e3 81 servos.position(SimpleRCservos::Servo4, position);
AjK 0:c680385286e3 82 wait(0.005);
AjK 0:c680385286e3 83 }
AjK 0:c680385286e3 84 wait(2);
AjK 0:c680385286e3 85 }
AjK 0:c680385286e3 86 }
AjK 0:c680385286e3 87