Tripple Controller for the TLE5206 H Bridge motor controller
Revision 3:b7d951c6f551, committed 2011-07-05
- Comitter:
- AjK
- Date:
- Tue Jul 05 14:27:56 2011 +0000
- Parent:
- 2:c5fbe0cb8a97
- Child:
- 4:d69f22061c03
- Commit message:
- 0.5 Beta See ChangeLog.h
Changed in this revision
--- a/inc/ChangeLog.h Tue Jul 05 13:50:45 2011 +0000 +++ b/inc/ChangeLog.h Tue Jul 05 14:27:56 2011 +0000 @@ -1,5 +1,13 @@ /* +0.5 Beta 5/Jul/2011 + * Added SimpleTLE5206Profiler.h which extends + the functionality to allow for linear acceleration + and deceleration profiles. + * Added example2.h to demonstrate the profiler. + * Added link to forum post in SimpleTLE5206.h + * Fixed some silly typos in comments. + 0.4 Beta 5/Jul/2011 * Added this file, ChangeLog.h * Added the ability to specify the duty cycle in Hz.
--- a/inc/SimpleTLE5206.h Tue Jul 05 13:50:45 2011 +0000 +++ b/inc/SimpleTLE5206.h Tue Jul 05 14:27:56 2011 +0000 @@ -20,6 +20,10 @@ THE SOFTWARE. */ +/* + See http://mbed.org/forum/mbed/topic/2463/ +*/ + #ifndef AJK_SIMPLETLE5206_H #define AJK_SIMPLETLE5206_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/SimpleTLE5206Profiler.h Tue Jul 05 14:27:56 2011 +0000 @@ -0,0 +1,108 @@ +/* + 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_SIMPLETLE5206PROFILER_H +#define AJK_SIMPLETLE5206PROFILER_H + +#include "mbed.h" +#include "SimpleTLE5206.h" + +namespace AjK { + +class SimpleTLE5206Profiler : public SimpleTLE5206 { + +protected: + Ticker _poll; + double _desired_speed; + double _actual_speed; + double _accel_rate; + double _decel_rate; + int _poll_interval; + + void poll(void) + { + if (_actual_speed < _desired_speed) { + _actual_speed += _accel_rate; + if (_actual_speed > _desired_speed) { + _actual_speed = _desired_speed; + } + SimpleTLE5206::setSpeed(_actual_speed); + } + if (_actual_speed > _desired_speed) { + _actual_speed -= _decel_rate; + if (_actual_speed < _desired_speed) { + _actual_speed = _desired_speed; + } + SimpleTLE5206::setSpeed(_actual_speed); + } + } + +public: + + friend class Ticker; + + SimpleTLE5206Profiler(SimpleTLE5206Output *in1, SimpleTLE5206Output *in2, int duty_cycle_hz) : + SimpleTLE5206(in1, in2, duty_cycle_hz) + { + _desired_speed = 0.0; + _actual_speed = 0.0; + _accel_rate = 0.01; + _decel_rate = 0.01; + _poll_interval = 10000; // 10ms + _poll.attach_us(this, &SimpleTLE5206Profiler::poll, _poll_interval); + } + + int getPollInterval(void) { return _poll_interval; } + void setPollInterval(int i) + { + _poll_interval = i; + _poll.detach(); + _poll.attach_us(this, &SimpleTLE5206Profiler::poll, _poll_interval); + } + + void setAccelRate(double rate) { _accel_rate = rate; } + double getAccelRate(void) { return _accel_rate; } + + void setDecelRate(double rate) { _decel_rate = rate; } + double getDecelRate(void) { return _decel_rate; } + + void setSpeed(double demand_speed) + { + _desired_speed = demand_speed; + } + + double getSpeed(void) { return _actual_speed; } + + void eStop(void) + { + _desired_speed = 0; + SimpleTLE5206::setSpeed(_desired_speed); + } + + +}; + +}; // namespace AjK ends. + + +#endif
--- a/inc/example1.h Tue Jul 05 13:50:45 2011 +0000 +++ b/inc/example1.h Tue Jul 05 14:27:56 2011 +0000 @@ -30,7 +30,7 @@ #define DUTY_CYCLE_IN_HERTZ 50 -// Create a motor "A", driven by a TLE5206 on pins 21 and 22. +// Create a motor "A", driven by a TLE5206 on pins 21 and 22 (attach scope first, not a motor!) SimpleTLE5206Output Ain1(p21); // TLE5206 In1 is connected to p21 SimpleTLE5206Output Ain2(p22); // TLE5206 In2 is connected to p22 SimpleTLE5206 motorA(&Ain1, &Ain2, DUTY_CYCLE_IN_HERTZ); // Create the TLE5206 controller. @@ -100,7 +100,7 @@ C.attach(Ccallback, 0.0025); while(1) { - /* The main loop has little to do as the Ticker cakkbacks + /* The main loop has little to do as the Ticker callbacks set-up the speed changes for the example. So give it something to do. Maybe change this and use the spare time to calculate PI more accurately? Lol, just kidding. */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/example2.h Tue Jul 05 14:27:56 2011 +0000 @@ -0,0 +1,73 @@ + +#include "mbed.h" +#include "SimpleTLE5206Profiler.h" + +Serial pc(USBTX, USBRX); + +/* See example1.h for basic notes. + * + * This example shows how to use the acceleration and deceleration + * profiler to manage changing speed in a simple linear fashion. + * + * The default accel and decel rates are 0.01/10ms. So when a speed + * of say +1.0 is demanded, it actually takes 1seond to reach that + * target desired speed due to the acceleration profiler. Likewise + * for deceleration. + * + * You can adjust the rates by altering the "poll interval" and the + * step change size. + */ + +#define DUTY_CYCLE_IN_HERTZ 50 + +// Create a motor "A", driven by a TLE5206 on pins 21 and 22 (attach scope first, not a motor!) +SimpleTLE5206Output Ain1(p21); // TLE5206 In1 is connected to p21 +SimpleTLE5206Output Ain2(p22); // TLE5206 In2 is connected to p22 +SimpleTLE5206Profiler motorA(&Ain1, &Ain2, DUTY_CYCLE_IN_HERTZ); // Create the TLE5206 controller. + +int main() { + + pc.baud(115200); + + while(1) { + // Start from stationary. + motorA.setSpeed(0); + wait(1); + + // Command full desired speed CW + motorA.setSpeed(1.0); + + // Wait for it to reach that speed. + while( motorA.getSpeed() != 1.0) ; + + // Wait for 3seconds + wait(3); + + // Stop the motor + motorA.setSpeed(0.0); + while( motorA.getSpeed() != 0.0) ; + + // Wait for 3seconds + wait(3); + + // Command full desired speed CCW + motorA.setSpeed(-1.0); + + // Wait for it to reach that speed. + while( motorA.getSpeed() != -1.0) ; + + // Wait for 3seconds + wait(3); + + // Stop the motor + motorA.setSpeed(0.0); + while( motorA.getSpeed() != 0.0) ; + + // Wait for 3seconds + wait(3); + + // repeat the cycle. + } +} + +