Robot,StepperMotor: per-actuator speed limits
[clinton/Smoothieware.git] / src / libs / StepperMotor.h
1 /*
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.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
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
16 class StepperMotor {
17 public:
18 StepperMotor();
19 StepperMotor(Pin& step, Pin& dir, Pin& en);
20 void tick();
21 void step();
22 void unstep();
23
24 void enable(bool);
25
26
27 void move_finished();
28 void move( bool direction, unsigned int steps );
29 void signal_move_finished();
30 void set_speed( float speed );
31 void update_exit_tick();
32 void pause();
33 void unpause();
34
35 void change_steps_per_mm(float);
36 void change_last_milestone(float);
37
38 int steps_to_target(float);
39
40 template<typename T> void attach( T *optr, uint32_t ( T::*fptr )( uint32_t ) ){
41 Hook* hook = new Hook();
42 hook->attach(optr, fptr);
43 this->end_hook = hook;
44 }
45
46 template<typename T> void attach_signal_step(uint32_t step, T *optr, uint32_t ( T::*fptr )( uint32_t ) ){
47 this->step_signal_hook->attach(optr, fptr);
48 this->signal_step_number = step;
49 this->signal_step = true;
50 }
51
52 Hook* end_hook;
53 Hook* step_signal_hook;
54
55 bool signal_step;
56 uint32_t signal_step_number;
57
58 StepTicker* step_ticker;
59 Pin step_pin;
60 Pin dir_pin;
61 Pin en_pin;
62
63 float steps_per_second;
64
65 volatile bool moving;
66 bool paused;
67
68 float steps_per_mm;
69 float max_rate;
70
71 int32_t last_milestone_steps;
72 float last_milestone_mm;
73
74 uint32_t steps_to_move;
75 uint32_t stepped;
76 uint32_t fx_counter;
77 uint32_t fx_ticks_per_step;
78
79 bool direction;
80
81 //bool exit_tick;
82 bool remove_from_active_list_next_reset;
83
84 bool is_move_finished; // Whether the move just finished
85 };
86
87
88 // Called a great many times per second, to step if we have to now
89 inline void StepperMotor::tick(){
90
91 // increase the ( fixed point ) counter by one tick 11t
92 this->fx_counter += (uint32_t)(1<<16);
93
94 // if we are to step now 10t
95 if( this->fx_counter >= this->fx_ticks_per_step ){ this->step(); }
96
97 }
98
99 inline void StepperMotor::unstep()
100 {
101 step_pin.set(0);
102 }
103
104 inline void StepperMotor::enable(bool state)
105 {
106 en_pin.set(!state);
107 }
108
109
110 #endif
111