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