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"
30 #define extruder_module_enable_checksum CHECKSUM("extruder_module_enable")
31 #define extruder_steps_per_mm_checksum CHECKSUM("extruder_steps_per_mm")
32 #define extruder_filament_diameter_checksum CHECKSUM("extruder_filament_diameter")
33 #define extruder_acceleration_checksum CHECKSUM("extruder_acceleration")
34 #define extruder_step_pin_checksum CHECKSUM("extruder_step_pin")
35 #define extruder_dir_pin_checksum CHECKSUM("extruder_dir_pin")
36 #define extruder_en_pin_checksum CHECKSUM("extruder_en_pin")
37 #define extruder_max_speed_checksum CHECKSUM("extruder_max_speed")
39 #define extruder_checksum CHECKSUM("extruder")
41 #define default_feed_rate_checksum CHECKSUM("default_feed_rate")
42 #define steps_per_mm_checksum CHECKSUM("steps_per_mm")
43 #define filament_diameter_checksum CHECKSUM("filament_diameter")
44 #define acceleration_checksum CHECKSUM("acceleration")
45 #define step_pin_checksum CHECKSUM("step_pin")
46 #define dir_pin_checksum CHECKSUM("dir_pin")
47 #define en_pin_checksum CHECKSUM("en_pin")
48 #define max_speed_checksum CHECKSUM("max_speed")
49 #define x_offset_checksum CHECKSUM("x_offset")
50 #define y_offset_checksum CHECKSUM("y_offset")
51 #define z_offset_checksum CHECKSUM("z_offset")
61 #define PI 3.14159265358979F
63 #define max(a,b) (((a) > (b)) ? (a) : (b))
65 /* The extruder module controls a filament extruder for 3D printing: http://en.wikipedia.org/wiki/Fused_deposition_modeling
66 * 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 )
67 * or the head moves, and the extruder moves plastic at a speed proportional to the movement of the head ( FOLLOW mode here ).
70 Extruder::Extruder( uint16_t config_identifier
, bool single
)
72 this->absolute_mode
= true;
73 this->enabled
= false;
75 this->single_config
= single
;
76 this->identifier
= config_identifier
;
78 memset(this->offset
, 0, sizeof(this->offset
));
81 void Extruder::on_module_loaded()
85 this->on_config_reload(this);
87 // We work on the same Block as Stepper, so we need to know when it gets a new one and drops one
88 this->register_for_event(ON_BLOCK_BEGIN
);
89 this->register_for_event(ON_BLOCK_END
);
90 this->register_for_event(ON_GCODE_RECEIVED
);
91 this->register_for_event(ON_GCODE_EXECUTE
);
92 this->register_for_event(ON_PLAY
);
93 this->register_for_event(ON_PAUSE
);
94 this->register_for_event(ON_SPEED_CHANGE
);
95 this->register_for_event(ON_GET_PUBLIC_DATA
);
98 this->target_position
= 0;
99 this->current_position
= 0;
100 this->unstepped_distance
= 0;
101 this->current_block
= NULL
;
104 // Update speed every *acceleration_ticks_per_second*
105 // TODO: Make this an independent setting
106 THEKERNEL
->slow_ticker
->attach( THEKERNEL
->stepper
->get_acceleration_ticks_per_second() , this, &Extruder::acceleration_tick
);
108 // Stepper motor object for the extruder
109 this->stepper_motor
= THEKERNEL
->step_ticker
->add_stepper_motor( new StepperMotor(step_pin
, dir_pin
, en_pin
) );
110 this->stepper_motor
->attach(this, &Extruder::stepper_motor_finished_move
);
115 void Extruder::on_config_reload(void *argument
)
117 if( this->single_config
) {
118 // If this module uses the old "single extruder" configuration style
120 this->steps_per_millimeter_setting
= THEKERNEL
->config
->value(extruder_steps_per_mm_checksum
)->by_default(1)->as_number();
121 this->filament_diameter
= THEKERNEL
->config
->value(extruder_filament_diameter_checksum
)->by_default(0)->as_number();
122 this->acceleration
= THEKERNEL
->config
->value(extruder_acceleration_checksum
)->by_default(1000)->as_number();
123 this->max_speed
= THEKERNEL
->config
->value(extruder_max_speed_checksum
)->by_default(1000)->as_number();
124 this->feed_rate
= THEKERNEL
->config
->value(default_feed_rate_checksum
)->by_default(1000)->as_number();
126 this->step_pin
.from_string( THEKERNEL
->config
->value(extruder_step_pin_checksum
)->by_default("nc" )->as_string())->as_output();
127 this->dir_pin
.from_string( THEKERNEL
->config
->value(extruder_dir_pin_checksum
)->by_default("nc" )->as_string())->as_output();
128 this->en_pin
.from_string( THEKERNEL
->config
->value(extruder_en_pin_checksum
)->by_default("nc" )->as_string())->as_output();
130 for(int i
= 0; i
< 3; i
++) {
134 this->enabled
= true;
137 // If this module was created with the new multi extruder configuration style
139 this->steps_per_millimeter_setting
= THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, steps_per_mm_checksum
)->by_default(1)->as_number();
140 this->filament_diameter
= THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, filament_diameter_checksum
)->by_default(0)->as_number();
141 this->acceleration
= THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, acceleration_checksum
)->by_default(1000)->as_number();
142 this->max_speed
= THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, max_speed_checksum
)->by_default(1000)->as_number();
143 this->feed_rate
= THEKERNEL
->config
->value( default_feed_rate_checksum
)->by_default(1000)->as_number();
145 this->step_pin
.from_string( THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, step_pin_checksum
)->by_default("nc" )->as_string())->as_output();
146 this->dir_pin
.from_string( THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, dir_pin_checksum
)->by_default("nc" )->as_string())->as_output();
147 this->en_pin
.from_string( THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, en_pin_checksum
)->by_default("nc" )->as_string())->as_output();
149 this->offset
[X_AXIS
] = THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, x_offset_checksum
)->by_default(0)->as_number();
150 this->offset
[Y_AXIS
] = THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, y_offset_checksum
)->by_default(0)->as_number();
151 this->offset
[Z_AXIS
] = THEKERNEL
->config
->value(extruder_checksum
, this->identifier
, z_offset_checksum
)->by_default(0)->as_number();
154 this->update_steps_per_millimeter();
157 void Extruder::on_get_public_data(void* argument
){
158 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
160 if(!pdr
->starts_with(extruder_checksum
)) return;
163 // Note this is allowing both step/mm and filament diameter to be exposed via public data
164 pdr
->set_data_ptr(&this->steps_per_millimeter_setting
);
169 // When the play/pause button is set to pause, or a module calls the ON_PAUSE event
170 void Extruder::on_pause(void *argument
)
173 this->stepper_motor
->pause();
176 // When the play/pause button is set to play, or a module calls the ON_PLAY event
177 void Extruder::on_play(void *argument
)
179 this->paused
= false;
180 this->stepper_motor
->unpause();
183 void Extruder::on_gcode_received(void *argument
)
185 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
187 // Gcodes to execute immediately
189 if (gcode
->m
== 114 && this->enabled
) {
191 int n
= snprintf(buf
, sizeof(buf
), " E:%1.3f ", this->current_position
);
192 gcode
->txt_after_ok
.append(buf
, n
);
193 gcode
->mark_as_taken();
195 } else if (gcode
->m
== 92 && ( (this->enabled
&& !gcode
->has_letter('P')) || (gcode
->has_letter('P') && gcode
->get_value('P') == this->identifier
) ) ) {
196 float spm
= this->steps_per_millimeter
;
197 if (gcode
->has_letter('E')) {
198 spm
= gcode
->get_value('E');
199 this->steps_per_millimeter_setting
= spm
;
200 this->update_steps_per_millimeter();
203 gcode
->stream
->printf("E:%g ", spm
);
204 gcode
->add_nl
= true;
205 gcode
->mark_as_taken();
207 } else if (gcode
->m
== 200 && ( (this->enabled
&& !gcode
->has_letter('P')) || (gcode
->has_letter('P') && gcode
->get_value('P') == this->identifier
)) ) {
208 if (gcode
->has_letter('D')) {
209 this->filament_diameter
= gcode
->get_value('D');
210 this->update_steps_per_millimeter();
212 gcode
->mark_as_taken();
214 } else if (gcode
->m
== 500 || gcode
->m
== 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings
215 if( this->single_config
) {
216 gcode
->stream
->printf(";E Steps per mm:\nM92 E%1.4f\n", this->steps_per_millimeter_setting
);
217 gcode
->stream
->printf(";E Filament diameter:\nM200 D%1.4f\n", this->filament_diameter
);
219 gcode
->stream
->printf(";E Steps per mm:\nM92 E%1.4f P%d\n", this->steps_per_millimeter_setting
, this->identifier
);
220 gcode
->stream
->printf(";E Filament diameter:\nM200 D%1.4f P%d\n", this->filament_diameter
, this->identifier
);
222 gcode
->mark_as_taken();
227 // Gcodes to pass along to on_gcode_execute
228 if( ( gcode
->has_m
&& (gcode
->m
== 17 || gcode
->m
== 18 || gcode
->m
== 82 || gcode
->m
== 83 || gcode
->m
== 84 ) ) || ( gcode
->has_g
&& gcode
->g
== 92 && gcode
->has_letter('E') ) || ( gcode
->has_g
&& ( gcode
->g
== 90 || gcode
->g
== 91 ) ) ) {
229 THEKERNEL
->conveyor
->append_gcode(gcode
);
232 // Add to the queue for on_gcode_execute to process
233 if( gcode
->has_g
&& gcode
->g
< 4 && gcode
->has_letter('E') && this->enabled
) {
234 if( !gcode
->has_letter('X') && !gcode
->has_letter('Y') && !gcode
->has_letter('Z') ) {
235 THEKERNEL
->conveyor
->append_gcode(gcode
);
236 // This is a solo move, we add an empty block to the queue to prevent subsequent gcodes being executed at the same time
237 THEKERNEL
->conveyor
->queue_head_block();
240 // This is for follow move
245 // Compute extrusion speed based on parameters and gcode distance of travel
246 void Extruder::on_gcode_execute(void *argument
)
248 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
250 // Absolute/relative mode
252 if( gcode
->m
== 17 ) {
255 if( gcode
->m
== 18 ) {
258 if( gcode
->m
== 82 ) {
259 this->absolute_mode
= true;
261 if( gcode
->m
== 83 ) {
262 this->absolute_mode
= false;
264 if( gcode
->m
== 84 ) {
269 // The mode is OFF by default, and SOLO or FOLLOW only if we need to extrude
273 // G92: Reset extruder position
274 if( gcode
->g
== 92 && this->enabled
) {
275 if( gcode
->has_letter('E') ) {
276 this->current_position
= gcode
->get_value('E');
277 this->target_position
= this->current_position
;
278 this->unstepped_distance
= 0;
279 } else if( gcode
->get_num_args() == 0) {
280 this->current_position
= 0.0;
281 this->target_position
= this->current_position
;
282 this->unstepped_distance
= 0;
285 } else if (((gcode
->g
== 0) || (gcode
->g
== 1)) && this->enabled
) {
286 // Extrusion length from 'G' Gcode
287 if( gcode
->has_letter('E' )) {
288 // Get relative extrusion distance depending on mode ( in absolute mode we must substract target_position )
289 float extrusion_distance
= gcode
->get_value('E');
290 float relative_extrusion_distance
= extrusion_distance
;
291 if (this->absolute_mode
) {
292 relative_extrusion_distance
-= this->target_position
;
293 this->target_position
= extrusion_distance
;
295 this->target_position
+= relative_extrusion_distance
;
298 // If the robot is moving, we follow it's movement, otherwise, we move alone
299 if( fabs(gcode
->millimeters_of_travel
) < 0.0001 ) { // With floating numbers, we can have 0 != 0 ... beeeh. For more info see : http://upload.wikimedia.org/wikipedia/commons/0/0a/Cain_Henri_Vidal_Tuileries.jpg
301 this->travel_distance
= relative_extrusion_distance
;
303 // We move proportionally to the robot's movement
305 this->travel_ratio
= relative_extrusion_distance
/ gcode
->millimeters_of_travel
;
306 // TODO: check resulting flowrate, limit robot speed if it exceeds max_speed
312 if (gcode
->has_letter('F')) {
313 feed_rate
= gcode
->get_value('F') / THEKERNEL
->robot
->get_seconds_per_minute();
314 if (feed_rate
> max_speed
)
315 feed_rate
= max_speed
;
318 } else if( gcode
->g
== 90 ) {
319 this->absolute_mode
= true;
321 } else if( gcode
->g
== 91 ) {
322 this->absolute_mode
= false;
328 // When a new block begins, either follow the robot, or step by ourselves ( or stay back and do nothing )
329 void Extruder::on_block_begin(void *argument
)
331 if(!this->enabled
) return;
332 Block
*block
= static_cast<Block
*>(argument
);
335 if( this->mode
== SOLO
) {
336 // In solo mode we take the block so we can move even if the stepper has nothing to do
338 this->current_position
+= this->travel_distance
;
340 int steps_to_step
= abs(int(floor(this->steps_per_millimeter_setting
* (this->travel_distance
+ this->unstepped_distance
) )));
342 if ( this->travel_distance
> 0 ) {
343 this->unstepped_distance
+= this->travel_distance
- (steps_to_step
/ this->steps_per_millimeter_setting
); //catch any overflow
345 this->unstepped_distance
+= this->travel_distance
+ (steps_to_step
/ this->steps_per_millimeter_setting
); //catch any overflow
348 if( steps_to_step
!= 0 ) {
350 // We take the block, we have to release it or everything gets stuck
352 this->current_block
= block
;
354 this->stepper_motor
->set_steps_per_second(0);
355 this->stepper_motor
->move( ( this->travel_distance
> 0 ), steps_to_step
);
358 this->current_block
= NULL
;
361 } else if( this->mode
== FOLLOW
) {
362 // In non-solo mode, we just follow the stepper module
363 this->travel_distance
= block
->millimeters
* this->travel_ratio
;
365 this->current_position
+= this->travel_distance
;
367 int steps_to_step
= abs(int(floor(this->steps_per_millimeter
* (this->travel_distance
+ this->unstepped_distance
) )));
369 if ( this->travel_distance
> 0 ) {
370 this->unstepped_distance
+= this->travel_distance
- (steps_to_step
/ this->steps_per_millimeter
); //catch any overflow
372 this->unstepped_distance
+= this->travel_distance
+ (steps_to_step
/ this->steps_per_millimeter
); //catch any overflow
375 if( steps_to_step
!= 0 ) {
377 this->current_block
= block
;
379 this->stepper_motor
->move( ( this->travel_distance
> 0 ), steps_to_step
);
380 this->on_speed_change(0); // initialise speed in case we get called first
382 this->current_block
= NULL
;
385 } else if( this->mode
== OFF
) {
386 // No movement means we must reset our speed
387 this->current_block
= NULL
;
388 //this->stepper_motor->set_speed(0);
394 // When a block ends, pause the stepping interrupt
395 void Extruder::on_block_end(void *argument
)
397 if(!this->enabled
) return;
398 this->current_block
= NULL
;
401 // Called periodically to change the speed to match acceleration or to match the speed of the robot
402 uint32_t Extruder::acceleration_tick(uint32_t dummy
)
404 if(!this->enabled
) return 0;
406 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
407 if( this->current_block
== NULL
|| this->paused
|| this->mode
!= SOLO
) {
411 uint32_t current_rate
= this->stepper_motor
->get_steps_per_second();
412 uint32_t target_rate
= int(floor(this->feed_rate
* this->steps_per_millimeter_setting
)); // NOTE we use real steps here not the volumetric ones
414 if( current_rate
< target_rate
) {
415 uint32_t rate_increase
= int(floor((this->acceleration
/ THEKERNEL
->stepper
->get_acceleration_ticks_per_second()) * this->steps_per_millimeter_setting
));
416 current_rate
= min( target_rate
, current_rate
+ rate_increase
);
418 if( current_rate
> target_rate
) {
419 current_rate
= target_rate
;
423 this->stepper_motor
->set_speed(max(current_rate
, THEKERNEL
->stepper
->get_minimum_steps_per_second()));
428 // Speed has been updated for the robot's stepper, we must update accordingly
429 void Extruder::on_speed_change( void *argument
)
431 if(!this->enabled
) return;
433 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
434 if( this->current_block
== NULL
|| this->paused
|| this->mode
!= FOLLOW
|| this->stepper_motor
->is_moving() != true ) {
439 * nominal block duration = current block's steps / ( current block's nominal rate )
440 * nominal extruder rate = extruder steps / nominal block duration
441 * actual extruder rate = nominal extruder rate * ( ( stepper's steps per second ) / ( current block's nominal rate ) )
442 * 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 ) )
443 * or simplified : extruder steps * ( stepper's steps per second ) ) / current block's steps
444 * or even : ( stepper steps per second ) * ( extruder steps / current block's steps )
447 this->stepper_motor
->set_speed( max( ( THEKERNEL
->stepper
->get_trapezoid_adjusted_rate()) * ( (float)this->stepper_motor
->get_steps_to_move() / (float)this->current_block
->steps_event_count
), THEKERNEL
->stepper
->get_minimum_steps_per_second() ) );
451 // When the stepper has finished it's move
452 uint32_t Extruder::stepper_motor_finished_move(uint32_t dummy
)
454 if(!this->enabled
) return 0;
456 //printf("extruder releasing\r\n");
458 if (this->current_block
) { // this should always be true, but sometimes it isn't. TODO: find out why
459 Block
*block
= this->current_block
;
460 this->current_block
= NULL
;
467 void Extruder::update_steps_per_millimeter() {
468 if(this->filament_diameter
> 0.01) {
469 this->steps_per_millimeter
= this->steps_per_millimeter_setting
/ (powf(this->filament_diameter
/ 2, 2) * PI
);
471 this->steps_per_millimeter
= this->steps_per_millimeter_setting
;