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;
85 this->single_config
= single
;
86 this->identifier
= config_identifier
;
87 this->retracted
= false;
88 this->volumetric_multiplier
= 1.0F
;
89 this->extruder_multiplier
= 1.0F
;
90 this->stepper_motor
= nullptr;
91 this->milestone_last_position
= 0;
92 this->max_volumetric_rate
= 0;
94 memset(this->offset
, 0, sizeof(this->offset
));
102 void Extruder::on_halt(void *arg
)
110 void Extruder::on_module_loaded()
113 this->on_config_reload(this);
116 this->target_position
= 0;
117 this->current_position
= 0;
118 this->unstepped_distance
= 0;
119 this->current_block
= NULL
;
122 // We work on the same Block as Stepper, so we need to know when it gets a new one and drops one
123 this->register_for_event(ON_BLOCK_BEGIN
);
124 this->register_for_event(ON_BLOCK_END
);
125 this->register_for_event(ON_GCODE_RECEIVED
);
126 this->register_for_event(ON_GCODE_EXECUTE
);
127 this->register_for_event(ON_HALT
);
128 this->register_for_event(ON_SPEED_CHANGE
);
129 this->register_for_event(ON_GET_PUBLIC_DATA
);
130 this->register_for_event(ON_SET_PUBLIC_DATA
);
132 // Update speed every *acceleration_ticks_per_second*
133 THEKERNEL
->step_ticker
->register_acceleration_tick_handler([this]() {
139 void Extruder::on_config_reload(void *argument
)
141 if( this->single_config
) {
142 // If this module uses the old "single extruder" configuration style
144 this->steps_per_millimeter
= THEKERNEL
->config
->value(extruder_steps_per_mm_checksum
)->by_default(1)->as_number();
145 this->filament_diameter
= THEKERNEL
->config
->value(extruder_filament_diameter_checksum
)->by_default(0)->as_number();
146 this->acceleration
= THEKERNEL
->config
->value(extruder_acceleration_checksum
)->by_default(1000)->as_number();
147 this->feed_rate
= THEKERNEL
->config
->value(extruder_default_feed_rate_checksum
)->by_default(1000)->as_number();
149 this->step_pin
.from_string( THEKERNEL
->config
->value(extruder_step_pin_checksum
)->by_default("nc" )->as_string())->as_output();
150 this->dir_pin
.from_string( THEKERNEL
->config
->value(extruder_dir_pin_checksum
)->by_default("nc" )->as_string())->as_output();
151 this->en_pin
.from_string( THEKERNEL
->config
->value(extruder_en_pin_checksum
)->by_default("nc" )->as_string())->as_output();
153 for(int i
= 0; i
< 3; i
++) {
157 this->enabled
= true;
160 // If this module was created with the new multi extruder configuration style
162 this->steps_per_millimeter
= THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, steps_per_mm_checksum
)->by_default(1)->as_number();
163 this->filament_diameter
= THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, filament_diameter_checksum
)->by_default(0)->as_number();
164 this->acceleration
= THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, acceleration_checksum
)->by_default(1000)->as_number();
165 this->feed_rate
= THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, default_feed_rate_checksum
)->by_default(1000)->as_number();
167 this->step_pin
.from_string( THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, step_pin_checksum
)->by_default("nc" )->as_string())->as_output();
168 this->dir_pin
.from_string( THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, dir_pin_checksum
)->by_default("nc" )->as_string())->as_output();
169 this->en_pin
.from_string( THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, en_pin_checksum
)->by_default("nc" )->as_string())->as_output();
171 this->offset
[X_AXIS
] = THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, x_offset_checksum
)->by_default(0)->as_number();
172 this->offset
[Y_AXIS
] = THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, y_offset_checksum
)->by_default(0)->as_number();
173 this->offset
[Z_AXIS
] = THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, z_offset_checksum
)->by_default(0)->as_number();
177 // these are only supported in the new syntax, no need to be backward compatible as they did not exist before the change
178 this->retract_length
= THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, retract_length_checksum
)->by_default(3)->as_number();
179 this->retract_feedrate
= THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, retract_feedrate_checksum
)->by_default(45)->as_number();
180 this->retract_recover_length
= THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, retract_recover_length_checksum
)->by_default(0)->as_number();
181 this->retract_recover_feedrate
= THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, retract_recover_feedrate_checksum
)->by_default(8)->as_number();
182 this->retract_zlift_length
= THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, retract_zlift_length_checksum
)->by_default(0)->as_number();
183 this->retract_zlift_feedrate
= THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, retract_zlift_feedrate_checksum
)->by_default(100 * 60)->as_number(); // mm/min
185 if(filament_diameter
> 0.01F
) {
186 this->volumetric_multiplier
= 1.0F
/ (powf(this->filament_diameter
/ 2, 2) * PI
);
189 // Stepper motor object for the extruder
190 this->stepper_motor
= new StepperMotor(step_pin
, dir_pin
, en_pin
);
191 this->stepper_motor
->attach(this, &Extruder::stepper_motor_finished_move
);
192 if( this->single_config
) {
193 this->stepper_motor
->set_max_rate(THEKERNEL
->config
->value(extruder_max_speed_checksum
)->by_default(1000)->as_number());
195 this->stepper_motor
->set_max_rate(THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, max_speed_checksum
)->by_default(1000)->as_number());
199 void Extruder::on_get_public_data(void *argument
)
201 PublicDataRequest
*pdr
= static_cast<PublicDataRequest
*>(argument
);
203 if(!pdr
->starts_with(extruder_checksum
)) return;
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
);
212 // check against maximum speeds and return the rate modifier
213 float Extruder::check_max_speeds(float target
, float isecs
)
215 float rm
= 1.0F
; // default no rate modification
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
;
224 milestone_last_position
+= target
;
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
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
236 //THEKERNEL->streams->printf("requested flow rate: %f mm³/sec, corrected flow rate: %f mm³/sec\n", v, v * rm);
239 // check for max speed as well
240 float max_speed
= this->stepper_motor
->get_max_rate();
242 if(this->filament_diameter
> 0.01F
) {
243 // volumetric so need to convert delta which is mm³ to mm
244 delta
*= volumetric_multiplier
;
248 float v
= delta
* isecs
; // the speed in mm/sec
250 sm
*= (max_speed
/ v
);
252 //THEKERNEL->streams->printf("requested speed: %f mm/sec, corrected speed: %f mm/sec\n", v, v * sm);
258 void Extruder::on_set_public_data(void *argument
)
260 PublicDataRequest
*pdr
= static_cast<PublicDataRequest
*>(argument
);
262 if(!pdr
->starts_with(extruder_checksum
)) return;
264 // handle extrude rates request from robot
265 if(pdr
->second_element_is(target_checksum
)) {
266 // disabled extruders do not reply NOTE only one enabled extruder supported
267 if(!this->enabled
) return;
269 float *d
= static_cast<float *>(pdr
->get_data_ptr());
270 float target
= d
[0]; // the E passed in on Gcode is in mm³ (maybe absolute or relative)
271 float isecs
= d
[1]; // inverted secs
273 // check against maximum speeds and return rate modifier
274 d
[1] = check_max_speeds(target
, isecs
);
280 // save or restore state
281 if(pdr
->second_element_is(save_state_checksum
)) {
282 this->saved_current_position
= this->current_position
;
283 this->saved_absolute_mode
= this->absolute_mode
;
285 } else if(pdr
->second_element_is(restore_state_checksum
)) {
286 // NOTE this only gets called when the queue is empty so the milestones will be the same
287 this->milestone_last_position
= this->current_position
= this->saved_current_position
;
288 this->milestone_absolute_mode
= this->absolute_mode
= this->saved_absolute_mode
;
293 void Extruder::on_gcode_received(void *argument
)
295 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
297 // M codes most execute immediately, most only execute if enabled
299 if (gcode
->m
== 114 && gcode
->subcode
== 0 && this->enabled
) {
301 int n
= snprintf(buf
, sizeof(buf
), " E:%1.3f ", this->current_position
);
302 gcode
->txt_after_ok
.append(buf
, n
);
304 } else if (gcode
->m
== 92 && ( (this->enabled
&& !gcode
->has_letter('P')) || (gcode
->has_letter('P') && gcode
->get_value('P') == this->identifier
) ) ) {
305 float spm
= this->steps_per_millimeter
;
306 if (gcode
->has_letter('E')) {
307 spm
= gcode
->get_value('E');
308 this->steps_per_millimeter
= spm
;
311 gcode
->stream
->printf("E:%g ", spm
);
312 gcode
->add_nl
= true;
314 } else if (gcode
->m
== 200 && ( (this->enabled
&& !gcode
->has_letter('P')) || (gcode
->has_letter('P') && gcode
->get_value('P') == this->identifier
)) ) {
315 if (gcode
->has_letter('D')) {
316 THEKERNEL
->conveyor
->wait_for_empty_queue(); // only apply after the queue has emptied
317 this->filament_diameter
= gcode
->get_value('D');
318 if(filament_diameter
> 0.01F
) {
319 this->volumetric_multiplier
= 1.0F
/ (powf(this->filament_diameter
/ 2, 2) * PI
);
321 this->volumetric_multiplier
= 1.0F
;
324 if(filament_diameter
> 0.01F
) {
325 gcode
->stream
->printf("Filament Diameter: %f\n", this->filament_diameter
);
327 gcode
->stream
->printf("Volumetric extrusion is disabled\n");
331 } else if (gcode
->m
== 203 && ( (this->enabled
&& !gcode
->has_letter('P')) || (gcode
->has_letter('P') && gcode
->get_value('P') == this->identifier
)) ) {
332 // M203 Exxx Vyyy Set maximum feedrates xxx mm/sec and/or yyy mm³/sec
333 if(gcode
->get_num_args() == 0) {
334 gcode
->stream
->printf("E:%g V:%g", this->stepper_motor
->get_max_rate(), this->max_volumetric_rate
);
335 gcode
->add_nl
= true;
338 if(gcode
->has_letter('E')) {
339 this->stepper_motor
->set_max_rate(gcode
->get_value('E'));
341 if(gcode
->has_letter('V')) {
342 this->max_volumetric_rate
= gcode
->get_value('V');
346 } else if (gcode
->m
== 204 && gcode
->has_letter('E') &&
347 ( (this->enabled
&& !gcode
->has_letter('P')) || (gcode
->has_letter('P') && gcode
->get_value('P') == this->identifier
)) ) {
348 // extruder acceleration M204 Ennn mm/sec^2 (Pnnn sets the specific extruder for M500)
349 this->acceleration
= gcode
->get_value('E');
351 } else if (gcode
->m
== 207 && ( (this->enabled
&& !gcode
->has_letter('P')) || (gcode
->has_letter('P') && gcode
->get_value('P') == this->identifier
)) ) {
352 // M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop] Q[zlift feedrate mm/min]
353 if(gcode
->has_letter('S')) retract_length
= gcode
->get_value('S');
354 if(gcode
->has_letter('F')) retract_feedrate
= gcode
->get_value('F') / 60.0F
; // specified in mm/min converted to mm/sec
355 if(gcode
->has_letter('Z')) retract_zlift_length
= gcode
->get_value('Z');
356 if(gcode
->has_letter('Q')) retract_zlift_feedrate
= gcode
->get_value('Q');
358 } else if (gcode
->m
== 208 && ( (this->enabled
&& !gcode
->has_letter('P')) || (gcode
->has_letter('P') && gcode
->get_value('P') == this->identifier
)) ) {
359 // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/min]
360 if(gcode
->has_letter('S')) retract_recover_length
= gcode
->get_value('S');
361 if(gcode
->has_letter('F')) retract_recover_feedrate
= gcode
->get_value('F') / 60.0F
; // specified in mm/min converted to mm/sec
363 } else if (gcode
->m
== 221 && this->enabled
) { // M221 S100 change flow rate by percentage
364 if(gcode
->has_letter('S')) {
365 this->extruder_multiplier
= gcode
->get_value('S') / 100.0F
;
367 gcode
->stream
->printf("Flow rate at %6.2f %%\n", this->extruder_multiplier
* 100.0F
);
370 } else if (gcode
->m
== 500 || gcode
->m
== 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings
371 if( this->single_config
) {
372 gcode
->stream
->printf(";E Steps per mm:\nM92 E%1.4f\n", this->steps_per_millimeter
);
373 gcode
->stream
->printf(";E Filament diameter:\nM200 D%1.4f\n", this->filament_diameter
);
374 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
);
375 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
);
376 gcode
->stream
->printf(";E acceleration mm/sec²:\nM204 E%1.4f\n", this->acceleration
);
377 gcode
->stream
->printf(";E max feed rate mm/sec:\nM203 E%1.4f\n", this->stepper_motor
->get_max_rate());
378 if(this->max_volumetric_rate
> 0) {
379 gcode
->stream
->printf(";E max volumetric rate mm³/sec:\nM203 V%1.4f\n", this->max_volumetric_rate
);
383 gcode
->stream
->printf(";E Steps per mm:\nM92 E%1.4f P%d\n", this->steps_per_millimeter
, this->identifier
);
384 gcode
->stream
->printf(";E Filament diameter:\nM200 D%1.4f P%d\n", this->filament_diameter
, this->identifier
);
385 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
);
386 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
);
387 gcode
->stream
->printf(";E acceleration mm/sec²:\nM204 E%1.4f P%d\n", this->acceleration
, this->identifier
);
388 gcode
->stream
->printf(";E max feed rate mm/sec:\nM203 E%1.4f P%d\n", this->stepper_motor
->get_max_rate(), this->identifier
);
389 if(this->max_volumetric_rate
> 0) {
390 gcode
->stream
->printf(";E max volumetric rate mm³/sec:\nM203 V%1.4f P%d\n", this->max_volumetric_rate
, this->identifier
);
394 } else if( gcode
->m
== 17 || gcode
->m
== 18 || gcode
->m
== 82 || gcode
->m
== 83 || gcode
->m
== 84 ) {
395 // Mcodes to pass along to on_gcode_execute
396 THEKERNEL
->conveyor
->append_gcode(gcode
);
400 } else if(gcode
->has_g
) {
401 // G codes, NOTE some are ignored if not enabled
402 if( (gcode
->g
== 92 && gcode
->has_letter('E')) || (gcode
->g
== 90 || gcode
->g
== 91) ) {
403 // Gcodes to pass along to on_gcode_execute
404 THEKERNEL
->conveyor
->append_gcode(gcode
);
406 } 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
407 // NOTE was ... gcode->has_letter('E') && !gcode->has_letter('X') && !gcode->has_letter('Y') && !gcode->has_letter('Z') ) {
408 // This is a SOLO move, we add an empty block to the queue to prevent subsequent gcodes being executed at the same time
409 THEKERNEL
->conveyor
->append_gcode(gcode
);
410 THEKERNEL
->conveyor
->queue_head_block();
412 } else if( this->enabled
&& (gcode
->g
== 10 || gcode
->g
== 11) && !gcode
->has_letter('L') ) {
413 // firmware retract command (Ignore if has L parameter that is not for us)
414 // check we are in the correct state of retract or unretract
415 if(gcode
->g
== 10 && !retracted
) {
416 this->retracted
= true;
417 this->cancel_zlift_restore
= false;
418 } else if(gcode
->g
== 11 && retracted
) {
419 this->retracted
= false;
421 return; // ignore duplicates
423 // 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
424 // this way zlift will happen after retract, (or before for unretract) NOTE we call the robot->on_gcode_receive directly to avoid recursion
425 if(retract_zlift_length
> 0 && gcode
->g
== 11 && !this->cancel_zlift_restore
) {
426 // reverse zlift happens before unretract
427 // 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
429 int n
= snprintf(buf
, sizeof(buf
), "G0 Z%1.4f F%1.4f", -retract_zlift_length
, retract_zlift_feedrate
);
431 Gcode
gc(cmd
, &(StreamOutput::NullStream
));
432 THEROBOT
->push_state(); // save state includes feed rates etc
433 THEROBOT
->absolute_mode
= false; // needs to be relative mode
434 THEROBOT
->on_gcode_received(&gc
); // send to robot directly
435 THEROBOT
->pop_state(); // restore state includes feed rates etc
438 // This is a solo move, we add an empty block to the queue to prevent subsequent gcodes being executed at the same time
439 THEKERNEL
->conveyor
->append_gcode(gcode
);
440 THEKERNEL
->conveyor
->queue_head_block();
442 if(retract_zlift_length
> 0 && gcode
->g
== 10) {
444 int n
= snprintf(buf
, sizeof(buf
), "G0 Z%1.4f F%1.4f", retract_zlift_length
, retract_zlift_feedrate
);
446 Gcode
gc(cmd
, &(StreamOutput::NullStream
));
447 THEROBOT
->push_state(); // save state includes feed rates etc
448 THEROBOT
->absolute_mode
= false; // needs to be relative mode
449 THEROBOT
->on_gcode_received(&gc
); // send to robot directly
450 THEROBOT
->pop_state(); // restore state includes feed rates etc
453 } else if( this->enabled
&& this->retracted
&& (gcode
->g
== 0 || gcode
->g
== 1) && gcode
->has_letter('Z')) {
454 // NOTE we cancel the zlift restore for the following G11 as we have moved to an absolute Z which we need to stay at
455 this->cancel_zlift_restore
= true;
459 // handle some codes now for the volumetric rate limiting
460 // G90 G91 G92 M82 M83
463 case 82: this->milestone_absolute_mode
= true; break;
464 case 83: this->milestone_absolute_mode
= false; break;
467 } else if(gcode
->has_g
) {
469 case 90: this->milestone_absolute_mode
= true; break;
470 case 91: this->milestone_absolute_mode
= false; break;
473 if(gcode
->has_letter('E')) {
474 this->milestone_last_position
= gcode
->get_value('E');
475 } else if(gcode
->get_num_args() == 0) {
476 this->milestone_last_position
= 0;
484 // Compute extrusion speed based on parameters and gcode distance of travel
485 void Extruder::on_gcode_execute(void *argument
)
487 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
489 // The mode is OFF by default, and SOLO or FOLLOW only if we need to extrude
492 // Absolute/relative mode, globably modal affect all extruders whether enabled or not
502 this->absolute_mode
= true;
505 this->absolute_mode
= false;
513 } else if( gcode
->has_g
&& (gcode
->g
== 90 || gcode
->g
== 91) ) {
514 this->absolute_mode
= (gcode
->g
== 90);
519 if( gcode
->has_g
&& this->enabled
) {
520 // G92: Reset extruder position
521 if( gcode
->g
== 92 ) {
522 if( gcode
->has_letter('E') ) {
523 this->current_position
= gcode
->get_value('E');
524 this->target_position
= this->current_position
;
525 this->unstepped_distance
= 0;
526 } else if( gcode
->get_num_args() == 0) {
527 this->current_position
= 0.0;
528 this->target_position
= this->current_position
;
529 this->unstepped_distance
= 0;
532 } else if (gcode
->g
== 10) {
533 // FW retract command
534 feed_rate
= retract_feedrate
; // mm/sec
536 this->travel_distance
= -retract_length
;
537 this->target_position
+= this->travel_distance
;
540 } else if (gcode
->g
== 11) {
541 // un retract command
542 feed_rate
= retract_recover_feedrate
; // mm/sec
544 this->travel_distance
= (retract_length
+ retract_recover_length
);
545 this->target_position
+= this->travel_distance
;
548 } else if (gcode
->g
<= 3) {
549 // Extrusion length from 'G' Gcode
550 if( gcode
->has_letter('E' )) {
551 // Get relative extrusion distance depending on mode ( in absolute mode we must subtract target_position )
552 float extrusion_distance
= gcode
->get_value('E');
553 float relative_extrusion_distance
= extrusion_distance
;
554 if (this->absolute_mode
) {
555 relative_extrusion_distance
-= this->target_position
;
556 this->target_position
= extrusion_distance
;
558 this->target_position
+= relative_extrusion_distance
;
561 // If the robot is moving, we follow it's movement, otherwise, we move alone
562 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
564 this->travel_distance
= relative_extrusion_distance
;
566 // We move proportionally to the robot's movement
568 this->travel_ratio
= (relative_extrusion_distance
* this->volumetric_multiplier
* this->extruder_multiplier
) / gcode
->millimeters_of_travel
; // adjust for volumetric extrusion and extruder multiplier
574 // 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
575 if (gcode
->has_letter('F')) {
576 feed_rate
= gcode
->get_value('F') / THEROBOT
->get_seconds_per_minute();
577 if (stepper_motor
->get_max_rate() > 0 && feed_rate
> stepper_motor
->get_max_rate())
578 feed_rate
= stepper_motor
->get_max_rate();
584 // When a new block begins, either follow the robot, or step by ourselves ( or stay back and do nothing )
585 void Extruder::on_block_begin(void *argument
)
587 if(!this->enabled
) return;
589 if( this->mode
== OFF
) {
590 this->current_block
= NULL
;
591 this->stepper_motor
->set_moved_last_block(false);
595 Block
*block
= static_cast<Block
*>(argument
);
596 if( this->mode
== FOLLOW
) {
597 // In FOLLOW mode, we just follow the stepper module
598 this->travel_distance
= block
->millimeters
* this->travel_ratio
;
601 // common for both FOLLOW and SOLO
602 this->current_position
+= this->travel_distance
;
604 // round down, we take care of the fractional part next time
605 int steps_to_step
= abs((int)floorf(this->steps_per_millimeter
* (this->travel_distance
+ this->unstepped_distance
) ));
607 // accumulate the fractional part
608 if ( this->travel_distance
> 0 ) {
609 this->unstepped_distance
+= this->travel_distance
- (steps_to_step
/ this->steps_per_millimeter
);
611 this->unstepped_distance
+= this->travel_distance
+ (steps_to_step
/ this->steps_per_millimeter
);
614 if( steps_to_step
!= 0 ) {
615 // We take the block, we have to release it or everything gets stuck
617 this->current_block
= block
;
618 this->stepper_motor
->move( (this->travel_distance
> 0), steps_to_step
);
620 if(this->mode
== FOLLOW
) {
621 on_speed_change(this); // set initial speed
622 this->stepper_motor
->set_moved_last_block(true);
625 uint32_t target_rate
= floorf(this->feed_rate
* this->steps_per_millimeter
);
626 this->stepper_motor
->set_speed(min( target_rate
, rate_increase() )); // start at first acceleration step
627 this->stepper_motor
->set_moved_last_block(false);
631 // no steps to take this time
632 this->current_block
= NULL
;
633 this->stepper_motor
->set_moved_last_block(false);
638 // When a block ends, pause the stepping interrupt
639 void Extruder::on_block_end(void *argument
)
641 if(!this->enabled
) return;
642 this->current_block
= NULL
;
645 uint32_t Extruder::rate_increase() const
647 return floorf((this->acceleration
/ THEKERNEL
->acceleration_ticks_per_second
) * this->steps_per_millimeter
);
650 // Called periodically to change the speed to match acceleration or to match the speed of the robot
651 // Only used in SOLO mode
652 void Extruder::acceleration_tick(void)
654 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
655 if(!this->enabled
|| this->mode
!= SOLO
|| this->current_block
== NULL
|| !this->stepper_motor
->is_moving() ) {
659 uint32_t current_rate
= this->stepper_motor
->get_steps_per_second();
660 uint32_t target_rate
= floorf(this->feed_rate
* this->steps_per_millimeter
);
662 if( current_rate
< target_rate
) {
663 current_rate
= min( target_rate
, current_rate
+ rate_increase() );
665 this->stepper_motor
->set_speed(current_rate
);
671 // When the stepper has finished it's move
672 uint32_t Extruder::stepper_motor_finished_move(uint32_t dummy
)
674 if(!this->enabled
) return 0;
676 //printf("extruder releasing\r\n");
678 if (this->current_block
) { // this should always be true, but sometimes it isn't. TODO: find out why
679 Block
*block
= this->current_block
;
680 this->current_block
= NULL
;