Fix M92 Ennn to set the steps/mm for current tool
[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;
4cba6755 180 if (gcode->has_letter('E')){
7369629d 181 spm = gcode->get_value('E');
4cba6755
JM
182 this->steps_per_millimeter = spm;
183 }
184
7369629d
MM
185 gcode->stream->printf("E:%g ", spm);
186 gcode->add_nl = true;
74b6303c 187 gcode->mark_as_taken();
33e4cc02 188
17c89e4d 189 } else if (gcode->m == 500 || gcode->m == 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings
4710532a 190 if( this->single_config ) {
17c89e4d 191 gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f\n", this->steps_per_millimeter);
4710532a 192 } else {
17c89e4d
JM
193 gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f P%d\n", this->steps_per_millimeter, this->identifier);
194 }
33e4cc02 195 gcode->mark_as_taken();
f2f0dfed 196 return;
7369629d 197 }
6989211c 198 }
8519d744 199
5dcb2ff3 200 // Gcodes to pass along to on_gcode_execute
4cba6755 201 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 ) ) ) {
e0ee24ed 202 THEKERNEL->conveyor->append_gcode(gcode);
3c4f2dd8
AW
203 }
204
205 // Add to the queue for on_gcode_execute to process
17c89e4d
JM
206 if( gcode->has_g && gcode->g < 4 && gcode->has_letter('E') && this->enabled ) {
207 if( !gcode->has_letter('X') && !gcode->has_letter('Y') && !gcode->has_letter('Z') ) {
e0ee24ed 208 THEKERNEL->conveyor->append_gcode(gcode);
518e225f 209 // This is a solo move, we add an empty block to the queue to prevent subsequent gcodes being executed at the same time
2134bcf2 210 THEKERNEL->conveyor->queue_head_block();
3c4f2dd8 211 }
17c89e4d 212 } else {
d149c730
AW
213 // This is for follow move
214
3c4f2dd8
AW
215 }
216}
217
ded56b35 218// Compute extrusion speed based on parameters and gcode distance of travel
17c89e4d
JM
219void Extruder::on_gcode_execute(void *argument)
220{
221 Gcode *gcode = static_cast<Gcode *>(argument);
ca037905 222
436a2cd1 223 // Absolute/relative mode
17c89e4d
JM
224 if( gcode->has_m ) {
225 if( gcode->m == 17 ) {
226 this->en_pin.set(0);
227 }
228 if( gcode->m == 18 ) {
229 this->en_pin.set(1);
230 }
231 if( gcode->m == 82 ) {
232 this->absolute_mode = true;
233 }
234 if( gcode->m == 83 ) {
235 this->absolute_mode = false;
236 }
237 if( gcode->m == 84 ) {
238 this->en_pin.set(1);
239 }
ca037905
MM
240 }
241
242 // The mode is OFF by default, and SOLO or FOLLOW only if we need to extrude
ded56b35 243 this->mode = OFF;
ca037905 244
17c89e4d 245 if( gcode->has_g ) {
7b49793d 246 // G92: Reset extruder position
17c89e4d 247 if( gcode->g == 92 && this->enabled ) {
74b6303c 248 gcode->mark_as_taken();
17c89e4d 249 if( gcode->has_letter('E') ) {
e2b4a32b 250 this->current_position = gcode->get_value('E');
1a2d88eb 251 this->target_position = this->current_position;
150bce10 252 this->unstepped_distance = 0;
17c89e4d 253 } else if( gcode->get_num_args() == 0) {
b2aa3a55
L
254 this->current_position = 0.0;
255 this->target_position = this->current_position;
150bce10 256 this->unstepped_distance = 0;
ca037905 257 }
17c89e4d 258 } else if (((gcode->g == 0) || (gcode->g == 1)) && this->enabled) {
ca037905 259 // Extrusion length from 'G' Gcode
17c89e4d 260 if( gcode->has_letter('E' )) {
ca037905 261 // Get relative extrusion distance depending on mode ( in absolute mode we must substract target_position )
1ad23cd3
MM
262 float extrusion_distance = gcode->get_value('E');
263 float relative_extrusion_distance = extrusion_distance;
17c89e4d 264 if (this->absolute_mode) {
f8dc0043
MM
265 relative_extrusion_distance -= this->target_position;
266 this->target_position = extrusion_distance;
17c89e4d 267 } else {
f8dc0043
MM
268 this->target_position += relative_extrusion_distance;
269 }
ca037905 270
ded56b35 271 // If the robot is moving, we follow it's movement, otherwise, we move alone
17c89e4d 272 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
273 this->mode = SOLO;
274 this->travel_distance = relative_extrusion_distance;
17c89e4d 275 } else {
ca037905 276 // We move proportionally to the robot's movement
4464301d 277 this->mode = FOLLOW;
ca037905 278 this->travel_ratio = relative_extrusion_distance / gcode->millimeters_of_travel;
7dab41f3 279 // TODO: check resulting flowrate, limit robot speed if it exceeds max_speed
ca037905
MM
280 }
281
62bd4cfa 282 this->en_pin.set(0);
ca037905 283 }
17c89e4d
JM
284 } else if( gcode->g == 90 ) {
285 this->absolute_mode = true;
286 } else if( gcode->g == 91 ) {
287 this->absolute_mode = false;
1a2d88eb 288 }
ca037905 289 }
8adf2390 290
17c89e4d 291 if (gcode->has_letter('F') && this->enabled) {
8adf2390
L
292 feed_rate = gcode->get_value('F') / THEKERNEL->robot->seconds_per_minute;
293 if (feed_rate > max_speed)
294 feed_rate = max_speed;
295 }
da24d6ae
AW
296}
297
ded56b35 298// When a new block begins, either follow the robot, or step by ourselves ( or stay back and do nothing )
17c89e4d
JM
299void Extruder::on_block_begin(void *argument)
300{
c974b296 301 if(!this->enabled) return;
17c89e4d 302 Block *block = static_cast<Block *>(argument);
4464301d
AW
303
304
17c89e4d 305 if( this->mode == SOLO ) {
ded56b35 306 // In solo mode we take the block so we can move even if the stepper has nothing to do
58baeec1 307
99826186 308 this->current_position += this->travel_distance ;
58baeec1 309
17c89e4d 310 int steps_to_step = abs(int(floor(this->steps_per_millimeter * (this->travel_distance + this->unstepped_distance) )));
d0c14c30 311
17c89e4d
JM
312 if ( this->travel_distance > 0 ) {
313 this->unstepped_distance += this->travel_distance - (steps_to_step / this->steps_per_millimeter); //catch any overflow
150bce10 314 } else {
17c89e4d 315 this->unstepped_distance += this->travel_distance + (steps_to_step / this->steps_per_millimeter); //catch any overflow
150bce10 316 }
4464301d 317
17c89e4d 318 if( steps_to_step != 0 ) {
58baeec1 319
4464301d
AW
320 // We take the block, we have to release it or everything gets stuck
321 block->take();
322 this->current_block = block;
58baeec1 323
ca7f724e 324 this->stepper_motor->steps_per_second = 0;
58baeec1 325 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step);
4464301d 326
17c89e4d 327 } else {
d149c730 328 this->current_block = NULL;
4464301d
AW
329 }
330
17c89e4d 331 } else if( this->mode == FOLLOW ) {
1a2d88eb 332 // In non-solo mode, we just follow the stepper module
b5a9a6c4 333 this->travel_distance = block->millimeters * this->travel_ratio;
99826186
MM
334
335 this->current_position += this->travel_distance;
336
150bce10
MM
337 int steps_to_step = abs(int(floor(this->steps_per_millimeter * (this->travel_distance + this->unstepped_distance) )));
338
17c89e4d
JM
339 if ( this->travel_distance > 0 ) {
340 this->unstepped_distance += this->travel_distance - (steps_to_step / this->steps_per_millimeter); //catch any overflow
150bce10 341 } else {
17c89e4d 342 this->unstepped_distance += this->travel_distance + (steps_to_step / this->steps_per_millimeter); //catch any overflow
150bce10 343 }
99826186 344
17c89e4d 345 if( steps_to_step != 0 ) {
4464301d 346 block->take();
b5a9a6c4
MM
347 this->current_block = block;
348
349 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step );
d0c14c30 350 this->on_speed_change(0); // initialise speed in case we get called first
17c89e4d 351 } else {
d149c730 352 this->current_block = NULL;
4464301d 353 }
be8332cd 354
17c89e4d 355 } else if( this->mode == OFF ) {
be8332cd 356 // No movement means we must reset our speed
d149c730 357 this->current_block = NULL;
4464301d 358 //this->stepper_motor->set_speed(0);
58baeec1 359
ca037905 360 }
e2b4a32b 361
4cff3ded
AW
362}
363
ded56b35 364// When a block ends, pause the stepping interrupt
17c89e4d
JM
365void Extruder::on_block_end(void *argument)
366{
c974b296 367 if(!this->enabled) return;
ca037905
MM
368 this->current_block = NULL;
369}
4cff3ded 370
ded56b35 371// Called periodically to change the speed to match acceleration or to match the speed of the robot
17c89e4d
JM
372uint32_t Extruder::acceleration_tick(uint32_t dummy)
373{
c974b296 374 if(!this->enabled) return 0;
1a2d88eb 375
ca037905 376 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
17c89e4d
JM
377 if( this->current_block == NULL || this->paused || this->mode != SOLO ) {
378 return 0;
379 }
be8332cd
AW
380
381 uint32_t current_rate = this->stepper_motor->steps_per_second;
99826186 382 uint32_t target_rate = int(floor(this->feed_rate * this->steps_per_millimeter));
58baeec1 383
17c89e4d
JM
384 if( current_rate < target_rate ) {
385 uint32_t rate_increase = int(floor((this->acceleration / THEKERNEL->stepper->acceleration_ticks_per_second) * this->steps_per_millimeter));
4464301d 386 current_rate = min( target_rate, current_rate + rate_increase );
0eb11a06 387 }
17c89e4d
JM
388 if( current_rate > target_rate ) {
389 current_rate = target_rate;
390 }
1a2d88eb 391
8519d744 392 // steps per second
da947c62 393 this->stepper_motor->set_speed(max(current_rate, THEKERNEL->stepper->minimum_steps_per_second));
58baeec1 394
be8332cd 395 return 0;
ded56b35 396}
1a2d88eb 397
be8332cd 398// Speed has been updated for the robot's stepper, we must update accordingly
17c89e4d
JM
399void Extruder::on_speed_change( void *argument )
400{
c974b296 401 if(!this->enabled) return;
ca037905 402
be8332cd 403 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
17c89e4d
JM
404 if( this->current_block == NULL || this->paused || this->mode != FOLLOW || this->stepper_motor->moving != true ) {
405 return;
406 }
4464301d
AW
407
408 /*
da947c62 409 * nominal block duration = current block's steps / ( current block's nominal rate )
58baeec1 410 * nominal extruder rate = extruder steps / nominal block duration
0ac1713f 411 * actual extruder rate = nominal extruder rate * ( ( stepper's steps per second ) / ( current block's nominal rate ) )
da947c62
MM
412 * 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 ) )
413 * or simplified : extruder steps * ( stepper's steps per second ) ) / current block's steps
414 * or even : ( stepper steps per second ) * ( extruder steps / current block's steps )
4464301d 415 */
58baeec1 416
da947c62 417 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 418
4cff3ded
AW
419}
420
be8332cd 421// When the stepper has finished it's move
17c89e4d
JM
422uint32_t Extruder::stepper_motor_finished_move(uint32_t dummy)
423{
c974b296 424 if(!this->enabled) return 0;
4cff3ded 425
4464301d
AW
426 //printf("extruder releasing\r\n");
427
17c89e4d
JM
428 if (this->current_block) { // this should always be true, but sometimes it isn't. TODO: find out why
429 Block *block = this->current_block;
d149c730
AW
430 this->current_block = NULL;
431 block->release();
8519d744 432 }
f2203544 433 return 0;
feb204be 434
be8332cd 435}
feb204be 436