timer overflow problem fixed, rare small offsets still remaining
[clinton/Smoothieware.git] / src / modules / tools / extruder / Extruder.cpp
CommitLineData
ca037905 1/*
cd011f58
AW
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.
ca037905 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"
13e4a3f9 10#include "modules/robot/Player.h"
4cff3ded
AW
11#include "modules/robot/Block.h"
12#include "modules/tools/extruder/Extruder.h"
13
ca037905
MM
14#define extruder_step_pin_checksum 40763
15#define extruder_dir_pin_checksum 57277
16#define extruder_en_pin_checksum 8017
17
4464301d
AW
18/* The extruder module controls a filament extruder for 3D printing : http://en.wikipedia.org/wiki/Fused_deposition_modeling
19 * 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 )
20 * or the head moves, and the extruder moves plastic at a speed proportional to the movement of the head ( FOLLOW mode here ).
21*/
22
ca037905 23Extruder::Extruder() {
436a2cd1 24 this->absolute_mode = true;
d9ebc974
AW
25 this->step_counter = 0;
26 this->counter_increment = 0;
81b547a1 27 this->paused = false;
436a2cd1 28}
4cff3ded
AW
29
30void Extruder::on_module_loaded() {
f5598f5b 31
ded56b35 32 // Do not do anything if not enabledd
ca037905 33 if( this->kernel->config->value( extruder_module_enable_checksum )->by_default(false)->as_bool() == false ){ return; }
f5598f5b 34
4cff3ded 35 // Settings
da24d6ae 36 this->on_config_reload(this);
4cff3ded 37
4464301d
AW
38 // We start with the enable pin off
39 this->en_pin->set(1);
5c07c6dc 40
4cff3ded
AW
41 // We work on the same Block as Stepper, so we need to know when it gets a new one and drops one
42 this->register_for_event(ON_BLOCK_BEGIN);
43 this->register_for_event(ON_BLOCK_END);
436a2cd1 44 this->register_for_event(ON_GCODE_EXECUTE);
81b547a1
AW
45 this->register_for_event(ON_PLAY);
46 this->register_for_event(ON_PAUSE);
be8332cd 47 this->register_for_event(ON_SPEED_CHANGE);
ca037905 48
ded56b35 49 // Start values
4cff3ded
AW
50 this->target_position = 0;
51 this->current_position = 0;
4464301d 52 this->current_steps = 0;
4cff3ded 53 this->current_block = NULL;
ded56b35 54 this->mode = OFF;
ca037905 55
ded56b35
AW
56 // Update speed every *acceleration_ticks_per_second*
57 // TODO: Make this an independent setting
d9ebc974 58 this->kernel->slow_ticker->attach( this->kernel->stepper->acceleration_ticks_per_second , this, &Extruder::acceleration_tick );
ded56b35 59
be8332cd
AW
60 // Stepper motor object for the extruder
61 this->stepper_motor = this->kernel->step_ticker->add_stepper_motor( new StepperMotor(this->step_pin,this->dir_pin,this->en_pin) );
62 this->stepper_motor->attach(this, &Extruder::stepper_motor_finished_move );
3b1e82d2 63
4cff3ded
AW
64}
65
2bb8b390 66// Get config
da24d6ae 67void Extruder::on_config_reload(void* argument){
8c309ca9 68 this->microseconds_per_step_pulse = this->kernel->config->value(microseconds_per_step_pulse_checksum)->by_default(5)->as_number();
be8332cd
AW
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();
ca037905 72
5c07c6dc
AW
73 this->step_pin = this->kernel->config->value(extruder_step_pin_checksum )->by_default("nc" )->as_pin()->as_output();
74 this->dir_pin = this->kernel->config->value(extruder_dir_pin_checksum )->by_default("nc" )->as_pin()->as_output();
75 this->en_pin = this->kernel->config->value(extruder_en_pin_checksum )->by_default("nc" )->as_pin()->as_output()->as_open_drain();
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
80void 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
86void Extruder::on_play(void* argument){
87 this->paused = false;
83ecfc46 88 this->stepper_motor->unpause();
81b547a1
AW
89}
90
91
92
ded56b35 93// Compute extrusion speed based on parameters and gcode distance of travel
436a2cd1
AW
94void Extruder::on_gcode_execute(void* argument){
95 Gcode* gcode = static_cast<Gcode*>(argument);
ca037905 96
436a2cd1
AW
97 // Absolute/relative mode
98 if( gcode->has_letter('M')){
ca037905 99 int code = (int) gcode->get_value('M');
abc7d730
MM
100 if( code == 82 ){ this->absolute_mode = true; }
101 if( code == 83 ){ this->absolute_mode = false; }
5c07c6dc 102 if( code == 84 ){ this->en_pin->set(1); }
ca037905
MM
103 }
104
105 // The mode is OFF by default, and SOLO or FOLLOW only if we need to extrude
ded56b35 106 this->mode = OFF;
ca037905 107
1a2d88eb 108 if( gcode->has_letter('G') ){
ded56b35 109 // G92: Reset extruder position
1a2d88eb 110 if( gcode->get_value('G') == 92 ){
1a2d88eb 111 if( gcode->has_letter('E') ){
e2b4a32b 112 this->current_position = gcode->get_value('E');
1a2d88eb 113 this->target_position = this->current_position;
4464301d 114 this->current_steps = int(floor(this->steps_per_millimeter * this->current_position));
ca037905 115 }
436a2cd1 116 }else{
ca037905 117 // Extrusion length from 'G' Gcode
1a2d88eb 118 if( gcode->has_letter('E' )){
ca037905 119 // Get relative extrusion distance depending on mode ( in absolute mode we must substract target_position )
ded56b35
AW
120 double relative_extrusion_distance = gcode->get_value('E');
121 if( this->absolute_mode == true ){ relative_extrusion_distance = relative_extrusion_distance - this->target_position; }
ca037905 122
ded56b35 123 // If the robot is moving, we follow it's movement, otherwise, we move alone
4464301d 124 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
125 this->mode = SOLO;
126 this->travel_distance = relative_extrusion_distance;
127 if( gcode->has_letter('F') ){ this->feed_rate = gcode->get_value('F'); }
1a2d88eb 128 }else{
ca037905 129 // We move proportionally to the robot's movement
4464301d 130 this->mode = FOLLOW;
ca037905
MM
131 this->travel_ratio = relative_extrusion_distance / gcode->millimeters_of_travel;
132 }
133
5c07c6dc 134 this->en_pin->set(0);
ca037905 135 }
1a2d88eb 136 }
ca037905
MM
137 }
138
da24d6ae
AW
139}
140
ded56b35 141// When a new block begins, either follow the robot, or step by ourselves ( or stay back and do nothing )
4cff3ded
AW
142void Extruder::on_block_begin(void* argument){
143 Block* block = static_cast<Block*>(argument);
4464301d
AW
144
145
ded56b35
AW
146 if( this->mode == SOLO ){
147 // In solo mode we take the block so we can move even if the stepper has nothing to do
be8332cd 148
be8332cd 149 this->target_position = this->current_position + this->travel_distance ;
4464301d
AW
150
151 //int32_t steps_to_step = abs( int( floor(this->steps_per_millimeter*this->target_position) - floor(this->steps_per_millimeter*this->current_position) ) );
152
153 int old_steps = this->current_steps;
154 int target_steps = int( floor(this->steps_per_millimeter*this->target_position) );
155 int steps_to_step = abs( target_steps - old_steps );
156 this->current_steps = target_steps;
157
158 if( steps_to_step != 0 ){
159
160 // We take the block, we have to release it or everything gets stuck
161 block->take();
162 this->current_block = block;
be8332cd 163
4464301d
AW
164 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step);
165
166 }
167
be8332cd 168
ded56b35 169 }else if( this->mode == FOLLOW ){
1a2d88eb 170 // In non-solo mode, we just follow the stepper module
be8332cd 171
ca037905 172 this->current_block = block;
be8332cd 173 this->target_position = this->current_position + ( this->current_block->millimeters * this->travel_ratio );
4464301d
AW
174
175 //int32_t steps_to_step = abs( int( floor(this->steps_per_millimeter*this->target_position) - floor(this->steps_per_millimeter*this->current_position) ) );
176
177 int old_steps = this->current_steps;
178 int target_steps = int( floor(this->steps_per_millimeter*this->target_position) );
83ecfc46 179 int steps_to_step = target_steps - old_steps ;
4464301d
AW
180 this->current_steps = target_steps;
181
182
183 if( steps_to_step != 0 ){
184
185 //printf("taken for extruder: %u \r\n", steps_to_step);
186
187 block->take();
188
189 //printf("spm:%f td:%f steps:%d ( %f - %f ) \r\n", this->steps_per_millimeter, this->travel_distance, steps_to_step, this->target_position, this->current_position );
190
83ecfc46 191 this->stepper_motor->move( ( steps_to_step > 0 ), abs(steps_to_step) );
4464301d
AW
192
193
194
195 }
be8332cd
AW
196
197 }else if( this->mode == OFF ){
198 // No movement means we must reset our speed
199
4464301d 200 //this->stepper_motor->set_speed(0);
be8332cd 201
ca037905 202 }
e2b4a32b 203
4cff3ded
AW
204}
205
ded56b35 206// When a block ends, pause the stepping interrupt
4cff3ded 207void Extruder::on_block_end(void* argument){
4464301d
AW
208
209 //printf("Block end\r\n");
210
4cff3ded 211 Block* block = static_cast<Block*>(argument);
ca037905
MM
212 this->current_block = NULL;
213}
4cff3ded 214
ded56b35 215// Called periodically to change the speed to match acceleration or to match the speed of the robot
8b8b3339 216uint32_t Extruder::acceleration_tick(uint32_t dummy){
1a2d88eb 217
ca037905 218 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
be8332cd
AW
219 if( this->current_block == NULL || this->paused || this->mode != SOLO ){ return 0; }
220
221 uint32_t current_rate = this->stepper_motor->steps_per_second;
222 uint32_t target_rate = int(floor((this->feed_rate/60)*this->steps_per_millimeter));
4464301d 223
4464301d
AW
224 if( current_rate < target_rate ){
225 uint32_t rate_increase = int(floor((this->acceleration/this->kernel->stepper->acceleration_ticks_per_second)*this->steps_per_millimeter));
226 current_rate = min( target_rate, current_rate + rate_increase );
0eb11a06 227 }
4464301d 228 if( current_rate > target_rate ){ current_rate = target_rate; }
1a2d88eb 229
83ecfc46 230 this->stepper_motor->set_speed(max(current_rate, this->kernel->stepper->minimum_steps_per_minute/60));
4464301d 231
be8332cd 232 return 0;
ded56b35 233}
1a2d88eb 234
be8332cd
AW
235// Speed has been updated for the robot's stepper, we must update accordingly
236void Extruder::on_speed_change( void* argument ){
ca037905 237
be8332cd 238 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
4464301d
AW
239 if( this->current_block == NULL || this->paused || this->mode != FOLLOW || this->stepper_motor->moving != true ){ return; }
240
241 /*
242 * nominal block duration = current block's steps / ( current block's nominal rate / 60 )
243 * nominal extruder rate = extruder steps / nominal block duration
244 * actual extruder rate = nominal extruder rate * ( ( stepper's steps per minute / 60 ) / ( current block's nominal rate / 60 ) )
245 * 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 ) )
246 * or simplified : ( extruder steps * ( stepper's steps per minute / 60 ) ) / current block's steps
247 * or even : ( stepper steps per minute / 60 ) * ( extruder steps / current block's steps )
248 */
83ecfc46
AW
249
250 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 251
4cff3ded
AW
252}
253
4cff3ded
AW
254
255
be8332cd
AW
256// When the stepper has finished it's move
257uint32_t Extruder::stepper_motor_finished_move(uint32_t dummy){
4cff3ded 258
4464301d
AW
259 //printf("extruder releasing\r\n");
260
be8332cd 261 this->current_position = this->target_position;
feb204be 262
be8332cd 263 this->current_block->release();
feb204be 264
be8332cd 265}
feb204be 266