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:
2:d6a018232650
1.3 See ChangeLog.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 2:d6a018232650 1 /*
AjK 2:d6a018232650 2 Copyright (c) 2011 Andy Kirkham
AjK 2:d6a018232650 3
AjK 2:d6a018232650 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 2:d6a018232650 5 of this software and associated documentation files (the "Software"), to deal
AjK 2:d6a018232650 6 in the Software without restriction, including without limitation the rights
AjK 2:d6a018232650 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 2:d6a018232650 8 copies of the Software, and to permit persons to whom the Software is
AjK 2:d6a018232650 9 furnished to do so, subject to the following conditions:
AjK 2:d6a018232650 10
AjK 2:d6a018232650 11 The above copyright notice and this permission notice shall be included in
AjK 2:d6a018232650 12 all copies or substantial portions of the Software.
AjK 2:d6a018232650 13
AjK 2:d6a018232650 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 2:d6a018232650 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 2:d6a018232650 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 2:d6a018232650 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 2:d6a018232650 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 2:d6a018232650 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 2:d6a018232650 20 THE SOFTWARE.
AjK 2:d6a018232650 21 */
AjK 2:d6a018232650 22
AjK 2:d6a018232650 23 // Derived from forum post http://mbed.org/forum/helloworld/topic/2303
AjK 2:d6a018232650 24
AjK 2:d6a018232650 25 #ifndef AJK_SIMPLESERVOCONTROL_H
AjK 2:d6a018232650 26 #define AJK_SIMPLESERVOCONTROL_H
AjK 2:d6a018232650 27
AjK 2:d6a018232650 28 #include "mbed.h"
AjK 2:d6a018232650 29 #include "SimpleRCservos.h"
AjK 2:d6a018232650 30
AjK 2:d6a018232650 31 namespace AjK {
AjK 2:d6a018232650 32
AjK 2:d6a018232650 33 /** SimpleServoControl
AjK 2:d6a018232650 34 *
AjK 2:d6a018232650 35 * Very simple servo controller class.
AjK 2:d6a018232650 36 *
AjK 2:d6a018232650 37 * @see http://mbed.org/forum/helloworld/topic/2303
AjK 2:d6a018232650 38 */
AjK 2:d6a018232650 39 class SimpleServoControl {
AjK 2:d6a018232650 40
AjK 2:d6a018232650 41 protected:
AjK 2:d6a018232650 42 double _current_position;
AjK 2:d6a018232650 43 double _desired_position;
AjK 2:d6a018232650 44 double _step;
AjK 2:d6a018232650 45 int _poll_interval;
AjK 2:d6a018232650 46 SimpleRCservos::Servo _motor;
AjK 2:d6a018232650 47 SimpleRCservos *_servos;
AjK 2:d6a018232650 48 Ticker _servo_poll;
AjK 2:d6a018232650 49
AjK 2:d6a018232650 50 public:
AjK 2:d6a018232650 51
AjK 2:d6a018232650 52 // Constructor.
AjK 2:d6a018232650 53 SimpleServoControl(SimpleRCservos *servos, SimpleRCservos::Servo motor, double min = -90.0, double max = +90.0)
AjK 2:d6a018232650 54 {
AjK 2:d6a018232650 55 _servos = servos;
AjK 2:d6a018232650 56 _motor = motor;
AjK 2:d6a018232650 57 _servos->setLimits(_motor, min, max); // define logical limits.
AjK 2:d6a018232650 58 _servos->enable(_motor); // Enable the PWM outout.
AjK 2:d6a018232650 59 _current_position = 0.0;
AjK 2:d6a018232650 60 _desired_position = 0.0;
AjK 2:d6a018232650 61 _step = 1.0;
AjK 2:d6a018232650 62 _poll_interval = 10000; // 100ms.
AjK 2:d6a018232650 63 _servo_poll.attach_us(this, &SimpleServoControl::poll, _poll_interval);
AjK 2:d6a018232650 64 }
AjK 2:d6a018232650 65
AjK 2:d6a018232650 66 void poll(void)
AjK 2:d6a018232650 67 {
AjK 2:d6a018232650 68 if (_desired_position > _current_position) {
AjK 2:d6a018232650 69 _current_position += _step;
AjK 2:d6a018232650 70 // Don't allow the servo to oscillate around _desired_position.
AjK 2:d6a018232650 71 if (_desired_position < _current_position) {
AjK 2:d6a018232650 72 _current_position = _desired_position;
AjK 2:d6a018232650 73 }
AjK 2:d6a018232650 74 _servos->position(_motor, _current_position);
AjK 2:d6a018232650 75 }
AjK 2:d6a018232650 76 else if (_desired_position < _current_position) {
AjK 2:d6a018232650 77 _current_position -= _step;
AjK 2:d6a018232650 78 // Don't allow the servo to oscillate around _desired_position.
AjK 2:d6a018232650 79 if (_desired_position > _current_position) {
AjK 2:d6a018232650 80 _current_position = _desired_position;
AjK 2:d6a018232650 81 }
AjK 2:d6a018232650 82 _servos->position(_motor, _current_position);
AjK 2:d6a018232650 83 }
AjK 2:d6a018232650 84 }
AjK 2:d6a018232650 85
AjK 2:d6a018232650 86 void position(double position = 90.0) // spins the servo 90º to the left
AjK 2:d6a018232650 87 {
AjK 2:d6a018232650 88 _desired_position = position;
AjK 2:d6a018232650 89 }
AjK 2:d6a018232650 90
AjK 2:d6a018232650 91 void setStep(double d)
AjK 2:d6a018232650 92 {
AjK 2:d6a018232650 93 _step = d;
AjK 2:d6a018232650 94 }
AjK 2:d6a018232650 95
AjK 2:d6a018232650 96 double getStep(void)
AjK 2:d6a018232650 97 {
AjK 2:d6a018232650 98 return _step;
AjK 2:d6a018232650 99 }
AjK 2:d6a018232650 100
AjK 2:d6a018232650 101 void setPollInterval(int i)
AjK 2:d6a018232650 102 {
AjK 2:d6a018232650 103 _poll_interval = i;
AjK 2:d6a018232650 104 _servo_poll.detach();
AjK 2:d6a018232650 105 _servo_poll.attach_us(this, &SimpleServoControl::poll, _poll_interval);
AjK 2:d6a018232650 106 }
AjK 2:d6a018232650 107
AjK 2:d6a018232650 108 int getPollInterval(void)
AjK 2:d6a018232650 109 {
AjK 2:d6a018232650 110 return _poll_interval;
AjK 2:d6a018232650 111 }
AjK 2:d6a018232650 112
AjK 2:d6a018232650 113 };
AjK 2:d6a018232650 114
AjK 2:d6a018232650 115 }; // namespace AjK ends.
AjK 2:d6a018232650 116
AjK 2:d6a018232650 117 using namespace AjK;
AjK 2:d6a018232650 118
AjK 2:d6a018232650 119 #endif