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 | ||
feb204be | 11 | #include "libs/Hook.h" |
5673fe39 | 12 | #include "Pin.h" |
feb204be AW |
13 | |
14 | class StepTicker; | |
5673fe39 | 15 | class Hook; |
feb204be | 16 | |
670fa10b | 17 | class StepperMotor { |
feb204be | 18 | public: |
670fa10b | 19 | StepperMotor(); |
9c5fa39a | 20 | StepperMotor(Pin& step, Pin& dir, Pin& en); |
9c5fa39a | 21 | |
203213ae MM |
22 | // Called a great many times per second, to step if we have to now |
23 | inline void tick() { | |
24 | // increase the ( fixed point ) counter by one tick 11t | |
25 | fx_counter += (uint32_t)(1<<16); | |
26 | ||
27 | // if we are to step now 10t | |
28 | if (fx_counter >= fx_ticks_per_step) | |
29 | step(); | |
30 | }; | |
31 | ||
32 | void step(); | |
33 | inline void unstep() { step_pin.set(0); }; | |
9c5fa39a | 34 | |
203213ae | 35 | inline void enable(bool state) { en_pin.set(!state); }; |
9c5fa39a | 36 | |
2fa50ca0 | 37 | bool is_moving() { return moving; } |
670fa10b | 38 | void move_finished(); |
feb204be | 39 | void move( bool direction, unsigned int steps ); |
bd0f7508 | 40 | void signal_move_finished(); |
1ad23cd3 | 41 | void set_speed( float speed ); |
672298b2 | 42 | void update_exit_tick(); |
83ecfc46 AW |
43 | void pause(); |
44 | void unpause(); | |
670fa10b | 45 | |
2fa50ca0 JM |
46 | float get_steps_per_second() const { return steps_per_second; } |
47 | void set_steps_per_second(float ss) { steps_per_second= ss; } | |
48 | float get_steps_per_mm() const { return steps_per_mm; } | |
78d0e16a MM |
49 | void change_steps_per_mm(float); |
50 | void change_last_milestone(float); | |
670fa10b | 51 | |
78d0e16a | 52 | int steps_to_target(float); |
2fa50ca0 JM |
53 | uint32_t get_steps_to_move() const { return steps_to_move; } |
54 | uint32_t get_stepped() const { return stepped; } | |
670fa10b L |
55 | |
56 | template<typename T> void attach( T *optr, uint32_t ( T::*fptr )( uint32_t ) ){ | |
57 | Hook* hook = new Hook(); | |
58 | hook->attach(optr, fptr); | |
59 | this->end_hook = hook; | |
60 | } | |
61 | ||
62 | template<typename T> void attach_signal_step(uint32_t step, T *optr, uint32_t ( T::*fptr )( uint32_t ) ){ | |
63 | this->step_signal_hook->attach(optr, fptr); | |
64 | this->signal_step_number = step; | |
65 | this->signal_step = true; | |
66 | } | |
67 | ||
2fa50ca0 JM |
68 | friend class StepTicker; |
69 | friend class Stepper; | |
70 | friend class Planner; | |
71 | friend class Robot; | |
72 | ||
73 | private: | |
670fa10b L |
74 | Hook* end_hook; |
75 | Hook* step_signal_hook; | |
76 | ||
77 | bool signal_step; | |
78 | uint32_t signal_step_number; | |
79 | ||
feb204be | 80 | StepTicker* step_ticker; |
9c5fa39a MM |
81 | Pin step_pin; |
82 | Pin dir_pin; | |
83 | Pin en_pin; | |
feb204be | 84 | |
1ad23cd3 | 85 | float steps_per_second; |
670fa10b L |
86 | |
87 | volatile bool moving; | |
88 | bool paused; | |
89 | ||
78d0e16a | 90 | float steps_per_mm; |
df6a30f2 | 91 | float max_rate; |
78d0e16a MM |
92 | |
93 | int32_t last_milestone_steps; | |
94 | float last_milestone_mm; | |
95 | ||
670fa10b L |
96 | uint32_t steps_to_move; |
97 | uint32_t stepped; | |
98 | uint32_t fx_counter; | |
99 | uint32_t fx_ticks_per_step; | |
df6a30f2 | 100 | |
9c5fa39a | 101 | bool direction; |
670fa10b L |
102 | |
103 | //bool exit_tick; | |
104 | bool remove_from_active_list_next_reset; | |
105 | ||
bd0f7508 | 106 | bool is_move_finished; // Whether the move just finished |
feb204be AW |
107 | }; |
108 | ||
feb204be AW |
109 | #endif |
110 |