A simple API/Software interface to the LPC17xx PWM peripheral to provide control to up to six RC type servo controllers.
Revision 0:c680385286e3, committed 2011-05-02
- Comitter:
- AjK
- Date:
- Mon May 02 18:38:45 2011 +0000
- Child:
- 1:fbb8acf19e77
- Commit message:
- 1.0 See ChangeLog.h
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/ChangeLog.h Mon May 02 18:38:45 2011 +0000 @@ -0,0 +1,9 @@ +/* + +SimpleRCservos change log history. +================================== + +1.0 2/May/2011 + * Initial release. + +*/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/SimpleRCmbed.h Mon May 02 18:38:45 2011 +0000 @@ -0,0 +1,85 @@ +/**************************************************************************** + * Product: LIB17 Open Source Library + * + * Steller Technologies Limited + * ---------------------------- + * + * Copyright (C) 2002-2011 Steller Technologies Limited. All rights reserved. + * + * This software may be distributed and modified under the terms of the GNU + * General Public License version 2 (GPL) as published by the Free Software + * Foundation and appearing in the file GPL.TXT included in the packaging of + * this file. Please note that GPL Section 2[b] requires that all works based + * on this software must also be made publicly available under the terms of + * the GPL ("Copyleft"). + * + * Alternatively, this software may be distributed and modified under the + * terms of Steller Technologies Limited commercial licenses, which expressly + * supersede the GPL and are specifically designed for licensees interested in + * retaining the proprietary status of their code. + * + ***************************************************************************/ + +#ifndef AJK_SIMPLERCMBED_H +#define AJK_SIMPLERCMBED_H + +#ifndef MBED_PINNAMES_H +#define MBED_PINNAMES_H + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + P0_0 = LPC_GPIO0_BASE, P0_1, P0_2, P0_3, P0_4, P0_5, P0_6, P0_7 + , P0_8, P0_9, P0_10, P0_11, P0_12, P0_13, P0_14, P0_15 + , P0_16, P0_17, P0_18, P0_19, P0_20, P0_21, P0_22, P0_23 + , P0_24, P0_25, P0_26, P0_27, P0_28, P0_29, P0_30, P0_31 + , P1_0, P1_1, P1_2, P1_3, P1_4, P1_5, P1_6, P1_7 + , P1_8, P1_9, P1_10, P1_11, P1_12, P1_13, P1_14, P1_15 + , P1_16, P1_17, P1_18, P1_19, P1_20, P1_21, P1_22, P1_23 + , P1_24, P1_25, P1_26, P1_27, P1_28, P1_29, P1_30, P1_31 + , P2_0, P2_1, P2_2, P2_3, P2_4, P2_5, P2_6, P2_7 + , P2_8, P2_9, P2_10, P2_11, P2_12, P2_13, P2_14, P2_15 + , P2_16, P2_17, P2_18, P2_19, P2_20, P2_21, P2_22, P2_23 + , P2_24, P2_25, P2_26, P2_27, P2_28, P2_29, P2_30, P2_31 + , P3_0, P3_1, P3_2, P3_3, P3_4, P3_5, P3_6, P3_7 + , P3_8, P3_9, P3_10, P3_11, P3_12, P3_13, P3_14, P3_15 + , P3_16, P3_17, P3_18, P3_19, P3_20, P3_21, P3_22, P3_23 + , P3_24, P3_25, P3_26, P3_27, P3_28, P3_29, P3_30, P3_31 + , P4_0, P4_1, P4_2, P4_3, P4_4, P4_5, P4_6, P4_7 + , P4_8, P4_9, P4_10, P4_11, P4_12, P4_13, P4_14, P4_15 + , P4_16, P4_17, P4_18, P4_19, P4_20, P4_21, P4_22, P4_23 + , P4_24, P4_25, P4_26, P4_27, P4_28, P4_29, P4_30, P4_31 + + // Mbed DIP Pin Names + , p5 = P0_9, p6 = P0_8, p7 = P0_7, p8 = P0_6, p9 = P0_0, p10 = P0_1 + , p11 = P0_18, p12 = P0_17, p13 = P0_15, p14 = P0_16, p15 = P0_23 + , p16 = P0_24, p17 = P0_25, p18 = P0_26, p19 = P1_30, p20 = P1_31 + , p21 = P2_5, p22 = P2_4, p23 = P2_3, p24 = P2_2, p25 = P2_1 + , p26 = P2_0, p27 = P0_11, p28 = P0_10, p29 = P0_5, p30 = P0_4 + + // Other Mbed Pin Names + , LED1 = P1_18, LED2 = P1_20, LED3 = P1_21, LED4 = P1_23 + , USBTX = P0_2, USBRX = P0_3 + + // Not connected + , NC = (int)0xFFFFFFFF + +} PinName; + +typedef enum { + PullUp = 0 + , Repeater = 1 + , PullNone = 2 + , PullDown = 3 + , OpenDrain = 4 +} PinMode; + +#ifdef __cplusplus +} +#endif + +#endif /* MBED_PINNAMES_H */ + +#endif /* AJK_SIMPLERCMBED_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/SimpleRCservos.h Mon May 02 18:38:45 2011 +0000 @@ -0,0 +1,247 @@ +/* + Copyright (c) 2011 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef AJK_SIMPLERCSERVOS_H +#define AJK_SIMPLERCSERVOS_H + +#include "LPC17xx.h" +#include "SimpleRCmbed.h" + +namespace AjK { + +class SimpleRCservos { + +public: + enum Servo { NoServo0, Servo1, Servo2, Servo3, Servo4, Servo5, Servo6, NumOfServos }; + +protected: + PinName _pinName; + + uint32_t _duty; + + uint32_t _mid[NumOfServos]; + + double _limitMax[NumOfServos]; + + double _limitMin[NumOfServos]; + + uint32_t _msMin[NumOfServos]; + + uint32_t _msMax[NumOfServos]; + + void init(uint32_t duty); + + void setRangeMin(Servo ch, uint32_t u) { _msMin[ch] = u * 24; } + + void setRangeMax(Servo ch, uint32_t u) { _msMax[ch] = (u * 24) - _msMin[ch]; } + + void setMRx(Servo ch, uint32_t value); + + double limit(Servo ch, double degrees); + +public: + + /** Constructor + */ + SimpleRCservos() { init(480000UL); setDuty(); } + + /** setDuty + * + * Set's the duty, or period, of the PWM pulses. + * The default is 20ms and the parameter passed in is the + * number of "clock ticks" that represents the period. + * 480000 gives rise to 20ms by default. It's rare that + * you would alter this value. Remember, with the PWM + * peripheral there is only a single, common, period for + * all six PWM outputs. + * + * @param duty The number of "clock ticks" for the duty cycle. + */ + void setDuty(uint32_t duty = 480000); + + /** enable1 + * + * Enable PWM channel 1. + * + * @param PinName p26(P2_0) or LED1(P1_18) + */ + void enable1(PinName pin = p26); + + /** enable2 + * + * Enable PWM channel 2. + * + * @param PinName p25(P2_1) or LED2(P1_20) + */ + void enable2(PinName pin = p25); + + /** enable3 + * + * Enable PWM channel 3. + * + * @param PinName p24(P2_2) or LED3(P1_21) + */ + void enable3(PinName pin = p24); + + /** enable4 + * + * Enable PWM channel 4. + * + * @param PinName p23(P2_3) or LED4(P1_23) + */ + void enable4(PinName pin = p23); + + /** enable5 + * + * Enable PWM channel 5. + * + * @param PinName p22(P2_4) or P1_24 + */ + void enable5(PinName pin = p22); + + /** enable6 + * + * Enable PWM channel 6. + * + * @param PinName p21(P2_5) or P1_26 + */ + void enable6(PinName pin = p21); + + /** enable + * + * Enable the specified channel. The default pin on port 2 is assumed. + * + * @param Servo The channel to enable, Servo1 to Servo6 + */ + void enable(Servo ch); + + /** setLimitMin + * + * Set the minimum logical limit for the specified channel. + * + * @param Servo channel + * @param double The lower logical limit. + */ + void setLimitMin(Servo ch, double d) { _limitMin[ch] = d; } + + /** getLimitMin + * + * Get the minimum logical limit for the specified channel. + * + * @param Servo channel + * @return double The lower logical limit. + */ + double getlimitMin(Servo ch) { return _limitMin[ch]; } + + /** setLimitMax + * + * Set the maximum logical limit for the specified channel. + * + * @param Servo channel + * @param double The upper logical limit. + */ + void setLimitMax(Servo ch, double d) { _limitMax[ch] = d; } + + /** getLimitMax + * + * Get the maximum logical limit for the specified channel. + * + * @param Servo channel + * @return double The upper logical limit. + */ + double getLimitMax(Servo ch) { return _limitMax[ch]; } + + /** setLimits + * + * Set the logical upper and lower limits for channel. + * + * @param Servo channel + * @param double The minimum logical limit + * @param double The maximum logical limit + */ + void setLimits(Servo ch, double min, double max) { + _limitMin[ch] = min; + _limitMax[ch] = max; + } + + /** getRangeMin + * + * Set the minimum physical limit for the specified channel. + * + * @param Servo channel + * @param double The lower phyiscal limit in microseconds + */ + uint32_t getRangeMin(Servo ch, uint32_t u) { return _msMin[ch] / 24; } + + /** getRangeMax + * + * Set the maximum physical limit for the specified channel. + * + * @param Servo channel + * @param uint32 The upper phyiscal limit in microseconds + */ + uint32_t getRangeMax(Servo ch, uint32_t u) { return (_msMax[ch] / 24) + _msMin[ch]; } + + /** setRange + * + * Set the minimum and maximum physical limits for the specified channel. + * + * @param Servo channel + * @param uint32 The lower phyiscal limit in microseconds + * @param uint32 The upper phyiscal limit in microseconds + */ + void setRange(Servo ch, uint32_t min, uint32_t max) { + setRangeMin(ch, min); + setRangeMax(ch, max); + } + + /** position + * + * Set the desired position of the servo. + * This value should be between the lower and upper logical limits. + * + * Under normal circumstances this fuction returns the same position is + * was supplied. However, if the supplied position is outside the logical + * system limits we return the value we actually went to. This is almost + * always the value of the logical limit that is set for that direction. + * + * @param Servo channel + * @param double the logical position to move to. + * @return double The actual calculated position moved to. + */ + double position(Servo ch, double degrees); + + /** neutral + * + * Tell the servo to move to it's neutral position (1.5ms pulse width) + * + * @param Servo channel + */ + void neutral(Servo ch); + +}; // class RCservo ends. + +}; // namespace AjK ends. + +using namespace AjK; + +#endif /* AJK_SIMPLERCSERVOS_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/example1.h Mon May 02 18:38:45 2011 +0000 @@ -0,0 +1,67 @@ + + +#include "mbed.h" +#include "SimpleRCservos.h" + +#define N_90 -90.0 +#define P_90 +90.0 + +SimpleRCservos servos; + +int main() { + double position = 0; + + // The default scaling range is -1.0 to +1.0 + // Alter the scaling range to -90.0 to +90.0 + servos.setLimits(SimpleRCservos::Servo6, N_90, P_90); // Mbed p21 + servos.setLimits(SimpleRCservos::Servo5, N_90, P_90); // Mbed p22 + servos.setLimits(SimpleRCservos::Servo4, N_90, P_90); // Mbed p23 + + // The default range is 1ms to 2ms with 1.5ms being the neutral. + // This line isn't actually needed as 1ms/2ms is the library's + // default but is included here to show how altering it may be + // achieved. + servos.setRange(SimpleRCservos::Servo6, 1000, 2000); // Servo4 uses p21 + + // Add a servo pulse range, 900us to 2.1ms, neutral 1.5ms. + servos.setRange(SimpleRCservos::Servo5, 900, 2100); // Servo4 uses p22 + + // Add a servo pulse range, 600us to 2.4ms, neutral 1.5ms. + servos.setRange(SimpleRCservos::Servo4, 600, 2400); // Servo4 uses p23 + + // Enable the Servo (starts producing pulses). + servos.enable(SimpleRCservos::Servo6); + servos.enable(SimpleRCservos::Servo5); + servos.enable(SimpleRCservos::Servo4); + + while(1) { + + // Slide up to max + for(position = 0; position < P_90; position += 0.1) { + servos.position(SimpleRCservos::Servo6, position); + servos.position(SimpleRCservos::Servo5, position); + servos.position(SimpleRCservos::Servo4, position); + wait(0.005); + } + wait(2); + + // Slide down to min + for(position = P_90; position > N_90; position -= 0.1) { + servos.position(SimpleRCservos::Servo6, position); + servos.position(SimpleRCservos::Servo5, position); + servos.position(SimpleRCservos::Servo4, position); + wait(0.005); + } + wait(2); + + // Slide back up to neutral + for(position = N_90; position < 0; position += 0.1) { + servos.position(SimpleRCservos::Servo6, position); + servos.position(SimpleRCservos::Servo5, position); + servos.position(SimpleRCservos::Servo4, position); + wait(0.005); + } + wait(2); + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SimpleRCservos.cpp Mon May 02 18:38:45 2011 +0000 @@ -0,0 +1,226 @@ + + +#include "mbed.h" +#include "SimpleRCservos.h" + +namespace AjK { + +void +SimpleRCservos::init(uint32_t duty) +{ + _duty = duty; + + for (int i = 0; i < NumOfServos; i++) { + _limitMin[i] = -1.0; + _limitMax[i] = +1.0; + + // The following would give 1ms to 2ms + setRange((Servo)i, 1000, 2000); + + // The following would give 900us to 2.1ms as required Hitec HS-322HD servo module. + //setRange((Servo)i, 900, 2100); + + // The following would give 600us to 2.4ms + //setRange((Servo)i, 600, 2400); + + _mid[i] = duty * 0.075f; // 1.5ms + + } +} + +void +SimpleRCservos::setDuty(uint32_t duty) +{ + // Ensure powered up (default is 1) + LPC_SC->PCONP |= (1UL << 6); + + // CCLK/4 = 24MHz + LPC_SC->PCLKSEL0 &= ~(3UL << 12); + + // Reset. + LPC_PWM1->TCR = 2; + + _duty = duty; + + LPC_PWM1->PR = 0; + LPC_PWM1->MR0 = _duty; + LPC_PWM1->MR1 = 0; + LPC_PWM1->MR2 = 0; + LPC_PWM1->MR3 = 0; + LPC_PWM1->MR4 = 0; + LPC_PWM1->MR5 = 0; + LPC_PWM1->MR6 = 0; + + LPC_PWM1->MCR = 2; // MR0 resets TC. + + LPC_PWM1->TCR = 9; // Enable. +} + +double +SimpleRCservos::position(Servo ch, double pos) +{ + pos = limit(ch, pos); + double a = pos - _limitMin[ch]; + double set = a / (_limitMax[ch] - _limitMin[ch]); + uint32_t ps = _msMax[ch] * set; + setMRx(ch, ps + _msMin[ch]); + return pos; +} + +void +SimpleRCservos::neutral(Servo ch) +{ + setMRx(ch, _mid[ch]); +} + +double +SimpleRCservos::limit(Servo ch, double pos) +{ + if (pos >= _limitMin[ch] && pos <= _limitMax[ch]) return pos; + if (pos < _limitMin[ch]) return _limitMin[ch]; + if (pos > _limitMax[ch]) return _limitMax[ch]; + return 0.0; // Keep the compiler happy. +} + +void +SimpleRCservos::setMRx(Servo ch, uint32_t value) +{ + switch(ch) { + case Servo1: LPC_PWM1->MR1 = value; break; + case Servo2: LPC_PWM1->MR2 = value; break; + case Servo3: LPC_PWM1->MR3 = value; break; + case Servo4: LPC_PWM1->MR4 = value; break; + case Servo5: LPC_PWM1->MR5 = value; break; + case Servo6: LPC_PWM1->MR6 = value; break; + } + LPC_PWM1->LER |= (1UL << ch); +} + +void +SimpleRCservos::enable(Servo ch) +{ + switch(ch) { + case 1: enable1(); break; + case 2: enable2(); break; + case 3: enable3(); break; + case 4: enable4(); break; + case 5: enable5(); break; + case 6: enable6(); break; + } +} + +void +SimpleRCservos::enable1(PinName pin) +{ + setMRx(Servo1, _mid[1]); + + switch(pin) { + case P2_0: + LPC_PINCON->PINSEL4 &= ~(3UL << 0); // Mbed p26 P2.0 clr bits + LPC_PINCON->PINSEL4 |= (1UL << 0); // Mbed p26 P2.0 set bits + break; + case P1_18: // Mbed LED1 + LPC_PINCON->PINSEL3 &= ~(3UL << 4); // Mbed LED2 P1.18 clr bits + LPC_PINCON->PINSEL3 |= (2UL << 4); // Mbed LED2 P1.18 set bits + break; + } + + LPC_PWM1->PCR |= (1UL << 9); +} + +void +SimpleRCservos::enable2(PinName pin) +{ + setMRx(Servo2, _mid[2]); + + switch(pin) { + case P2_1: + LPC_PINCON->PINSEL4 &= ~(3UL << 2); // Mbed p25 P2.1 clr bits + LPC_PINCON->PINSEL4 |= (1UL << 2); // Mbed p25 P2.1 set bits + break; + case P1_20: // Mbed LED2 + LPC_PINCON->PINSEL3 &= ~(3UL << 8); // Mbed LED2 P1.20 clr bits + LPC_PINCON->PINSEL3 |= (2UL << 8); // Mbed LED2 P1.20 set bits + break; + } + + LPC_PWM1->PCR |= (1UL << 10); +} + +void +SimpleRCservos::enable3(PinName pin) +{ + setMRx(Servo3, _mid[3]); + + switch(pin) { + case P2_2: + LPC_PINCON->PINSEL4 &= ~(3UL << 4); // Mbed p24 P2.2 clr bits + LPC_PINCON->PINSEL4 |= (1UL << 4); // Mbed p24 P2.2 set bits + break; + case P1_21: // Mbed LED3 + LPC_PINCON->PINSEL3 &= ~(3UL << 10); // Mbed LED3 P1.21 clr bits + LPC_PINCON->PINSEL3 |= (2UL << 10); // Mbed LED3 P1.21 set bits + break; + } + + LPC_PWM1->PCR |= (1UL << 11); +} + +void +SimpleRCservos::enable4(PinName pin) +{ + setMRx(Servo4, _mid[4]); + + switch(pin) { + case P2_3: + LPC_PINCON->PINSEL4 &= ~(3UL << 6); // Mbed p23 P2.3 clr bits + LPC_PINCON->PINSEL4 |= (1UL << 6); // Mbed p23 P2.3 set bits + break; + case P1_23: // Mbed LED4 + LPC_PINCON->PINSEL3 &= ~(3UL << 14); // Mbed LED4 P1.23 clr bits + LPC_PINCON->PINSEL3 |= (2UL << 14); // Mbed LED4 P1.23 set bits + break; + } + + LPC_PWM1->PCR |= (1UL << 12); +} + +void +SimpleRCservos::enable5(PinName pin) +{ + setMRx(Servo5, _mid[5]); + + switch(pin) { + case P2_4: // Mbed p22 + LPC_PINCON->PINSEL4 &= ~(3UL << 8); // Mbed p22 P2.4 clr bits + LPC_PINCON->PINSEL4 |= (1UL << 8); // Mbed p22 P2.4 set bits + break; + case P1_24: + LPC_PINCON->PINSEL3 &= ~(3UL << 16); // P1.24 clr bits + LPC_PINCON->PINSEL3 |= (2UL << 16); // P1.24 set bits + break; + } + + LPC_PWM1->PCR |= (1UL << 13); +} + +void +SimpleRCservos::enable6(PinName pin) +{ + setMRx(Servo6, _mid[6]); + + switch(pin) { + case P2_5: // Mbed p21 + LPC_PINCON->PINSEL4 &= ~(3UL << 10); // Mbed p21 P2.5 clr bits + LPC_PINCON->PINSEL4 |= (1UL << 10); // Mbed p21 P2.5 set bits + break; + case P1_26: + LPC_PINCON->PINSEL3 &= ~(3UL << 20); // P1.26 clr bits + LPC_PINCON->PINSEL3 |= (2UL << 20); // P1.26 set bits + break; + } + + LPC_PWM1->PCR |= (1UL << 14); +} + +}; // namespace AjK ends.