remove copies of steppermotor pointers in modules and use the ones in robot
[clinton/Smoothieware.git] / src / modules / tools / extruder / Extruder.cpp
CommitLineData
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
5673fe39
MM
8#include "Extruder.h"
9
4cff3ded
AW
10#include "libs/Module.h"
11#include "libs/Kernel.h"
5673fe39 12
3fceb8eb 13#include "modules/robot/Conveyor.h"
4cff3ded 14#include "modules/robot/Block.h"
5673fe39 15#include "StepperMotor.h"
61134a65
JM
16#include "SlowTicker.h"
17#include "Stepper.h"
18#include "StepTicker.h"
19#include "Config.h"
20#include "StepperMotor.h"
21#include "Robot.h"
7af0714f 22#include "checksumm.h"
8d54c34c 23#include "ConfigValue.h"
66383b80 24#include "Gcode.h"
fc7b9a7b 25#include "libs/StreamOutput.h"
d87968be 26#include "PublicDataRequest.h"
5673fe39 27
5dcb2ff3 28#include <mri.h>
4cff3ded 29
43424972
JM
30#define extruder_module_enable_checksum CHECKSUM("extruder_module_enable")
31#define extruder_steps_per_mm_checksum CHECKSUM("extruder_steps_per_mm")
74079661 32#define extruder_filament_diameter_checksum CHECKSUM("extruder_filament_diameter")
43424972
JM
33#define extruder_acceleration_checksum CHECKSUM("extruder_acceleration")
34#define extruder_step_pin_checksum CHECKSUM("extruder_step_pin")
35#define extruder_dir_pin_checksum CHECKSUM("extruder_dir_pin")
36#define extruder_en_pin_checksum CHECKSUM("extruder_en_pin")
37#define extruder_max_speed_checksum CHECKSUM("extruder_max_speed")
38
39#define extruder_checksum CHECKSUM("extruder")
40
41#define default_feed_rate_checksum CHECKSUM("default_feed_rate")
42#define steps_per_mm_checksum CHECKSUM("steps_per_mm")
74079661 43#define filament_diameter_checksum CHECKSUM("filament_diameter")
43424972
JM
44#define acceleration_checksum CHECKSUM("acceleration")
45#define step_pin_checksum CHECKSUM("step_pin")
46#define dir_pin_checksum CHECKSUM("dir_pin")
47#define en_pin_checksum CHECKSUM("en_pin")
48#define max_speed_checksum CHECKSUM("max_speed")
8adf2390
L
49#define x_offset_checksum CHECKSUM("x_offset")
50#define y_offset_checksum CHECKSUM("y_offset")
51#define z_offset_checksum CHECKSUM("z_offset")
52
53#define X_AXIS 0
54#define Y_AXIS 1
55#define Z_AXIS 2
43424972 56
17c89e4d
JM
57#define OFF 0
58#define SOLO 1
59#define FOLLOW 2
60
8a3ae3d0 61#define PI 3.14159265358979F
74079661 62
41fd89e0
JM
63#define max(a,b) (((a) > (b)) ? (a) : (b))
64
58baeec1
MM
65/* The extruder module controls a filament extruder for 3D printing: http://en.wikipedia.org/wiki/Fused_deposition_modeling
66* 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 )
67* or the head moves, and the extruder moves plastic at a speed proportional to the movement of the head ( FOLLOW mode here ).
4464301d
AW
68*/
69
17c89e4d
JM
70Extruder::Extruder( uint16_t config_identifier, bool single )
71{
436a2cd1 72 this->absolute_mode = true;
540c8365 73 this->enabled = false;
14ecdbd7 74 this->paused = false;
17c89e4d 75 this->single_config = single;
14ecdbd7 76 this->identifier = config_identifier;
8adf2390 77
48a10598 78 memset(this->offset, 0, sizeof(this->offset));
436a2cd1 79}
4cff3ded 80
17c89e4d
JM
81void Extruder::on_module_loaded()
82{
f5598f5b 83
4cff3ded 84 // Settings
da24d6ae 85 this->on_config_reload(this);
4cff3ded
AW
86
87 // We work on the same Block as Stepper, so we need to know when it gets a new one and drops one
476dcb96 88 register_for_event(ON_CONFIG_RELOAD);
4cff3ded
AW
89 this->register_for_event(ON_BLOCK_BEGIN);
90 this->register_for_event(ON_BLOCK_END);
6989211c 91 this->register_for_event(ON_GCODE_RECEIVED);
436a2cd1 92 this->register_for_event(ON_GCODE_EXECUTE);
81b547a1
AW
93 this->register_for_event(ON_PLAY);
94 this->register_for_event(ON_PAUSE);
be8332cd 95 this->register_for_event(ON_SPEED_CHANGE);
d87968be 96 this->register_for_event(ON_GET_PUBLIC_DATA);
ca037905 97
ded56b35 98 // Start values
4cff3ded
AW
99 this->target_position = 0;
100 this->current_position = 0;
150bce10 101 this->unstepped_distance = 0;
4cff3ded 102 this->current_block = NULL;
ded56b35 103 this->mode = OFF;
ca037905 104
ded56b35 105 // Update speed every *acceleration_ticks_per_second*
7b49793d 106 // TODO: Make this an independent setting
38bf9a1c 107 THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->get_acceleration_ticks_per_second() , this, &Extruder::acceleration_tick );
ded56b35 108
be8332cd 109 // Stepper motor object for the extruder
9c5fa39a 110 this->stepper_motor = THEKERNEL->step_ticker->add_stepper_motor( new StepperMotor(step_pin, dir_pin, en_pin) );
58baeec1 111 this->stepper_motor->attach(this, &Extruder::stepper_motor_finished_move );
3b1e82d2 112
4cff3ded
AW
113}
114
2bb8b390 115// Get config
17c89e4d
JM
116void Extruder::on_config_reload(void *argument)
117{
17c89e4d
JM
118 if( this->single_config ) {
119 // If this module uses the old "single extruder" configuration style
14ecdbd7 120
f39a2ecf 121 this->steps_per_millimeter_setting = THEKERNEL->config->value(extruder_steps_per_mm_checksum )->by_default(1)->as_number();
112ad2fc 122 this->filament_diameter = THEKERNEL->config->value(extruder_filament_diameter_checksum )->by_default(0)->as_number();
314ab8f7
MM
123 this->acceleration = THEKERNEL->config->value(extruder_acceleration_checksum )->by_default(1000)->as_number();
124 this->max_speed = THEKERNEL->config->value(extruder_max_speed_checksum )->by_default(1000)->as_number();
125 this->feed_rate = THEKERNEL->config->value(default_feed_rate_checksum )->by_default(1000)->as_number();
14ecdbd7 126
314ab8f7
MM
127 this->step_pin.from_string( THEKERNEL->config->value(extruder_step_pin_checksum )->by_default("nc" )->as_string())->as_output();
128 this->dir_pin.from_string( THEKERNEL->config->value(extruder_dir_pin_checksum )->by_default("nc" )->as_string())->as_output();
129 this->en_pin.from_string( THEKERNEL->config->value(extruder_en_pin_checksum )->by_default("nc" )->as_string())->as_output();
14ecdbd7 130
17c89e4d 131 for(int i = 0; i < 3; i++) {
8adf2390
L
132 this->offset[i] = 0;
133 }
134
540c8365
L
135 this->enabled = true;
136
17c89e4d
JM
137 } else {
138 // If this module was created with the new multi extruder configuration style
14ecdbd7 139
f39a2ecf 140 this->steps_per_millimeter_setting = THEKERNEL->config->value(extruder_checksum, this->identifier, steps_per_mm_checksum )->by_default(1)->as_number();
112ad2fc 141 this->filament_diameter = THEKERNEL->config->value(extruder_checksum, this->identifier, filament_diameter_checksum )->by_default(0)->as_number();
17c89e4d
JM
142 this->acceleration = THEKERNEL->config->value(extruder_checksum, this->identifier, acceleration_checksum )->by_default(1000)->as_number();
143 this->max_speed = THEKERNEL->config->value(extruder_checksum, this->identifier, max_speed_checksum )->by_default(1000)->as_number();
144 this->feed_rate = THEKERNEL->config->value( default_feed_rate_checksum )->by_default(1000)->as_number();
14ecdbd7 145
17c89e4d
JM
146 this->step_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, step_pin_checksum )->by_default("nc" )->as_string())->as_output();
147 this->dir_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, dir_pin_checksum )->by_default("nc" )->as_string())->as_output();
148 this->en_pin.from_string( THEKERNEL->config->value(extruder_checksum, this->identifier, en_pin_checksum )->by_default("nc" )->as_string())->as_output();
8adf2390 149
17c89e4d
JM
150 this->offset[X_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, x_offset_checksum )->by_default(0)->as_number();
151 this->offset[Y_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, y_offset_checksum )->by_default(0)->as_number();
152 this->offset[Z_AXIS] = THEKERNEL->config->value(extruder_checksum, this->identifier, z_offset_checksum )->by_default(0)->as_number();
14ecdbd7
AW
153 }
154
112ad2fc 155 this->update_steps_per_millimeter();
436a2cd1
AW
156}
157
d87968be
JM
158void Extruder::on_get_public_data(void* argument){
159 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
160
161 if(!pdr->starts_with(extruder_checksum)) return;
162
163 if(this->enabled) {
8a3ae3d0
JM
164 // Note this is allowing both step/mm and filament diameter to be exposed via public data
165 pdr->set_data_ptr(&this->steps_per_millimeter_setting);
d87968be
JM
166 pdr->set_taken();
167 }
168}
81b547a1
AW
169
170// When the play/pause button is set to pause, or a module calls the ON_PAUSE event
17c89e4d
JM
171void Extruder::on_pause(void *argument)
172{
81b547a1 173 this->paused = true;
83ecfc46 174 this->stepper_motor->pause();
81b547a1
AW
175}
176
177// When the play/pause button is set to play, or a module calls the ON_PLAY event
17c89e4d
JM
178void Extruder::on_play(void *argument)
179{
81b547a1 180 this->paused = false;
83ecfc46 181 this->stepper_motor->unpause();
81b547a1
AW
182}
183
17c89e4d
JM
184void Extruder::on_gcode_received(void *argument)
185{
186 Gcode *gcode = static_cast<Gcode *>(argument);
5dcb2ff3
AW
187
188 // Gcodes to execute immediately
17c89e4d
JM
189 if (gcode->has_m) {
190 if (gcode->m == 114 && this->enabled) {
4d9f562f 191 char buf[16];
17c89e4d 192 int n = snprintf(buf, sizeof(buf), " E:%1.3f ", this->current_position);
4d9f562f 193 gcode->txt_after_ok.append(buf, n);
74b6303c 194 gcode->mark_as_taken();
33e4cc02 195
17c89e4d 196 } else if (gcode->m == 92 && ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier) ) ) {
1ad23cd3 197 float spm = this->steps_per_millimeter;
6b451be8 198 if (gcode->has_letter('E')) {
7369629d 199 spm = gcode->get_value('E');
112ad2fc
L
200 this->steps_per_millimeter_setting = spm;
201 this->update_steps_per_millimeter();
4cba6755
JM
202 }
203
7369629d
MM
204 gcode->stream->printf("E:%g ", spm);
205 gcode->add_nl = true;
74b6303c 206 gcode->mark_as_taken();
33e4cc02 207
f39a2ecf
L
208 } else if (gcode->m == 200 && ( (this->enabled && !gcode->has_letter('P')) || (gcode->has_letter('P') && gcode->get_value('P') == this->identifier)) ) {
209 if (gcode->has_letter('D')) {
112ad2fc
L
210 this->filament_diameter = gcode->get_value('D');
211 this->update_steps_per_millimeter();
f39a2ecf 212 }
112ad2fc 213 gcode->mark_as_taken();
8a3ae3d0 214
17c89e4d 215 } else if (gcode->m == 500 || gcode->m == 503) { // M500 saves some volatile settings to config override file, M503 just prints the settings
4710532a 216 if( this->single_config ) {
8a3ae3d0 217 gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f\n", this->steps_per_millimeter_setting);
112ad2fc 218 gcode->stream->printf(";E Filament diameter:\nM200 D%1.4f\n", this->filament_diameter);
4710532a 219 } else {
8a3ae3d0 220 gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f P%d\n", this->steps_per_millimeter_setting, this->identifier);
112ad2fc 221 gcode->stream->printf(";E Filament diameter:\nM200 D%1.4f P%d\n", this->filament_diameter, this->identifier);
17c89e4d 222 }
33e4cc02 223 gcode->mark_as_taken();
f2f0dfed 224 return;
7369629d 225 }
6989211c 226 }
8519d744 227
5dcb2ff3 228 // Gcodes to pass along to on_gcode_execute
4cba6755 229 if( ( gcode->has_m && (gcode->m == 17 || gcode->m == 18 || gcode->m == 82 || gcode->m == 83 || gcode->m == 84 ) ) || ( gcode->has_g && gcode->g == 92 && gcode->has_letter('E') ) || ( gcode->has_g && ( gcode->g == 90 || gcode->g == 91 ) ) ) {
e0ee24ed 230 THEKERNEL->conveyor->append_gcode(gcode);
3c4f2dd8
AW
231 }
232
233 // Add to the queue for on_gcode_execute to process
17c89e4d
JM
234 if( gcode->has_g && gcode->g < 4 && gcode->has_letter('E') && this->enabled ) {
235 if( !gcode->has_letter('X') && !gcode->has_letter('Y') && !gcode->has_letter('Z') ) {
e0ee24ed 236 THEKERNEL->conveyor->append_gcode(gcode);
518e225f 237 // This is a solo move, we add an empty block to the queue to prevent subsequent gcodes being executed at the same time
2134bcf2 238 THEKERNEL->conveyor->queue_head_block();
3c4f2dd8 239 }
17c89e4d 240 } else {
d149c730
AW
241 // This is for follow move
242
3c4f2dd8
AW
243 }
244}
245
ded56b35 246// Compute extrusion speed based on parameters and gcode distance of travel
17c89e4d
JM
247void Extruder::on_gcode_execute(void *argument)
248{
249 Gcode *gcode = static_cast<Gcode *>(argument);
ca037905 250
436a2cd1 251 // Absolute/relative mode
17c89e4d
JM
252 if( gcode->has_m ) {
253 if( gcode->m == 17 ) {
254 this->en_pin.set(0);
255 }
256 if( gcode->m == 18 ) {
257 this->en_pin.set(1);
258 }
259 if( gcode->m == 82 ) {
260 this->absolute_mode = true;
261 }
262 if( gcode->m == 83 ) {
263 this->absolute_mode = false;
264 }
265 if( gcode->m == 84 ) {
266 this->en_pin.set(1);
267 }
ca037905
MM
268 }
269
270 // The mode is OFF by default, and SOLO or FOLLOW only if we need to extrude
ded56b35 271 this->mode = OFF;
ca037905 272
17c89e4d 273 if( gcode->has_g ) {
7b49793d 274 // G92: Reset extruder position
17c89e4d 275 if( gcode->g == 92 && this->enabled ) {
74b6303c 276 gcode->mark_as_taken();
17c89e4d 277 if( gcode->has_letter('E') ) {
e2b4a32b 278 this->current_position = gcode->get_value('E');
1a2d88eb 279 this->target_position = this->current_position;
150bce10 280 this->unstepped_distance = 0;
17c89e4d 281 } else if( gcode->get_num_args() == 0) {
b2aa3a55
L
282 this->current_position = 0.0;
283 this->target_position = this->current_position;
150bce10 284 this->unstepped_distance = 0;
ca037905 285 }
6b451be8 286
17c89e4d 287 } else if (((gcode->g == 0) || (gcode->g == 1)) && this->enabled) {
ca037905 288 // Extrusion length from 'G' Gcode
17c89e4d 289 if( gcode->has_letter('E' )) {
ca037905 290 // Get relative extrusion distance depending on mode ( in absolute mode we must substract target_position )
1ad23cd3
MM
291 float extrusion_distance = gcode->get_value('E');
292 float relative_extrusion_distance = extrusion_distance;
17c89e4d 293 if (this->absolute_mode) {
f8dc0043
MM
294 relative_extrusion_distance -= this->target_position;
295 this->target_position = extrusion_distance;
17c89e4d 296 } else {
f8dc0043
MM
297 this->target_position += relative_extrusion_distance;
298 }
ca037905 299
ded56b35 300 // If the robot is moving, we follow it's movement, otherwise, we move alone
17c89e4d 301 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
302 this->mode = SOLO;
303 this->travel_distance = relative_extrusion_distance;
17c89e4d 304 } else {
ca037905 305 // We move proportionally to the robot's movement
4464301d 306 this->mode = FOLLOW;
ca037905 307 this->travel_ratio = relative_extrusion_distance / gcode->millimeters_of_travel;
7dab41f3 308 // TODO: check resulting flowrate, limit robot speed if it exceeds max_speed
ca037905
MM
309 }
310
62bd4cfa 311 this->en_pin.set(0);
ca037905 312 }
6b451be8
JM
313
314 if (gcode->has_letter('F')) {
dd0a7cfa 315 feed_rate = gcode->get_value('F') / THEKERNEL->robot->get_seconds_per_minute();
6b451be8
JM
316 if (feed_rate > max_speed)
317 feed_rate = max_speed;
318 }
319
17c89e4d
JM
320 } else if( gcode->g == 90 ) {
321 this->absolute_mode = true;
6b451be8 322
17c89e4d
JM
323 } else if( gcode->g == 91 ) {
324 this->absolute_mode = false;
1a2d88eb 325 }
ca037905 326 }
8adf2390 327
da24d6ae
AW
328}
329
ded56b35 330// When a new block begins, either follow the robot, or step by ourselves ( or stay back and do nothing )
17c89e4d
JM
331void Extruder::on_block_begin(void *argument)
332{
c974b296 333 if(!this->enabled) return;
17c89e4d 334 Block *block = static_cast<Block *>(argument);
4464301d
AW
335
336
17c89e4d 337 if( this->mode == SOLO ) {
ded56b35 338 // In solo mode we take the block so we can move even if the stepper has nothing to do
58baeec1 339
99826186 340 this->current_position += this->travel_distance ;
58baeec1 341
0ebf8cd2 342 int steps_to_step = abs(int(floor(this->steps_per_millimeter_setting * (this->travel_distance + this->unstepped_distance) )));
d0c14c30 343
17c89e4d 344 if ( this->travel_distance > 0 ) {
0ebf8cd2 345 this->unstepped_distance += this->travel_distance - (steps_to_step / this->steps_per_millimeter_setting); //catch any overflow
150bce10 346 } else {
0ebf8cd2 347 this->unstepped_distance += this->travel_distance + (steps_to_step / this->steps_per_millimeter_setting); //catch any overflow
150bce10 348 }
4464301d 349
17c89e4d 350 if( steps_to_step != 0 ) {
58baeec1 351
4464301d
AW
352 // We take the block, we have to release it or everything gets stuck
353 block->take();
354 this->current_block = block;
58baeec1 355
2fa50ca0 356 this->stepper_motor->set_steps_per_second(0);
58baeec1 357 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step);
4464301d 358
17c89e4d 359 } else {
d149c730 360 this->current_block = NULL;
4464301d
AW
361 }
362
17c89e4d 363 } else if( this->mode == FOLLOW ) {
1a2d88eb 364 // In non-solo mode, we just follow the stepper module
b5a9a6c4 365 this->travel_distance = block->millimeters * this->travel_ratio;
99826186
MM
366
367 this->current_position += this->travel_distance;
368
150bce10
MM
369 int steps_to_step = abs(int(floor(this->steps_per_millimeter * (this->travel_distance + this->unstepped_distance) )));
370
17c89e4d
JM
371 if ( this->travel_distance > 0 ) {
372 this->unstepped_distance += this->travel_distance - (steps_to_step / this->steps_per_millimeter); //catch any overflow
150bce10 373 } else {
17c89e4d 374 this->unstepped_distance += this->travel_distance + (steps_to_step / this->steps_per_millimeter); //catch any overflow
150bce10 375 }
99826186 376
17c89e4d 377 if( steps_to_step != 0 ) {
4464301d 378 block->take();
b5a9a6c4
MM
379 this->current_block = block;
380
381 this->stepper_motor->move( ( this->travel_distance > 0 ), steps_to_step );
d0c14c30 382 this->on_speed_change(0); // initialise speed in case we get called first
17c89e4d 383 } else {
d149c730 384 this->current_block = NULL;
4464301d 385 }
be8332cd 386
17c89e4d 387 } else if( this->mode == OFF ) {
be8332cd 388 // No movement means we must reset our speed
d149c730 389 this->current_block = NULL;
4464301d 390 //this->stepper_motor->set_speed(0);
58baeec1 391
ca037905 392 }
e2b4a32b 393
4cff3ded
AW
394}
395
ded56b35 396// When a block ends, pause the stepping interrupt
17c89e4d
JM
397void Extruder::on_block_end(void *argument)
398{
c974b296 399 if(!this->enabled) return;
ca037905
MM
400 this->current_block = NULL;
401}
4cff3ded 402
ded56b35 403// Called periodically to change the speed to match acceleration or to match the speed of the robot
17c89e4d
JM
404uint32_t Extruder::acceleration_tick(uint32_t dummy)
405{
c974b296 406 if(!this->enabled) return 0;
1a2d88eb 407
ca037905 408 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
17c89e4d
JM
409 if( this->current_block == NULL || this->paused || this->mode != SOLO ) {
410 return 0;
411 }
be8332cd 412
2fa50ca0 413 uint32_t current_rate = this->stepper_motor->get_steps_per_second();
0ebf8cd2 414 uint32_t target_rate = int(floor(this->feed_rate * this->steps_per_millimeter_setting)); // NOTE we use real steps here not the volumetric ones
58baeec1 415
17c89e4d 416 if( current_rate < target_rate ) {
0ebf8cd2 417 uint32_t rate_increase = int(floor((this->acceleration / THEKERNEL->stepper->get_acceleration_ticks_per_second()) * this->steps_per_millimeter_setting));
4464301d 418 current_rate = min( target_rate, current_rate + rate_increase );
0eb11a06 419 }
17c89e4d
JM
420 if( current_rate > target_rate ) {
421 current_rate = target_rate;
422 }
1a2d88eb 423
8519d744 424 // steps per second
38bf9a1c 425 this->stepper_motor->set_speed(max(current_rate, THEKERNEL->stepper->get_minimum_steps_per_second()));
58baeec1 426
be8332cd 427 return 0;
ded56b35 428}
1a2d88eb 429
be8332cd 430// Speed has been updated for the robot's stepper, we must update accordingly
17c89e4d
JM
431void Extruder::on_speed_change( void *argument )
432{
c974b296 433 if(!this->enabled) return;
ca037905 434
be8332cd 435 // Avoid trying to work when we really shouldn't ( between blocks or re-entry )
2fa50ca0 436 if( this->current_block == NULL || this->paused || this->mode != FOLLOW || this->stepper_motor->is_moving() != true ) {
17c89e4d
JM
437 return;
438 }
4464301d
AW
439
440 /*
da947c62 441 * nominal block duration = current block's steps / ( current block's nominal rate )
58baeec1 442 * nominal extruder rate = extruder steps / nominal block duration
0ac1713f 443 * actual extruder rate = nominal extruder rate * ( ( stepper's steps per second ) / ( current block's nominal rate ) )
da947c62
MM
444 * or actual extruder rate = ( ( extruder steps * ( current block's nominal_rate ) ) / current block's steps ) * ( ( stepper's steps per second ) / ( current block's nominal rate ) )
445 * or simplified : extruder steps * ( stepper's steps per second ) ) / current block's steps
446 * or even : ( stepper steps per second ) * ( extruder steps / current block's steps )
4464301d 447 */
58baeec1 448
2fa50ca0 449 this->stepper_motor->set_speed( max( ( THEKERNEL->stepper->get_trapezoid_adjusted_rate()) * ( (float)this->stepper_motor->get_steps_to_move() / (float)this->current_block->steps_event_count ), THEKERNEL->stepper->get_minimum_steps_per_second() ) );
ca037905 450
4cff3ded
AW
451}
452
be8332cd 453// When the stepper has finished it's move
17c89e4d
JM
454uint32_t Extruder::stepper_motor_finished_move(uint32_t dummy)
455{
c974b296 456 if(!this->enabled) return 0;
4cff3ded 457
4464301d
AW
458 //printf("extruder releasing\r\n");
459
17c89e4d
JM
460 if (this->current_block) { // this should always be true, but sometimes it isn't. TODO: find out why
461 Block *block = this->current_block;
d149c730
AW
462 this->current_block = NULL;
463 block->release();
8519d744 464 }
f2203544 465 return 0;
feb204be 466
be8332cd 467}
feb204be 468
112ad2fc 469void Extruder::update_steps_per_millimeter() {
c31b23e7 470 if(this->filament_diameter > 0.01) {
112ad2fc
L
471 this->steps_per_millimeter = this->steps_per_millimeter_setting / (powf(this->filament_diameter / 2, 2) * PI);
472 } else {
473 this->steps_per_millimeter = this->steps_per_millimeter_setting;
474 }
475}
476