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