Remove play/pause
[clinton/Smoothieware.git] / src / modules / robot / Stepper.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) with additions from Sungeun K. Jeon (https://github.com/chamnit/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 #include "Stepper.h"
9
10 #include "libs/Module.h"
11 #include "libs/Kernel.h"
12 #include "Planner.h"
13 #include "Conveyor.h"
14 #include "StepperMotor.h"
15 #include "Robot.h"
16 #include "checksumm.h"
17 #include "SlowTicker.h"
18 #include "Config.h"
19 #include "ConfigValue.h"
20 #include "Gcode.h"
21 #include "Block.h"
22 #include "StepTicker.h"
23
24 #include <vector>
25 using namespace std;
26
27 #include "libs/nuts_bolts.h"
28 #include "libs/Hook.h"
29
30 #include <mri.h>
31
32 // The stepper reacts to blocks that have XYZ movement to transform them into actual stepper motor moves
33 // TODO: This does accel, accel should be in StepperMotor
34
35 Stepper::Stepper()
36 {
37 this->current_block = NULL;
38 this->force_speed_update = false;
39 this->halted= false;
40 }
41
42 //Called when the module has just been loaded
43 void Stepper::on_module_loaded()
44 {
45 this->register_for_event(ON_BLOCK_BEGIN);
46 this->register_for_event(ON_BLOCK_END);
47 this->register_for_event(ON_GCODE_EXECUTE);
48 this->register_for_event(ON_GCODE_RECEIVED);
49 this->register_for_event(ON_HALT);
50
51 // Get onfiguration
52 this->on_config_reload(this);
53
54 // Acceleration ticker
55 THEKERNEL->step_ticker->register_acceleration_tick_handler([this](){trapezoid_generator_tick(); });
56
57 // Attach to the end_of_move stepper event
58 THEKERNEL->robot->alpha_stepper_motor->attach(this, &Stepper::stepper_motor_finished_move );
59 THEKERNEL->robot->beta_stepper_motor->attach( this, &Stepper::stepper_motor_finished_move );
60 THEKERNEL->robot->gamma_stepper_motor->attach(this, &Stepper::stepper_motor_finished_move );
61 }
62
63 // Get configuration from the config file
64 void Stepper::on_config_reload(void *argument)
65 {
66 // Steppers start off by default
67 this->turn_enable_pins_off();
68 }
69
70 void Stepper::on_halt(void *argument)
71 {
72 if(argument == nullptr) {
73 this->turn_enable_pins_off();
74 this->halted= true;
75 }else{
76 this->halted= false;
77 }
78 }
79
80 void Stepper::on_gcode_received(void *argument)
81 {
82 Gcode *gcode = static_cast<Gcode *>(argument);
83 // Attach gcodes to the last block for on_gcode_execute
84 if( gcode->has_m && (gcode->m == 84 || gcode->m == 17 || gcode->m == 18 )) {
85 THEKERNEL->conveyor->append_gcode(gcode);
86 }
87 }
88
89 // React to enable/disable gcodes
90 void Stepper::on_gcode_execute(void *argument)
91 {
92 Gcode *gcode = static_cast<Gcode *>(argument);
93
94 if( gcode->has_m) {
95 if( gcode->m == 17 ) {
96 this->turn_enable_pins_on();
97 }
98 if( (gcode->m == 84 || gcode->m == 18) && !gcode->has_letter('E') ) {
99 this->turn_enable_pins_off();
100 }
101 }
102 }
103
104 // Enable steppers
105 void Stepper::turn_enable_pins_on()
106 {
107 for (StepperMotor *m : THEKERNEL->robot->actuators)
108 m->enable(true);
109 this->enable_pins_status = true;
110 }
111
112 // Disable steppers
113 void Stepper::turn_enable_pins_off()
114 {
115 for (StepperMotor *m : THEKERNEL->robot->actuators)
116 m->enable(false);
117 this->enable_pins_status = false;
118 }
119
120 // A new block is popped from the queue
121 void Stepper::on_block_begin(void *argument)
122 {
123 Block *block = static_cast<Block *>(argument);
124
125 // Mark the new block as of interrest to us, handle blocks that have no axis moves properly (like Extrude blocks etc)
126 if(block->millimeters > 0.0F && (block->steps[ALPHA_STEPPER] > 0 || block->steps[BETA_STEPPER] > 0 || block->steps[GAMMA_STEPPER] > 0) ) {
127 block->take();
128
129 } else {
130 // none of the steppers move this block so make sure they know that
131 for(auto a : THEKERNEL->robot->actuators) {
132 a->set_moved_last_block(false);
133 }
134 return;
135 }
136
137 // We can't move with the enable pins off
138 if( this->enable_pins_status == false ) {
139 this->turn_enable_pins_on();
140 }
141
142 // Setup : instruct stepper motors to move
143 // Find the stepper with the more steps, it's the one the speed calculations will want to follow
144 this->main_stepper= nullptr;
145 if( block->steps[ALPHA_STEPPER] > 0 ) {
146 THEKERNEL->robot->alpha_stepper_motor->move( block->direction_bits[ALPHA_STEPPER], block->steps[ALPHA_STEPPER])->set_moved_last_block(true);
147 this->main_stepper = THEKERNEL->robot->alpha_stepper_motor;
148 }else{
149 THEKERNEL->robot->alpha_stepper_motor->set_moved_last_block(false);
150 }
151
152 if( block->steps[BETA_STEPPER ] > 0 ) {
153 THEKERNEL->robot->beta_stepper_motor->move( block->direction_bits[BETA_STEPPER], block->steps[BETA_STEPPER ])->set_moved_last_block(true);
154 if(this->main_stepper == nullptr || THEKERNEL->robot->beta_stepper_motor->get_steps_to_move() > this->main_stepper->get_steps_to_move())
155 this->main_stepper = THEKERNEL->robot->beta_stepper_motor;
156 }else{
157 THEKERNEL->robot->beta_stepper_motor->set_moved_last_block(false);
158 }
159
160 if( block->steps[GAMMA_STEPPER] > 0 ) {
161 THEKERNEL->robot->gamma_stepper_motor->move( block->direction_bits[GAMMA_STEPPER], block->steps[GAMMA_STEPPER])->set_moved_last_block(true);
162 if(this->main_stepper == nullptr || THEKERNEL->robot->gamma_stepper_motor->get_steps_to_move() > this->main_stepper->get_steps_to_move())
163 this->main_stepper = THEKERNEL->robot->gamma_stepper_motor;
164 }else{
165 THEKERNEL->robot->gamma_stepper_motor->set_moved_last_block(false);
166 }
167
168 this->current_block = block;
169
170 // Setup acceleration for this block
171 this->trapezoid_generator_reset();
172
173 // Set the initial speed for this move
174 this->trapezoid_generator_tick();
175
176 // synchronize the acceleration timer with the start of the new block so it does not drift and randomly fire during the block
177 THEKERNEL->step_ticker->synchronize_acceleration(false);
178
179 // set a flag to synchronize the acceleration timer with the deceleration step, and fire it immediately we get to that step
180 if( block->decelerate_after > 0 && block->decelerate_after+1 < this->main_stepper->steps_to_move ) {
181 this->main_stepper->signal_step= block->decelerate_after+1; // we make it +1 as deceleration does not start until steps > decelerate_after
182 }
183 }
184
185 // Current block is discarded
186 void Stepper::on_block_end(void *argument)
187 {
188 this->current_block = NULL; //stfu !
189 }
190
191 // When a stepper motor has finished it's assigned movement
192 uint32_t Stepper::stepper_motor_finished_move(uint32_t dummy)
193 {
194 // We care only if none is still moving
195 if( THEKERNEL->robot->alpha_stepper_motor->moving || THEKERNEL->robot->beta_stepper_motor->moving || THEKERNEL->robot->gamma_stepper_motor->moving ) {
196 return 0;
197 }
198
199 // This block is finished, release it
200 if( this->current_block != NULL ) {
201 this->current_block->release();
202 }
203
204 return 0;
205 }
206
207
208 // This is called ACCELERATION_TICKS_PER_SECOND times per second by the step_event
209 // interrupt. It can be assumed that the trapezoid-generator-parameters and the
210 // current_block stays untouched by outside handlers for the duration of this function call.
211 // NOTE caled at the same priority as PendSV so it may make that longer but it is better that having htis pre empted by pendsv
212 void Stepper::trapezoid_generator_tick(void)
213 {
214 // Do not do the accel math for nothing
215 if(this->current_block && this->main_stepper->moving ) {
216
217 // Store this here because we use it a lot down there
218 uint32_t current_steps_completed = this->main_stepper->stepped;
219 float last_rate= trapezoid_adjusted_rate;
220
221 if( this->force_speed_update ) {
222 // Do not accel, just set the value
223 this->force_speed_update = false;
224 last_rate= -1;
225
226 } else if(THEKERNEL->conveyor->is_flushing()) {
227 // if we are flushing the queue, decelerate to 0 then finish this block
228 if (trapezoid_adjusted_rate > current_block->rate_delta * 1.5F) {
229 trapezoid_adjusted_rate -= current_block->rate_delta;
230
231 } else if (trapezoid_adjusted_rate == current_block->rate_delta * 0.5F) {
232 for (auto i : THEKERNEL->robot->actuators) i->move(i->direction, 0); // stop motors
233 if (current_block) current_block->release();
234 THEKERNEL->call_event(ON_SPEED_CHANGE, 0); // tell others we stopped
235 return;
236
237 } else {
238 trapezoid_adjusted_rate = current_block->rate_delta * 0.5F;
239 }
240
241 } else if(current_steps_completed <= this->current_block->accelerate_until) {
242 // If we are accelerating
243 // Increase speed
244 this->trapezoid_adjusted_rate += this->current_block->rate_delta;
245 if (this->trapezoid_adjusted_rate > this->current_block->nominal_rate ) {
246 this->trapezoid_adjusted_rate = this->current_block->nominal_rate;
247 }
248
249 } else if (current_steps_completed > this->current_block->decelerate_after) {
250 // If we are decelerating
251 // Reduce speed
252 // NOTE: We will only reduce speed if the result will be > 0. This catches small
253 // rounding errors that might leave steps hanging after the last trapezoid tick.
254 if(this->trapezoid_adjusted_rate > this->current_block->rate_delta * 1.5F) {
255 this->trapezoid_adjusted_rate -= this->current_block->rate_delta;
256 } else {
257 this->trapezoid_adjusted_rate = this->current_block->rate_delta * 1.5F;
258 }
259 if(this->trapezoid_adjusted_rate < this->current_block->final_rate ) {
260 this->trapezoid_adjusted_rate = this->current_block->final_rate;
261 }
262
263 } else if (trapezoid_adjusted_rate != current_block->nominal_rate) {
264 // If we are cruising
265 // Make sure we cruise at exactly nominal rate
266 this->trapezoid_adjusted_rate = this->current_block->nominal_rate;
267 }
268
269 if(last_rate != trapezoid_adjusted_rate) {
270 // don't call this if speed did not change
271 this->set_step_events_per_second(this->trapezoid_adjusted_rate);
272 }
273 }
274 }
275
276 // Initializes the trapezoid generator from the current block. Called whenever a new
277 // block begins.
278 inline void Stepper::trapezoid_generator_reset()
279 {
280 this->trapezoid_adjusted_rate = this->current_block->initial_rate;
281 this->force_speed_update = true;
282 }
283
284 // Update the speed for all steppers
285 void Stepper::set_step_events_per_second( float steps_per_second )
286 {
287 float isps= steps_per_second / this->current_block->steps_event_count;
288
289 // Instruct the stepper motors
290 if( THEKERNEL->robot->alpha_stepper_motor->moving ) {
291 THEKERNEL->robot->alpha_stepper_motor->set_speed(isps * this->current_block->steps[ALPHA_STEPPER]);
292 }
293 if( THEKERNEL->robot->beta_stepper_motor->moving ) {
294 THEKERNEL->robot->beta_stepper_motor->set_speed(isps * this->current_block->steps[BETA_STEPPER]);
295 }
296 if( THEKERNEL->robot->gamma_stepper_motor->moving ) {
297 THEKERNEL->robot->gamma_stepper_motor->set_speed(isps * this->current_block->steps[GAMMA_STEPPER]);
298 }
299
300 // Other modules might want to know the speed changed
301 THEKERNEL->call_event(ON_SPEED_CHANGE, this);
302 }
303
304