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