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