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