Fork of Smoothie to port to mbed non-LPC targets.
Fork of Smoothie by
libs/StepperMotor.h@2:1df0b61d3b5a, 2014-02-28 (annotated)
- Committer:
- Michael J. Spencer
- Date:
- Fri Feb 28 18:52:52 2014 -0800
- Revision:
- 2:1df0b61d3b5a
Update to latest Smoothie.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Michael J. Spencer |
2:1df0b61d3b5a | 1 | /* |
Michael J. Spencer |
2:1df0b61d3b5a | 2 | This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). |
Michael J. Spencer |
2:1df0b61d3b5a | 3 | Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. |
Michael J. Spencer |
2:1df0b61d3b5a | 4 | Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Michael J. Spencer |
2:1df0b61d3b5a | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
Michael J. Spencer |
2:1df0b61d3b5a | 6 | */ |
Michael J. Spencer |
2:1df0b61d3b5a | 7 | |
Michael J. Spencer |
2:1df0b61d3b5a | 8 | #ifndef STEPPERMOTOR_H |
Michael J. Spencer |
2:1df0b61d3b5a | 9 | #define STEPPERMOTOR_H |
Michael J. Spencer |
2:1df0b61d3b5a | 10 | |
Michael J. Spencer |
2:1df0b61d3b5a | 11 | #include "libs/Hook.h" |
Michael J. Spencer |
2:1df0b61d3b5a | 12 | #include "Pin.h" |
Michael J. Spencer |
2:1df0b61d3b5a | 13 | |
Michael J. Spencer |
2:1df0b61d3b5a | 14 | class StepTicker; |
Michael J. Spencer |
2:1df0b61d3b5a | 15 | class Hook; |
Michael J. Spencer |
2:1df0b61d3b5a | 16 | |
Michael J. Spencer |
2:1df0b61d3b5a | 17 | class StepperMotor { |
Michael J. Spencer |
2:1df0b61d3b5a | 18 | public: |
Michael J. Spencer |
2:1df0b61d3b5a | 19 | StepperMotor(); |
Michael J. Spencer |
2:1df0b61d3b5a | 20 | StepperMotor(Pin& step, Pin& dir, Pin& en); |
Michael J. Spencer |
2:1df0b61d3b5a | 21 | |
Michael J. Spencer |
2:1df0b61d3b5a | 22 | // Called a great many times per second, to step if we have to now |
Michael J. Spencer |
2:1df0b61d3b5a | 23 | inline void tick() { |
Michael J. Spencer |
2:1df0b61d3b5a | 24 | // increase the ( fixed point ) counter by one tick 11t |
Michael J. Spencer |
2:1df0b61d3b5a | 25 | fx_counter += (uint32_t)(1<<16); |
Michael J. Spencer |
2:1df0b61d3b5a | 26 | |
Michael J. Spencer |
2:1df0b61d3b5a | 27 | // if we are to step now 10t |
Michael J. Spencer |
2:1df0b61d3b5a | 28 | if (fx_counter >= fx_ticks_per_step) |
Michael J. Spencer |
2:1df0b61d3b5a | 29 | step(); |
Michael J. Spencer |
2:1df0b61d3b5a | 30 | }; |
Michael J. Spencer |
2:1df0b61d3b5a | 31 | |
Michael J. Spencer |
2:1df0b61d3b5a | 32 | void step(); |
Michael J. Spencer |
2:1df0b61d3b5a | 33 | inline void unstep() { step_pin.set(0); }; |
Michael J. Spencer |
2:1df0b61d3b5a | 34 | |
Michael J. Spencer |
2:1df0b61d3b5a | 35 | inline void enable(bool state) { en_pin.set(!state); }; |
Michael J. Spencer |
2:1df0b61d3b5a | 36 | |
Michael J. Spencer |
2:1df0b61d3b5a | 37 | void move_finished(); |
Michael J. Spencer |
2:1df0b61d3b5a | 38 | void move( bool direction, unsigned int steps ); |
Michael J. Spencer |
2:1df0b61d3b5a | 39 | void signal_move_finished(); |
Michael J. Spencer |
2:1df0b61d3b5a | 40 | void set_speed( float speed ); |
Michael J. Spencer |
2:1df0b61d3b5a | 41 | void update_exit_tick(); |
Michael J. Spencer |
2:1df0b61d3b5a | 42 | void pause(); |
Michael J. Spencer |
2:1df0b61d3b5a | 43 | void unpause(); |
Michael J. Spencer |
2:1df0b61d3b5a | 44 | |
Michael J. Spencer |
2:1df0b61d3b5a | 45 | void change_steps_per_mm(float); |
Michael J. Spencer |
2:1df0b61d3b5a | 46 | void change_last_milestone(float); |
Michael J. Spencer |
2:1df0b61d3b5a | 47 | |
Michael J. Spencer |
2:1df0b61d3b5a | 48 | int steps_to_target(float); |
Michael J. Spencer |
2:1df0b61d3b5a | 49 | |
Michael J. Spencer |
2:1df0b61d3b5a | 50 | template<typename T> void attach( T *optr, uint32_t ( T::*fptr )( uint32_t ) ){ |
Michael J. Spencer |
2:1df0b61d3b5a | 51 | Hook* hook = new Hook(); |
Michael J. Spencer |
2:1df0b61d3b5a | 52 | hook->attach(optr, fptr); |
Michael J. Spencer |
2:1df0b61d3b5a | 53 | this->end_hook = hook; |
Michael J. Spencer |
2:1df0b61d3b5a | 54 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 55 | |
Michael J. Spencer |
2:1df0b61d3b5a | 56 | template<typename T> void attach_signal_step(uint32_t step, T *optr, uint32_t ( T::*fptr )( uint32_t ) ){ |
Michael J. Spencer |
2:1df0b61d3b5a | 57 | this->step_signal_hook->attach(optr, fptr); |
Michael J. Spencer |
2:1df0b61d3b5a | 58 | this->signal_step_number = step; |
Michael J. Spencer |
2:1df0b61d3b5a | 59 | this->signal_step = true; |
Michael J. Spencer |
2:1df0b61d3b5a | 60 | } |
Michael J. Spencer |
2:1df0b61d3b5a | 61 | |
Michael J. Spencer |
2:1df0b61d3b5a | 62 | Hook* end_hook; |
Michael J. Spencer |
2:1df0b61d3b5a | 63 | Hook* step_signal_hook; |
Michael J. Spencer |
2:1df0b61d3b5a | 64 | |
Michael J. Spencer |
2:1df0b61d3b5a | 65 | bool signal_step; |
Michael J. Spencer |
2:1df0b61d3b5a | 66 | uint32_t signal_step_number; |
Michael J. Spencer |
2:1df0b61d3b5a | 67 | |
Michael J. Spencer |
2:1df0b61d3b5a | 68 | StepTicker* step_ticker; |
Michael J. Spencer |
2:1df0b61d3b5a | 69 | Pin step_pin; |
Michael J. Spencer |
2:1df0b61d3b5a | 70 | Pin dir_pin; |
Michael J. Spencer |
2:1df0b61d3b5a | 71 | Pin en_pin; |
Michael J. Spencer |
2:1df0b61d3b5a | 72 | |
Michael J. Spencer |
2:1df0b61d3b5a | 73 | float steps_per_second; |
Michael J. Spencer |
2:1df0b61d3b5a | 74 | |
Michael J. Spencer |
2:1df0b61d3b5a | 75 | volatile bool moving; |
Michael J. Spencer |
2:1df0b61d3b5a | 76 | bool paused; |
Michael J. Spencer |
2:1df0b61d3b5a | 77 | |
Michael J. Spencer |
2:1df0b61d3b5a | 78 | float steps_per_mm; |
Michael J. Spencer |
2:1df0b61d3b5a | 79 | float max_rate; |
Michael J. Spencer |
2:1df0b61d3b5a | 80 | |
Michael J. Spencer |
2:1df0b61d3b5a | 81 | int32_t last_milestone_steps; |
Michael J. Spencer |
2:1df0b61d3b5a | 82 | float last_milestone_mm; |
Michael J. Spencer |
2:1df0b61d3b5a | 83 | |
Michael J. Spencer |
2:1df0b61d3b5a | 84 | uint32_t steps_to_move; |
Michael J. Spencer |
2:1df0b61d3b5a | 85 | uint32_t stepped; |
Michael J. Spencer |
2:1df0b61d3b5a | 86 | uint32_t fx_counter; |
Michael J. Spencer |
2:1df0b61d3b5a | 87 | uint32_t fx_ticks_per_step; |
Michael J. Spencer |
2:1df0b61d3b5a | 88 | |
Michael J. Spencer |
2:1df0b61d3b5a | 89 | bool direction; |
Michael J. Spencer |
2:1df0b61d3b5a | 90 | |
Michael J. Spencer |
2:1df0b61d3b5a | 91 | //bool exit_tick; |
Michael J. Spencer |
2:1df0b61d3b5a | 92 | bool remove_from_active_list_next_reset; |
Michael J. Spencer |
2:1df0b61d3b5a | 93 | |
Michael J. Spencer |
2:1df0b61d3b5a | 94 | bool is_move_finished; // Whether the move just finished |
Michael J. Spencer |
2:1df0b61d3b5a | 95 | }; |
Michael J. Spencer |
2:1df0b61d3b5a | 96 | |
Michael J. Spencer |
2:1df0b61d3b5a | 97 | #endif |
Michael J. Spencer |
2:1df0b61d3b5a | 98 |