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