Provides an API software interface to TIMER2 to control upto four stepper motors.
Embed:
(wiki syntax)
Show/hide line numbers
SimpleStepperProfiler.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 00024 #ifndef AJK_SIMPLESTEPERPROFILER_H 00025 #define AJK_SIMPLESTEPERPROFILER_H 00026 00027 #include "mbed.h" 00028 #include "SimpleSteppers.h" 00029 00030 namespace AjK { 00031 00032 class SimpleStepperProfiler : public SimpleStepper { 00033 00034 protected: 00035 Ticker _poll; 00036 int _desired_speed; 00037 int _step_pos; 00038 int _step_neg; 00039 int _poll_interval; 00040 00041 void poll(void) 00042 { 00043 if (_steps_per_second < _desired_speed) { 00044 _steps_per_second += _step_pos; 00045 if (_steps_per_second > _desired_speed) { 00046 _steps_per_second = _desired_speed; 00047 } 00048 SimpleStepper::setSpeed(_steps_per_second); 00049 } 00050 if (_steps_per_second > _desired_speed) { 00051 _steps_per_second -= _step_neg; 00052 if (_steps_per_second < _desired_speed) { 00053 _steps_per_second = _desired_speed; 00054 } 00055 SimpleStepper::setSpeed(_steps_per_second); 00056 } 00057 } 00058 00059 public: 00060 00061 friend class Ticker; 00062 00063 SimpleStepperProfiler(PinName pulse, SimpleStepperOutput *direction = 0) : 00064 SimpleStepper(pulse, direction) 00065 { 00066 _desired_speed = 0; 00067 _step_pos = 10; 00068 _step_neg = 10; 00069 _poll_interval = 10000; // 10ms 00070 _poll.attach_us(this, &SimpleStepperProfiler::poll, _poll_interval); 00071 } 00072 00073 int getStepPos(void) { return _step_pos; } 00074 void setStepPos(int i) { _step_pos = i; } 00075 00076 int getStepNeg(void) { return _step_neg; } 00077 void setStepNeg(int i) { _step_neg = i; } 00078 00079 int getPollInterval(void) { return _poll_interval; } 00080 void setPollInterval(int i) 00081 { 00082 _poll_interval = i; 00083 _poll.detach(); 00084 _poll.attach_us(this, &SimpleStepperProfiler::poll, _poll_interval); 00085 } 00086 00087 void setSpeed(int steps_per_second = 0, uint32_t raw = 0) 00088 { 00089 _desired_speed = steps_per_second; 00090 } 00091 00092 void eStop(void) 00093 { 00094 _desired_speed = _steps_per_second = 0; 00095 SimpleStepper::setSpeed(_steps_per_second); 00096 } 00097 00098 00099 }; 00100 00101 }; // namespace AjK ends. 00102 00103 00104 #endif
Generated on Thu Jul 14 2022 17:06:02 by 1.7.2