use push/pop state to save current state on suspend/resume
[clinton/Smoothieware.git] / src / modules / tools / extruder / Extruder.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
8 #include "Extruder.h"
9
10 #include "libs/Module.h"
11 #include "libs/Kernel.h"
12
13 #include "modules/robot/Conveyor.h"
14 #include "modules/robot/Block.h"
15 #include "StepperMotor.h"
16 #include "SlowTicker.h"
17 #include "Stepper.h"
18 #include "StepTicker.h"
19 #include "Config.h"
20 #include "StepperMotor.h"
21 #include "Robot.h"
22 #include "checksumm.h"
23 #include "ConfigValue.h"
24 #include "Gcode.h"
25 #include "libs/StreamOutput.h"
26 #include "PublicDataRequest.h"
27 #include "StreamOutputPool.h"
28 #include "ExtruderPublicAccess.h"
29
30 #include <mri.h>
31
32 // OLD config names for backwards compatibility, NOTE new configs will not be added here
33 #define extruder_module_enable_checksum CHECKSUM("extruder_module_enable")
34 #define extruder_steps_per_mm_checksum CHECKSUM("extruder_steps_per_mm")
35 #define extruder_filament_diameter_checksum CHECKSUM("extruder_filament_diameter")
36 #define extruder_acceleration_checksum CHECKSUM("extruder_acceleration")
37 #define extruder_step_pin_checksum CHECKSUM("extruder_step_pin")
38 #define extruder_dir_pin_checksum CHECKSUM("extruder_dir_pin")
39 #define extruder_en_pin_checksum CHECKSUM("extruder_en_pin")
40 #define extruder_max_speed_checksum CHECKSUM("extruder_max_speed")
41 #define extruder_default_feed_rate_checksum CHECKSUM("extruder_default_feed_rate")
42
43 // NEW config names
44
45 #define default_feed_rate_checksum CHECKSUM("default_feed_rate")
46 #define steps_per_mm_checksum CHECKSUM("steps_per_mm")
47 #define filament_diameter_checksum CHECKSUM("filament_diameter")
48 #define acceleration_checksum CHECKSUM("acceleration")
49 #define step_pin_checksum CHECKSUM("step_pin")
50 #define dir_pin_checksum CHECKSUM("dir_pin")
51 #define en_pin_checksum CHECKSUM("en_pin")
52 #define max_speed_checksum CHECKSUM("max_speed")
53 #define x_offset_checksum CHECKSUM("x_offset")
54 #define y_offset_checksum CHECKSUM("y_offset")
55 #define z_offset_checksum CHECKSUM("z_offset")
56
57 #define retract_length_checksum CHECKSUM("retract_length")
58 #define retract_feedrate_checksum CHECKSUM("retract_feedrate")
59 #define retract_recover_length_checksum CHECKSUM("retract_recover_length")
60 #define retract_recover_feedrate_checksum CHECKSUM("retract_recover_feedrate")
61 #define retract_zlift_length_checksum CHECKSUM("retract_zlift_length")
62 #define retract_zlift_feedrate_checksum CHECKSUM("retract_zlift_feedrate")
63
64 #define X_AXIS 0
65 #define Y_AXIS 1
66 #define Z_AXIS 2
67
68 #define OFF 0
69 #define SOLO 1
70 #define FOLLOW 2
71
72 #define PI 3.14159265358979F
73
74
75 /* The extruder module controls a filament extruder for 3D printing: http://en.wikipedia.org/wiki/Fused_deposition_modeling
76 * It can work in two modes : either the head does not move, and the extruder moves the filament at a specified speed ( SOLO mode here )
77 * or the head moves, and the extruder moves plastic at a speed proportional to the movement of the head ( FOLLOW mode here ).
78 */
79
80 Extruder::Extruder( uint16_t config_identifier, bool single )
81 {
82 this->absolute_mode = true;
83 this->milestone_absolute_mode = true;
84 this->enabled = false;
85 this->paused = false;
86 this->single_config = single;
87 this->identifier = config_identifier;
88 this->retracted = false;
89 this->volumetric_multiplier = 1.0F;
90 this->extruder_multiplier = 1.0F;
91 this->stepper_motor = nullptr;
92 this->milestone_last_position = 0;
93 this->max_volumetric_rate = 0;
94
95 memset(this->offset, 0, sizeof(this->offset));
96 }
97
98 Extruder::~Extruder()
99 {
100 delete stepper_motor;
101 }
102
103 void Extruder::on_halt(void *arg)
104 {
105 if(arg == nullptr) {
106 // turn off motor
107 this->en_pin.set(1);
108 }
109 }
110
111 void Extruder::on_module_loaded()
112 {
113 // Settings
114 this->on_config_reload(this);
115
116 // Start values
117 this->target_position = 0;
118 this->current_position = 0;
119 this->unstepped_distance = 0;
120 this->current_block = NULL;
121 this->mode = OFF;
122
123 // We work on the same Block as Stepper, so we need to know when it gets a new one and drops one
124 this->register_for_event(ON_BLOCK_BEGIN);
125 this->register_for_event(ON_BLOCK_END);
126 this->register_for_event(ON_GCODE_RECEIVED);
127 this->register_for_event(ON_GCODE_EXECUTE);
128 this->register_for_event(ON_PLAY);
129 this->register_for_event(ON_PAUSE);
130 this->register_for_event(ON_HALT);
131 this->register_for_event(ON_SPEED_CHANGE);
132 this->register_for_event(ON_GET_PUBLIC_DATA);
133 this->register_for_event(ON_SET_PUBLIC_DATA);
134
135 // Update speed every *acceleration_ticks_per_second*
136 THEKERNEL->step_ticker->register_acceleration_tick_handler([this]() {
137 acceleration_tick();
138 });
139 }
140
141 // Get config
142 void Extruder::on_config_reload(void *argument)
143 {
144 if( this->single_config ) {
145 // If this module uses the old "single extruder" configuration style
146
147 this->steps_per_millimeter = THEKERNEL->config->value(extruder_steps_per_mm_checksum )->by_default(1)->as_number();
148 this->filament_diameter = THEKERNEL->config->value(extruder_filament_diameter_checksum )->by_default(0)->as_number();
149 this->acceleration = THEKERNEL->config->value(extruder_acceleration_checksum )->by_default(1000)->as_number();
150 this->feed_rate = THEKERNEL->config->value(extruder_default_feed_rate_checksum )->by_default(1000)->as_number();
151
152 this->step_pin.from_string( THEKERNEL->config->value(extruder_step_pin_checksum )->by_default("nc" )->as_string())->as_output();
153 this->dir_pin.from_string( THEKERNEL->config->value(extruder_dir_pin_checksum )->by_default("nc" )->as_string())->as_output();
154 this->en_pin.from_string( THEKERNEL->config->value(extruder_en_pin_checksum )->by_default("nc" )->as_string())->as_output();
155
156 for(int i = 0; i < 3; i++) {
157 this->offset[i] = 0;
158 }
159
160 this->enabled = true;
161
162 } else {
163 // If this module was created with the new multi extruder configuration style
164
165 this->steps_per_millimeter = THEKERNEL->config->value(extruder_checksum, this->identifier, steps_per_mm_checksum )->by_default(1)->as_number();
166 this->filament_diameter = THEKERNEL->config->value(extruder_checksum, this->identifier, filament_diameter_checksum )->by_default(0)->as_number();
167 this->acceleration = THEKERNEL->config->value(extruder_checksum, this->identifier, acceleration_checksum )->by_default(1000)->as_number();
168 this->feed_rate = THEKERNEL->config->value(extruder_checksum, this->identifier, default_feed_rate_checksum )->by_default(1000)->as_number();
169
170 this->step_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, step_pin_checksum )->by_default("nc" )->as_string())->as_output();
171 this->dir_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, dir_pin_checksum )->by_default("nc" )->as_string())->as_output();
172 this->en_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, en_pin_checksum )->by_default("nc" )->as_string())->as_output();
173
174 this->offset[X_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, x_offset_checksum )->by_default(0)->as_number();
175 this->offset[Y_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, y_offset_checksum )->by_default(0)->as_number();
176 this->offset[Z_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, z_offset_checksum )->by_default(0)->as_number();
177
178 }
179
180 // these are only supported in the new syntax, no need to be backward compatible as they did not exist before the change
181 this->retract_length = THEKERNEL->config->value(extruder_checksum, this->identifier, retract_length_checksum)->by_default(3)->as_number();
182 this->retract_feedrate = THEKERNEL->config->value(extruder_checksum, this->identifier, retract_feedrate_checksum)->by_default(45)->as_number();
183 this->retract_recover_length = THEKERNEL->config->value(extruder_checksum, this->identifier, retract_recover_length_checksum)->by_default(0)->as_number();
184 this->retract_recover_feedrate = THEKERNEL->config->value(extruder_checksum, this->identifier, retract_recover_feedrate_checksum)->by_default(8)->as_number();
185 this->retract_zlift_length = THEKERNEL->config->value(extruder_checksum, this->identifier, retract_zlift_length_checksum)->by_default(0)->as_number();
186 this->retract_zlift_feedrate = THEKERNEL->config->value(extruder_checksum, this->identifier, retract_zlift_feedrate_checksum)->by_default(100 * 60)->as_number(); // mm/min
187
188 if(filament_diameter > 0.01F) {
189 this->volumetric_multiplier = 1.0F / (powf(this->filament_diameter / 2, 2) * PI);
190 }
191
192 // Stepper motor object for the extruder
193 this->stepper_motor = new StepperMotor(step_pin, dir_pin, en_pin);
194 this->stepper_motor->attach(this, &Extruder::stepper_motor_finished_move );
195 if( this->single_config ) {
196 this->stepper_motor->set_max_rate(THEKERNEL->config->value(extruder_max_speed_checksum)->by_default(1000)->as_number());
197 } else {
198 this->stepper_motor->set_max_rate(THEKERNEL->config->value(extruder_checksum, this->identifier, max_speed_checksum)->by_default(1000)->as_number());
199 }
200 }
201
202 void Extruder::on_get_public_data(void *argument)
203 {
204 PublicDataRequest *pdr = static_cast<PublicDataRequest *>(argument);
205
206 if(!pdr->starts_with(extruder_checksum)) return;
207
208 if(this->enabled) {
209 // Note this is allowing both step/mm and filament diameter to be exposed via public data
210 pdr->set_data_ptr(&this->steps_per_millimeter);
211 pdr->set_taken();
212 }
213 }
214
215 // check against maximum speeds and return the rate modifier
216 float Extruder::check_max_speeds(float target, float isecs)
217 {
218 float rm = 1.0F; // default no rate modification
219 float delta;
220 // get change in E (may be mm or mm³)
221 if(milestone_absolute_mode) {
222 delta = fabsf(target - milestone_last_position); // delta move
223 milestone_last_position = target;
224
225 } else {
226 delta = target;
227 milestone_last_position += target;
228 }
229
230 if(this->max_volumetric_rate > 0 && this->filament_diameter > 0.01F) {
231 // volumetric enabled and check for volumetric rate
232 float v = delta * isecs; // the flow rate in mm³/sec
233
234 // return the rate change needed to stay within the max rate
235 if(v > max_volumetric_rate) {
236 rm = max_volumetric_rate / v;
237 isecs *= rm; // this slows the rate down for the next test
238 }
239 //THEKERNEL->streams->printf("requested flow rate: %f mm³/sec, corrected flow rate: %f mm³/sec\n", v, v * rm);
240 }
241
242 // check for max speed as well
243 float max_speed = this->stepper_motor->get_max_rate();
244 if(max_speed > 0) {
245 if(this->filament_diameter > 0.01F) {
246 // volumetric so need to convert delta which is mm³ to mm
247 delta *= volumetric_multiplier;
248 }
249
250 float sm = 1.0F;
251 float v = delta * isecs; // the speed in mm/sec
252 if(v > max_speed) {
253 sm *= (max_speed / v);
254 }
255 //THEKERNEL->streams->printf("requested speed: %f mm/sec, corrected speed: %f mm/sec\n", v, v * sm);
256 rm *= sm;
257 }
258 return rm;
259 }
260
261 void Extruder::on_set_public_data(void *argument)
262 {
263 PublicDataRequest *pdr = static_cast<PublicDataRequest *>(argument);
264
265 if(!pdr->starts_with(extruder_checksum)) return;
266
267 // handle extrude rates request from robot
268 if(pdr->second_element_is(target_checksum)) {
269 // disabled extruders do not reply NOTE only one enabled extruder supported
270 if(!this->enabled) return;
271
272 float *d = static_cast<float *>(pdr->get_data_ptr());
273 float target = d[0]; // the E passed in on Gcode is in mm³ (maybe absolute or relative)
274 float isecs = d[1]; // inverted secs
275
276 // check against maximum speeds and return rate modifier
277 d[1] = check_max_speeds(target, isecs);
278
279 pdr->set_taken();
280 return;
281 }
282
283 // save or restore state
284 if(pdr->second_element_is(save_state_checksum)) {
285 this->saved_current_position = this->current_position;
286 this->saved_absolute_mode = this->absolute_mode;
287 pdr->set_taken();
288 } else if(pdr->second_element_is(restore_state_checksum)) {
289 // NOTE this only gets called when the queue is empty so the milestones will be the same
290 this->milestone_last_position= this->current_position = this->saved_current_position;
291 this->milestone_absolute_mode= this->absolute_mode = this->saved_absolute_mode;
292 pdr->set_taken();
293 }
294 }
295
296 // When the play/pause button is set to pause, or a module calls the ON_PAUSE event
297 void Extruder::on_pause(void *argument)
298 {
299 this->paused = true;
300 this->stepper_motor->pause();
301 }
302
303 // When the play/pause button is set to play, or a module calls the ON_PLAY event
304 void Extruder::on_play(void *argument)
305 {
306 this->paused = false;
307 this->stepper_motor->unpause();
308 }
309
310 void Extruder::on_gcode_received(void *argument)
311 {
312 Gcode *gcode = static_cast<Gcode *>(argument);
313
314 // M codes most execute immediately, most only execute if enabled
315 if (gcode->has_m) {
316 if (gcode->m == 114 && this->enabled) {
317 char buf[16];
318 int n = snprintf(buf, sizeof(buf), " E:%1.3f ", this->current_position);
319 gcode->txt_after_ok.append(buf, n);
320
321 } else if (gcode->m == 92 && ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier) ) ) {
322 float spm = this->steps_per_millimeter;
323 if (gcode->has_letter('E')) {
324 spm = gcode->get_value('E');
325 this->steps_per_millimeter = spm;
326 }
327
328 gcode->stream->printf("E:%g ", spm);
329 gcode->add_nl = true;
330
331 } else if (gcode->m == 200 && ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier)) ) {
332 if (gcode->has_letter('D')) {
333 THEKERNEL->conveyor->wait_for_empty_queue(); // only apply after the queue has emptied
334 this->filament_diameter = gcode->get_value('D');
335 if(filament_diameter > 0.01F) {
336 this->volumetric_multiplier = 1.0F / (powf(this->filament_diameter / 2, 2) * PI);
337 } else {
338 this->volumetric_multiplier = 1.0F;
339 }
340 } else {
341 if(filament_diameter > 0.01F) {
342 gcode->stream->printf("Filament Diameter: %f\n", this->filament_diameter);
343 } else {
344 gcode->stream->printf("Volumetric extrusion is disabled\n");
345 }
346 }
347
348 } else if (gcode->m == 203 && ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier)) ) {
349 // M203 Exxx Vyyy Set maximum feedrates xxx mm/sec and/or yyy mm³/sec
350 if(gcode->get_num_args() == 0) {
351 gcode->stream->printf("E:%g V:%g", this->stepper_motor->get_max_rate(), this->max_volumetric_rate);
352 gcode->add_nl = true;
353
354 } else {
355 if(gcode->has_letter('E')) {
356 this->stepper_motor->set_max_rate(gcode->get_value('E'));
357 }
358 if(gcode->has_letter('V')) {
359 this->max_volumetric_rate = gcode->get_value('V');
360 }
361 }
362
363 } else if (gcode->m == 204 && gcode->has_letter('E') &&
364 ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier)) ) {
365 // extruder acceleration M204 Ennn mm/sec^2 (Pnnn sets the specific extruder for M500)
366 this->acceleration = gcode->get_value('E');
367
368 } else if (gcode->m == 207 && ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier)) ) {
369 // M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop] Q[zlift feedrate mm/min]
370 if(gcode->has_letter('S')) retract_length = gcode->get_value('S');
371 if(gcode->has_letter('F')) retract_feedrate = gcode->get_value('F') / 60.0F; // specified in mm/min converted to mm/sec
372 if(gcode->has_letter('Z')) retract_zlift_length = gcode->get_value('Z');
373 if(gcode->has_letter('Q')) retract_zlift_feedrate = gcode->get_value('Q');
374
375 } else if (gcode->m == 208 && ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier)) ) {
376 // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/min]
377 if(gcode->has_letter('S')) retract_recover_length = gcode->get_value('S');
378 if(gcode->has_letter('F')) retract_recover_feedrate = gcode->get_value('F') / 60.0F; // specified in mm/min converted to mm/sec
379
380 } else if (gcode->m == 221 && this->enabled) { // M221 S100 change flow rate by percentage
381 if(gcode->has_letter('S')) {
382 this->extruder_multiplier = gcode->get_value('S') / 100.0F;
383 } else {
384 gcode->stream->printf("Flow rate at %6.2f %%\n", this->extruder_multiplier * 100.0F);
385 }
386
387 } else if (gcode->m == 500 || gcode->m == 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings
388 if( this->single_config ) {
389 gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f\n", this->steps_per_millimeter);
390 gcode->stream->printf(";E Filament diameter:\nM200 D%1.4f\n", this->filament_diameter);
391 gcode->stream->printf(";E retract length, feedrate, zlift length, feedrate:\nM207 S%1.4f F%1.4f Z%1.4f Q%1.4f\n", this->retract_length, this->retract_feedrate * 60.0F, this->retract_zlift_length, this->retract_zlift_feedrate);
392 gcode->stream->printf(";E retract recover length, feedrate:\nM208 S%1.4f F%1.4f\n", this->retract_recover_length, this->retract_recover_feedrate * 60.0F);
393 gcode->stream->printf(";E acceleration mm/sec²:\nM204 E%1.4f\n", this->acceleration);
394 gcode->stream->printf(";E max feed rate mm/sec:\nM203 E%1.4f\n", this->stepper_motor->get_max_rate());
395 if(this->max_volumetric_rate > 0) {
396 gcode->stream->printf(";E max volumetric rate mm³/sec:\nM203 V%1.4f\n", this->max_volumetric_rate);
397 }
398
399 } else {
400 gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f P%d\n", this->steps_per_millimeter, this->identifier);
401 gcode->stream->printf(";E Filament diameter:\nM200 D%1.4f P%d\n", this->filament_diameter, this->identifier);
402 gcode->stream->printf(";E retract length, feedrate:\nM207 S%1.4f F%1.4f Z%1.4f Q%1.4f P%d\n", this->retract_length, this->retract_feedrate * 60.0F, this->retract_zlift_length, this->retract_zlift_feedrate, this->identifier);
403 gcode->stream->printf(";E retract recover length, feedrate:\nM208 S%1.4f F%1.4f P%d\n", this->retract_recover_length, this->retract_recover_feedrate * 60.0F, this->identifier);
404 gcode->stream->printf(";E acceleration mm/sec²:\nM204 E%1.4f P%d\n", this->acceleration, this->identifier);
405 gcode->stream->printf(";E max feed rate mm/sec:\nM203 E%1.4f P%d\n", this->stepper_motor->get_max_rate(), this->identifier);
406 if(this->max_volumetric_rate > 0) {
407 gcode->stream->printf(";E max volumetric rate mm³/sec:\nM203 V%1.4f P%d\n", this->max_volumetric_rate, this->identifier);
408 }
409 }
410
411 } else if( gcode->m == 17 || gcode->m == 18 || gcode->m == 82 || gcode->m == 83 || gcode->m == 84 ) {
412 // Mcodes to pass along to on_gcode_execute
413 THEKERNEL->conveyor->append_gcode(gcode);
414
415 }
416
417 } else if(gcode->has_g) {
418 // G codes, NOTE some are ignored if not enabled
419 if( (gcode->g == 92 && gcode->has_letter('E')) || (gcode->g == 90 || gcode->g == 91) ) {
420 // Gcodes to pass along to on_gcode_execute
421 THEKERNEL->conveyor->append_gcode(gcode);
422
423 } else if( this->enabled && gcode->g < 4 && gcode->has_letter('E') && fabsf(gcode->millimeters_of_travel) < 0.00001F ) { // With floating numbers, we can have 0 != 0, NOTE needs to be same as in Robot.cpp#745
424 // NOTE was ... gcode->has_letter('E') && !gcode->has_letter('X') && !gcode->has_letter('Y') && !gcode->has_letter('Z') ) {
425 // This is a SOLO move, we add an empty block to the queue to prevent subsequent gcodes being executed at the same time
426 THEKERNEL->conveyor->append_gcode(gcode);
427 THEKERNEL->conveyor->queue_head_block();
428
429 } else if( this->enabled && (gcode->g == 10 || gcode->g == 11) ) { // firmware retract command
430 // check we are in the correct state of retract or unretract
431 if(gcode->g == 10 && !retracted) {
432 this->retracted = true;
433 this->cancel_zlift_restore = false;
434 } else if(gcode->g == 11 && retracted) {
435 this->retracted = false;
436 } else
437 return; // ignore duplicates
438
439 // now we do a special hack to add zlift if needed, this should go in Robot but if it did the zlift would be executed before retract which is bad
440 // this way zlift will happen after retract, (or before for unretract) NOTE we call the robot->on_gcode_receive directly to avoid recursion
441 if(retract_zlift_length > 0 && gcode->g == 11 && !this->cancel_zlift_restore) {
442 // reverse zlift happens before unretract
443 // NOTE we do not do this if cancel_zlift_restore is set to true, which happens if there is an absolute Z move inbetween G10 and G11
444 char buf[32];
445 int n = snprintf(buf, sizeof(buf), "G0 Z%1.4f F%1.4f", -retract_zlift_length, retract_zlift_feedrate);
446 string cmd(buf, n);
447 Gcode gc(cmd, &(StreamOutput::NullStream));
448 bool oldmode = THEKERNEL->robot->absolute_mode;
449 THEKERNEL->robot->absolute_mode = false; // needs to be relative mode
450 THEKERNEL->robot->on_gcode_received(&gc); // send to robot directly
451 THEKERNEL->robot->absolute_mode = oldmode; // restore mode
452 }
453
454 // This is a solo move, we add an empty block to the queue to prevent subsequent gcodes being executed at the same time
455 THEKERNEL->conveyor->append_gcode(gcode);
456 THEKERNEL->conveyor->queue_head_block();
457
458 if(retract_zlift_length > 0 && gcode->g == 10) {
459 char buf[32];
460 int n = snprintf(buf, sizeof(buf), "G0 Z%1.4f F%1.4f", retract_zlift_length, retract_zlift_feedrate);
461 string cmd(buf, n);
462 Gcode gc(cmd, &(StreamOutput::NullStream));
463 bool oldmode = THEKERNEL->robot->absolute_mode;
464 THEKERNEL->robot->absolute_mode = false; // needs to be relative mode
465 THEKERNEL->robot->on_gcode_received(&gc); // send to robot directly
466 THEKERNEL->robot->absolute_mode = oldmode; // restore mode
467 }
468
469 } else if( this->enabled && this->retracted && (gcode->g == 0 || gcode->g == 1) && gcode->has_letter('Z')) {
470 // NOTE we cancel the zlift restore for the following G11 as we have moved to an absolute Z which we need to stay at
471 this->cancel_zlift_restore = true;
472 }
473 }
474
475 // handle some codes now for the volumetric rate limiting
476 // G90 G91 G92 M82 M83
477 if(gcode->has_m) {
478 switch(gcode->m) {
479 case 82: this->milestone_absolute_mode = true; break;
480 case 83: this->milestone_absolute_mode = false; break;
481 }
482
483 } else if(gcode->has_g) {
484 switch(gcode->g) {
485 case 90: this->milestone_absolute_mode = true; break;
486 case 91: this->milestone_absolute_mode = false; break;
487 case 92:
488 if(this->enabled) {
489 if(gcode->has_letter('E')) {
490 this->milestone_last_position = gcode->get_value('E');
491 } else if(gcode->get_num_args() == 0) {
492 this->milestone_last_position = 0;
493 }
494 }
495 break;
496 }
497 }
498 }
499
500 // Compute extrusion speed based on parameters and gcode distance of travel
501 void Extruder::on_gcode_execute(void *argument)
502 {
503 Gcode *gcode = static_cast<Gcode *>(argument);
504
505 // The mode is OFF by default, and SOLO or FOLLOW only if we need to extrude
506 this->mode = OFF;
507
508 // Absolute/relative mode, globably modal affect all extruders whether enabled or not
509 if( gcode->has_m ) {
510 switch(gcode->m) {
511 case 17:
512 this->en_pin.set(0);
513 break;
514 case 18:
515 this->en_pin.set(1);
516 break;
517 case 82:
518 this->absolute_mode = true;
519 break;
520 case 83:
521 this->absolute_mode = false;
522 break;
523 case 84:
524 this->en_pin.set(1);
525 break;
526 }
527 return;
528
529 } else if( gcode->has_g && (gcode->g == 90 || gcode->g == 91) ) {
530 this->absolute_mode = (gcode->g == 90);
531 return;
532 }
533
534
535 if( gcode->has_g && this->enabled ) {
536 // G92: Reset extruder position
537 if( gcode->g == 92 ) {
538 if( gcode->has_letter('E') ) {
539 this->current_position = gcode->get_value('E');
540 this->target_position = this->current_position;
541 this->unstepped_distance = 0;
542 } else if( gcode->get_num_args() == 0) {
543 this->current_position = 0.0;
544 this->target_position = this->current_position;
545 this->unstepped_distance = 0;
546 }
547
548 } else if (gcode->g == 10) {
549 // FW retract command
550 feed_rate = retract_feedrate; // mm/sec
551 this->mode = SOLO;
552 this->travel_distance = -retract_length;
553 this->target_position += this->travel_distance;
554 this->en_pin.set(0);
555
556 } else if (gcode->g == 11) {
557 // un retract command
558 feed_rate = retract_recover_feedrate; // mm/sec
559 this->mode = SOLO;
560 this->travel_distance = (retract_length + retract_recover_length);
561 this->target_position += this->travel_distance;
562 this->en_pin.set(0);
563
564 } else if (gcode->g == 0 || gcode->g == 1) {
565 // Extrusion length from 'G' Gcode
566 if( gcode->has_letter('E' )) {
567 // Get relative extrusion distance depending on mode ( in absolute mode we must subtract target_position )
568 float extrusion_distance = gcode->get_value('E');
569 float relative_extrusion_distance = extrusion_distance;
570 if (this->absolute_mode) {
571 relative_extrusion_distance -= this->target_position;
572 this->target_position = extrusion_distance;
573 } else {
574 this->target_position += relative_extrusion_distance;
575 }
576
577 // If the robot is moving, we follow it's movement, otherwise, we move alone
578 if( fabsf(gcode->millimeters_of_travel) < 0.00001F ) { // With floating numbers, we can have 0 != 0, NOTE needs to be same as in Robot.cpp#745
579 this->mode = SOLO;
580 this->travel_distance = relative_extrusion_distance;
581 } else {
582 // We move proportionally to the robot's movement
583 this->mode = FOLLOW;
584 this->travel_ratio = (relative_extrusion_distance * this->volumetric_multiplier * this->extruder_multiplier) / gcode->millimeters_of_travel; // adjust for volumetric extrusion and extruder multiplier
585 }
586
587 this->en_pin.set(0);
588 }
589
590 // NOTE this is only used in SOLO mode, but any F on a G0/G1 will set the speed for future retracts that are not firmware retracts
591 if (gcode->has_letter('F')) {
592 feed_rate = gcode->get_value('F') / THEKERNEL->robot->get_seconds_per_minute();
593 if (stepper_motor->get_max_rate() > 0 && feed_rate > stepper_motor->get_max_rate())
594 feed_rate = stepper_motor->get_max_rate();
595 }
596 }
597 }
598 }
599
600 // When a new block begins, either follow the robot, or step by ourselves ( or stay back and do nothing )
601 void Extruder::on_block_begin(void *argument)
602 {
603 if(!this->enabled) return;
604
605 if( this->mode == OFF ) {
606 this->current_block = NULL;
607 this->stepper_motor->set_moved_last_block(false);
608 return;
609 }
610
611 Block *block = static_cast<Block *>(argument);
612 if( this->mode == FOLLOW ) {
613 // In FOLLOW mode, we just follow the stepper module
614 this->travel_distance = block->millimeters * this->travel_ratio;
615 }
616
617 // common for both FOLLOW and SOLO
618 this->current_position += this->travel_distance ;
619
620 // round down, we take care of the fractional part next time
621 int steps_to_step = abs(floorf(this->steps_per_millimeter * (this->travel_distance + this->unstepped_distance) ));
622
623 // accumulate the fractional part
624 if ( this->travel_distance > 0 ) {
625 this->unstepped_distance += this->travel_distance - (steps_to_step / this->steps_per_millimeter);
626 } else {
627 this->unstepped_distance += this->travel_distance + (steps_to_step / this->steps_per_millimeter);
628 }
629
630 if( steps_to_step != 0 ) {
631 // We take the block, we have to release it or everything gets stuck
632 block->take();
633 this->current_block = block;
634 this->stepper_motor->move( (this->travel_distance > 0), steps_to_step);
635
636 if(this->mode == FOLLOW) {
637 on_speed_change(this); // set initial speed
638 this->stepper_motor->set_moved_last_block(true);
639 } else {
640 // SOLO
641 uint32_t target_rate = floorf(this->feed_rate * this->steps_per_millimeter);
642 this->stepper_motor->set_speed(min( target_rate, rate_increase() )); // start at first acceleration step
643 this->stepper_motor->set_moved_last_block(false);
644 }
645
646 } else {
647 // no steps to take this time
648 this->current_block = NULL;
649 this->stepper_motor->set_moved_last_block(false);
650 }
651
652 }
653
654 // When a block ends, pause the stepping interrupt
655 void Extruder::on_block_end(void *argument)
656 {
657 if(!this->enabled) return;
658 this->current_block = NULL;
659 }
660
661 uint32_t Extruder::rate_increase() const
662 {
663 return floorf((this->acceleration / THEKERNEL->acceleration_ticks_per_second) * this->steps_per_millimeter);
664 }
665
666 // Called periodically to change the speed to match acceleration or to match the speed of the robot
667 // Only used in SOLO mode
668 void Extruder::acceleration_tick(void)
669 {
670 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
671 if(!this->enabled || this->mode != SOLO || this->current_block == NULL || !this->stepper_motor->is_moving() || this->paused ) {
672 return;
673 }
674
675 uint32_t current_rate = this->stepper_motor->get_steps_per_second();
676 uint32_t target_rate = floorf(this->feed_rate * this->steps_per_millimeter);
677
678 if( current_rate < target_rate ) {
679 current_rate = min( target_rate, current_rate + rate_increase() );
680 // steps per second
681 this->stepper_motor->set_speed(current_rate);
682 }
683
684 return;
685 }
686
687 // Speed has been updated for the robot's stepper, we must update accordingly
688 void Extruder::on_speed_change( void *argument )
689 {
690 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
691 if(!this->enabled || this->current_block == NULL || this->paused || this->mode != FOLLOW || !this->stepper_motor->is_moving()) {
692 return;
693 }
694
695 // if we are flushing the queue we need to stop the motor when it has decelerated to zero, we get this call with argumnet == 0 when this happens
696 // this is what steppermotor does
697 if(argument == 0) {
698 this->stepper_motor->move(0, 0);
699 this->current_block->release();
700 this->current_block = NULL;
701 return;
702 }
703
704 /*
705 * nominal block duration = current block's steps / ( current block's nominal rate )
706 * nominal extruder rate = extruder steps / nominal block duration
707 * actual extruder rate = nominal extruder rate * ( ( stepper's steps per second ) / ( current block's nominal rate ) )
708 * or actual extruder rate = ( ( extruder steps * ( current block's nominal_rate ) ) / current block's steps ) * ( ( stepper's steps per second ) / ( current block's nominal rate ) )
709 * or simplified : extruder steps * ( stepper's steps per second ) ) / current block's steps
710 * or even : ( stepper steps per second ) * ( extruder steps / current block's steps )
711 */
712
713 this->stepper_motor->set_speed(THEKERNEL->stepper->get_trapezoid_adjusted_rate() * (float)this->stepper_motor->get_steps_to_move() / (float)this->current_block->steps_event_count);
714 }
715
716 // When the stepper has finished it's move
717 uint32_t Extruder::stepper_motor_finished_move(uint32_t dummy)
718 {
719 if(!this->enabled) return 0;
720
721 //printf("extruder releasing\r\n");
722
723 if (this->current_block) { // this should always be true, but sometimes it isn't. TODO: find out why
724 Block *block = this->current_block;
725 this->current_block = NULL;
726 block->release();
727 }
728 return 0;
729
730 }