cooling.t 8.1 KB

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