2016-09-13 09:24:55 +00:00
|
|
|
# A pure perl (no C++ implementation) G-code filter, to control the pressure inside the nozzle.
|
|
|
|
|
2014-11-24 17:22:39 +00:00
|
|
|
package Slic3r::GCode::PressureRegulator;
|
|
|
|
use Moo;
|
|
|
|
|
|
|
|
has 'config' => (is => 'ro', required => 1);
|
|
|
|
has 'enable' => (is => 'rw', default => sub { 0 });
|
|
|
|
has 'reader' => (is => 'ro', default => sub { Slic3r::GCode::Reader->new });
|
|
|
|
has '_extrusion_axis' => (is => 'rw', default => sub { "E" });
|
|
|
|
has '_tool' => (is => 'rw', default => sub { 0 });
|
2014-11-24 17:32:18 +00:00
|
|
|
has '_last_print_F' => (is => 'rw', default => sub { 0 });
|
2014-11-24 17:22:39 +00:00
|
|
|
has '_advance' => (is => 'rw', default => sub { 0 }); # extra E injected
|
|
|
|
|
|
|
|
use Slic3r::Geometry qw(epsilon);
|
|
|
|
|
|
|
|
# Acknowledgements:
|
|
|
|
# The advance algorithm was proposed by Matthew Roberts.
|
|
|
|
# The initial work on this Slic3r feature was done by Luís Andrade (lluis)
|
|
|
|
|
|
|
|
sub BUILD {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
$self->reader->apply_print_config($self->config);
|
|
|
|
$self->_extrusion_axis($self->config->get_extrusion_axis);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub process {
|
|
|
|
my $self = shift;
|
2015-01-05 23:35:39 +00:00
|
|
|
my ($gcode, $flush) = @_;
|
2014-11-24 17:22:39 +00:00
|
|
|
|
|
|
|
my $new_gcode = "";
|
|
|
|
|
|
|
|
$self->reader->parse($gcode, sub {
|
|
|
|
my ($reader, $cmd, $args, $info) = @_;
|
|
|
|
|
|
|
|
if ($cmd =~ /^T(\d+)/) {
|
|
|
|
$self->_tool($1);
|
|
|
|
} elsif ($info->{extruding} && $info->{dist_XY} > 0) {
|
|
|
|
# This is a print move.
|
2014-11-24 17:32:18 +00:00
|
|
|
my $F = $args->{F} // $reader->F;
|
2017-01-26 02:27:10 +00:00
|
|
|
if ($F != $self->_last_print_F || ($F == $self->_last_print_F && $self->_advance == 0)) {
|
|
|
|
# We are setting a (potentially) new speed or a discharge event happend since the last speed change, so we calculate the new advance amount.
|
2014-11-24 17:22:39 +00:00
|
|
|
|
|
|
|
# First calculate relative flow rate (mm of filament over mm of travel)
|
|
|
|
my $rel_flow_rate = $info->{dist_E} / $info->{dist_XY};
|
|
|
|
|
|
|
|
# Then calculate absolute flow rate (mm/sec of feedstock)
|
2015-07-02 18:24:16 +00:00
|
|
|
my $flow_rate = $rel_flow_rate * $F / 60;
|
2014-11-24 17:22:39 +00:00
|
|
|
|
|
|
|
# And finally calculate advance by using the user-configured K factor.
|
|
|
|
my $new_advance = $self->config->pressure_advance * ($flow_rate**2);
|
|
|
|
|
|
|
|
if (abs($new_advance - $self->_advance) > 1E-5) {
|
|
|
|
my $new_E = ($self->config->use_relative_e_distances ? 0 : $reader->E) + ($new_advance - $self->_advance);
|
|
|
|
$new_gcode .= sprintf "G1 %s%.5f F%.3f ; pressure advance\n",
|
2015-01-05 23:35:39 +00:00
|
|
|
$self->_extrusion_axis, $new_E, $self->_unretract_speed;
|
2014-11-24 17:22:39 +00:00
|
|
|
$new_gcode .= sprintf "G92 %s%.5f ; restore E\n", $self->_extrusion_axis, $reader->E
|
|
|
|
if !$self->config->use_relative_e_distances;
|
2017-01-26 02:27:10 +00:00
|
|
|
$new_gcode .= sprintf "G1 F%.3f ; restore F\n", $F;
|
2014-11-24 17:22:39 +00:00
|
|
|
$self->_advance($new_advance);
|
|
|
|
}
|
2014-11-24 17:32:18 +00:00
|
|
|
|
|
|
|
$self->_last_print_F($F);
|
2014-11-24 17:22:39 +00:00
|
|
|
}
|
|
|
|
} elsif (($info->{retracting} || $cmd eq 'G10') && $self->_advance != 0) {
|
|
|
|
# We need to bring pressure to zero when retracting.
|
2017-01-26 02:27:10 +00:00
|
|
|
$new_gcode .= $self->_discharge($args->{F}, $args->{F} // $reader->F);
|
2014-11-24 17:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$new_gcode .= "$info->{raw}\n";
|
|
|
|
});
|
|
|
|
|
2015-01-05 23:35:39 +00:00
|
|
|
if ($flush) {
|
|
|
|
$new_gcode .= $self->_discharge;
|
|
|
|
}
|
|
|
|
|
2014-11-24 17:22:39 +00:00
|
|
|
return $new_gcode;
|
|
|
|
}
|
|
|
|
|
2015-01-05 23:35:39 +00:00
|
|
|
sub _discharge {
|
2017-01-26 02:27:10 +00:00
|
|
|
my ($self, $F, $oldSpeed) = @_;
|
2015-01-05 23:35:39 +00:00
|
|
|
|
|
|
|
my $new_E = ($self->config->use_relative_e_distances ? 0 : $self->reader->E) - $self->_advance;
|
|
|
|
my $gcode = sprintf "G1 %s%.5f F%.3f ; pressure discharge\n",
|
|
|
|
$self->_extrusion_axis, $new_E, $F // $self->_unretract_speed;
|
|
|
|
$gcode .= sprintf "G92 %s%.5f ; restore E\n", $self->_extrusion_axis, $self->reader->E
|
|
|
|
if !$self->config->use_relative_e_distances;
|
2017-02-18 00:50:47 +00:00
|
|
|
$gcode .= sprintf "G1 F%.3f ; restore F\n", $oldSpeed
|
|
|
|
if $oldSpeed;
|
2015-01-05 23:35:39 +00:00
|
|
|
$self->_advance(0);
|
|
|
|
|
|
|
|
return $gcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _unretract_speed {
|
2014-11-24 17:22:39 +00:00
|
|
|
my ($self) = @_;
|
|
|
|
return $self->config->get_at('retract_speed', $self->_tool) * 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|