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