Don't chain header includes if we don't have to, use predeclaration if we only need...
[clinton/Smoothieware.git] / src / libs / StepperMotor.cpp
CommitLineData
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 6*/
670fa10b 7#include "StepperMotor.h"
5673fe39
MM
8
9#include "Kernel.h"
8f91e4e6 10#include "MRI_Hooks.h"
feb204be 11
d337942a
MM
12// A StepperMotor represents an actual stepper motor. It is used to generate steps that move the actual motor at a given speed
13// TODO : Abstract this into Actuator
93694d6b 14
670fa10b
L
15StepperMotor::StepperMotor(){
16 this->moving = false;
17 this->paused = false;
18 this->fx_counter = 0;
19 this->stepped = 0;
20 this->fx_ticks_per_step = 0;
21 this->steps_to_move = 0;
22 this->remove_from_active_list_next_reset = false;
23 this->is_move_finished = false;
24 this->signal_step = false;
25 this->step_signal_hook = new Hook();
78d0e16a 26
df6a30f2
MM
27 steps_per_mm = 1.0F;
28 max_rate = 50.0F;
29
78d0e16a
MM
30 last_milestone_steps = 0;
31 last_milestone_mm = 0.0F;
670fa10b
L
32}
33
9c5fa39a 34StepperMotor::StepperMotor(Pin& step, Pin& dir, Pin& en) : step_pin(step), dir_pin(dir), en_pin(en) {
feb204be
AW
35 this->moving = false;
36 this->paused = false;
37 this->fx_counter = 0;
4464301d 38 this->stepped = 0;
feb204be
AW
39 this->fx_ticks_per_step = 0;
40 this->steps_to_move = 0;
bd0f7508
AW
41 this->remove_from_active_list_next_reset = false;
42 this->is_move_finished = false;
2f7d3dba 43 this->signal_step = false;
7b49793d 44 this->step_signal_hook = new Hook();
8f91e4e6 45
9c5fa39a
MM
46 enable(false);
47 set_high_on_debug(en.port_number, en.pin);
78d0e16a 48
df6a30f2
MM
49 steps_per_mm = 1.0F;
50 max_rate = 50.0F;
51
78d0e16a
MM
52 last_milestone_steps = 0;
53 last_milestone_mm = 0.0F;
feb204be
AW
54}
55
93694d6b
AW
56// This is called ( see the .h file, we had to put a part of things there for obscure inline reasons ) when a step has to be generated
57// we also here check if the move is finished etc ...
8aea2a35 58void StepperMotor::step(){
feb204be 59
8aea2a35 60 // output to pins 37t
9c5fa39a 61 this->step_pin.set( 1 );
8aea2a35 62 this->step_ticker->reset_step_pins = true;
7b49793d 63
8aea2a35
AW
64 // move counter back 11t
65 this->fx_counter -= this->fx_ticks_per_step;
813727fb 66
8aea2a35
AW
67 // we have moved a step 9t
68 this->stepped++;
feb204be 69
8aea2a35
AW
70 // Do we need to signal this step
71 if( this->stepped == this->signal_step_number && this->signal_step ){
72 this->step_signal_hook->call();
73 }
4464301d 74
93694d6b 75 // Is this move finished ?
8aea2a35 76 if( this->stepped == this->steps_to_move ){
93694d6b
AW
77 // Mark it as finished, then StepTicker will call signal_mode_finished()
78 // This is so we don't call that before all the steps have been generated for this tick()
8aea2a35
AW
79 this->is_move_finished = true;
80 this->step_ticker->moves_finished = true;
bd0f7508 81 }
4464301d 82
bd0f7508 83}
feb204be 84
8aea2a35 85
6b080aff 86// If the move is finished, the StepTicker will call this ( because we asked it to in tick() )
bd0f7508 87void StepperMotor::signal_move_finished(){
3add9a23 88
feb204be 89 // work is done ! 8t
672298b2 90 this->moving = false;
4464301d 91 this->steps_to_move = 0;
7b49793d 92
feb204be
AW
93 // signal it to whatever cares 41t 411t
94 this->end_hook->call();
95
bd0f7508
AW
96 // We only need to do this if we were not instructed to move
97 if( this->moving == false ){
7b49793d 98 this->update_exit_tick();
bd0f7508 99 }
4464301d 100
bd0f7508 101 this->is_move_finished = false;
feb204be
AW
102}
103
672298b2
AW
104// This is just a way not to check for ( !this->moving || this->paused || this->fx_ticks_per_step == 0 ) at every tick()
105inline void StepperMotor::update_exit_tick(){
83ecfc46 106 if( !this->moving || this->paused || this->steps_to_move == 0 ){
7b49793d 107 // We must exit tick() after setting the pins, no bresenham is done
69735c09 108 //this->remove_from_active_list_next_reset = true;
7b49793d 109 this->step_ticker->remove_motor_from_active_list(this);
672298b2 110 }else{
796c9f32 111 // We must do the bresenham in tick()
bd0f7508 112 // We have to do this or there could be a bug where the removal still happens when it doesn't need to
bd0f7508 113 this->step_ticker->add_motor_to_active_list(this);
672298b2
AW
114 }
115}
116
117
feb204be
AW
118
119// Instruct the StepperMotor to move a certain number of steps
120void StepperMotor::move( bool direction, unsigned int steps ){
7b49793d 121 // We do not set the direction directly, we will set the pin just before the step pin on the next tick
9c5fa39a
MM
122 this->dir_pin.set(direction);
123 this->direction = direction;
feb204be
AW
124
125 // How many steps we have to move until the move is done
126 this->steps_to_move = steps;
127
128 // Zero our tool counters
129 this->fx_counter = 0; // Bresenheim counter
130 this->stepped = 0;
131
2f7d3dba
AW
132 // Do not signal steps until we get instructed to
133 this->signal_step = false;
134
feb204be 135 // Starting now we are moving
7b49793d
MM
136 if( steps > 0 ){
137 this->moving = true;
138 }else{
139 this->moving = false;
83ecfc46 140 }
7b49793d 141 this->update_exit_tick();
feb204be
AW
142
143}
144
145// Set the speed at which this steper moves
1ad23cd3 146void StepperMotor::set_speed( float speed ){
69735c09 147
eb97f206
MM
148 if (speed < 20.0)
149 speed = 20.0;
a07ee235 150
feb204be
AW
151 // How many steps we must output per second
152 this->steps_per_second = speed;
153
154 // How many ticks ( base steps ) between each actual step at this speed, in fixed point 64
1ad23cd3
MM
155 float ticks_per_step = (float)( (float)this->step_ticker->frequency / speed );
156 float double_fx_ticks_per_step = (float)(1<<8) * ( (float)(1<<8) * ticks_per_step ); // 8x8 because we had to do 16x16 because 32 did not work
c26b7486 157 this->fx_ticks_per_step = (uint32_t)( floor(double_fx_ticks_per_step) );
83ecfc46 158
feb204be
AW
159}
160
93694d6b 161// Pause this stepper motor
83ecfc46
AW
162void StepperMotor::pause(){
163 this->paused = true;
164 this->update_exit_tick();
165}
feb204be 166
93694d6b 167// Unpause this stepper motor
83ecfc46
AW
168void StepperMotor::unpause(){
169 this->paused = false;
170 this->update_exit_tick();
171}
feb204be
AW
172
173
78d0e16a
MM
174void StepperMotor::change_steps_per_mm(float new_steps)
175{
176 steps_per_mm = new_steps;
177 last_milestone_steps = lround(last_milestone_mm * steps_per_mm);
178}
179
180void StepperMotor::change_last_milestone(float new_milestone)
181{
182 last_milestone_mm = new_milestone;
183 last_milestone_steps = lround(last_milestone_mm * steps_per_mm);
184}
185
186int StepperMotor::steps_to_target(float target)
187{
338beb48 188 int target_steps = lround(target * steps_per_mm);
78d0e16a
MM
189 return target_steps - last_milestone_steps;
190}