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