Merge branch 'edge'
[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
28 #include <mri.h>
29
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")
38
39 #define extruder_checksum CHECKSUM("extruder")
40
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")
52
53 #define X_AXIS 0
54 #define Y_AXIS 1
55 #define Z_AXIS 2
56
57 #define OFF 0
58 #define SOLO 1
59 #define FOLLOW 2
60
61 #define PI 3.14159265358979F
62
63 #define max(a,b) (((a) > (b)) ? (a) : (b))
64
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 ).
68 */
69
70 Extruder::Extruder( uint16_t config_identifier, bool single )
71 {
72 this->absolute_mode = true;
73 this->enabled = false;
74 this->paused = false;
75 this->single_config = single;
76 this->identifier = config_identifier;
77
78 memset(this->offset, 0, sizeof(this->offset));
79 }
80
81 void Extruder::on_module_loaded()
82 {
83
84 // Settings
85 this->on_config_reload(this);
86
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);
96
97 // Start values
98 this->target_position = 0;
99 this->current_position = 0;
100 this->unstepped_distance = 0;
101 this->current_block = NULL;
102 this->mode = OFF;
103
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 );
107
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 );
111
112 }
113
114 // Get config
115 void Extruder::on_config_reload(void *argument)
116 {
117 if( this->single_config ) {
118 // If this module uses the old "single extruder" configuration style
119
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();
125
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();
129
130 for(int i = 0; i < 3; i++) {
131 this->offset[i] = 0;
132 }
133
134 this->enabled = true;
135
136 } else {
137 // If this module was created with the new multi extruder configuration style
138
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();
144
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();
148
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();
152 }
153
154 this->update_steps_per_millimeter();
155 }
156
157 void Extruder::on_get_public_data(void* argument){
158 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
159
160 if(!pdr->starts_with(extruder_checksum)) return;
161
162 if(this->enabled) {
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);
165 pdr->set_taken();
166 }
167 }
168
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)
171 {
172 this->paused = true;
173 this->stepper_motor->pause();
174 }
175
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)
178 {
179 this->paused = false;
180 this->stepper_motor->unpause();
181 }
182
183 void Extruder::on_gcode_received(void *argument)
184 {
185 Gcode *gcode = static_cast<Gcode *>(argument);
186
187 // Gcodes to execute immediately
188 if (gcode->has_m) {
189 if (gcode->m == 114 && this->enabled) {
190 char buf[16];
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();
194
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();
201 }
202
203 gcode->stream->printf("E:%g ", spm);
204 gcode->add_nl = true;
205 gcode->mark_as_taken();
206
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();
211 }
212 gcode->mark_as_taken();
213
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);
218 } else {
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);
221 }
222 gcode->mark_as_taken();
223 return;
224 }
225 }
226
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);
230 }
231
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();
238 }
239 } else {
240 // This is for follow move
241
242 }
243 }
244
245 // Compute extrusion speed based on parameters and gcode distance of travel
246 void Extruder::on_gcode_execute(void *argument)
247 {
248 Gcode *gcode = static_cast<Gcode *>(argument);
249
250 // Absolute/relative mode
251 if( gcode->has_m ) {
252 if( gcode->m == 17 ) {
253 this->en_pin.set(0);
254 }
255 if( gcode->m == 18 ) {
256 this->en_pin.set(1);
257 }
258 if( gcode->m == 82 ) {
259 this->absolute_mode = true;
260 }
261 if( gcode->m == 83 ) {
262 this->absolute_mode = false;
263 }
264 if( gcode->m == 84 ) {
265 this->en_pin.set(1);
266 }
267 }
268
269 // The mode is OFF by default, and SOLO or FOLLOW only if we need to extrude
270 this->mode = OFF;
271
272 if( gcode->has_g ) {
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;
283 }
284
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;
294 } else {
295 this->target_position += relative_extrusion_distance;
296 }
297
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
300 this->mode = SOLO;
301 this->travel_distance = relative_extrusion_distance;
302 } else {
303 // We move proportionally to the robot's movement
304 this->mode = FOLLOW;
305 this->travel_ratio = relative_extrusion_distance / gcode->millimeters_of_travel;
306 // TODO: check resulting flowrate, limit robot speed if it exceeds max_speed
307 }
308
309 this->en_pin.set(0);
310 }
311
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;
316 }
317
318 } else if( gcode->g == 90 ) {
319 this->absolute_mode = true;
320
321 } else if( gcode->g == 91 ) {
322 this->absolute_mode = false;
323 }
324 }
325
326 }
327
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)
330 {
331 if(!this->enabled) return;
332 Block *block = static_cast<Block *>(argument);
333
334
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
337
338 this->current_position += this->travel_distance ;
339
340 int steps_to_step = abs(int(floor(this->steps_per_millimeter_setting * (this->travel_distance + this->unstepped_distance) )));
341
342 if ( this->travel_distance > 0 ) {
343 this->unstepped_distance += this->travel_distance - (steps_to_step / this->steps_per_millimeter_setting); //catch any overflow
344 } else {
345 this->unstepped_distance += this->travel_distance + (steps_to_step / this->steps_per_millimeter_setting); //catch any overflow
346 }
347
348 if( steps_to_step != 0 ) {
349
350 // We take the block, we have to release it or everything gets stuck
351 block->take();
352 this->current_block = block;
353
354 this->stepper_motor->set_steps_per_second(0);
355 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step);
356
357 } else {
358 this->current_block = NULL;
359 }
360
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;
364
365 this->current_position += this->travel_distance;
366
367 int steps_to_step = abs(int(floor(this->steps_per_millimeter * (this->travel_distance + this->unstepped_distance) )));
368
369 if ( this->travel_distance > 0 ) {
370 this->unstepped_distance += this->travel_distance - (steps_to_step / this->steps_per_millimeter); //catch any overflow
371 } else {
372 this->unstepped_distance += this->travel_distance + (steps_to_step / this->steps_per_millimeter); //catch any overflow
373 }
374
375 if( steps_to_step != 0 ) {
376 block->take();
377 this->current_block = block;
378
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
381 } else {
382 this->current_block = NULL;
383 }
384
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);
389
390 }
391
392 }
393
394 // When a block ends, pause the stepping interrupt
395 void Extruder::on_block_end(void *argument)
396 {
397 if(!this->enabled) return;
398 this->current_block = NULL;
399 }
400
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)
403 {
404 if(!this->enabled) return 0;
405
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 ) {
408 return 0;
409 }
410
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
413
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 );
417 }
418 if( current_rate > target_rate ) {
419 current_rate = target_rate;
420 }
421
422 // steps per second
423 this->stepper_motor->set_speed(max(current_rate, THEKERNEL->stepper->get_minimum_steps_per_second()));
424
425 return 0;
426 }
427
428 // Speed has been updated for the robot's stepper, we must update accordingly
429 void Extruder::on_speed_change( void *argument )
430 {
431 if(!this->enabled) return;
432
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 ) {
435 return;
436 }
437
438 /*
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 )
445 */
446
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() ) );
448
449 }
450
451 // When the stepper has finished it's move
452 uint32_t Extruder::stepper_motor_finished_move(uint32_t dummy)
453 {
454 if(!this->enabled) return 0;
455
456 //printf("extruder releasing\r\n");
457
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;
461 block->release();
462 }
463 return 0;
464
465 }
466
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);
470 } else {
471 this->steps_per_millimeter = this->steps_per_millimeter_setting;
472 }
473 }
474