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/>.
10 #include "libs/Module.h"
11 #include "libs/Kernel.h"
13 #include "modules/robot/Conveyor.h"
14 #include "modules/robot/Block.h"
15 #include "StepperMotor.h"
16 #include "SlowTicker.h"
18 #include "StepTicker.h"
20 #include "StepperMotor.h"
22 #include "checksumm.h"
23 #include "ConfigValue.h"
25 #include "libs/StreamOutput.h"
26 #include "PublicDataRequest.h"
27 #include "StreamOutputPool.h"
28 #include "ExtruderPublicAccess.h"
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")
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")
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")
72 #define PI 3.14159265358979F
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 ).
80 Extruder::Extruder( uint16_t config_identifier
, bool single
)
82 this->absolute_mode
= true;
83 this->milestone_absolute_mode
= true;
84 this->enabled
= 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;
95 memset(this->offset
, 0, sizeof(this->offset
));
100 delete stepper_motor
;
103 void Extruder::on_halt(void *arg
)
111 void Extruder::on_module_loaded()
114 this->on_config_reload(this);
117 this->target_position
= 0;
118 this->current_position
= 0;
119 this->unstepped_distance
= 0;
120 this->current_block
= NULL
;
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
);
135 // Update speed every *acceleration_ticks_per_second*
136 THEKERNEL
->step_ticker
->register_acceleration_tick_handler([this]() {
142 void Extruder::on_config_reload(void *argument
)
144 if( this->single_config
) {
145 // If this module uses the old "single extruder" configuration style
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();
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();
156 for(int i
= 0; i
< 3; i
++) {
160 this->enabled
= true;
163 // If this module was created with the new multi extruder configuration style
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();
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();
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();
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
188 if(filament_diameter
> 0.01F
) {
189 this->volumetric_multiplier
= 1.0F
/ (powf(this->filament_diameter
/ 2, 2) * PI
);
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());
198 this->stepper_motor
->set_max_rate(THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, max_speed_checksum
)->by_default(1000)->as_number());
202 void Extruder::on_get_public_data(void *argument
)
204 PublicDataRequest
*pdr
= static_cast<PublicDataRequest
*>(argument
);
206 if(!pdr
->starts_with(extruder_checksum
)) return;
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
);
215 // check against maximum speeds and return the rate modifier
216 float Extruder::check_max_speeds(float target
, float isecs
)
218 float rm
= 1.0F
; // default no rate modification
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
;
227 milestone_last_position
+= target
;
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
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
239 //THEKERNEL->streams->printf("requested flow rate: %f mm³/sec, corrected flow rate: %f mm³/sec\n", v, v * rm);
242 // check for max speed as well
243 float max_speed
= this->stepper_motor
->get_max_rate();
245 if(this->filament_diameter
> 0.01F
) {
246 // volumetric so need to convert delta which is mm³ to mm
247 delta
*= volumetric_multiplier
;
251 float v
= delta
* isecs
; // the speed in mm/sec
253 sm
*= (max_speed
/ v
);
255 //THEKERNEL->streams->printf("requested speed: %f mm/sec, corrected speed: %f mm/sec\n", v, v * sm);
261 void Extruder::on_set_public_data(void *argument
)
263 PublicDataRequest
*pdr
= static_cast<PublicDataRequest
*>(argument
);
265 if(!pdr
->starts_with(extruder_checksum
)) return;
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;
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
276 // check against maximum speeds and return rate modifier
277 d
[1] = check_max_speeds(target
, isecs
);
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
;
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
;
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
)
300 this->stepper_motor
->pause();
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
)
306 this->paused
= false;
307 this->stepper_motor
->unpause();
310 void Extruder::on_gcode_received(void *argument
)
312 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
314 // M codes most execute immediately, most only execute if enabled
316 if (gcode
->m
== 114 && this->enabled
) {
318 int n
= snprintf(buf
, sizeof(buf
), " E:%1.3f ", this->current_position
);
319 gcode
->txt_after_ok
.append(buf
, n
);
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
;
328 gcode
->stream
->printf("E:%g ", spm
);
329 gcode
->add_nl
= true;
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
);
338 this->volumetric_multiplier
= 1.0F
;
341 if(filament_diameter
> 0.01F
) {
342 gcode
->stream
->printf("Filament Diameter: %f\n", this->filament_diameter
);
344 gcode
->stream
->printf("Volumetric extrusion is disabled\n");
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;
355 if(gcode
->has_letter('E')) {
356 this->stepper_motor
->set_max_rate(gcode
->get_value('E'));
358 if(gcode
->has_letter('V')) {
359 this->max_volumetric_rate
= gcode
->get_value('V');
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');
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');
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
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
;
384 gcode
->stream
->printf("Flow rate at %6.2f %%\n", this->extruder_multiplier
* 100.0F
);
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
);
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
);
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
);
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
);
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();
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;
437 return; // ignore duplicates
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
445 int n
= snprintf(buf
, sizeof(buf
), "G0 Z%1.4f F%1.4f", -retract_zlift_length
, retract_zlift_feedrate
);
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
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();
458 if(retract_zlift_length
> 0 && gcode
->g
== 10) {
460 int n
= snprintf(buf
, sizeof(buf
), "G0 Z%1.4f F%1.4f", retract_zlift_length
, retract_zlift_feedrate
);
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
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;
475 // handle some codes now for the volumetric rate limiting
476 // G90 G91 G92 M82 M83
479 case 82: this->milestone_absolute_mode
= true; break;
480 case 83: this->milestone_absolute_mode
= false; break;
483 } else if(gcode
->has_g
) {
485 case 90: this->milestone_absolute_mode
= true; break;
486 case 91: this->milestone_absolute_mode
= false; break;
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;
500 // Compute extrusion speed based on parameters and gcode distance of travel
501 void Extruder::on_gcode_execute(void *argument
)
503 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
505 // The mode is OFF by default, and SOLO or FOLLOW only if we need to extrude
508 // Absolute/relative mode, globably modal affect all extruders whether enabled or not
518 this->absolute_mode
= true;
521 this->absolute_mode
= false;
529 } else if( gcode
->has_g
&& (gcode
->g
== 90 || gcode
->g
== 91) ) {
530 this->absolute_mode
= (gcode
->g
== 90);
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;
548 } else if (gcode
->g
== 10) {
549 // FW retract command
550 feed_rate
= retract_feedrate
; // mm/sec
552 this->travel_distance
= -retract_length
;
553 this->target_position
+= this->travel_distance
;
556 } else if (gcode
->g
== 11) {
557 // un retract command
558 feed_rate
= retract_recover_feedrate
; // mm/sec
560 this->travel_distance
= (retract_length
+ retract_recover_length
);
561 this->target_position
+= this->travel_distance
;
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
;
574 this->target_position
+= relative_extrusion_distance
;
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
580 this->travel_distance
= relative_extrusion_distance
;
582 // We move proportionally to the robot's movement
584 this->travel_ratio
= (relative_extrusion_distance
* this->volumetric_multiplier
* this->extruder_multiplier
) / gcode
->millimeters_of_travel
; // adjust for volumetric extrusion and extruder multiplier
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();
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
)
603 if(!this->enabled
) return;
605 if( this->mode
== OFF
) {
606 this->current_block
= NULL
;
607 this->stepper_motor
->set_moved_last_block(false);
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
;
617 // common for both FOLLOW and SOLO
618 this->current_position
+= this->travel_distance
;
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
) ));
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
);
627 this->unstepped_distance
+= this->travel_distance
+ (steps_to_step
/ this->steps_per_millimeter
);
630 if( steps_to_step
!= 0 ) {
631 // We take the block, we have to release it or everything gets stuck
633 this->current_block
= block
;
634 this->stepper_motor
->move( (this->travel_distance
> 0), steps_to_step
);
636 if(this->mode
== FOLLOW
) {
637 on_speed_change(this); // set initial speed
638 this->stepper_motor
->set_moved_last_block(true);
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);
647 // no steps to take this time
648 this->current_block
= NULL
;
649 this->stepper_motor
->set_moved_last_block(false);
654 // When a block ends, pause the stepping interrupt
655 void Extruder::on_block_end(void *argument
)
657 if(!this->enabled
) return;
658 this->current_block
= NULL
;
661 uint32_t Extruder::rate_increase() const
663 return floorf((this->acceleration
/ THEKERNEL
->acceleration_ticks_per_second
) * this->steps_per_millimeter
);
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)
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
) {
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
);
678 if( current_rate
< target_rate
) {
679 current_rate
= min( target_rate
, current_rate
+ rate_increase() );
681 this->stepper_motor
->set_speed(current_rate
);
687 // Speed has been updated for the robot's stepper, we must update accordingly
688 void Extruder::on_speed_change( void *argument
)
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()) {
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
698 this->stepper_motor
->move(0, 0);
699 this->current_block
->release();
700 this->current_block
= NULL
;
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 )
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
);
716 // When the stepper has finished it's move
717 uint32_t Extruder::stepper_motor_finished_move(uint32_t dummy
)
719 if(!this->enabled
) return 0;
721 //printf("extruder releasing\r\n");
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
;