Merge pull request #857 from wolfmanjm/refactor/G38-processing-fix-for-deltas
[clinton/Smoothieware.git] / src / libs / StepperMotor.cpp
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 #include "StepperMotor.h"
8
9 #include "Kernel.h"
10 #include "MRI_Hooks.h"
11 #include "StepTicker.h"
12
13 #include <math.h>
14
15 // in steps/sec the default minimum speed (was 20steps/sec hardcoded)
16 float StepperMotor::default_minimum_actuator_rate= 20.0F;
17
18 // A StepperMotor represents an actual stepper motor. It is used to generate steps that move the actual motor at a given speed
19
20 StepperMotor::StepperMotor()
21 {
22 init();
23 }
24
25 StepperMotor::StepperMotor(Pin &step, Pin &dir, Pin &en) : step_pin(step), dir_pin(dir), en_pin(en)
26 {
27 init();
28 enable(false);
29 set_high_on_debug(en.port_number, en.pin);
30 }
31
32 StepperMotor::~StepperMotor()
33 {
34 }
35
36 void StepperMotor::init()
37 {
38 // register this motor with the step ticker, and get its index in that array and bit position
39 this->index= THEKERNEL->step_ticker->register_motor(this);
40 this->moving = false;
41 this->fx_counter = 0;
42 this->fx_ticks_per_step = 0xFFFFF000UL; // some big number so we don't start stepping before it is set
43 this->stepped = 0;
44 this->steps_to_move = 0;
45 this->is_move_finished = false;
46 this->last_step_tick_valid= false;
47 this->last_step_tick= 0;
48 this->force_finish= false;
49
50 steps_per_mm = 1.0F;
51 max_rate = 50.0F;
52 minimum_step_rate = default_minimum_actuator_rate;
53
54 last_milestone_steps = 0;
55 last_milestone_mm = 0.0F;
56 current_position_steps= 0;
57 signal_step= 0;
58 }
59
60
61 // 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
62 // we also here check if the move is finished etc ..
63 // This is in highest priority interrupt so cannot be pre-empted
64 void StepperMotor::step()
65 {
66 // ignore if we are still processing the end of a block
67 if(this->is_move_finished) return;
68
69 if(!this->force_finish) {
70 // output to pins 37t
71 this->step_pin.set( 1 );
72
73 // move counter back 11t
74 this->fx_counter -= this->fx_ticks_per_step;
75
76 // we have moved a step 9t
77 this->stepped++;
78
79 // keep track of actuators actual position in steps
80 this->current_position_steps += (this->direction ? -1 : 1);
81
82 // we may need to callback on a specific step, usually used to synchronize deceleration timer
83 if(this->signal_step != 0 && this->stepped == this->signal_step) {
84 THEKERNEL->step_ticker->synchronize_acceleration(true);
85 this->signal_step= 0;
86 }
87 }
88
89 // Is this move finished ?
90 if( this->force_finish || this->stepped == this->steps_to_move) {
91 // Mark it as finished, then StepTicker will call signal_move_finished()
92 // This is so we don't call that before all the steps have been generated for this tick()
93 this->is_move_finished = true;
94 THEKERNEL->step_ticker->a_move_finished= true;
95 this->last_step_tick= THEKERNEL->step_ticker->get_tick_cnt(); // remember when last step was
96 if(this->force_finish) this->steps_to_move = stepped;
97 }
98 }
99
100 // If the move is finished, the StepTicker will call this ( because we asked it to in tick() )
101 void StepperMotor::signal_move_finished()
102 {
103 // work is done ! 8t
104 this->moving = false;
105 this->steps_to_move = 0;
106 this->minimum_step_rate = default_minimum_actuator_rate;
107
108 // signal it to whatever cares
109 // in this call a new block may start, new moves set and new speeds
110 this->end_hook->call();
111
112 // We only need to do this if we were not instructed to move
113 if( !this->moving ) {
114 this->update_exit_tick();
115 }
116
117 this->is_move_finished = false;
118 }
119
120 // This is just a way not to check for ( !this->moving || this->fx_ticks_per_step == 0 ) at every tick()
121 void StepperMotor::update_exit_tick()
122 {
123 if( !this->moving || this->steps_to_move == 0 ) {
124 // No more ticks will be recieved and no more events from StepTicker
125 THEKERNEL->step_ticker->remove_motor_from_active_list(this);
126 } else {
127 // we will now get ticks and StepTIcker will send us events
128 THEKERNEL->step_ticker->add_motor_to_active_list(this);
129 }
130 }
131
132 // Instruct the StepperMotor to move a certain number of steps
133 StepperMotor* StepperMotor::move( bool direction, unsigned int steps, float initial_speed)
134 {
135 this->dir_pin.set(direction);
136 this->direction = direction;
137 this->force_finish= false;
138
139 // How many steps we have to move until the move is done
140 this->steps_to_move = steps;
141
142 // Zero our tool counters
143 this->stepped = 0;
144 this->fx_ticks_per_step = 0xFFFFF000UL; // some big number so we don't start stepping before it is set again
145 if(this->last_step_tick_valid) {
146 // we set this based on when the last step was, thus compensating for missed ticks
147 uint32_t ts= THEKERNEL->step_ticker->ticks_since(this->last_step_tick);
148 // if an axis stops too soon then we can get a huge number of ticks here which causes problems, so if the number of ticks is too great we ignore them
149 // example of when this happens is when one axis is going very slow an the min 20steps/sec kicks in, the axis will reach its target much sooner leaving a long gap
150 // until the end of the block.
151 // TODO we may need to set this based on the current step rate, trouble is we don't know what that is yet, we could use the last fx_ticks_per_step as a guide
152 if(ts > 5) ts= 5; // limit to 50us catch up around 1-2 steps
153 else if(ts > 15) ts= 0; // no way to know what the delay was
154 this->fx_counter= ts*fx_increment;
155 }else{
156 this->fx_counter = 0; // set to zero as there was no step last block
157 }
158
159 // Starting now we are moving
160 if( steps > 0 ) {
161 if(initial_speed >= 0.0F) set_speed(initial_speed);
162 this->moving = true;
163 } else {
164 this->moving = false;
165 }
166 this->update_exit_tick();
167 return this;
168 }
169
170 // Set the speed at which this stepper moves in steps/sec, should be called set_step_rate()
171 // we need to make sure that we have a minimum speed here and that it fits the 32bit fixed point fx counters
172 // Note nothing will really ever go as slow as the minimum speed here, it is just forced to avoid bad errors
173 // fx_ticks_per_step is what actually sets the step rate, it is fixed point 18.14
174 StepperMotor* StepperMotor::set_speed( float speed )
175 {
176 if(speed < minimum_step_rate) {
177 speed= minimum_step_rate;
178 }
179
180 // if(speed <= 0.0F) { // we can't actually do 0 but we can get close, need to avoid divide by zero later on
181 // this->fx_ticks_per_step= 0xFFFFFFFFUL; // 0.381 steps/sec
182 // this->steps_per_second = THEKERNEL->step_ticker->get_frequency() / (this->fx_ticks_per_step >> fx_shift);
183 // return;
184 // }
185
186 // How many steps we must output per second
187 this->steps_per_second = speed;
188
189 // set the new speed, NOTE this can be pre-empted by stepticker so the following write needs to be atomic
190 this->fx_ticks_per_step= floor(fx_increment * THEKERNEL->step_ticker->get_frequency() / speed);
191 return this;
192 }
193
194 void StepperMotor::change_steps_per_mm(float new_steps)
195 {
196 steps_per_mm = new_steps;
197 last_milestone_steps = lroundf(last_milestone_mm * steps_per_mm);
198 current_position_steps = last_milestone_steps;
199 }
200
201 void StepperMotor::change_last_milestone(float new_milestone)
202 {
203 last_milestone_mm = new_milestone;
204 last_milestone_steps = lroundf(last_milestone_mm * steps_per_mm);
205 current_position_steps = last_milestone_steps;
206 }
207
208 int StepperMotor::steps_to_target(float target)
209 {
210 int target_steps = lroundf(target * steps_per_mm);
211 return target_steps - last_milestone_steps;
212 }