white space fixes only
[clinton/Smoothieware.git] / src / modules / tools / extruder / Extruder.cpp
CommitLineData
ca037905 1/*
58baeec1
MM
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/>.
cd011f58
AW
6*/
7
5673fe39
MM
8#include "Extruder.h"
9
4cff3ded
AW
10#include "libs/Module.h"
11#include "libs/Kernel.h"
5673fe39 12
3fceb8eb 13#include "modules/robot/Conveyor.h"
4cff3ded 14#include "modules/robot/Block.h"
5673fe39 15#include "StepperMotor.h"
61134a65
JM
16#include "SlowTicker.h"
17#include "Stepper.h"
18#include "StepTicker.h"
19#include "Config.h"
20#include "StepperMotor.h"
21#include "Robot.h"
7af0714f 22#include "checksumm.h"
8d54c34c 23#include "ConfigValue.h"
66383b80 24#include "Gcode.h"
fc7b9a7b 25#include "libs/StreamOutput.h"
5673fe39 26
5dcb2ff3 27#include <mri.h>
4cff3ded 28
43424972
JM
29#define extruder_module_enable_checksum CHECKSUM("extruder_module_enable")
30#define extruder_steps_per_mm_checksum CHECKSUM("extruder_steps_per_mm")
31#define extruder_acceleration_checksum CHECKSUM("extruder_acceleration")
32#define extruder_step_pin_checksum CHECKSUM("extruder_step_pin")
33#define extruder_dir_pin_checksum CHECKSUM("extruder_dir_pin")
34#define extruder_en_pin_checksum CHECKSUM("extruder_en_pin")
35#define extruder_max_speed_checksum CHECKSUM("extruder_max_speed")
36
37#define extruder_checksum CHECKSUM("extruder")
38
39#define default_feed_rate_checksum CHECKSUM("default_feed_rate")
40#define steps_per_mm_checksum CHECKSUM("steps_per_mm")
41#define acceleration_checksum CHECKSUM("acceleration")
42#define step_pin_checksum CHECKSUM("step_pin")
43#define dir_pin_checksum CHECKSUM("dir_pin")
44#define en_pin_checksum CHECKSUM("en_pin")
45#define max_speed_checksum CHECKSUM("max_speed")
8adf2390
L
46#define x_offset_checksum CHECKSUM("x_offset")
47#define y_offset_checksum CHECKSUM("y_offset")
48#define z_offset_checksum CHECKSUM("z_offset")
49
50#define X_AXIS 0
51#define Y_AXIS 1
52#define Z_AXIS 2
43424972 53
17c89e4d
JM
54#define OFF 0
55#define SOLO 1
56#define FOLLOW 2
57
41fd89e0
JM
58#define max(a,b) (((a) > (b)) ? (a) : (b))
59
58baeec1
MM
60/* The extruder module controls a filament extruder for 3D printing: http://en.wikipedia.org/wiki/Fused_deposition_modeling
61* 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 )
62* or the head moves, and the extruder moves plastic at a speed proportional to the movement of the head ( FOLLOW mode here ).
4464301d
AW
63*/
64
17c89e4d
JM
65Extruder::Extruder( uint16_t config_identifier, bool single )
66{
436a2cd1 67 this->absolute_mode = true;
540c8365 68 this->enabled = false;
14ecdbd7 69 this->paused = false;
17c89e4d 70 this->single_config = single;
14ecdbd7 71 this->identifier = config_identifier;
8adf2390 72
17c89e4d 73 memset(this->offset, sizeof(this->offset), 0);
436a2cd1 74}
4cff3ded 75
17c89e4d
JM
76void Extruder::on_module_loaded()
77{
f5598f5b 78
4cff3ded 79 // Settings
da24d6ae 80 this->on_config_reload(this);
4cff3ded
AW
81
82 // We work on the same Block as Stepper, so we need to know when it gets a new one and drops one
476dcb96 83 register_for_event(ON_CONFIG_RELOAD);
4cff3ded
AW
84 this->register_for_event(ON_BLOCK_BEGIN);
85 this->register_for_event(ON_BLOCK_END);
6989211c 86 this->register_for_event(ON_GCODE_RECEIVED);
436a2cd1 87 this->register_for_event(ON_GCODE_EXECUTE);
81b547a1
AW
88 this->register_for_event(ON_PLAY);
89 this->register_for_event(ON_PAUSE);
be8332cd 90 this->register_for_event(ON_SPEED_CHANGE);
ca037905 91
ded56b35 92 // Start values
4cff3ded
AW
93 this->target_position = 0;
94 this->current_position = 0;
150bce10 95 this->unstepped_distance = 0;
4cff3ded 96 this->current_block = NULL;
ded56b35 97 this->mode = OFF;
ca037905 98
ded56b35 99 // Update speed every *acceleration_ticks_per_second*
7b49793d 100 // TODO: Make this an independent setting
314ab8f7 101 THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->acceleration_ticks_per_second , this, &Extruder::acceleration_tick );
ded56b35 102
be8332cd 103 // Stepper motor object for the extruder
9c5fa39a 104 this->stepper_motor = THEKERNEL->step_ticker->add_stepper_motor( new StepperMotor(step_pin, dir_pin, en_pin) );
58baeec1 105 this->stepper_motor->attach(this, &Extruder::stepper_motor_finished_move );
3b1e82d2 106
4cff3ded
AW
107}
108
2bb8b390 109// Get config
17c89e4d
JM
110void Extruder::on_config_reload(void *argument)
111{
58baeec1 112
17c89e4d
JM
113 if( this->single_config ) {
114 // If this module uses the old "single extruder" configuration style
14ecdbd7 115
314ab8f7
MM
116 this->steps_per_millimeter = THEKERNEL->config->value(extruder_steps_per_mm_checksum )->by_default(1)->as_number();
117 this->acceleration = THEKERNEL->config->value(extruder_acceleration_checksum )->by_default(1000)->as_number();
118 this->max_speed = THEKERNEL->config->value(extruder_max_speed_checksum )->by_default(1000)->as_number();
119 this->feed_rate = THEKERNEL->config->value(default_feed_rate_checksum )->by_default(1000)->as_number();
14ecdbd7 120
314ab8f7
MM
121 this->step_pin.from_string( THEKERNEL->config->value(extruder_step_pin_checksum )->by_default("nc" )->as_string())->as_output();
122 this->dir_pin.from_string( THEKERNEL->config->value(extruder_dir_pin_checksum )->by_default("nc" )->as_string())->as_output();
123 this->en_pin.from_string( THEKERNEL->config->value(extruder_en_pin_checksum )->by_default("nc" )->as_string())->as_output();
14ecdbd7 124
17c89e4d 125 for(int i = 0; i < 3; i++) {
8adf2390
L
126 this->offset[i] = 0;
127 }
128
540c8365
L
129 this->enabled = true;
130
17c89e4d
JM
131 } else {
132 // If this module was created with the new multi extruder configuration style
14ecdbd7 133
17c89e4d
JM
134 this->steps_per_millimeter = THEKERNEL->config->value(extruder_checksum, this->identifier, steps_per_mm_checksum )->by_default(1)->as_number();
135 this->acceleration = THEKERNEL->config->value(extruder_checksum, this->identifier, acceleration_checksum )->by_default(1000)->as_number();
136 this->max_speed = THEKERNEL->config->value(extruder_checksum, this->identifier, max_speed_checksum )->by_default(1000)->as_number();
137 this->feed_rate = THEKERNEL->config->value( default_feed_rate_checksum )->by_default(1000)->as_number();
14ecdbd7 138
17c89e4d
JM
139 this->step_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, step_pin_checksum )->by_default("nc" )->as_string())->as_output();
140 this->dir_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, dir_pin_checksum )->by_default("nc" )->as_string())->as_output();
141 this->en_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, en_pin_checksum )->by_default("nc" )->as_string())->as_output();
8adf2390 142
17c89e4d
JM
143 this->offset[X_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, x_offset_checksum )->by_default(0)->as_number();
144 this->offset[Y_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, y_offset_checksum )->by_default(0)->as_number();
145 this->offset[Z_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, z_offset_checksum )->by_default(0)->as_number();
14ecdbd7
AW
146 }
147
436a2cd1
AW
148}
149
81b547a1
AW
150
151// When the play/pause button is set to pause, or a module calls the ON_PAUSE event
17c89e4d
JM
152void Extruder::on_pause(void *argument)
153{
81b547a1 154 this->paused = true;
83ecfc46 155 this->stepper_motor->pause();
81b547a1
AW
156}
157
158// When the play/pause button is set to play, or a module calls the ON_PLAY event
17c89e4d
JM
159void Extruder::on_play(void *argument)
160{
81b547a1 161 this->paused = false;
83ecfc46 162 this->stepper_motor->unpause();
81b547a1
AW
163}
164
5dcb2ff3 165
17c89e4d
JM
166void Extruder::on_gcode_received(void *argument)
167{
168 Gcode *gcode = static_cast<Gcode *>(argument);
5dcb2ff3
AW
169
170 // Gcodes to execute immediately
17c89e4d
JM
171 if (gcode->has_m) {
172 if (gcode->m == 114 && this->enabled) {
4d9f562f 173 char buf[16];
17c89e4d 174 int n = snprintf(buf, sizeof(buf), " E:%1.3f ", this->current_position);
4d9f562f 175 gcode->txt_after_ok.append(buf, n);
74b6303c 176 gcode->mark_as_taken();
33e4cc02 177
17c89e4d 178 } else if (gcode->m == 92 && ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier) ) ) {
1ad23cd3 179 float spm = this->steps_per_millimeter;
7369629d
MM
180 if (gcode->has_letter('E'))
181 spm = gcode->get_value('E');
182 gcode->stream->printf("E:%g ", spm);
183 gcode->add_nl = true;
74b6303c 184 gcode->mark_as_taken();
33e4cc02 185
17c89e4d 186 } else if (gcode->m == 500 || gcode->m == 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings
4710532a 187 if( this->single_config ) {
17c89e4d 188 gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f\n", this->steps_per_millimeter);
4710532a 189 } else {
17c89e4d
JM
190 gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f P%d\n", this->steps_per_millimeter, this->identifier);
191 }
33e4cc02 192 gcode->mark_as_taken();
f2f0dfed 193 return;
7369629d 194 }
6989211c 195 }
8519d744 196
5dcb2ff3 197 // Gcodes to pass along to on_gcode_execute
17c89e4d 198 if( ( gcode->has_m && (gcode->m == 17 || gcode->m == 18 || gcode->m == 82 || gcode->m == 83 || gcode->m == 84 || gcode->m == 92 ) ) || ( gcode->has_g && gcode->g == 92 && gcode->has_letter('E') ) || ( gcode->has_g && ( gcode->g == 90 || gcode->g == 91 ) ) ) {
e0ee24ed 199 THEKERNEL->conveyor->append_gcode(gcode);
3c4f2dd8
AW
200 }
201
202 // Add to the queue for on_gcode_execute to process
17c89e4d
JM
203 if( gcode->has_g && gcode->g < 4 && gcode->has_letter('E') && this->enabled ) {
204 if( !gcode->has_letter('X') && !gcode->has_letter('Y') && !gcode->has_letter('Z') ) {
e0ee24ed 205 THEKERNEL->conveyor->append_gcode(gcode);
518e225f 206 // This is a solo move, we add an empty block to the queue to prevent subsequent gcodes being executed at the same time
2134bcf2 207 THEKERNEL->conveyor->queue_head_block();
3c4f2dd8 208 }
17c89e4d 209 } else {
d149c730
AW
210 // This is for follow move
211
3c4f2dd8
AW
212 }
213}
214
ded56b35 215// Compute extrusion speed based on parameters and gcode distance of travel
17c89e4d
JM
216void Extruder::on_gcode_execute(void *argument)
217{
218 Gcode *gcode = static_cast<Gcode *>(argument);
ca037905 219
436a2cd1 220 // Absolute/relative mode
17c89e4d
JM
221 if( gcode->has_m ) {
222 if( gcode->m == 17 ) {
223 this->en_pin.set(0);
224 }
225 if( gcode->m == 18 ) {
226 this->en_pin.set(1);
227 }
228 if( gcode->m == 82 ) {
229 this->absolute_mode = true;
230 }
231 if( gcode->m == 83 ) {
232 this->absolute_mode = false;
233 }
234 if( gcode->m == 84 ) {
235 this->en_pin.set(1);
236 }
237 if (gcode->m == 92 ) {
238 if (gcode->has_letter('E')) {
0fb5b438 239 this->steps_per_millimeter = gcode->get_value('E');
0fb5b438 240 }
0fb5b438 241 }
ca037905
MM
242 }
243
244 // The mode is OFF by default, and SOLO or FOLLOW only if we need to extrude
ded56b35 245 this->mode = OFF;
ca037905 246
17c89e4d 247 if( gcode->has_g ) {
7b49793d 248 // G92: Reset extruder position
17c89e4d 249 if( gcode->g == 92 && this->enabled ) {
74b6303c 250 gcode->mark_as_taken();
17c89e4d 251 if( gcode->has_letter('E') ) {
e2b4a32b 252 this->current_position = gcode->get_value('E');
1a2d88eb 253 this->target_position = this->current_position;
150bce10 254 this->unstepped_distance = 0;
17c89e4d 255 } else if( gcode->get_num_args() == 0) {
b2aa3a55
L
256 this->current_position = 0.0;
257 this->target_position = this->current_position;
150bce10 258 this->unstepped_distance = 0;
ca037905 259 }
17c89e4d 260 } else if (((gcode->g == 0) || (gcode->g == 1)) && this->enabled) {
ca037905 261 // Extrusion length from 'G' Gcode
17c89e4d 262 if( gcode->has_letter('E' )) {
ca037905 263 // Get relative extrusion distance depending on mode ( in absolute mode we must substract target_position )
1ad23cd3
MM
264 float extrusion_distance = gcode->get_value('E');
265 float relative_extrusion_distance = extrusion_distance;
17c89e4d 266 if (this->absolute_mode) {
f8dc0043
MM
267 relative_extrusion_distance -= this->target_position;
268 this->target_position = extrusion_distance;
17c89e4d 269 } else {
f8dc0043
MM
270 this->target_position += relative_extrusion_distance;
271 }
ca037905 272
ded56b35 273 // If the robot is moving, we follow it's movement, otherwise, we move alone
17c89e4d 274 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
ded56b35
AW
275 this->mode = SOLO;
276 this->travel_distance = relative_extrusion_distance;
17c89e4d 277 } else {
ca037905 278 // We move proportionally to the robot's movement
4464301d 279 this->mode = FOLLOW;
ca037905 280 this->travel_ratio = relative_extrusion_distance / gcode->millimeters_of_travel;
7dab41f3 281 // TODO: check resulting flowrate, limit robot speed if it exceeds max_speed
ca037905
MM
282 }
283
62bd4cfa 284 this->en_pin.set(0);
ca037905 285 }
17c89e4d
JM
286 } else if( gcode->g == 90 ) {
287 this->absolute_mode = true;
288 } else if( gcode->g == 91 ) {
289 this->absolute_mode = false;
1a2d88eb 290 }
ca037905 291 }
8adf2390 292
17c89e4d 293 if (gcode->has_letter('F') && this->enabled) {
8adf2390
L
294 feed_rate = gcode->get_value('F') / THEKERNEL->robot->seconds_per_minute;
295 if (feed_rate > max_speed)
296 feed_rate = max_speed;
297 }
da24d6ae
AW
298}
299
ded56b35 300// When a new block begins, either follow the robot, or step by ourselves ( or stay back and do nothing )
17c89e4d
JM
301void Extruder::on_block_begin(void *argument)
302{
c974b296 303 if(!this->enabled) return;
17c89e4d 304 Block *block = static_cast<Block *>(argument);
4464301d
AW
305
306
17c89e4d 307 if( this->mode == SOLO ) {
ded56b35 308 // In solo mode we take the block so we can move even if the stepper has nothing to do
58baeec1 309
99826186 310 this->current_position += this->travel_distance ;
58baeec1 311
17c89e4d 312 int steps_to_step = abs(int(floor(this->steps_per_millimeter * (this->travel_distance + this->unstepped_distance) )));
d0c14c30 313
17c89e4d
JM
314 if ( this->travel_distance > 0 ) {
315 this->unstepped_distance += this->travel_distance - (steps_to_step / this->steps_per_millimeter); //catch any overflow
150bce10 316 } else {
17c89e4d 317 this->unstepped_distance += this->travel_distance + (steps_to_step / this->steps_per_millimeter); //catch any overflow
150bce10 318 }
4464301d 319
17c89e4d 320 if( steps_to_step != 0 ) {
58baeec1 321
4464301d
AW
322 // We take the block, we have to release it or everything gets stuck
323 block->take();
324 this->current_block = block;
58baeec1 325
ca7f724e 326 this->stepper_motor->steps_per_second = 0;
58baeec1 327 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step);
4464301d 328
17c89e4d 329 } else {
d149c730 330 this->current_block = NULL;
4464301d
AW
331 }
332
17c89e4d 333 } else if( this->mode == FOLLOW ) {
1a2d88eb 334 // In non-solo mode, we just follow the stepper module
b5a9a6c4 335 this->travel_distance = block->millimeters * this->travel_ratio;
99826186
MM
336
337 this->current_position += this->travel_distance;
338
150bce10
MM
339 int steps_to_step = abs(int(floor(this->steps_per_millimeter * (this->travel_distance + this->unstepped_distance) )));
340
17c89e4d
JM
341 if ( this->travel_distance > 0 ) {
342 this->unstepped_distance += this->travel_distance - (steps_to_step / this->steps_per_millimeter); //catch any overflow
150bce10 343 } else {
17c89e4d 344 this->unstepped_distance += this->travel_distance + (steps_to_step / this->steps_per_millimeter); //catch any overflow
150bce10 345 }
99826186 346
17c89e4d 347 if( steps_to_step != 0 ) {
4464301d 348 block->take();
b5a9a6c4
MM
349 this->current_block = block;
350
351 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step );
d0c14c30 352 this->on_speed_change(0); // initialise speed in case we get called first
17c89e4d 353 } else {
d149c730 354 this->current_block = NULL;
4464301d 355 }
be8332cd 356
17c89e4d 357 } else if( this->mode == OFF ) {
be8332cd 358 // No movement means we must reset our speed
d149c730 359 this->current_block = NULL;
4464301d 360 //this->stepper_motor->set_speed(0);
58baeec1 361
ca037905 362 }
e2b4a32b 363
4cff3ded
AW
364}
365
ded56b35 366// When a block ends, pause the stepping interrupt
17c89e4d
JM
367void Extruder::on_block_end(void *argument)
368{
c974b296 369 if(!this->enabled) return;
ca037905
MM
370 this->current_block = NULL;
371}
4cff3ded 372
ded56b35 373// Called periodically to change the speed to match acceleration or to match the speed of the robot
17c89e4d
JM
374uint32_t Extruder::acceleration_tick(uint32_t dummy)
375{
c974b296 376 if(!this->enabled) return 0;
1a2d88eb 377
ca037905 378 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
17c89e4d
JM
379 if( this->current_block == NULL || this->paused || this->mode != SOLO ) {
380 return 0;
381 }
be8332cd
AW
382
383 uint32_t current_rate = this->stepper_motor->steps_per_second;
99826186 384 uint32_t target_rate = int(floor(this->feed_rate * this->steps_per_millimeter));
58baeec1 385
17c89e4d
JM
386 if( current_rate < target_rate ) {
387 uint32_t rate_increase = int(floor((this->acceleration / THEKERNEL->stepper->acceleration_ticks_per_second) * this->steps_per_millimeter));
4464301d 388 current_rate = min( target_rate, current_rate + rate_increase );
0eb11a06 389 }
17c89e4d
JM
390 if( current_rate > target_rate ) {
391 current_rate = target_rate;
392 }
1a2d88eb 393
8519d744 394 // steps per second
da947c62 395 this->stepper_motor->set_speed(max(current_rate, THEKERNEL->stepper->minimum_steps_per_second));
58baeec1 396
be8332cd 397 return 0;
ded56b35 398}
1a2d88eb 399
be8332cd 400// Speed has been updated for the robot's stepper, we must update accordingly
17c89e4d
JM
401void Extruder::on_speed_change( void *argument )
402{
c974b296 403 if(!this->enabled) return;
ca037905 404
be8332cd 405 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
17c89e4d
JM
406 if( this->current_block == NULL || this->paused || this->mode != FOLLOW || this->stepper_motor->moving != true ) {
407 return;
408 }
4464301d
AW
409
410 /*
da947c62 411 * nominal block duration = current block's steps / ( current block's nominal rate )
58baeec1 412 * nominal extruder rate = extruder steps / nominal block duration
0ac1713f 413 * actual extruder rate = nominal extruder rate * ( ( stepper's steps per second ) / ( current block's nominal rate ) )
da947c62
MM
414 * 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 ) )
415 * or simplified : extruder steps * ( stepper's steps per second ) ) / current block's steps
416 * or even : ( stepper steps per second ) * ( extruder steps / current block's steps )
4464301d 417 */
58baeec1 418
da947c62 419 this->stepper_motor->set_speed( max( ( THEKERNEL->stepper->trapezoid_adjusted_rate) * ( (float)this->stepper_motor->steps_to_move / (float)this->current_block->steps_event_count ), THEKERNEL->stepper->minimum_steps_per_second ) );
ca037905 420
4cff3ded
AW
421}
422
be8332cd 423// When the stepper has finished it's move
17c89e4d
JM
424uint32_t Extruder::stepper_motor_finished_move(uint32_t dummy)
425{
c974b296 426 if(!this->enabled) return 0;
4cff3ded 427
4464301d
AW
428 //printf("extruder releasing\r\n");
429
17c89e4d
JM
430 if (this->current_block) { // this should always be true, but sometimes it isn't. TODO: find out why
431 Block *block = this->current_block;
d149c730
AW
432 this->current_block = NULL;
433 block->release();
8519d744 434 }
f2203544 435 return 0;
feb204be 436
be8332cd 437}
feb204be 438