replace all uses of module::kernel pointer with kernel::instance singleton pointer
[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 "libs/Module.h"
9 #include "libs/Kernel.h"
10 #include "modules/robot/Conveyor.h"
11 #include "modules/robot/Block.h"
12 #include "modules/tools/extruder/Extruder.h"
13 #include <mri.h>
14
15 #define extruder_module_enable_checksum CHECKSUM("extruder_module_enable")
16 #define extruder_steps_per_mm_checksum CHECKSUM("extruder_steps_per_mm")
17 #define extruder_acceleration_checksum CHECKSUM("extruder_acceleration")
18 #define extruder_step_pin_checksum CHECKSUM("extruder_step_pin")
19 #define extruder_dir_pin_checksum CHECKSUM("extruder_dir_pin")
20 #define extruder_en_pin_checksum CHECKSUM("extruder_en_pin")
21 #define extruder_max_speed_checksum CHECKSUM("extruder_max_speed")
22
23 #define extruder_checksum CHECKSUM("extruder")
24
25 #define default_feed_rate_checksum CHECKSUM("default_feed_rate")
26 #define steps_per_mm_checksum CHECKSUM("steps_per_mm")
27 #define acceleration_checksum CHECKSUM("acceleration")
28 #define step_pin_checksum CHECKSUM("step_pin")
29 #define dir_pin_checksum CHECKSUM("dir_pin")
30 #define en_pin_checksum CHECKSUM("en_pin")
31 #define max_speed_checksum CHECKSUM("max_speed")
32
33 #define max(a,b) (((a) > (b)) ? (a) : (b))
34
35 /* The extruder module controls a filament extruder for 3D printing: http://en.wikipedia.org/wiki/Fused_deposition_modeling
36 * 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 )
37 * or the head moves, and the extruder moves plastic at a speed proportional to the movement of the head ( FOLLOW mode here ).
38 */
39
40 Extruder::Extruder( uint16_t config_identifier ) {
41 this->absolute_mode = true;
42 this->paused = false;
43 this->single_config = false;
44 this->identifier = config_identifier;
45 }
46
47 void Extruder::on_module_loaded() {
48
49 // Settings
50 this->on_config_reload(this);
51
52 // We start with the enable pin off
53 this->en_pin.set(1);
54
55 // We work on the same Block as Stepper, so we need to know when it gets a new one and drops one
56 register_for_event(ON_CONFIG_RELOAD);
57 this->register_for_event(ON_BLOCK_BEGIN);
58 this->register_for_event(ON_BLOCK_END);
59 this->register_for_event(ON_GCODE_RECEIVED);
60 this->register_for_event(ON_GCODE_EXECUTE);
61 this->register_for_event(ON_PLAY);
62 this->register_for_event(ON_PAUSE);
63 this->register_for_event(ON_SPEED_CHANGE);
64
65 // Start values
66 this->target_position = 0;
67 this->current_position = 0;
68 this->unstepped_distance = 0;
69 this->current_block = NULL;
70 this->mode = OFF;
71
72 // Update speed every *acceleration_ticks_per_second*
73 // TODO: Make this an independent setting
74 THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->acceleration_ticks_per_second , this, &Extruder::acceleration_tick );
75
76 // Stepper motor object for the extruder
77 this->stepper_motor = THEKERNEL->step_ticker->add_stepper_motor( new StepperMotor(&step_pin, &dir_pin, &en_pin) );
78 this->stepper_motor->attach(this, &Extruder::stepper_motor_finished_move );
79
80 }
81
82 // Get config
83 void Extruder::on_config_reload(void* argument){
84
85 // If this module uses the old "single extruder" configuration style
86 if( this->single_config ){
87
88 this->steps_per_millimeter = THEKERNEL->config->value(extruder_steps_per_mm_checksum )->by_default(1)->as_number();
89 this->acceleration = THEKERNEL->config->value(extruder_acceleration_checksum )->by_default(1000)->as_number();
90 this->max_speed = THEKERNEL->config->value(extruder_max_speed_checksum )->by_default(1000)->as_number();
91 this->feed_rate = THEKERNEL->config->value(default_feed_rate_checksum )->by_default(1000)->as_number();
92
93 this->step_pin.from_string( THEKERNEL->config->value(extruder_step_pin_checksum )->by_default("nc" )->as_string())->as_output();
94 this->dir_pin.from_string( THEKERNEL->config->value(extruder_dir_pin_checksum )->by_default("nc" )->as_string())->as_output();
95 this->en_pin.from_string( THEKERNEL->config->value(extruder_en_pin_checksum )->by_default("nc" )->as_string())->as_output();
96
97 }else{
98 // If this module was created with the new multi extruder configuration style
99
100 this->steps_per_millimeter = THEKERNEL->config->value(extruder_checksum, this->identifier, steps_per_mm_checksum )->by_default(1)->as_number();
101 this->acceleration = THEKERNEL->config->value(extruder_checksum, this->identifier, acceleration_checksum )->by_default(1000)->as_number();
102 this->max_speed = THEKERNEL->config->value(extruder_checksum, this->identifier, max_speed_checksum )->by_default(1000)->as_number();
103 this->feed_rate = THEKERNEL->config->value( default_feed_rate_checksum )->by_default(1000)->as_number();
104
105 this->step_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, step_pin_checksum )->by_default("nc" )->as_string())->as_output();
106 this->dir_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, dir_pin_checksum )->by_default("nc" )->as_string())->as_output();
107 this->en_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, en_pin_checksum )->by_default("nc" )->as_string())->as_output();
108
109 }
110
111 // disable by default
112 this->en_pin.set(1);
113
114 }
115
116
117 // When the play/pause button is set to pause, or a module calls the ON_PAUSE event
118 void Extruder::on_pause(void* argument){
119 this->paused = true;
120 this->stepper_motor->pause();
121 }
122
123 // When the play/pause button is set to play, or a module calls the ON_PLAY event
124 void Extruder::on_play(void* argument){
125 this->paused = false;
126 this->stepper_motor->unpause();
127 }
128
129
130 void Extruder::on_gcode_received(void *argument){
131 Gcode *gcode = static_cast<Gcode*>(argument);
132
133 // Gcodes to execute immediately
134 if (gcode->has_m){
135 if (gcode->m == 114){
136 gcode->stream->printf("E:%4.1f ", this->current_position);
137 gcode->add_nl = true;
138 gcode->mark_as_taken();
139
140 }else if (gcode->m == 92 ){
141 double spm = this->steps_per_millimeter;
142 if (gcode->has_letter('E'))
143 spm = gcode->get_value('E');
144 gcode->stream->printf("E:%g ", spm);
145 gcode->add_nl = true;
146 gcode->mark_as_taken();
147
148 }else if (gcode->m == 500 || gcode->m == 503){// M500 saves some volatile settings to config override file, M503 just prints the settings
149 gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f\n", this->steps_per_millimeter);
150 gcode->mark_as_taken();
151 return;
152 }
153 }
154
155 // Gcodes to pass along to on_gcode_execute
156 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 ) ) ){
157 gcode->mark_as_taken();
158 if( THEKERNEL->conveyor->queue.size() == 0 ){
159 THEKERNEL->call_event(ON_GCODE_EXECUTE, gcode );
160 }else{
161 Block* block = THEKERNEL->conveyor->queue.get_ref( THEKERNEL->conveyor->queue.size() - 1 );
162 block->append_gcode(gcode);
163 }
164 }
165
166 // Add to the queue for on_gcode_execute to process
167 if( gcode->has_g && gcode->g < 4 && gcode->has_letter('E') ){
168 if( !gcode->has_letter('X') && !gcode->has_letter('Y') && !gcode->has_letter('Z') ){
169 // This is a solo move, we add an empty block to the queue
170 //If the queue is empty, execute immediatly, otherwise attach to the last added block
171 if( THEKERNEL->conveyor->queue.size() == 0 ){
172 THEKERNEL->call_event(ON_GCODE_EXECUTE, gcode );
173 this->append_empty_block();
174 }else{
175 Block* block = THEKERNEL->conveyor->queue.get_ref( THEKERNEL->conveyor->queue.size() - 1 );
176 block->append_gcode(gcode);
177 this->append_empty_block();
178 }
179 }
180 }else{
181 // This is for follow move
182
183 }
184 }
185
186 // Append an empty block in the queue so that solo mode can pick it up
187 Block* Extruder::append_empty_block(){
188 THEKERNEL->conveyor->wait_for_queue(2);
189 Block* block = THEKERNEL->conveyor->new_block();
190 block->planner = THEKERNEL->planner;
191 block->millimeters = 0;
192 block->steps[0] = 0;
193 block->steps[1] = 0;
194 block->steps[2] = 0;
195 // feed the block into the system. Will execute it if we are at the beginning of the queue
196 block->ready();
197
198 return block;
199 }
200
201 // Compute extrusion speed based on parameters and gcode distance of travel
202 void Extruder::on_gcode_execute(void* argument){
203 Gcode* gcode = static_cast<Gcode*>(argument);
204
205 // Absolute/relative mode
206 if( gcode->has_m ){
207 if( gcode->m == 17 ){ this->en_pin.set(0); }
208 if( gcode->m == 18 ){ this->en_pin.set(1); }
209 if( gcode->m == 82 ){ this->absolute_mode = true; }
210 if( gcode->m == 83 ){ this->absolute_mode = false; }
211 if( gcode->m == 84 ){ this->en_pin.set(1); }
212 if (gcode->m == 92 ){
213 if (gcode->has_letter('E')){
214 this->steps_per_millimeter = gcode->get_value('E');
215 }
216 }
217 }
218
219 // The mode is OFF by default, and SOLO or FOLLOW only if we need to extrude
220 this->mode = OFF;
221
222 if( gcode->has_g ){
223 // G92: Reset extruder position
224 if( gcode->g == 92 ){
225 gcode->mark_as_taken();
226 if( gcode->has_letter('E') ){
227 this->current_position = gcode->get_value('E');
228 this->target_position = this->current_position;
229 this->unstepped_distance = 0;
230 }else if( gcode->get_num_args() == 0){
231 this->current_position = 0.0;
232 this->target_position = this->current_position;
233 this->unstepped_distance = 0;
234 }
235 }else if ((gcode->g == 0) || (gcode->g == 1)){
236 // Extrusion length from 'G' Gcode
237 if( gcode->has_letter('E' )){
238 // Get relative extrusion distance depending on mode ( in absolute mode we must substract target_position )
239 double extrusion_distance = gcode->get_value('E');
240 double relative_extrusion_distance = extrusion_distance;
241 if (this->absolute_mode)
242 {
243 relative_extrusion_distance -= this->target_position;
244 this->target_position = extrusion_distance;
245 }
246 else
247 {
248 this->target_position += relative_extrusion_distance;
249 }
250
251 // If the robot is moving, we follow it's movement, otherwise, we move alone
252 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
253 this->mode = SOLO;
254 this->travel_distance = relative_extrusion_distance;
255 }else{
256 // We move proportionally to the robot's movement
257 this->mode = FOLLOW;
258 this->travel_ratio = relative_extrusion_distance / gcode->millimeters_of_travel;
259 // TODO: check resulting flowrate, limit robot speed if it exceeds max_speed
260 }
261
262 this->en_pin.set(0);
263 }
264 if (gcode->has_letter('F'))
265 {
266 this->feed_rate = gcode->get_value('F');
267 if (this->feed_rate > (this->max_speed * THEKERNEL->robot->seconds_per_minute))
268 this->feed_rate = this->max_speed * THEKERNEL->robot->seconds_per_minute;
269 feed_rate /= THEKERNEL->robot->seconds_per_minute;
270 }
271 }else if( gcode->g == 90 ){ this->absolute_mode = true;
272 }else if( gcode->g == 91 ){ this->absolute_mode = false;
273 }
274 }
275 }
276
277 // When a new block begins, either follow the robot, or step by ourselves ( or stay back and do nothing )
278 void Extruder::on_block_begin(void* argument){
279 Block* block = static_cast<Block*>(argument);
280
281
282 if( this->mode == SOLO ){
283 // In solo mode we take the block so we can move even if the stepper has nothing to do
284
285 this->current_position += this->travel_distance ;
286
287 int steps_to_step = abs(int(floor(this->steps_per_millimeter * (this->travel_distance +this->unstepped_distance) )));
288
289 if ( this->travel_distance > 0 ){
290 this->unstepped_distance += this->travel_distance -(steps_to_step/this->steps_per_millimeter); //catch any overflow
291 } else {
292 this->unstepped_distance += this->travel_distance +(steps_to_step/this->steps_per_millimeter); //catch any overflow
293 }
294
295 if( steps_to_step != 0 ){
296
297 // We take the block, we have to release it or everything gets stuck
298 block->take();
299 this->current_block = block;
300
301 this->stepper_motor->steps_per_second = 0;
302 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step);
303
304 }else{
305 this->current_block = NULL;
306 }
307
308 }else if( this->mode == FOLLOW ){
309 // In non-solo mode, we just follow the stepper module
310 this->travel_distance = block->millimeters * this->travel_ratio;
311
312 this->current_position += this->travel_distance;
313
314 int steps_to_step = abs(int(floor(this->steps_per_millimeter * (this->travel_distance + this->unstepped_distance) )));
315
316 if ( this->travel_distance > 0 ){
317 this->unstepped_distance += this->travel_distance -(steps_to_step/this->steps_per_millimeter); //catch any overflow
318 } else {
319 this->unstepped_distance += this->travel_distance +(steps_to_step/this->steps_per_millimeter); //catch any overflow
320 }
321
322 if( steps_to_step != 0 ){
323 block->take();
324 this->current_block = block;
325
326 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step );
327 this->on_speed_change(0); // initialise speed in case we get called first
328 }else{
329 this->current_block = NULL;
330 }
331
332 }else if( this->mode == OFF ){
333 // No movement means we must reset our speed
334 this->current_block = NULL;
335 //this->stepper_motor->set_speed(0);
336
337 }
338
339 }
340
341 // When a block ends, pause the stepping interrupt
342 void Extruder::on_block_end(void* argument){
343 this->current_block = NULL;
344 }
345
346 // Called periodically to change the speed to match acceleration or to match the speed of the robot
347 uint32_t Extruder::acceleration_tick(uint32_t dummy){
348
349 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
350 if( this->current_block == NULL || this->paused || this->mode != SOLO ){ return 0; }
351
352 uint32_t current_rate = this->stepper_motor->steps_per_second;
353 uint32_t target_rate = int(floor(this->feed_rate * this->steps_per_millimeter));
354
355 if( current_rate < target_rate ){
356 uint32_t rate_increase = int(floor((this->acceleration/THEKERNEL->stepper->acceleration_ticks_per_second)*this->steps_per_millimeter));
357 current_rate = min( target_rate, current_rate + rate_increase );
358 }
359 if( current_rate > target_rate ){ current_rate = target_rate; }
360
361 // steps per second
362 this->stepper_motor->set_speed(max(current_rate, THEKERNEL->stepper->minimum_steps_per_minute/60));
363
364 return 0;
365 }
366
367 // Speed has been updated for the robot's stepper, we must update accordingly
368 void Extruder::on_speed_change( void* argument ){
369
370 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
371 if( this->current_block == NULL || this->paused || this->mode != FOLLOW || this->stepper_motor->moving != true ){ return; }
372
373 /*
374 * nominal block duration = current block's steps / ( current block's nominal rate / 60 )
375 * nominal extruder rate = extruder steps / nominal block duration
376 * actual extruder rate = nominal extruder rate * ( ( stepper's steps per minute / 60 ) / ( current block's nominal rate / 60 ) )
377 * 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 ) )
378 * or simplified : extruder steps * ( stepper's steps per minute / 60 ) ) / current block's steps
379 * or even : ( stepper steps per minute / 60 ) * ( extruder steps / current block's steps )
380 */
381
382 this->stepper_motor->set_speed( max( ( THEKERNEL->stepper->trapezoid_adjusted_rate /60.0) * ( (double)this->stepper_motor->steps_to_move / (double)this->current_block->steps_event_count ), THEKERNEL->stepper->minimum_steps_per_minute/60.0 ) );
383
384 }
385
386
387
388 // When the stepper has finished it's move
389 uint32_t Extruder::stepper_motor_finished_move(uint32_t dummy){
390
391 //printf("extruder releasing\r\n");
392
393 if (this->current_block){ // this should always be true, but sometimes it isn't. TODO: find out why
394 Block* block = this->current_block;
395 this->current_block = NULL;
396 block->release();
397 }
398 return 0;
399
400 }
401