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