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;
|
|
|
|
if ($F != $self->_last_print_F) {
|
2014-11-24 17:22:39 +00:00
|
|
|
# We are setting a (potentially) new speed, so we calculate the new advance amount.
|
|
|
|
|
|
|
|
# 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;
|
|
|
|
$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.
|
2015-01-05 23:35:39 +00:00
|
|
|
$new_gcode .= $self->_discharge($args->{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 {
|
|
|
|
my ($self, $F) = @_;
|
|
|
|
|
|
|
|
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;
|
|
|
|
$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;
|