cooling.t 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. use Test::More;
  2. use strict;
  3. use warnings;
  4. plan tests => 14;
  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;
  16. if (defined($config)) {
  17. $config = $config->clone();
  18. } else {
  19. $config = Slic3r::Config->new;
  20. }
  21. my $config_override = shift;
  22. foreach my $key (keys %{$config_override}) {
  23. $config->set($key, ${$config_override}{$key});
  24. }
  25. my $print_config = Slic3r::Config::Print->new;
  26. $print_config->apply_dynamic($config);
  27. $gcodegen = Slic3r::GCode->new;
  28. $gcodegen->apply_print_config($print_config);
  29. $gcodegen->set_layer_count(10);
  30. my $extruders_ref = shift;
  31. $extruders_ref = [ 0 ] if !defined $extruders_ref;
  32. $gcodegen->set_extruders($extruders_ref);
  33. return Slic3r::GCode::CoolingBuffer->new($gcodegen);
  34. }
  35. my $gcode1 = "G1 X100 E1 F3000\n";
  36. my $print_time1 = 100 / (3000 / 60); # 2 sec
  37. my $gcode2 = $gcode1 . "G1 X0 E1 F3000\n";
  38. my $print_time2 = 2 * $print_time1; # 4 sec
  39. my $config = Slic3r::Config::new_from_defaults;
  40. # Default cooling settings.
  41. $config->set('bridge_fan_speed', [ 100 ]);
  42. $config->set('cooling', [ 1 ]);
  43. $config->set('fan_always_on', [ 0 ]);
  44. $config->set('fan_below_layer_time', [ 60 ]);
  45. $config->set('max_fan_speed', [ 100 ]);
  46. $config->set('min_print_speed', [ 10 ]);
  47. $config->set('slowdown_below_layer_time', [ 5 ]);
  48. # Default print speeds.
  49. $config->set('bridge_speed', 60);
  50. $config->set('external_perimeter_speed', '50%');
  51. $config->set('first_layer_speed', 30);
  52. $config->set('gap_fill_speed', 20);
  53. $config->set('infill_speed', 80);
  54. $config->set('perimeter_speed', 60);
  55. $config->set('small_perimeter_speed', 15);
  56. $config->set('solid_infill_speed', 20);
  57. $config->set('top_solid_infill_speed', 15);
  58. $config->set('max_print_speed', 80);
  59. # Override for tests.
  60. $config->set('disable_fan_first_layers', [ 0 ]);
  61. {
  62. my $gcode_src = "G1 F3000;_EXTRUDE_SET_SPEED\nG1 X100 E1";
  63. # Print time of $gcode.
  64. my $print_time = 100 / (3000 / 60);
  65. my $buffer = buffer($config, { 'slowdown_below_layer_time' => [ $print_time * 0.999 ] });
  66. my $gcode = $buffer->process_layer($gcode_src, 0);
  67. like $gcode, qr/F3000/, 'speed is not altered when elapsed time is greater than slowdown threshold';
  68. }
  69. {
  70. my $gcode_src =
  71. "G1 X50 F2500\n" .
  72. "G1 F3000;_EXTRUDE_SET_SPEED\n" .
  73. "G1 X100 E1\n" .
  74. ";_EXTRUDE_END\n" .
  75. "G1 E4 F400",
  76. # Print time of $gcode.
  77. my $print_time = 50 / (2500 / 60) + 100 / (3000 / 60) + 4 / (400 / 60);
  78. my $buffer = buffer($config, { 'slowdown_below_layer_time' => [ $print_time * 1.001 ] });
  79. my $gcode = $buffer->process_layer($gcode_src, 0);
  80. unlike $gcode, qr/F3000/, 'speed is altered when elapsed time is lower than slowdown threshold';
  81. like $gcode, qr/F2500/, 'speed is not altered for travel moves';
  82. like $gcode, qr/F400/, 'speed is not altered for extruder-only moves';
  83. }
  84. {
  85. my $buffer = buffer($config, {
  86. 'fan_below_layer_time' => [ $print_time1 * 0.88 ],
  87. 'slowdown_below_layer_time' => [ $print_time1 * 0.99 ]
  88. });
  89. my $gcode = $buffer->process_layer($gcode1, 0);
  90. unlike $gcode, qr/M106/, 'fan is not activated when elapsed time is greater than fan threshold';
  91. }
  92. {
  93. my $gcode .= buffer($config, { 'slowdown_below_layer_time', [ $print_time2 * 0.99 ] })->process_layer($gcode2, 0);
  94. like $gcode, qr/F3000/, 'slowdown is computed on all objects printing at the same Z';
  95. }
  96. {
  97. # use an elapsed time which is < the threshold but greater than it when summed twice
  98. my $buffer = buffer($config, {
  99. 'fan_below_layer_time' => [ $print_time2 * 0.65],
  100. 'slowdown_below_layer_time' => [ $print_time2 * 0.7 ]
  101. });
  102. my $gcode = $buffer->process_layer($gcode2, 0) .
  103. $buffer->process_layer($gcode2, 1);
  104. unlike $gcode, qr/M106/, 'fan is not activated on all objects printing at different Z';
  105. }
  106. {
  107. # use an elapsed time which is < the threshold even when summed twice
  108. my $buffer = buffer($config, {
  109. 'fan_below_layer_time' => [ $print_time2 + 1 ],
  110. 'slowdown_below_layer_time' => [ $print_time2 + 2 ]
  111. });
  112. my $gcode = $buffer->process_layer($gcode2, 0) .
  113. $buffer->process_layer($gcode2, 1);
  114. like $gcode, qr/M106/, 'fan is activated on all objects printing at different Z';
  115. }
  116. {
  117. my $buffer = buffer($config, {
  118. 'cooling' => [ 1 , 0 ],
  119. 'fan_below_layer_time' => [ $print_time2 + 1, $print_time2 + 1 ],
  120. 'slowdown_below_layer_time' => [ $print_time2 + 2, $print_time2 + 2 ]
  121. },
  122. [ 0, 1]);
  123. my $gcode = $buffer->process_layer($gcode1 . "T1\nG1 X0 E1 F3000\n", 0);
  124. like $gcode, qr/^M106/, 'fan is activated for the 1st tool';
  125. like $gcode, qr/.*M107/, 'fan is disabled for the 2nd tool';
  126. }
  127. {
  128. my $config = Slic3r::Config::new_from_defaults;
  129. $config->set('cooling', [ 1 ]);
  130. $config->set('bridge_fan_speed', [ 100 ]);
  131. $config->set('fan_below_layer_time', [ 0 ]);
  132. $config->set('slowdown_below_layer_time', [ 0 ]);
  133. $config->set('bridge_speed', 99);
  134. $config->set('top_solid_layers', 1); # internal bridges use solid_infil speed
  135. $config->set('bottom_solid_layers', 1); # internal bridges use solid_infil speed
  136. my $print = Slic3r::Test::init_print('overhang', config => $config);
  137. my $fan = 0;
  138. my $fan_with_incorrect_speeds = my $fan_with_incorrect_print_speeds = 0;
  139. my $bridge_with_no_fan = 0;
  140. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  141. my ($self, $cmd, $args, $info) = @_;
  142. if ($cmd eq 'M106') {
  143. $fan = $args->{S};
  144. $fan_with_incorrect_speeds++ if $fan != 255;
  145. } elsif ($cmd eq 'M107') {
  146. $fan = 0;
  147. } elsif ($info->{extruding} && $info->{dist_XY} > 0) {
  148. $fan_with_incorrect_print_speeds++
  149. if ($fan > 0) && ($args->{F} // $self->F) != 60*$config->bridge_speed;
  150. $bridge_with_no_fan++
  151. if !$fan && ($args->{F} // $self->F) == 60*$config->bridge_speed;
  152. }
  153. });
  154. ok !$fan_with_incorrect_speeds, 'bridge fan speed is applied correctly';
  155. ok !$fan_with_incorrect_print_speeds, 'bridge fan is only turned on for bridges';
  156. ok !$bridge_with_no_fan, 'bridge fan is turned on for all bridges';
  157. }
  158. {
  159. my $config = Slic3r::Config::new_from_defaults;
  160. $config->set('cooling', [ 1 ]);
  161. $config->set('fan_below_layer_time', [ 0 ]);
  162. $config->set('slowdown_below_layer_time', [ 10 ]);
  163. $config->set('min_print_speed', [ 0 ]);
  164. $config->set('start_gcode', '');
  165. $config->set('first_layer_speed', '100%');
  166. $config->set('external_perimeter_speed', 99);
  167. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  168. my @layer_times = (0); # in seconds
  169. my %layer_external = (); # z => 1
  170. Slic3r::GCode::Reader->new->parse(my $gcode = Slic3r::Test::gcode($print), sub {
  171. my ($self, $cmd, $args, $info) = @_;
  172. if ($cmd eq 'G1') {
  173. if ($info->{dist_Z}) {
  174. push @layer_times, 0;
  175. $layer_external{ $args->{Z} } = 0;
  176. }
  177. $layer_times[-1] += abs($info->{dist_XY} || $info->{dist_E} || $info->{dist_Z} || 0) / ($args->{F} // $self->F) * 60;
  178. if ($args->{F} && $args->{F} == $config->external_perimeter_speed*60) {
  179. $layer_external{ $self->Z }++;
  180. }
  181. }
  182. });
  183. @layer_times = grep $_, @layer_times;
  184. my $all_below = none { $_ < $config->slowdown_below_layer_time->[0] } @layer_times;
  185. ok $all_below, 'slowdown_below_layer_time is honored';
  186. # check that all layers have at least one unaltered external perimeter speed
  187. # my $external = all { $_ > 0 } values %layer_external;
  188. # ok $external, 'slowdown_below_layer_time does not alter external perimeters';
  189. }
  190. __END__