4c2269c883e672c41cf63dc47002b800e753237c
[clinton/Smoothieware.git] / src / modules / tools / extruder / Extruder.cpp
1 /*
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/>.
6 */
7
8 #include "Extruder.h"
9
10 #include "libs/Module.h"
11 #include "libs/Kernel.h"
12
13 #include "modules/robot/Conveyor.h"
14 #include "modules/robot/Block.h"
15 #include "StepperMotor.h"
16 #include "SlowTicker.h"
17 #include "Stepper.h"
18 #include "StepTicker.h"
19 #include "Config.h"
20 #include "StepperMotor.h"
21 #include "Robot.h"
22 #include "checksumm.h"
23 #include "ConfigValue.h"
24 #include "Gcode.h"
25 #include "libs/StreamOutput.h"
26 #include "PublicDataRequest.h"
27
28 #include <mri.h>
29
30 // OLD config names for backwards compatibility, NOTE new configs will not be added here
31 #define extruder_module_enable_checksum CHECKSUM("extruder_module_enable")
32 #define extruder_steps_per_mm_checksum CHECKSUM("extruder_steps_per_mm")
33 #define extruder_filament_diameter_checksum CHECKSUM("extruder_filament_diameter")
34 #define extruder_acceleration_checksum CHECKSUM("extruder_acceleration")
35 #define extruder_step_pin_checksum CHECKSUM("extruder_step_pin")
36 #define extruder_dir_pin_checksum CHECKSUM("extruder_dir_pin")
37 #define extruder_en_pin_checksum CHECKSUM("extruder_en_pin")
38 #define extruder_max_speed_checksum CHECKSUM("extruder_max_speed")
39
40 // NEW config names
41 #define extruder_checksum CHECKSUM("extruder")
42
43 #define default_feed_rate_checksum CHECKSUM("default_feed_rate")
44 #define steps_per_mm_checksum CHECKSUM("steps_per_mm")
45 #define filament_diameter_checksum CHECKSUM("filament_diameter")
46 #define acceleration_checksum CHECKSUM("acceleration")
47 #define step_pin_checksum CHECKSUM("step_pin")
48 #define dir_pin_checksum CHECKSUM("dir_pin")
49 #define en_pin_checksum CHECKSUM("en_pin")
50 #define max_speed_checksum CHECKSUM("max_speed")
51 #define x_offset_checksum CHECKSUM("x_offset")
52 #define y_offset_checksum CHECKSUM("y_offset")
53 #define z_offset_checksum CHECKSUM("z_offset")
54
55 #define retract_length_checksum CHECKSUM("retract_length")
56 #define retract_feedrate_checksum CHECKSUM("retract_feedrate")
57 #define retract_recover_length_checksum CHECKSUM("retract_recover_length")
58 #define retract_recover_feedrate_checksum CHECKSUM("retract_recover_feedrate")
59 #define retract_zlift_length_checksum CHECKSUM("retract_zlift_length")
60 #define retract_zlift_feedrate_checksum CHECKSUM("retract_zlift_feedrate")
61
62 #define X_AXIS 0
63 #define Y_AXIS 1
64 #define Z_AXIS 2
65
66 #define OFF 0
67 #define SOLO 1
68 #define FOLLOW 2
69
70 #define PI 3.14159265358979F
71
72 #define max(a,b) (((a) > (b)) ? (a) : (b))
73
74 /* The extruder module controls a filament extruder for 3D printing: http://en.wikipedia.org/wiki/Fused_deposition_modeling
75 * 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 )
76 * or the head moves, and the extruder moves plastic at a speed proportional to the movement of the head ( FOLLOW mode here ).
77 */
78
79 Extruder::Extruder( uint16_t config_identifier, bool single )
80 {
81 this->absolute_mode = true;
82 this->enabled = false;
83 this->paused = false;
84 this->single_config = single;
85 this->identifier = config_identifier;
86 this->retracted = false;
87 this->volumetric_multiplier = 1.0F;
88 this->extruder_multiplier = 1.0F;
89 this->stepper_motor= nullptr;
90
91 memset(this->offset, 0, sizeof(this->offset));
92 }
93
94 Extruder::~Extruder()
95 {
96 delete stepper_motor;
97 }
98
99 void Extruder::on_halt(void *arg)
100 {
101 if(arg == nullptr) {
102 // turn off motor
103 this->en_pin.set(1);
104 // disable if multi extruder
105 if(!this->single_config)
106 this->enabled= false;
107 }
108 }
109
110 void Extruder::on_module_loaded()
111 {
112 // Settings
113 this->on_config_reload(this);
114
115 // Start values
116 this->target_position = 0;
117 this->current_position = 0;
118 this->unstepped_distance = 0;
119 this->current_block = NULL;
120 this->mode = OFF;
121
122 // We work on the same Block as Stepper, so we need to know when it gets a new one and drops one
123 this->register_for_event(ON_BLOCK_BEGIN);
124 this->register_for_event(ON_BLOCK_END);
125 this->register_for_event(ON_GCODE_RECEIVED);
126 this->register_for_event(ON_GCODE_EXECUTE);
127 this->register_for_event(ON_PLAY);
128 this->register_for_event(ON_PAUSE);
129 this->register_for_event(ON_HALT);
130 this->register_for_event(ON_SPEED_CHANGE);
131 this->register_for_event(ON_GET_PUBLIC_DATA);
132
133 // Update speed every *acceleration_ticks_per_second*
134 THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->get_acceleration_ticks_per_second() , this, &Extruder::acceleration_tick );
135 }
136
137 // Get config
138 void Extruder::on_config_reload(void *argument)
139 {
140 if( this->single_config ) {
141 // If this module uses the old "single extruder" configuration style
142
143 this->steps_per_millimeter = THEKERNEL->config->value(extruder_steps_per_mm_checksum )->by_default(1)->as_number();
144 this->filament_diameter = THEKERNEL->config->value(extruder_filament_diameter_checksum )->by_default(0)->as_number();
145 this->acceleration = THEKERNEL->config->value(extruder_acceleration_checksum )->by_default(1000)->as_number();
146 this->feed_rate = THEKERNEL->config->value(default_feed_rate_checksum )->by_default(1000)->as_number();
147
148 this->step_pin.from_string( THEKERNEL->config->value(extruder_step_pin_checksum )->by_default("nc" )->as_string())->as_output();
149 this->dir_pin.from_string( THEKERNEL->config->value(extruder_dir_pin_checksum )->by_default("nc" )->as_string())->as_output();
150 this->en_pin.from_string( THEKERNEL->config->value(extruder_en_pin_checksum )->by_default("nc" )->as_string())->as_output();
151
152 for(int i = 0; i < 3; i++) {
153 this->offset[i] = 0;
154 }
155
156 this->enabled = true;
157
158 } else {
159 // If this module was created with the new multi extruder configuration style
160
161 this->steps_per_millimeter = THEKERNEL->config->value(extruder_checksum, this->identifier, steps_per_mm_checksum )->by_default(1)->as_number();
162 this->filament_diameter = THEKERNEL->config->value(extruder_checksum, this->identifier, filament_diameter_checksum )->by_default(0)->as_number();
163 this->acceleration = THEKERNEL->config->value(extruder_checksum, this->identifier, acceleration_checksum )->by_default(1000)->as_number();
164 this->feed_rate = THEKERNEL->config->value( default_feed_rate_checksum )->by_default(1000)->as_number();
165
166 this->step_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, step_pin_checksum )->by_default("nc" )->as_string())->as_output();
167 this->dir_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, dir_pin_checksum )->by_default("nc" )->as_string())->as_output();
168 this->en_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, en_pin_checksum )->by_default("nc" )->as_string())->as_output();
169
170 this->offset[X_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, x_offset_checksum )->by_default(0)->as_number();
171 this->offset[Y_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, y_offset_checksum )->by_default(0)->as_number();
172 this->offset[Z_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, z_offset_checksum )->by_default(0)->as_number();
173
174 }
175
176 // these are only supported in the new syntax, no need to be backward compatible as they did not exist before the change
177 this->retract_length = THEKERNEL->config->value(extruder_checksum, this->identifier, retract_length_checksum)->by_default(3)->as_number();
178 this->retract_feedrate = THEKERNEL->config->value(extruder_checksum, this->identifier, retract_feedrate_checksum)->by_default(45)->as_number();
179 this->retract_recover_length = THEKERNEL->config->value(extruder_checksum, this->identifier, retract_recover_length_checksum)->by_default(0)->as_number();
180 this->retract_recover_feedrate = THEKERNEL->config->value(extruder_checksum, this->identifier, retract_recover_feedrate_checksum)->by_default(8)->as_number();
181 this->retract_zlift_length = THEKERNEL->config->value(extruder_checksum, this->identifier, retract_zlift_length_checksum)->by_default(0)->as_number();
182 this->retract_zlift_feedrate = THEKERNEL->config->value(extruder_checksum, this->identifier, retract_zlift_feedrate_checksum)->by_default(100*60)->as_number(); // mm/min
183
184 if(filament_diameter > 0.01) {
185 this->volumetric_multiplier = 1.0F / (powf(this->filament_diameter / 2, 2) * PI);
186 }
187
188 // Stepper motor object for the extruder
189 this->stepper_motor = new StepperMotor(step_pin, dir_pin, en_pin);
190 this->stepper_motor->attach(this, &Extruder::stepper_motor_finished_move );
191 if( this->single_config ) {
192 this->stepper_motor->set_max_rate(THEKERNEL->config->value(extruder_max_speed_checksum)->by_default(1000)->as_number());
193 }else{
194 this->stepper_motor->set_max_rate(THEKERNEL->config->value(extruder_checksum, this->identifier, max_speed_checksum)->by_default(1000)->as_number());
195 }
196 }
197
198 void Extruder::on_get_public_data(void* argument){
199 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
200
201 if(!pdr->starts_with(extruder_checksum)) return;
202
203 if(this->enabled) {
204 // Note this is allowing both step/mm and filament diameter to be exposed via public data
205 pdr->set_data_ptr(&this->steps_per_millimeter);
206 pdr->set_taken();
207 }
208 }
209
210 // When the play/pause button is set to pause, or a module calls the ON_PAUSE event
211 void Extruder::on_pause(void *argument)
212 {
213 this->paused = true;
214 this->stepper_motor->pause();
215 }
216
217 // When the play/pause button is set to play, or a module calls the ON_PLAY event
218 void Extruder::on_play(void *argument)
219 {
220 this->paused = false;
221 this->stepper_motor->unpause();
222 }
223
224 void Extruder::on_gcode_received(void *argument)
225 {
226 Gcode *gcode = static_cast<Gcode *>(argument);
227
228 // M codes most execute immediately, most only execute if enabled
229 if (gcode->has_m) {
230 if (gcode->m == 114 && this->enabled) {
231 char buf[16];
232 int n = snprintf(buf, sizeof(buf), " E:%1.3f ", this->current_position);
233 gcode->txt_after_ok.append(buf, n);
234 gcode->mark_as_taken();
235
236 } else if (gcode->m == 92 && ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier) ) ) {
237 float spm = this->steps_per_millimeter;
238 if (gcode->has_letter('E')) {
239 spm = gcode->get_value('E');
240 this->steps_per_millimeter = spm;
241 }
242
243 gcode->stream->printf("E:%g ", spm);
244 gcode->add_nl = true;
245 gcode->mark_as_taken();
246
247 } else if (gcode->m == 200 && ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier)) ) {
248 if (gcode->has_letter('D')) {
249 THEKERNEL->conveyor->wait_for_empty_queue(); // only apply after the queue has emptied
250 this->filament_diameter = gcode->get_value('D');
251 if(filament_diameter > 0.01) {
252 this->volumetric_multiplier = 1.0F / (powf(this->filament_diameter / 2, 2) * PI);
253 }else{
254 this->volumetric_multiplier = 1.0F;
255 }
256 }
257 gcode->mark_as_taken();
258
259 } else if (gcode->m == 204 && gcode->has_letter('E') &&
260 ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier)) ) {
261 // extruder acceleration M204 Ennn mm/sec^2 (Pnnn sets the specific extruder for M500)
262 this->acceleration= gcode->get_value('E');
263 gcode->mark_as_taken();
264
265 } else if (gcode->m == 207 && ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier)) ) {
266 // M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop] Q[zlift feedrate mm/min]
267 if(gcode->has_letter('S')) retract_length = gcode->get_value('S');
268 if(gcode->has_letter('F')) retract_feedrate = gcode->get_value('F')/60.0F; // specified in mm/min converted to mm/sec
269 if(gcode->has_letter('Z')) retract_zlift_length = gcode->get_value('Z');
270 if(gcode->has_letter('Q')) retract_zlift_feedrate = gcode->get_value('Q');
271 gcode->mark_as_taken();
272
273 } else if (gcode->m == 208 && ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier)) ) {
274 // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/min]
275 if(gcode->has_letter('S')) retract_recover_length = gcode->get_value('S');
276 if(gcode->has_letter('F')) retract_recover_feedrate = gcode->get_value('F')/60.0F; // specified in mm/min converted to mm/sec
277 gcode->mark_as_taken();
278
279 } else if (gcode->m == 221 && this->enabled) { // M221 S100 change flow rate by percentage
280 if(gcode->has_letter('S')) this->extruder_multiplier= gcode->get_value('S')/100.0F;
281 gcode->mark_as_taken();
282
283 } else if (gcode->m == 500 || gcode->m == 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings
284 if( this->single_config ) {
285 gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f\n", this->steps_per_millimeter);
286 gcode->stream->printf(";E Filament diameter:\nM200 D%1.4f\n", this->filament_diameter);
287 gcode->stream->printf(";E retract length, feedrate, zlift length, feedrate:\nM207 S%1.4f F%1.4f Z%1.4f Q%1.4f\n", this->retract_length, this->retract_feedrate*60.0F, this->retract_zlift_length, this->retract_zlift_feedrate);
288 gcode->stream->printf(";E retract recover length, feedrate:\nM208 S%1.4f F%1.4f\n", this->retract_recover_length, this->retract_recover_feedrate*60.0F);
289 gcode->stream->printf(";E acceleration mm/sec^2:\nM204 E%1.4f\n", this->acceleration);
290
291 } else {
292 gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f P%d\n", this->steps_per_millimeter, this->identifier);
293 gcode->stream->printf(";E Filament diameter:\nM200 D%1.4f P%d\n", this->filament_diameter, this->identifier);
294 gcode->stream->printf(";E retract length, feedrate:\nM207 S%1.4f F%1.4f Z%1.4f Q%1.4f P%d\n", this->retract_length, this->retract_feedrate*60.0F, this->retract_zlift_length, this->retract_zlift_feedrate, this->identifier);
295 gcode->stream->printf(";E retract recover length, feedrate:\nM208 S%1.4f F%1.4f P%d\n", this->retract_recover_length, this->retract_recover_feedrate*60.0F, this->identifier);
296 gcode->stream->printf(";E acceleration mm/sec^2:\nM204 E%1.4f P%d\n", this->acceleration, this->identifier);
297 }
298 gcode->mark_as_taken();
299 } else if( gcode->m == 17 || gcode->m == 18 || gcode->m == 82 || gcode->m == 83 || gcode->m == 84 ) {
300 // Mcodes to pass along to on_gcode_execute
301 THEKERNEL->conveyor->append_gcode(gcode);
302 gcode->mark_as_taken();
303 }
304
305 }else if(gcode->has_g) {
306 // G codes, NOTE some are ignored if not enabled
307 if( (gcode->g == 92 && gcode->has_letter('E')) || (gcode->g == 90 || gcode->g == 91) ) {
308 // Gcodes to pass along to on_gcode_execute
309 THEKERNEL->conveyor->append_gcode(gcode);
310 gcode->mark_as_taken();
311
312 }else if( this->enabled && gcode->g < 4 && gcode->has_letter('E') && !gcode->has_letter('X') && !gcode->has_letter('Y') && !gcode->has_letter('Z') ) {
313 // This is a solo move, we add an empty block to the queue to prevent subsequent gcodes being executed at the same time
314 THEKERNEL->conveyor->append_gcode(gcode);
315 THEKERNEL->conveyor->queue_head_block();
316 gcode->mark_as_taken();
317
318 }else if( this->enabled && (gcode->g == 10 || gcode->g == 11) ) { // firmware retract command
319 gcode->mark_as_taken();
320 // check we are in the correct state of retract or unretract
321 if(gcode->g == 10 && !retracted) {
322 this->retracted= true;
323 this->cancel_zlift_restore= false;
324 } else if(gcode->g == 11 && retracted){
325 this->retracted= false;
326 } else
327 return; // ignore duplicates
328
329 // now we do a special hack to add zlift if needed, this should go in Robot but if it did the zlift would be executed before retract which is bad
330 // this way zlift will happen after retract, (or before for unretract) NOTE we call the robot->on_gcode_receive directly to avoid recursion
331 if(retract_zlift_length > 0 && gcode->g == 11 && !this->cancel_zlift_restore) {
332 // reverse zlift happens before unretract
333 // NOTE we do not do this if cancel_zlift_restore is set to true, which happens if there is an absolute Z move inbetween G10 and G11
334 char buf[32];
335 int n= snprintf(buf, sizeof(buf), "G0 Z%1.4f F%1.4f", -retract_zlift_length, retract_zlift_feedrate);
336 string cmd(buf, n);
337 Gcode gc(cmd, &(StreamOutput::NullStream));
338 bool oldmode= THEKERNEL->robot->absolute_mode;
339 THEKERNEL->robot->absolute_mode= false; // needs to be relative mode
340 THEKERNEL->robot->on_gcode_received(&gc); // send to robot directly
341 THEKERNEL->robot->absolute_mode= oldmode; // restore mode
342 }
343
344 // This is a solo move, we add an empty block to the queue to prevent subsequent gcodes being executed at the same time
345 THEKERNEL->conveyor->append_gcode(gcode);
346 THEKERNEL->conveyor->queue_head_block();
347
348 if(retract_zlift_length > 0 && gcode->g == 10) {
349 char buf[32];
350 int n= snprintf(buf, sizeof(buf), "G0 Z%1.4f F%1.4f", retract_zlift_length, retract_zlift_feedrate);
351 string cmd(buf, n);
352 Gcode gc(cmd, &(StreamOutput::NullStream));
353 bool oldmode= THEKERNEL->robot->absolute_mode;
354 THEKERNEL->robot->absolute_mode= false; // needs to be relative mode
355 THEKERNEL->robot->on_gcode_received(&gc); // send to robot directly
356 THEKERNEL->robot->absolute_mode= oldmode; // restore mode
357 }
358
359 }else if( this->enabled && this->retracted && (gcode->g == 0 || gcode->g == 1) && gcode->has_letter('Z')) {
360 // NOTE we cancel the zlift restore for the following G11 as we have moved to an absolute Z which we need to stay at
361 this->cancel_zlift_restore= true;
362 }
363 }
364 }
365
366 // Compute extrusion speed based on parameters and gcode distance of travel
367 void Extruder::on_gcode_execute(void *argument)
368 {
369 Gcode *gcode = static_cast<Gcode *>(argument);
370
371 // The mode is OFF by default, and SOLO or FOLLOW only if we need to extrude
372 this->mode = OFF;
373
374 // Absolute/relative mode, globably modal affect all extruders whether enabled or not
375 if( gcode->has_m ) {
376 switch(gcode->m) {
377 case 17:
378 this->en_pin.set(0);
379 break;
380 case 18:
381 this->en_pin.set(1);
382 break;
383 case 82:
384 this->absolute_mode = true;
385 break;
386 case 83:
387 this->absolute_mode = false;
388 break;
389 case 84:
390 this->en_pin.set(1);
391 break;
392 }
393 return;
394
395 } else if( gcode->has_g && (gcode->g == 90 || gcode->g == 91) ) {
396 this->absolute_mode = (gcode->g == 90);
397 return;
398 }
399
400
401 if( gcode->has_g && this->enabled ) {
402 // G92: Reset extruder position
403 if( gcode->g == 92 ) {
404 if( gcode->has_letter('E') ) {
405 this->current_position = gcode->get_value('E');
406 this->target_position = this->current_position;
407 this->unstepped_distance = 0;
408 } else if( gcode->get_num_args() == 0) {
409 this->current_position = 0.0;
410 this->target_position = this->current_position;
411 this->unstepped_distance = 0;
412 }
413
414 } else if (gcode->g == 10) {
415 // FW retract command
416 feed_rate= retract_feedrate; // mm/sec
417 this->mode = SOLO;
418 this->travel_distance = -retract_length;
419 this->target_position += this->travel_distance;
420 this->en_pin.set(0);
421
422 } else if (gcode->g == 11) {
423 // un retract command
424 feed_rate= retract_recover_feedrate; // mm/sec
425 this->mode = SOLO;
426 this->travel_distance = (retract_length + retract_recover_length);
427 this->target_position += this->travel_distance;
428 this->en_pin.set(0);
429
430 } else if (gcode->g == 0 || gcode->g == 1) {
431 // Extrusion length from 'G' Gcode
432 if( gcode->has_letter('E' )) {
433 // Get relative extrusion distance depending on mode ( in absolute mode we must substract target_position )
434 float extrusion_distance = gcode->get_value('E');
435 float relative_extrusion_distance = extrusion_distance;
436 if (this->absolute_mode) {
437 relative_extrusion_distance -= this->target_position;
438 this->target_position = extrusion_distance;
439 } else {
440 this->target_position += relative_extrusion_distance;
441 }
442
443 // If the robot is moving, we follow it's movement, otherwise, we move alone
444 if( fabs(gcode->millimeters_of_travel) < 0.0001F ) { // 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
445 this->mode = SOLO;
446 this->travel_distance = relative_extrusion_distance;
447 } else {
448 // We move proportionally to the robot's movement
449 this->mode = FOLLOW;
450 this->travel_ratio = (relative_extrusion_distance * this->volumetric_multiplier * this->extruder_multiplier) / gcode->millimeters_of_travel; // adjust for volumetric extrusion and extruder multiplier
451 // TODO: check resulting flowrate, limit robot speed if it exceeds max_speed
452 }
453
454 this->en_pin.set(0);
455 }
456
457 if (gcode->has_letter('F')) {
458 feed_rate = gcode->get_value('F') / THEKERNEL->robot->get_seconds_per_minute();
459 if (feed_rate > stepper_motor->get_max_rate())
460 feed_rate = stepper_motor->get_max_rate();
461 }
462 }
463 }
464 }
465
466 // When a new block begins, either follow the robot, or step by ourselves ( or stay back and do nothing )
467 void Extruder::on_block_begin(void *argument)
468 {
469 if(!this->enabled) return;
470 Block *block = static_cast<Block *>(argument);
471
472
473 if( this->mode == SOLO ) {
474 // In solo mode we take the block so we can move even if the stepper has nothing to do
475
476 this->current_position += this->travel_distance ;
477
478 int steps_to_step = abs(int(floor(this->steps_per_millimeter * (this->travel_distance + this->unstepped_distance) )));
479
480 if ( this->travel_distance > 0 ) {
481 this->unstepped_distance += this->travel_distance - (steps_to_step / this->steps_per_millimeter); //catch any overflow
482 } else {
483 this->unstepped_distance += this->travel_distance + (steps_to_step / this->steps_per_millimeter); //catch any overflow
484 }
485
486 if( steps_to_step != 0 ) {
487
488 // We take the block, we have to release it or everything gets stuck
489 block->take();
490 this->current_block = block;
491
492 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step, 0);
493
494 } else {
495 this->current_block = NULL;
496 }
497
498 } else if( this->mode == FOLLOW ) {
499 // In non-solo mode, we just follow the stepper module
500 this->travel_distance = block->millimeters * this->travel_ratio;
501
502 this->current_position += this->travel_distance;
503
504 int steps_to_step = abs(int(floor(this->steps_per_millimeter * (this->travel_distance + this->unstepped_distance) )));
505
506 if ( this->travel_distance > 0 ) {
507 this->unstepped_distance += this->travel_distance - (steps_to_step / this->steps_per_millimeter); //catch any overflow
508 } else {
509 this->unstepped_distance += this->travel_distance + (steps_to_step / this->steps_per_millimeter); //catch any overflow
510 }
511
512 if( steps_to_step != 0 ) {
513 block->take();
514 this->current_block = block;
515
516 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step);
517 on_speed_change(this); // set initial speed
518 } else {
519 this->current_block = NULL;
520 }
521
522 } else if( this->mode == OFF ) {
523 // No movement means we must reset our speed
524 this->current_block = NULL;
525 //this->stepper_motor->set_speed(0);
526
527 }
528
529 }
530
531 // When a block ends, pause the stepping interrupt
532 void Extruder::on_block_end(void *argument)
533 {
534 if(!this->enabled) return;
535 this->current_block = NULL;
536 }
537
538 // Called periodically to change the speed to match acceleration or to match the speed of the robot
539 // Only used in SOLO mode
540 uint32_t Extruder::acceleration_tick(uint32_t dummy)
541 {
542 if(!this->enabled) return 0;
543
544 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
545 if( this->current_block == NULL || this->paused || this->mode != SOLO ) {
546 return 0;
547 }
548
549 if(!this->stepper_motor->is_moving()) return 0;
550
551 uint32_t current_rate = this->stepper_motor->get_steps_per_second();
552 uint32_t target_rate = int(floor(this->feed_rate * this->steps_per_millimeter));
553
554 if( current_rate < target_rate ) {
555 uint32_t rate_increase = int(floor((this->acceleration / THEKERNEL->stepper->get_acceleration_ticks_per_second()) * this->steps_per_millimeter));
556 current_rate = min( target_rate, current_rate + rate_increase );
557 }
558 if( current_rate > target_rate ) {
559 current_rate = target_rate;
560 }
561
562 // steps per second
563 this->stepper_motor->set_speed(current_rate);
564
565 return 0;
566 }
567
568 // Speed has been updated for the robot's stepper, we must update accordingly
569 void Extruder::on_speed_change( void *argument )
570 {
571 if(!this->enabled) return;
572
573 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
574 if( this->current_block == NULL || this->paused || this->mode != FOLLOW || !this->stepper_motor->is_moving()) {
575 return;
576 }
577
578 // if we are flushing the queue we need to stop the motor when it has decelerated to zero, we get this call with argumnet == 0 when this happens
579 // this is what steppermotor does
580 if(argument == 0) {
581 this->stepper_motor->move(0, 0);
582 this->current_block->release();
583 this->current_block = NULL;
584 return;
585 }
586
587 /*
588 * nominal block duration = current block's steps / ( current block's nominal rate )
589 * nominal extruder rate = extruder steps / nominal block duration
590 * actual extruder rate = nominal extruder rate * ( ( stepper's steps per second ) / ( current block's nominal rate ) )
591 * 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 ) )
592 * or simplified : extruder steps * ( stepper's steps per second ) ) / current block's steps
593 * or even : ( stepper steps per second ) * ( extruder steps / current block's steps )
594 */
595
596 //this->stepper_motor->set_speed(THEKERNEL->stepper->get_trapezoid_adjusted_rate() * (float)this->stepper_motor->get_steps_to_move() / (float)this->current_block->steps_event_count);
597 this->stepper_motor->set_step_rate(THEKERNEL->stepper->get_trapezoid_adjusted_rate(), (float)this->current_block->steps_event_count);
598 }
599
600 // When the stepper has finished it's move
601 uint32_t Extruder::stepper_motor_finished_move(uint32_t dummy)
602 {
603 if(!this->enabled) return 0;
604
605 //printf("extruder releasing\r\n");
606
607 if (this->current_block) { // this should always be true, but sometimes it isn't. TODO: find out why
608 Block *block = this->current_block;
609 this->current_block = NULL;
610 block->release();
611 }
612 return 0;
613
614 }