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