make extra sure that heater outputs get turned off when target is set to 0
[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"
13e4a3f9 10#include "modules/robot/Player.h"
4cff3ded
AW
11#include "modules/robot/Block.h"
12#include "modules/tools/extruder/Extruder.h"
13
58baeec1
MM
14/* The extruder module controls a filament extruder for 3D printing: http://en.wikipedia.org/wiki/Fused_deposition_modeling
15* 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 )
16* or the head moves, and the extruder moves plastic at a speed proportional to the movement of the head ( FOLLOW mode here ).
4464301d
AW
17*/
18
7dab41f3
MM
19// extruder_acceleration_checksum
20
ca037905 21Extruder::Extruder() {
436a2cd1 22 this->absolute_mode = true;
d9ebc974
AW
23 this->step_counter = 0;
24 this->counter_increment = 0;
81b547a1 25 this->paused = false;
436a2cd1 26}
4cff3ded
AW
27
28void Extruder::on_module_loaded() {
f5598f5b 29
ded56b35 30 // Do not do anything if not enabledd
ca037905 31 if( this->kernel->config->value( extruder_module_enable_checksum )->by_default(false)->as_bool() == false ){ return; }
f5598f5b 32
4cff3ded 33 // Settings
da24d6ae 34 this->on_config_reload(this);
4cff3ded 35
4464301d
AW
36 // We start with the enable pin off
37 this->en_pin->set(1);
5c07c6dc 38
4cff3ded
AW
39 // We work on the same Block as Stepper, so we need to know when it gets a new one and drops one
40 this->register_for_event(ON_BLOCK_BEGIN);
41 this->register_for_event(ON_BLOCK_END);
436a2cd1 42 this->register_for_event(ON_GCODE_EXECUTE);
81b547a1
AW
43 this->register_for_event(ON_PLAY);
44 this->register_for_event(ON_PAUSE);
be8332cd 45 this->register_for_event(ON_SPEED_CHANGE);
ca037905 46
ded56b35 47 // Start values
4cff3ded
AW
48 this->target_position = 0;
49 this->current_position = 0;
4464301d 50 this->current_steps = 0;
4cff3ded 51 this->current_block = NULL;
ded56b35 52 this->mode = OFF;
ca037905 53
ded56b35 54 // Update speed every *acceleration_ticks_per_second*
7b49793d 55 // TODO: Make this an independent setting
d9ebc974 56 this->kernel->slow_ticker->attach( this->kernel->stepper->acceleration_ticks_per_second , this, &Extruder::acceleration_tick );
ded56b35 57
be8332cd 58 // Stepper motor object for the extruder
58baeec1
MM
59 this->stepper_motor = this->kernel->step_ticker->add_stepper_motor( new StepperMotor(this->step_pin,this->dir_pin,this->en_pin) );
60 this->stepper_motor->attach(this, &Extruder::stepper_motor_finished_move );
3b1e82d2 61
4cff3ded
AW
62}
63
2bb8b390 64// Get config
da24d6ae 65void Extruder::on_config_reload(void* argument){
58baeec1
MM
66 this->microseconds_per_step_pulse = this->kernel->config->value(microseconds_per_step_pulse_checksum)->by_default(5)->as_number();
67 this->steps_per_millimeter = this->kernel->config->value(extruder_steps_per_mm_checksum )->by_default(1)->as_number();
68 this->feed_rate = this->kernel->config->value(default_feed_rate_checksum )->by_default(1000)->as_number();
69 this->acceleration = this->kernel->config->value(extruder_acceleration_checksum )->by_default(1000)->as_number();
7dab41f3 70 this->max_speed = this->kernel->config->value(extruder_max_speed_checksum )->by_default(1000)->as_number();
58baeec1
MM
71
72 this->step_pin = this->kernel->config->value(extruder_step_pin_checksum )->by_default("nc" )->as_pin()->as_output();
73 this->dir_pin = this->kernel->config->value(extruder_dir_pin_checksum )->by_default("nc" )->as_pin()->as_output();
74 this->en_pin = this->kernel->config->value(extruder_en_pin_checksum )->by_default("nc" )->as_pin()->as_output()->as_open_drain();
5a884140
MM
75
76 // disable by default
77 this->en_pin->set(1);
436a2cd1
AW
78}
79
81b547a1
AW
80
81// When the play/pause button is set to pause, or a module calls the ON_PAUSE event
82void Extruder::on_pause(void* argument){
83 this->paused = true;
83ecfc46 84 this->stepper_motor->pause();
81b547a1
AW
85}
86
87// When the play/pause button is set to play, or a module calls the ON_PLAY event
88void Extruder::on_play(void* argument){
89 this->paused = false;
83ecfc46 90 this->stepper_motor->unpause();
81b547a1
AW
91}
92
93
94
ded56b35 95// Compute extrusion speed based on parameters and gcode distance of travel
436a2cd1
AW
96void Extruder::on_gcode_execute(void* argument){
97 Gcode* gcode = static_cast<Gcode*>(argument);
ca037905 98
436a2cd1 99 // Absolute/relative mode
e6b5ae25
AW
100 if( gcode->has_m ){
101 if( gcode->m == 82 ){ this->absolute_mode = true; }
102 if( gcode->m == 83 ){ this->absolute_mode = false; }
103 if( gcode->m == 84 ){ this->en_pin->set(1); }
0fb5b438
MM
104 if (gcode->m == 92 )
105 {
106 if (gcode->has_letter('E'))
107 {
108 this->steps_per_millimeter = gcode->get_value('E');
109 this->current_steps = int(floor(this->steps_per_millimeter * this->current_position));
110 }
111 gcode->stream->printf("E:%g ", this->steps_per_millimeter);
112 gcode->add_nl = true;
113 }
ca037905
MM
114 }
115
116 // The mode is OFF by default, and SOLO or FOLLOW only if we need to extrude
ded56b35 117 this->mode = OFF;
ca037905 118
e6b5ae25 119 if( gcode->has_g ){
7b49793d 120 // G92: Reset extruder position
e6b5ae25 121 if( gcode->g == 92 ){
1a2d88eb 122 if( gcode->has_letter('E') ){
e2b4a32b 123 this->current_position = gcode->get_value('E');
1a2d88eb 124 this->target_position = this->current_position;
4464301d 125 this->current_steps = int(floor(this->steps_per_millimeter * this->current_position));
b2aa3a55
L
126 }else if( gcode->get_num_args() == 0){
127 this->current_position = 0.0;
128 this->target_position = this->current_position;
129 this->current_steps = 0;
ca037905 130 }
7dab41f3 131 }else if ((gcode->g == 0) || (gcode->g == 1)){
ca037905 132 // Extrusion length from 'G' Gcode
1a2d88eb 133 if( gcode->has_letter('E' )){
ca037905 134 // Get relative extrusion distance depending on mode ( in absolute mode we must substract target_position )
ded56b35
AW
135 double relative_extrusion_distance = gcode->get_value('E');
136 if( this->absolute_mode == true ){ relative_extrusion_distance = relative_extrusion_distance - this->target_position; }
ca037905 137
ded56b35 138 // If the robot is moving, we follow it's movement, otherwise, we move alone
7b49793d 139 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
140 this->mode = SOLO;
141 this->travel_distance = relative_extrusion_distance;
142 if( gcode->has_letter('F') ){ this->feed_rate = gcode->get_value('F'); }
7dab41f3
MM
143 if (this->feed_rate > (this->max_speed * 60))
144 this->feed_rate = this->max_speed * 60;
1a2d88eb 145 }else{
ca037905 146 // We move proportionally to the robot's movement
4464301d 147 this->mode = FOLLOW;
ca037905 148 this->travel_ratio = relative_extrusion_distance / gcode->millimeters_of_travel;
7dab41f3 149 // TODO: check resulting flowrate, limit robot speed if it exceeds max_speed
ca037905
MM
150 }
151
5c07c6dc 152 this->en_pin->set(0);
ca037905 153 }
1a2d88eb 154 }
ca037905 155 }
da24d6ae
AW
156}
157
ded56b35 158// When a new block begins, either follow the robot, or step by ourselves ( or stay back and do nothing )
4cff3ded
AW
159void Extruder::on_block_begin(void* argument){
160 Block* block = static_cast<Block*>(argument);
4464301d
AW
161
162
ded56b35
AW
163 if( this->mode == SOLO ){
164 // In solo mode we take the block so we can move even if the stepper has nothing to do
58baeec1 165
be8332cd 166 this->target_position = this->current_position + this->travel_distance ;
58baeec1 167
4464301d
AW
168 //int32_t steps_to_step = abs( int( floor(this->steps_per_millimeter*this->target_position) - floor(this->steps_per_millimeter*this->current_position) ) );
169
170 int old_steps = this->current_steps;
171 int target_steps = int( floor(this->steps_per_millimeter*this->target_position) );
172 int steps_to_step = abs( target_steps - old_steps );
173 this->current_steps = target_steps;
174
175 if( steps_to_step != 0 ){
58baeec1 176
4464301d
AW
177 // We take the block, we have to release it or everything gets stuck
178 block->take();
179 this->current_block = block;
58baeec1
MM
180
181 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step);
4464301d
AW
182
183 }
184
be8332cd 185
ded56b35 186 }else if( this->mode == FOLLOW ){
1a2d88eb 187 // In non-solo mode, we just follow the stepper module
58baeec1 188
ca037905 189 this->current_block = block;
be8332cd 190 this->target_position = this->current_position + ( this->current_block->millimeters * this->travel_ratio );
4464301d
AW
191
192 //int32_t steps_to_step = abs( int( floor(this->steps_per_millimeter*this->target_position) - floor(this->steps_per_millimeter*this->current_position) ) );
193
194 int old_steps = this->current_steps;
195 int target_steps = int( floor(this->steps_per_millimeter*this->target_position) );
83ecfc46 196 int steps_to_step = target_steps - old_steps ;
4464301d 197 this->current_steps = target_steps;
58baeec1
MM
198
199
200 if( steps_to_step != 0 ){
4464301d
AW
201
202 //printf("taken for extruder: %u \r\n", steps_to_step);
58baeec1 203
4464301d 204 block->take();
58baeec1 205
4464301d
AW
206 //printf("spm:%f td:%f steps:%d ( %f - %f ) \r\n", this->steps_per_millimeter, this->travel_distance, steps_to_step, this->target_position, this->current_position );
207
58baeec1 208 this->stepper_motor->move( ( steps_to_step > 0 ), abs(steps_to_step) );
4464301d
AW
209
210
211
212 }
be8332cd
AW
213
214 }else if( this->mode == OFF ){
215 // No movement means we must reset our speed
58baeec1 216
4464301d 217 //this->stepper_motor->set_speed(0);
58baeec1 218
ca037905 219 }
e2b4a32b 220
4cff3ded
AW
221}
222
ded56b35 223// When a block ends, pause the stepping interrupt
4cff3ded 224void Extruder::on_block_end(void* argument){
ca037905
MM
225 this->current_block = NULL;
226}
4cff3ded 227
ded56b35 228// Called periodically to change the speed to match acceleration or to match the speed of the robot
8b8b3339 229uint32_t Extruder::acceleration_tick(uint32_t dummy){
1a2d88eb 230
ca037905 231 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
be8332cd
AW
232 if( this->current_block == NULL || this->paused || this->mode != SOLO ){ return 0; }
233
234 uint32_t current_rate = this->stepper_motor->steps_per_second;
235 uint32_t target_rate = int(floor((this->feed_rate/60)*this->steps_per_millimeter));
58baeec1 236
4464301d
AW
237 if( current_rate < target_rate ){
238 uint32_t rate_increase = int(floor((this->acceleration/this->kernel->stepper->acceleration_ticks_per_second)*this->steps_per_millimeter));
239 current_rate = min( target_rate, current_rate + rate_increase );
0eb11a06 240 }
4464301d 241 if( current_rate > target_rate ){ current_rate = target_rate; }
1a2d88eb 242
58baeec1
MM
243 this->stepper_motor->set_speed(max(current_rate, this->kernel->stepper->minimum_steps_per_minute/60));
244
be8332cd 245 return 0;
ded56b35 246}
1a2d88eb 247
be8332cd
AW
248// Speed has been updated for the robot's stepper, we must update accordingly
249void Extruder::on_speed_change( void* argument ){
ca037905 250
be8332cd 251 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
4464301d
AW
252 if( this->current_block == NULL || this->paused || this->mode != FOLLOW || this->stepper_motor->moving != true ){ return; }
253
254 /*
58baeec1
MM
255 * nominal block duration = current block's steps / ( current block's nominal rate / 60 )
256 * nominal extruder rate = extruder steps / nominal block duration
257 * actual extruder rate = nominal extruder rate * ( ( stepper's steps per minute / 60 ) / ( current block's nominal rate / 60 ) )
258 * 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 ) )
259 * or simplified : extruder steps * ( stepper's steps per minute / 60 ) ) / current block's steps
260 * or even : ( stepper steps per minute / 60 ) * ( extruder steps / current block's steps )
4464301d 261 */
58baeec1
MM
262
263 this->stepper_motor->set_speed( max( ( this->kernel->stepper->trapezoid_adjusted_rate /60L) * ( (double)this->stepper_motor->steps_to_move / (double)this->current_block->steps_event_count ), this->kernel->stepper->minimum_steps_per_minute/60 ) );
ca037905 264
4cff3ded
AW
265}
266
4cff3ded
AW
267
268
be8332cd
AW
269// When the stepper has finished it's move
270uint32_t Extruder::stepper_motor_finished_move(uint32_t dummy){
4cff3ded 271
4464301d
AW
272 //printf("extruder releasing\r\n");
273
be8332cd 274 this->current_position = this->target_position;
feb204be 275
be8332cd 276 this->current_block->release();
f2203544 277 return 0;
feb204be 278
be8332cd 279}
feb204be 280