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