cooling.t 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. use Test::More;
  2. use strict;
  3. use warnings;
  4. plan tests => 13;
  5. BEGIN {
  6. use FindBin;
  7. use lib "$FindBin::Bin/../lib";
  8. use local::lib "$FindBin::Bin/../local-lib";
  9. }
  10. use List::Util qw(none all);
  11. use Slic3r;
  12. use Slic3r::Test;
  13. my $gcodegen;
  14. sub buffer {
  15. my $config = shift || Slic3r::Config->new;
  16. my $print_config = Slic3r::Config::Print->new;
  17. $print_config->apply_dynamic($config);
  18. $gcodegen = Slic3r::GCode->new;
  19. $gcodegen->apply_print_config($print_config);
  20. $gcodegen->set_layer_count(10);
  21. return Slic3r::GCode::CoolingBuffer->new($gcodegen);
  22. }
  23. my $config = Slic3r::Config->new_from_defaults;
  24. $config->set('disable_fan_first_layers', 0);
  25. {
  26. my $buffer = buffer($config);
  27. $buffer->gcodegen->set_elapsed_time($buffer->gcodegen->config->slowdown_below_layer_time + 1);
  28. my $gcode = $buffer->append('G1 F3000;_EXTRUDE_SET_SPEED\nG1 X100 E1', 0, 0, 0.4) . $buffer->flush;
  29. like $gcode, qr/F3000/, 'speed is not altered when elapsed time is greater than slowdown threshold';
  30. }
  31. {
  32. my $buffer = buffer($config);
  33. $buffer->gcodegen->set_elapsed_time($buffer->gcodegen->config->slowdown_below_layer_time - 1);
  34. my $gcode = $buffer->append(
  35. "G1 X50 F2500\n" .
  36. "G1 F3000;_EXTRUDE_SET_SPEED\n" .
  37. "G1 X100 E1\n" .
  38. "G1 E4 F400",
  39. 0, 0, 0.4
  40. ) . $buffer->flush;
  41. unlike $gcode, qr/F3000/, 'speed is altered when elapsed time is lower than slowdown threshold';
  42. like $gcode, qr/F2500/, 'speed is not altered for travel moves';
  43. like $gcode, qr/F400/, 'speed is not altered for extruder-only moves';
  44. }
  45. {
  46. my $buffer = buffer($config);
  47. $buffer->gcodegen->set_elapsed_time($buffer->gcodegen->config->fan_below_layer_time + 1);
  48. my $gcode = $buffer->append('G1 X100 E1 F3000', 0, 0, 0.4) . $buffer->flush;
  49. unlike $gcode, qr/M106/, 'fan is not activated when elapsed time is greater than fan threshold';
  50. }
  51. {
  52. my $buffer = buffer($config);
  53. my $gcode = "";
  54. for my $obj_id (0 .. 1) {
  55. # use an elapsed time which is < the slowdown threshold but greater than it when summed twice
  56. $buffer->gcodegen->set_elapsed_time($buffer->gcodegen->config->slowdown_below_layer_time - 1);
  57. $gcode .= $buffer->append("G1 X100 E1 F3000\n", $obj_id, 0, 0.4);
  58. }
  59. $gcode .= $buffer->flush;
  60. like $gcode, qr/F3000/, 'slowdown is computed on all objects printing at same Z';
  61. }
  62. {
  63. my $buffer = buffer($config);
  64. my $gcode = "";
  65. for my $layer_id (0 .. 1) {
  66. for my $obj_id (0 .. 1) {
  67. # use an elapsed time which is < the threshold but greater than it when summed twice
  68. $buffer->gcodegen->set_elapsed_time($buffer->gcodegen->config->fan_below_layer_time - 1);
  69. $gcode .= $buffer->append("G1 X100 E1 F3000\n", $obj_id, $layer_id, 0.4 + 0.4*$layer_id + 0.1*$obj_id); # print same layer at distinct heights
  70. }
  71. }
  72. $gcode .= $buffer->flush;
  73. unlike $gcode, qr/M106/, 'fan activation is computed on all objects printing at different Z';
  74. }
  75. {
  76. my $buffer = buffer($config);
  77. my $gcode = "";
  78. for my $layer_id (0 .. 1) {
  79. for my $obj_id (0 .. 1) {
  80. # use an elapsed time which is < the threshold even when summed twice
  81. $buffer->gcodegen->set_elapsed_time($buffer->gcodegen->config->fan_below_layer_time/2 - 1);
  82. $gcode .= $buffer->append("G1 X100 E1 F3000\n", $obj_id, $layer_id, 0.4 + 0.4*$layer_id + 0.1*$obj_id); # print same layer at distinct heights
  83. }
  84. }
  85. $gcode .= $buffer->flush;
  86. like $gcode, qr/M106/, 'fan activation is computed on all objects printing at different Z';
  87. }
  88. {
  89. my $config = Slic3r::Config->new_from_defaults;
  90. $config->set('cooling', 1);
  91. $config->set('bridge_fan_speed', 100);
  92. $config->set('fan_below_layer_time', 0);
  93. $config->set('slowdown_below_layer_time', 0);
  94. $config->set('bridge_speed', 99);
  95. $config->set('top_solid_layers', 1); # internal bridges use solid_infil speed
  96. $config->set('bottom_solid_layers', 1); # internal bridges use solid_infil speed
  97. $config->set('vibration_limit', 30); # test that fan is turned on even when vibration limit (or other G-code post-processor) is enabled
  98. my $print = Slic3r::Test::init_print('overhang', config => $config);
  99. my $fan = 0;
  100. my $fan_with_incorrect_speeds = my $fan_with_incorrect_print_speeds = 0;
  101. my $bridge_with_no_fan = 0;
  102. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  103. my ($self, $cmd, $args, $info) = @_;
  104. if ($cmd eq 'M106') {
  105. $fan = $args->{S};
  106. $fan_with_incorrect_speeds++ if $fan != 255;
  107. } elsif ($cmd eq 'M107') {
  108. $fan = 0;
  109. } elsif ($info->{extruding} && $info->{dist_XY} > 0) {
  110. $fan_with_incorrect_print_speeds++
  111. if ($fan > 0) && ($args->{F} // $self->F) != 60*$config->bridge_speed;
  112. $bridge_with_no_fan++
  113. if !$fan && ($args->{F} // $self->F) == 60*$config->bridge_speed;
  114. }
  115. });
  116. ok !$fan_with_incorrect_speeds, 'bridge fan speed is applied correctly';
  117. ok !$fan_with_incorrect_print_speeds, 'bridge fan is only turned on for bridges';
  118. ok !$bridge_with_no_fan, 'bridge fan is turned on for all bridges';
  119. }
  120. {
  121. my $config = Slic3r::Config->new_from_defaults;
  122. $config->set('cooling', 1);
  123. $config->set('fan_below_layer_time', 0);
  124. $config->set('slowdown_below_layer_time', 10);
  125. $config->set('min_print_speed', 0);
  126. $config->set('start_gcode', '');
  127. $config->set('first_layer_speed', '100%');
  128. $config->set('external_perimeter_speed', 99);
  129. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  130. my @layer_times = (0); # in seconds
  131. my %layer_external = (); # z => 1
  132. Slic3r::GCode::Reader->new->parse(my $gcode = Slic3r::Test::gcode($print), sub {
  133. my ($self, $cmd, $args, $info) = @_;
  134. if ($cmd eq 'G1') {
  135. if ($info->{dist_Z}) {
  136. push @layer_times, 0;
  137. $layer_external{ $args->{Z} } = 0;
  138. }
  139. $layer_times[-1] += abs($info->{dist_XY} || $info->{dist_E} || $info->{dist_Z} || 0) / ($args->{F} // $self->F) * 60;
  140. if ($args->{F} && $args->{F} == $config->external_perimeter_speed*60) {
  141. $layer_external{ $self->Z }++;
  142. }
  143. }
  144. });
  145. @layer_times = grep $_, @layer_times;
  146. my $all_below = none { $_ < $config->slowdown_below_layer_time } @layer_times;
  147. ok $all_below, 'slowdown_below_layer_time is honored';
  148. # check that all layers have at least one unaltered external perimeter speed
  149. my $external = all { $_ > 0 } values %layer_external;
  150. ok $external, 'slowdown_below_layer_time does not alter external perimeters';
  151. }
  152. __END__