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