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