Commit | Line | Data |
---|---|---|
7b49793d | 1 | /* |
feb204be AW |
2 | This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). |
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. | |
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. | |
7b49793d | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
feb204be AW |
6 | */ |
7 | ||
8 | #ifndef STEPPERMOTOR_H | |
9 | #define STEPPERMOTOR_H | |
10 | ||
11 | #include "libs/Kernel.h" | |
12 | #include "libs/Hook.h" | |
13 | ||
14 | class StepTicker; | |
15 | ||
670fa10b | 16 | class StepperMotor { |
feb204be | 17 | public: |
670fa10b | 18 | StepperMotor(); |
9c5fa39a | 19 | StepperMotor(Pin& step, Pin& dir, Pin& en); |
9c5fa39a | 20 | |
203213ae MM |
21 | // Called a great many times per second, to step if we have to now |
22 | inline void tick() { | |
23 | // increase the ( fixed point ) counter by one tick 11t | |
24 | fx_counter += (uint32_t)(1<<16); | |
25 | ||
26 | // if we are to step now 10t | |
27 | if (fx_counter >= fx_ticks_per_step) | |
28 | step(); | |
29 | }; | |
30 | ||
31 | void step(); | |
32 | inline void unstep() { step_pin.set(0); }; | |
9c5fa39a | 33 | |
203213ae | 34 | inline void enable(bool state) { en_pin.set(!state); }; |
9c5fa39a | 35 | |
670fa10b | 36 | void move_finished(); |
feb204be | 37 | void move( bool direction, unsigned int steps ); |
bd0f7508 | 38 | void signal_move_finished(); |
1ad23cd3 | 39 | void set_speed( float speed ); |
672298b2 | 40 | void update_exit_tick(); |
83ecfc46 AW |
41 | void pause(); |
42 | void unpause(); | |
670fa10b | 43 | |
78d0e16a MM |
44 | void change_steps_per_mm(float); |
45 | void change_last_milestone(float); | |
670fa10b | 46 | |
78d0e16a | 47 | int steps_to_target(float); |
670fa10b L |
48 | |
49 | template<typename T> void attach( T *optr, uint32_t ( T::*fptr )( uint32_t ) ){ | |
50 | Hook* hook = new Hook(); | |
51 | hook->attach(optr, fptr); | |
52 | this->end_hook = hook; | |
53 | } | |
54 | ||
55 | template<typename T> void attach_signal_step(uint32_t step, T *optr, uint32_t ( T::*fptr )( uint32_t ) ){ | |
56 | this->step_signal_hook->attach(optr, fptr); | |
57 | this->signal_step_number = step; | |
58 | this->signal_step = true; | |
59 | } | |
60 | ||
61 | Hook* end_hook; | |
62 | Hook* step_signal_hook; | |
63 | ||
64 | bool signal_step; | |
65 | uint32_t signal_step_number; | |
66 | ||
feb204be | 67 | StepTicker* step_ticker; |
9c5fa39a MM |
68 | Pin step_pin; |
69 | Pin dir_pin; | |
70 | Pin en_pin; | |
feb204be | 71 | |
1ad23cd3 | 72 | float steps_per_second; |
670fa10b L |
73 | |
74 | volatile bool moving; | |
75 | bool paused; | |
76 | ||
78d0e16a | 77 | float steps_per_mm; |
df6a30f2 | 78 | float max_rate; |
78d0e16a MM |
79 | |
80 | int32_t last_milestone_steps; | |
81 | float last_milestone_mm; | |
82 | ||
670fa10b L |
83 | uint32_t steps_to_move; |
84 | uint32_t stepped; | |
85 | uint32_t fx_counter; | |
86 | uint32_t fx_ticks_per_step; | |
df6a30f2 | 87 | |
9c5fa39a | 88 | bool direction; |
670fa10b L |
89 | |
90 | //bool exit_tick; | |
91 | bool remove_from_active_list_next_reset; | |
92 | ||
bd0f7508 | 93 | bool is_move_finished; // Whether the move just finished |
feb204be AW |
94 | }; |
95 | ||
feb204be AW |
96 | #endif |
97 |