cooling.t 8.0 KB

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