Refactor E code and keep track of per-extruder consumend filament

This commit is contained in:
Alessandro Ranellucci 2013-08-28 20:13:18 +02:00
parent 70d824c9a7
commit c9f68ed28a
4 changed files with 29 additions and 29 deletions

View File

@ -12,9 +12,11 @@ use constant OPTIONS => [qw(
has 'id' => (is => 'rw', required => 1);
has $_ => (is => 'ro', required => 1) for @{&OPTIONS};
has 'config'=> (is => 'ro', required => 1);
has 'bridge_flow' => (is => 'lazy');
has 'e' => (is => 'rw', default => sub {0} );
has 'E' => (is => 'rw', default => sub {0} );
has 'absolute_E' => (is => 'rw', default => sub {0} );
has 'retracted' => (is => 'rw', default => sub {0} );
has 'restart_extra' => (is => 'rw', default => sub {0} );
has 'e_per_mm3' => (is => 'lazy');
@ -44,6 +46,19 @@ sub _build_scaled_wipe_distance {
return scale($self->retract_length / $self->retract_speed * $Slic3r::Config->travel_speed * 0.8);
}
sub extrude {
my ($self, $E) = @_;
$self->E(0) if $self->config->use_relative_e_distances;
$self->absolute_E($self->absolute_E + $E);
return $self->E($self->E + $E);
}
sub extruded_volume {
my ($self) = @_;
return $self->absolute_E * ($self->filament_diameter**2) * PI/4;
}
sub make_flow {
my $self = shift;
return Slic3r::Flow->new(nozzle_diameter => $self->nozzle_diameter, @_);

View File

@ -29,7 +29,6 @@ has 'new_object' => (is => 'rw', default => sub {0});
has 'straight_once' => (is => 'rw', default => sub {1});
has 'extruder' => (is => 'rw');
has 'elapsed_time' => (is => 'rw', default => sub {0} ); # seconds
has 'total_extrusion_length' => (is => 'rw', default => sub {0} );
has 'lifted' => (is => 'rw', default => sub {0} );
has 'last_pos' => (is => 'rw', default => sub { Slic3r::Point->new(0,0) } );
has 'last_speed' => (is => 'rw', default => sub {""});
@ -328,12 +327,7 @@ sub extrude_path {
# calculate extrusion length for this line
my $E = 0;
if ($e) {
$E = $e * $line_length;
$self->extruder->e(0) if $self->config->use_relative_e_distances;
$self->total_extrusion_length($self->total_extrusion_length + $E);
$E = $self->extruder->e($self->extruder->e + $E);
}
$E = $self->extruder->extrude($e * $line_length) if $e;
# compose G-code line
$gcode .= sprintf "G1 X%.3f Y%.3f",
@ -554,11 +548,9 @@ sub unretract {
if ($to_unretract) {
$self->speed('retract');
if ($self->config->extrusion_axis) {
$self->extruder->e(0) if $self->config->use_relative_e_distances;
$self->total_extrusion_length($self->total_extrusion_length + $to_unretract);
# use G1 instead of G0 because G0 will blend the restart with the previous travel move
$gcode .= sprintf "G1 E%.5f F%.3f",
$self->extruder->e($self->extruder->e + $to_unretract),
$self->extruder->extrude($to_unretract),
$self->extruder->retract_speed_mm_min;
$gcode .= " ; compensate retraction" if $self->config->gcode_comments;
$gcode .= "\n";
@ -574,7 +566,7 @@ sub reset_e {
my ($self) = @_;
return "" if $self->config->gcode_flavor =~ /^(?:mach3|makerware|sailfish)$/;
$self->extruder->e(0) if $self->extruder;
$self->extruder->E(0) if $self->extruder;
return sprintf "G92 %s0%s\n", $self->config->extrusion_axis, ($self->config->gcode_comments ? ' ; reset extrusion distance' : '')
if $self->config->extrusion_axis && !$self->config->use_relative_e_distances;
}
@ -627,10 +619,7 @@ sub _Gx {
# output extrusion distance
if ($e && $self->config->extrusion_axis) {
$self->extruder->e(0) if $self->config->use_relative_e_distances;
$self->extruder->e($self->extruder->e + $e);
$self->total_extrusion_length($self->total_extrusion_length + $e);
$gcode .= sprintf " %s%.5f", $self->config->extrusion_axis, $self->extruder->e;
$gcode .= sprintf " %s%.5f", $self->config->extrusion_axis, $self->extruder->extrude($e);
}
$gcode .= " ; $comment" if $comment && $self->config->gcode_comments;

View File

@ -697,6 +697,7 @@ sub export_gcode2 {
my %params = (
output_file => $output_file,
status_cb => sub { $params{progressbar}->(@_) },
quiet => 1,
);
if ($params{export_svg}) {
$print->export_svg(%params);

View File

@ -14,7 +14,6 @@ use Time::HiRes qw(gettimeofday tv_interval);
has 'config' => (is => 'rw', default => sub { Slic3r::Config->new_from_defaults }, trigger => 1);
has 'extra_variables' => (is => 'rw', default => sub {{}});
has 'objects' => (is => 'rw', default => sub {[]});
has 'total_extrusion_length' => (is => 'rw');
has 'processing_time' => (is => 'rw');
has 'extruders' => (is => 'rw', default => sub {[]});
has 'regions' => (is => 'rw', default => sub {[]});
@ -235,6 +234,7 @@ sub init_extruders {
);
for my $extruder_id (keys %{{ map {$_ => 1} @used_extruders }}) {
$self->extruders->[$extruder_id] = Slic3r::Extruder->new(
config => $self->config,
id => $extruder_id,
map { $_ => $self->config->get($_)->[$extruder_id] // $self->config->get($_)->[0] } #/
@{&Slic3r::Extruder::OPTIONS}
@ -472,8 +472,9 @@ sub export_gcode {
$self->processing_time - int($self->processing_time/60)*60;
# TODO: more statistics!
printf "Filament required: %.1fmm (%.1fcm3)\n",
$self->total_extrusion_length, $self->total_extrusion_volume;
print map sprintf("Filament required: %.1fmm (%.1fcm3)\n",
$_->absolute_E, $_->extruded_volume/1000),
@{$self->extruders};
}
}
@ -883,16 +884,15 @@ sub write_gcode {
print $fh $buffer->flush;
}
# save statistic data
$self->total_extrusion_length($gcodegen->total_extrusion_length);
# write end commands to file
print $fh $gcodegen->retract if $gcodegen->extruder; # empty prints don't even set an extruder
print $fh $gcodegen->set_fan(0);
printf $fh "%s\n", $Slic3r::Config->replace_options($Slic3r::Config->end_gcode);
printf $fh "; filament used = %.1fmm (%.1fcm3)\n",
$self->total_extrusion_length, $self->total_extrusion_volume;
foreach my $extruder (@{$self->extruders}) {
printf $fh "; filament used = %.1fmm (%.1fcm3)\n",
$extruder->absolute_E, $extruder->extruded_volume/1000;
}
if ($Slic3r::Config->gcode_comments) {
# append full config
@ -908,11 +908,6 @@ sub write_gcode {
close $fh;
}
sub total_extrusion_volume {
my $self = shift;
return $self->total_extrusion_length * ($self->extruders->[0]->filament_diameter**2) * PI/4 / 1000;
}
# this method will return the supplied input file path after expanding its
# format variables with their values
sub expanded_output_filepath {