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