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