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 SimpleServoControl.h Source File

SimpleServoControl.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 // Derived from forum post http://mbed.org/forum/helloworld/topic/2303
00024 
00025 #ifndef AJK_SIMPLESERVOCONTROL_H
00026 #define AJK_SIMPLESERVOCONTROL_H
00027 
00028 #include "mbed.h"
00029 #include "SimpleRCservos.h"
00030 
00031 namespace AjK {
00032 
00033 /** SimpleServoControl
00034  *
00035  * Very simple servo controller class.
00036  * 
00037  * @see http://mbed.org/forum/helloworld/topic/2303
00038  */
00039 class SimpleServoControl {
00040 
00041 protected:
00042     double _current_position;
00043     double _desired_position;
00044     double _step;
00045     int    _poll_interval;
00046     SimpleRCservos::Servo _motor;
00047     SimpleRCservos *_servos;
00048     Ticker _servo_poll;
00049     
00050 public:
00051 
00052     // Constructor.
00053     SimpleServoControl(SimpleRCservos *servos, SimpleRCservos::Servo motor, double min = -90.0, double max = +90.0) 
00054     { 
00055         _servos = servos;
00056         _motor = motor; 
00057         _servos->setLimits(_motor, min, max); // define logical limits.
00058         _servos->enable(_motor); // Enable the PWM outout.
00059         _current_position = 0.0;
00060         _desired_position = 0.0;
00061         _step = 1.0;
00062         _poll_interval = 10000; // 100ms.
00063         _servo_poll.attach_us(this, &SimpleServoControl::poll, _poll_interval); 
00064     }
00065     
00066     void poll(void) 
00067     {
00068         if (_desired_position > _current_position) {
00069             _current_position += _step;
00070             // Don't allow the servo to oscillate around _desired_position.
00071             if (_desired_position < _current_position) { 
00072                 _current_position = _desired_position; 
00073             }
00074             _servos->position(_motor, _current_position);                        
00075         }
00076         else if (_desired_position < _current_position) {
00077             _current_position -= _step;
00078             // Don't allow the servo to oscillate around _desired_position.
00079             if (_desired_position > _current_position) {
00080                 _current_position = _desired_position; 
00081             }
00082             _servos->position(_motor, _current_position);            
00083         }
00084     }
00085     
00086     void position(double position = 90.0)  // spins the servo 90º to the left
00087     {
00088         _desired_position = position;
00089     }
00090     
00091     void setStep(double d) 
00092     { 
00093         _step = d; 
00094     }
00095     
00096     double getStep(void) 
00097     { 
00098         return _step; 
00099     } 
00100     
00101     void setPollInterval(int i) 
00102     { 
00103         _poll_interval = i;
00104         _servo_poll.detach();
00105         _servo_poll.attach_us(this, &SimpleServoControl::poll, _poll_interval); 
00106     }
00107     
00108     int getPollInterval(void) 
00109     { 
00110         return _poll_interval; 
00111     }
00112     
00113 };
00114 
00115 }; // namespace AjK ends.
00116 
00117 using namespace AjK;
00118 
00119 #endif