Provides an API software interface to TIMER2 to control upto four stepper motors.
inc/SimpleStepperProfiler.h@4:7b7940df7865, 2011-05-20 (annotated)
- Committer:
- AjK
- Date:
- Fri May 20 09:13:31 2011 +0000
- Revision:
- 4:7b7940df7865
1.1 See ChangeLog.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AjK | 4:7b7940df7865 | 1 | /* |
AjK | 4:7b7940df7865 | 2 | Copyright (c) 2011 Andy Kirkham |
AjK | 4:7b7940df7865 | 3 | |
AjK | 4:7b7940df7865 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
AjK | 4:7b7940df7865 | 5 | of this software and associated documentation files (the "Software"), to deal |
AjK | 4:7b7940df7865 | 6 | in the Software without restriction, including without limitation the rights |
AjK | 4:7b7940df7865 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
AjK | 4:7b7940df7865 | 8 | copies of the Software, and to permit persons to whom the Software is |
AjK | 4:7b7940df7865 | 9 | furnished to do so, subject to the following conditions: |
AjK | 4:7b7940df7865 | 10 | |
AjK | 4:7b7940df7865 | 11 | The above copyright notice and this permission notice shall be included in |
AjK | 4:7b7940df7865 | 12 | all copies or substantial portions of the Software. |
AjK | 4:7b7940df7865 | 13 | |
AjK | 4:7b7940df7865 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
AjK | 4:7b7940df7865 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
AjK | 4:7b7940df7865 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
AjK | 4:7b7940df7865 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
AjK | 4:7b7940df7865 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
AjK | 4:7b7940df7865 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
AjK | 4:7b7940df7865 | 20 | THE SOFTWARE. |
AjK | 4:7b7940df7865 | 21 | */ |
AjK | 4:7b7940df7865 | 22 | |
AjK | 4:7b7940df7865 | 23 | |
AjK | 4:7b7940df7865 | 24 | #ifndef AJK_SIMPLESTEPERPROFILER_H |
AjK | 4:7b7940df7865 | 25 | #define AJK_SIMPLESTEPERPROFILER_H |
AjK | 4:7b7940df7865 | 26 | |
AjK | 4:7b7940df7865 | 27 | #include "mbed.h" |
AjK | 4:7b7940df7865 | 28 | #include "SimpleSteppers.h" |
AjK | 4:7b7940df7865 | 29 | |
AjK | 4:7b7940df7865 | 30 | namespace AjK { |
AjK | 4:7b7940df7865 | 31 | |
AjK | 4:7b7940df7865 | 32 | class SimpleStepperProfiler : public SimpleStepper { |
AjK | 4:7b7940df7865 | 33 | |
AjK | 4:7b7940df7865 | 34 | protected: |
AjK | 4:7b7940df7865 | 35 | Ticker _poll; |
AjK | 4:7b7940df7865 | 36 | int _desired_speed; |
AjK | 4:7b7940df7865 | 37 | int _step_pos; |
AjK | 4:7b7940df7865 | 38 | int _step_neg; |
AjK | 4:7b7940df7865 | 39 | int _poll_interval; |
AjK | 4:7b7940df7865 | 40 | |
AjK | 4:7b7940df7865 | 41 | void poll(void) |
AjK | 4:7b7940df7865 | 42 | { |
AjK | 4:7b7940df7865 | 43 | if (_steps_per_second < _desired_speed) { |
AjK | 4:7b7940df7865 | 44 | _steps_per_second += _step_pos; |
AjK | 4:7b7940df7865 | 45 | if (_steps_per_second > _desired_speed) { |
AjK | 4:7b7940df7865 | 46 | _steps_per_second = _desired_speed; |
AjK | 4:7b7940df7865 | 47 | } |
AjK | 4:7b7940df7865 | 48 | SimpleStepper::setSpeed(_steps_per_second); |
AjK | 4:7b7940df7865 | 49 | } |
AjK | 4:7b7940df7865 | 50 | if (_steps_per_second > _desired_speed) { |
AjK | 4:7b7940df7865 | 51 | _steps_per_second -= _step_neg; |
AjK | 4:7b7940df7865 | 52 | if (_steps_per_second < _desired_speed) { |
AjK | 4:7b7940df7865 | 53 | _steps_per_second = _desired_speed; |
AjK | 4:7b7940df7865 | 54 | } |
AjK | 4:7b7940df7865 | 55 | SimpleStepper::setSpeed(_steps_per_second); |
AjK | 4:7b7940df7865 | 56 | } |
AjK | 4:7b7940df7865 | 57 | } |
AjK | 4:7b7940df7865 | 58 | |
AjK | 4:7b7940df7865 | 59 | public: |
AjK | 4:7b7940df7865 | 60 | |
AjK | 4:7b7940df7865 | 61 | friend class Ticker; |
AjK | 4:7b7940df7865 | 62 | |
AjK | 4:7b7940df7865 | 63 | SimpleStepperProfiler(PinName pulse, SimpleStepperOutput *direction = 0) : |
AjK | 4:7b7940df7865 | 64 | SimpleStepper(pulse, direction) |
AjK | 4:7b7940df7865 | 65 | { |
AjK | 4:7b7940df7865 | 66 | _desired_speed = 0; |
AjK | 4:7b7940df7865 | 67 | _step_pos = 10; |
AjK | 4:7b7940df7865 | 68 | _step_neg = 10; |
AjK | 4:7b7940df7865 | 69 | _poll_interval = 10000; // 10ms |
AjK | 4:7b7940df7865 | 70 | _poll.attach_us(this, &SimpleStepperProfiler::poll, _poll_interval); |
AjK | 4:7b7940df7865 | 71 | } |
AjK | 4:7b7940df7865 | 72 | |
AjK | 4:7b7940df7865 | 73 | int getStepPos(void) { return _step_pos; } |
AjK | 4:7b7940df7865 | 74 | void setStepPos(int i) { _step_pos = i; } |
AjK | 4:7b7940df7865 | 75 | |
AjK | 4:7b7940df7865 | 76 | int getStepNeg(void) { return _step_neg; } |
AjK | 4:7b7940df7865 | 77 | void setStepNeg(int i) { _step_neg = i; } |
AjK | 4:7b7940df7865 | 78 | |
AjK | 4:7b7940df7865 | 79 | int getPollInterval(void) { return _poll_interval; } |
AjK | 4:7b7940df7865 | 80 | void setPollInterval(int i) |
AjK | 4:7b7940df7865 | 81 | { |
AjK | 4:7b7940df7865 | 82 | _poll_interval = i; |
AjK | 4:7b7940df7865 | 83 | _poll.detach(); |
AjK | 4:7b7940df7865 | 84 | _poll.attach_us(this, &SimpleStepperProfiler::poll, _poll_interval); |
AjK | 4:7b7940df7865 | 85 | } |
AjK | 4:7b7940df7865 | 86 | |
AjK | 4:7b7940df7865 | 87 | void setSpeed(int steps_per_second = 0, uint32_t raw = 0) |
AjK | 4:7b7940df7865 | 88 | { |
AjK | 4:7b7940df7865 | 89 | _desired_speed = steps_per_second; |
AjK | 4:7b7940df7865 | 90 | } |
AjK | 4:7b7940df7865 | 91 | |
AjK | 4:7b7940df7865 | 92 | void eStop(void) |
AjK | 4:7b7940df7865 | 93 | { |
AjK | 4:7b7940df7865 | 94 | _desired_speed = _steps_per_second = 0; |
AjK | 4:7b7940df7865 | 95 | SimpleStepper::setSpeed(_steps_per_second); |
AjK | 4:7b7940df7865 | 96 | } |
AjK | 4:7b7940df7865 | 97 | |
AjK | 4:7b7940df7865 | 98 | |
AjK | 4:7b7940df7865 | 99 | }; |
AjK | 4:7b7940df7865 | 100 | |
AjK | 4:7b7940df7865 | 101 | }; // namespace AjK ends. |
AjK | 4:7b7940df7865 | 102 | |
AjK | 4:7b7940df7865 | 103 | |
AjK | 4:7b7940df7865 | 104 | #endif |