remove copies of steppermotor pointers in modules and use the ones in robot
[clinton/Smoothieware.git] / src / modules / tools / extruder / Extruder.cpp
... / ...
CommitLineData
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#define extruder_module_enable_checksum CHECKSUM("extruder_module_enable")
31#define extruder_steps_per_mm_checksum CHECKSUM("extruder_steps_per_mm")
32#define extruder_filament_diameter_checksum CHECKSUM("extruder_filament_diameter")
33#define extruder_acceleration_checksum CHECKSUM("extruder_acceleration")
34#define extruder_step_pin_checksum CHECKSUM("extruder_step_pin")
35#define extruder_dir_pin_checksum CHECKSUM("extruder_dir_pin")
36#define extruder_en_pin_checksum CHECKSUM("extruder_en_pin")
37#define extruder_max_speed_checksum CHECKSUM("extruder_max_speed")
38
39#define extruder_checksum CHECKSUM("extruder")
40
41#define default_feed_rate_checksum CHECKSUM("default_feed_rate")
42#define steps_per_mm_checksum CHECKSUM("steps_per_mm")
43#define filament_diameter_checksum CHECKSUM("filament_diameter")
44#define acceleration_checksum CHECKSUM("acceleration")
45#define step_pin_checksum CHECKSUM("step_pin")
46#define dir_pin_checksum CHECKSUM("dir_pin")
47#define en_pin_checksum CHECKSUM("en_pin")
48#define max_speed_checksum CHECKSUM("max_speed")
49#define x_offset_checksum CHECKSUM("x_offset")
50#define y_offset_checksum CHECKSUM("y_offset")
51#define z_offset_checksum CHECKSUM("z_offset")
52
53#define X_AXIS 0
54#define Y_AXIS 1
55#define Z_AXIS 2
56
57#define OFF 0
58#define SOLO 1
59#define FOLLOW 2
60
61#define PI 3.14159265358979F
62
63#define max(a,b) (((a) > (b)) ? (a) : (b))
64
65/* The extruder module controls a filament extruder for 3D printing: http://en.wikipedia.org/wiki/Fused_deposition_modeling
66* 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 )
67* or the head moves, and the extruder moves plastic at a speed proportional to the movement of the head ( FOLLOW mode here ).
68*/
69
70Extruder::Extruder( uint16_t config_identifier, bool single )
71{
72 this->absolute_mode = true;
73 this->enabled = false;
74 this->paused = false;
75 this->single_config = single;
76 this->identifier = config_identifier;
77
78 memset(this->offset, 0, sizeof(this->offset));
79}
80
81void Extruder::on_module_loaded()
82{
83
84 // Settings
85 this->on_config_reload(this);
86
87 // We work on the same Block as Stepper, so we need to know when it gets a new one and drops one
88 register_for_event(ON_CONFIG_RELOAD);
89 this->register_for_event(ON_BLOCK_BEGIN);
90 this->register_for_event(ON_BLOCK_END);
91 this->register_for_event(ON_GCODE_RECEIVED);
92 this->register_for_event(ON_GCODE_EXECUTE);
93 this->register_for_event(ON_PLAY);
94 this->register_for_event(ON_PAUSE);
95 this->register_for_event(ON_SPEED_CHANGE);
96 this->register_for_event(ON_GET_PUBLIC_DATA);
97
98 // Start values
99 this->target_position = 0;
100 this->current_position = 0;
101 this->unstepped_distance = 0;
102 this->current_block = NULL;
103 this->mode = OFF;
104
105 // Update speed every *acceleration_ticks_per_second*
106 // TODO: Make this an independent setting
107 THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->get_acceleration_ticks_per_second() , this, &Extruder::acceleration_tick );
108
109 // Stepper motor object for the extruder
110 this->stepper_motor = THEKERNEL->step_ticker->add_stepper_motor( new StepperMotor(step_pin, dir_pin, en_pin) );
111 this->stepper_motor->attach(this, &Extruder::stepper_motor_finished_move );
112
113}
114
115// Get config
116void Extruder::on_config_reload(void *argument)
117{
118 if( this->single_config ) {
119 // If this module uses the old "single extruder" configuration style
120
121 this->steps_per_millimeter_setting = THEKERNEL->config->value(extruder_steps_per_mm_checksum )->by_default(1)->as_number();
122 this->filament_diameter = THEKERNEL->config->value(extruder_filament_diameter_checksum )->by_default(0)->as_number();
123 this->acceleration = THEKERNEL->config->value(extruder_acceleration_checksum )->by_default(1000)->as_number();
124 this->max_speed = THEKERNEL->config->value(extruder_max_speed_checksum )->by_default(1000)->as_number();
125 this->feed_rate = THEKERNEL->config->value(default_feed_rate_checksum )->by_default(1000)->as_number();
126
127 this->step_pin.from_string( THEKERNEL->config->value(extruder_step_pin_checksum )->by_default("nc" )->as_string())->as_output();
128 this->dir_pin.from_string( THEKERNEL->config->value(extruder_dir_pin_checksum )->by_default("nc" )->as_string())->as_output();
129 this->en_pin.from_string( THEKERNEL->config->value(extruder_en_pin_checksum )->by_default("nc" )->as_string())->as_output();
130
131 for(int i = 0; i < 3; i++) {
132 this->offset[i] = 0;
133 }
134
135 this->enabled = true;
136
137 } else {
138 // If this module was created with the new multi extruder configuration style
139
140 this->steps_per_millimeter_setting = THEKERNEL->config->value(extruder_checksum, this->identifier, steps_per_mm_checksum )->by_default(1)->as_number();
141 this->filament_diameter = THEKERNEL->config->value(extruder_checksum, this->identifier, filament_diameter_checksum )->by_default(0)->as_number();
142 this->acceleration = THEKERNEL->config->value(extruder_checksum, this->identifier, acceleration_checksum )->by_default(1000)->as_number();
143 this->max_speed = THEKERNEL->config->value(extruder_checksum, this->identifier, max_speed_checksum )->by_default(1000)->as_number();
144 this->feed_rate = THEKERNEL->config->value( default_feed_rate_checksum )->by_default(1000)->as_number();
145
146 this->step_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, step_pin_checksum )->by_default("nc" )->as_string())->as_output();
147 this->dir_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, dir_pin_checksum )->by_default("nc" )->as_string())->as_output();
148 this->en_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, en_pin_checksum )->by_default("nc" )->as_string())->as_output();
149
150 this->offset[X_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, x_offset_checksum )->by_default(0)->as_number();
151 this->offset[Y_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, y_offset_checksum )->by_default(0)->as_number();
152 this->offset[Z_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, z_offset_checksum )->by_default(0)->as_number();
153 }
154
155 this->update_steps_per_millimeter();
156}
157
158void Extruder::on_get_public_data(void* argument){
159 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
160
161 if(!pdr->starts_with(extruder_checksum)) return;
162
163 if(this->enabled) {
164 // Note this is allowing both step/mm and filament diameter to be exposed via public data
165 pdr->set_data_ptr(&this->steps_per_millimeter_setting);
166 pdr->set_taken();
167 }
168}
169
170// When the play/pause button is set to pause, or a module calls the ON_PAUSE event
171void Extruder::on_pause(void *argument)
172{
173 this->paused = true;
174 this->stepper_motor->pause();
175}
176
177// When the play/pause button is set to play, or a module calls the ON_PLAY event
178void Extruder::on_play(void *argument)
179{
180 this->paused = false;
181 this->stepper_motor->unpause();
182}
183
184void Extruder::on_gcode_received(void *argument)
185{
186 Gcode *gcode = static_cast<Gcode *>(argument);
187
188 // Gcodes to execute immediately
189 if (gcode->has_m) {
190 if (gcode->m == 114 && this->enabled) {
191 char buf[16];
192 int n = snprintf(buf, sizeof(buf), " E:%1.3f ", this->current_position);
193 gcode->txt_after_ok.append(buf, n);
194 gcode->mark_as_taken();
195
196 } else if (gcode->m == 92 && ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier) ) ) {
197 float spm = this->steps_per_millimeter;
198 if (gcode->has_letter('E')) {
199 spm = gcode->get_value('E');
200 this->steps_per_millimeter_setting = spm;
201 this->update_steps_per_millimeter();
202 }
203
204 gcode->stream->printf("E:%g ", spm);
205 gcode->add_nl = true;
206 gcode->mark_as_taken();
207
208 } else if (gcode->m == 200 && ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier)) ) {
209 if (gcode->has_letter('D')) {
210 this->filament_diameter = gcode->get_value('D');
211 this->update_steps_per_millimeter();
212 }
213 gcode->mark_as_taken();
214
215 } else if (gcode->m == 500 || gcode->m == 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings
216 if( this->single_config ) {
217 gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f\n", this->steps_per_millimeter_setting);
218 gcode->stream->printf(";E Filament diameter:\nM200 D%1.4f\n", this->filament_diameter);
219 } else {
220 gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f P%d\n", this->steps_per_millimeter_setting, this->identifier);
221 gcode->stream->printf(";E Filament diameter:\nM200 D%1.4f P%d\n", this->filament_diameter, this->identifier);
222 }
223 gcode->mark_as_taken();
224 return;
225 }
226 }
227
228 // Gcodes to pass along to on_gcode_execute
229 if( ( gcode->has_m && (gcode->m == 17 || gcode->m == 18 || gcode->m == 82 || gcode->m == 83 || gcode->m == 84 ) ) || ( gcode->has_g && gcode->g == 92 && gcode->has_letter('E') ) || ( gcode->has_g && ( gcode->g == 90 || gcode->g == 91 ) ) ) {
230 THEKERNEL->conveyor->append_gcode(gcode);
231 }
232
233 // Add to the queue for on_gcode_execute to process
234 if( gcode->has_g && gcode->g < 4 && gcode->has_letter('E') && this->enabled ) {
235 if( !gcode->has_letter('X') && !gcode->has_letter('Y') && !gcode->has_letter('Z') ) {
236 THEKERNEL->conveyor->append_gcode(gcode);
237 // This is a solo move, we add an empty block to the queue to prevent subsequent gcodes being executed at the same time
238 THEKERNEL->conveyor->queue_head_block();
239 }
240 } else {
241 // This is for follow move
242
243 }
244}
245
246// Compute extrusion speed based on parameters and gcode distance of travel
247void Extruder::on_gcode_execute(void *argument)
248{
249 Gcode *gcode = static_cast<Gcode *>(argument);
250
251 // Absolute/relative mode
252 if( gcode->has_m ) {
253 if( gcode->m == 17 ) {
254 this->en_pin.set(0);
255 }
256 if( gcode->m == 18 ) {
257 this->en_pin.set(1);
258 }
259 if( gcode->m == 82 ) {
260 this->absolute_mode = true;
261 }
262 if( gcode->m == 83 ) {
263 this->absolute_mode = false;
264 }
265 if( gcode->m == 84 ) {
266 this->en_pin.set(1);
267 }
268 }
269
270 // The mode is OFF by default, and SOLO or FOLLOW only if we need to extrude
271 this->mode = OFF;
272
273 if( gcode->has_g ) {
274 // G92: Reset extruder position
275 if( gcode->g == 92 && this->enabled ) {
276 gcode->mark_as_taken();
277 if( gcode->has_letter('E') ) {
278 this->current_position = gcode->get_value('E');
279 this->target_position = this->current_position;
280 this->unstepped_distance = 0;
281 } else if( gcode->get_num_args() == 0) {
282 this->current_position = 0.0;
283 this->target_position = this->current_position;
284 this->unstepped_distance = 0;
285 }
286
287 } else if (((gcode->g == 0) || (gcode->g == 1)) && this->enabled) {
288 // Extrusion length from 'G' Gcode
289 if( gcode->has_letter('E' )) {
290 // Get relative extrusion distance depending on mode ( in absolute mode we must substract target_position )
291 float extrusion_distance = gcode->get_value('E');
292 float relative_extrusion_distance = extrusion_distance;
293 if (this->absolute_mode) {
294 relative_extrusion_distance -= this->target_position;
295 this->target_position = extrusion_distance;
296 } else {
297 this->target_position += relative_extrusion_distance;
298 }
299
300 // If the robot is moving, we follow it's movement, otherwise, we move alone
301 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
302 this->mode = SOLO;
303 this->travel_distance = relative_extrusion_distance;
304 } else {
305 // We move proportionally to the robot's movement
306 this->mode = FOLLOW;
307 this->travel_ratio = relative_extrusion_distance / gcode->millimeters_of_travel;
308 // TODO: check resulting flowrate, limit robot speed if it exceeds max_speed
309 }
310
311 this->en_pin.set(0);
312 }
313
314 if (gcode->has_letter('F')) {
315 feed_rate = gcode->get_value('F') / THEKERNEL->robot->get_seconds_per_minute();
316 if (feed_rate > max_speed)
317 feed_rate = max_speed;
318 }
319
320 } else if( gcode->g == 90 ) {
321 this->absolute_mode = true;
322
323 } else if( gcode->g == 91 ) {
324 this->absolute_mode = false;
325 }
326 }
327
328}
329
330// When a new block begins, either follow the robot, or step by ourselves ( or stay back and do nothing )
331void Extruder::on_block_begin(void *argument)
332{
333 if(!this->enabled) return;
334 Block *block = static_cast<Block *>(argument);
335
336
337 if( this->mode == SOLO ) {
338 // In solo mode we take the block so we can move even if the stepper has nothing to do
339
340 this->current_position += this->travel_distance ;
341
342 int steps_to_step = abs(int(floor(this->steps_per_millimeter_setting * (this->travel_distance + this->unstepped_distance) )));
343
344 if ( this->travel_distance > 0 ) {
345 this->unstepped_distance += this->travel_distance - (steps_to_step / this->steps_per_millimeter_setting); //catch any overflow
346 } else {
347 this->unstepped_distance += this->travel_distance + (steps_to_step / this->steps_per_millimeter_setting); //catch any overflow
348 }
349
350 if( steps_to_step != 0 ) {
351
352 // We take the block, we have to release it or everything gets stuck
353 block->take();
354 this->current_block = block;
355
356 this->stepper_motor->set_steps_per_second(0);
357 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step);
358
359 } else {
360 this->current_block = NULL;
361 }
362
363 } else if( this->mode == FOLLOW ) {
364 // In non-solo mode, we just follow the stepper module
365 this->travel_distance = block->millimeters * this->travel_ratio;
366
367 this->current_position += this->travel_distance;
368
369 int steps_to_step = abs(int(floor(this->steps_per_millimeter * (this->travel_distance + this->unstepped_distance) )));
370
371 if ( this->travel_distance > 0 ) {
372 this->unstepped_distance += this->travel_distance - (steps_to_step / this->steps_per_millimeter); //catch any overflow
373 } else {
374 this->unstepped_distance += this->travel_distance + (steps_to_step / this->steps_per_millimeter); //catch any overflow
375 }
376
377 if( steps_to_step != 0 ) {
378 block->take();
379 this->current_block = block;
380
381 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step );
382 this->on_speed_change(0); // initialise speed in case we get called first
383 } else {
384 this->current_block = NULL;
385 }
386
387 } else if( this->mode == OFF ) {
388 // No movement means we must reset our speed
389 this->current_block = NULL;
390 //this->stepper_motor->set_speed(0);
391
392 }
393
394}
395
396// When a block ends, pause the stepping interrupt
397void Extruder::on_block_end(void *argument)
398{
399 if(!this->enabled) return;
400 this->current_block = NULL;
401}
402
403// Called periodically to change the speed to match acceleration or to match the speed of the robot
404uint32_t Extruder::acceleration_tick(uint32_t dummy)
405{
406 if(!this->enabled) return 0;
407
408 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
409 if( this->current_block == NULL || this->paused || this->mode != SOLO ) {
410 return 0;
411 }
412
413 uint32_t current_rate = this->stepper_motor->get_steps_per_second();
414 uint32_t target_rate = int(floor(this->feed_rate * this->steps_per_millimeter_setting)); // NOTE we use real steps here not the volumetric ones
415
416 if( current_rate < target_rate ) {
417 uint32_t rate_increase = int(floor((this->acceleration / THEKERNEL->stepper->get_acceleration_ticks_per_second()) * this->steps_per_millimeter_setting));
418 current_rate = min( target_rate, current_rate + rate_increase );
419 }
420 if( current_rate > target_rate ) {
421 current_rate = target_rate;
422 }
423
424 // steps per second
425 this->stepper_motor->set_speed(max(current_rate, THEKERNEL->stepper->get_minimum_steps_per_second()));
426
427 return 0;
428}
429
430// Speed has been updated for the robot's stepper, we must update accordingly
431void Extruder::on_speed_change( void *argument )
432{
433 if(!this->enabled) return;
434
435 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
436 if( this->current_block == NULL || this->paused || this->mode != FOLLOW || this->stepper_motor->is_moving() != true ) {
437 return;
438 }
439
440 /*
441 * nominal block duration = current block's steps / ( current block's nominal rate )
442 * nominal extruder rate = extruder steps / nominal block duration
443 * actual extruder rate = nominal extruder rate * ( ( stepper's steps per second ) / ( current block's nominal rate ) )
444 * 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 ) )
445 * or simplified : extruder steps * ( stepper's steps per second ) ) / current block's steps
446 * or even : ( stepper steps per second ) * ( extruder steps / current block's steps )
447 */
448
449 this->stepper_motor->set_speed( max( ( THEKERNEL->stepper->get_trapezoid_adjusted_rate()) * ( (float)this->stepper_motor->get_steps_to_move() / (float)this->current_block->steps_event_count ), THEKERNEL->stepper->get_minimum_steps_per_second() ) );
450
451}
452
453// When the stepper has finished it's move
454uint32_t Extruder::stepper_motor_finished_move(uint32_t dummy)
455{
456 if(!this->enabled) return 0;
457
458 //printf("extruder releasing\r\n");
459
460 if (this->current_block) { // this should always be true, but sometimes it isn't. TODO: find out why
461 Block *block = this->current_block;
462 this->current_block = NULL;
463 block->release();
464 }
465 return 0;
466
467}
468
469void Extruder::update_steps_per_millimeter() {
470 if(this->filament_diameter > 0.01) {
471 this->steps_per_millimeter = this->steps_per_millimeter_setting / (powf(this->filament_diameter / 2, 2) * PI);
472 } else {
473 this->steps_per_millimeter = this->steps_per_millimeter_setting;
474 }
475}
476