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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example1.h Source File

example1.h

00001 /*
00002     Copyright (c) 2011 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021 */
00022 
00023 #include "mbed.h"
00024 #include "SimpleRCservos.h"
00025 
00026 #define N_90 -90.0
00027 #define P_90 +90.0
00028 
00029 SimpleRCservos servos;
00030 
00031 int main() {
00032     double position = 0;
00033 
00034     // The default scaling range is -1.0 to +1.0
00035     // Alter the scaling range to -90.0 to +90.0
00036     servos.setLimits(SimpleRCservos::Servo6, N_90, P_90); // Mbed p21
00037     servos.setLimits(SimpleRCservos::Servo5, N_90, P_90); // Mbed p22
00038     servos.setLimits(SimpleRCservos::Servo4, N_90, P_90); // Mbed p23
00039     
00040     // The default range is 1ms to 2ms with 1.5ms being the neutral.
00041     // This line isn't actually needed as 1ms/2ms is the library's 
00042     // default but is included here to show how altering it may be
00043     // achieved.
00044     servos.setRange(SimpleRCservos::Servo6, 1000, 2000); // Servo6 uses p21
00045     
00046     // Add a servo pulse range, 900us to 2.1ms, neutral 1.5ms.
00047     servos.setRange(SimpleRCservos::Servo5, 900, 2100); // Servo5 uses p22
00048     
00049     // Add a servo pulse range, 600us to 2.4ms, neutral 1.5ms.
00050     servos.setRange(SimpleRCservos::Servo4, 600, 2400); // Servo4 uses p23
00051     
00052     // Enable the Servo (starts producing pulses).
00053     servos.enable(SimpleRCservos::Servo6);
00054     servos.enable(SimpleRCservos::Servo5);
00055     servos.enable(SimpleRCservos::Servo4);
00056     
00057     while(1) {
00058         
00059         // Slide up to max
00060         for(position = 0; position < P_90; position += 0.1) {
00061             servos.position(SimpleRCservos::Servo6, position);
00062             servos.position(SimpleRCservos::Servo5, position);
00063             servos.position(SimpleRCservos::Servo4, position);
00064             wait(0.005);
00065         }
00066         wait(2);
00067         
00068         // Slide down to min
00069         for(position = P_90; position > N_90; position -= 0.1) {
00070             servos.position(SimpleRCservos::Servo6, position);
00071             servos.position(SimpleRCservos::Servo5, position);
00072             servos.position(SimpleRCservos::Servo4, position);
00073             wait(0.005);
00074         }
00075         wait(2);
00076         
00077         // Slide back up to neutral
00078         for(position = N_90; position < 0; position += 0.1) {
00079             servos.position(SimpleRCservos::Servo6, position);
00080             servos.position(SimpleRCservos::Servo5, position);
00081             servos.position(SimpleRCservos::Servo4, position);
00082             wait(0.005);
00083         }
00084         wait(2); 
00085     }       
00086 }
00087