retraction.t 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. use Test::More tests => 26;
  2. use strict;
  3. use warnings;
  4. BEGIN {
  5. use FindBin;
  6. use lib "$FindBin::Bin/../lib";
  7. use local::lib "$FindBin::Bin/../local-lib";
  8. }
  9. use List::Util qw(any);
  10. use Slic3r;
  11. use Slic3r::Test qw(_eq);
  12. {
  13. my $config = Slic3r::Config::new_from_defaults;
  14. my $duplicate = 1;
  15. my $test = sub {
  16. my ($conf) = @_;
  17. $conf ||= $config;
  18. my $print = Slic3r::Test::init_print('20mm_cube', config => $conf, duplicate => $duplicate);
  19. my $tool = 0;
  20. my @toolchange_count = (); # track first usages so that we don't expect retract_length_toolchange when extruders are used for the first time
  21. my @retracted = (1); # ignore the first travel move from home to first point
  22. my @retracted_length = (0);
  23. my $lifted = 0;
  24. my $lift_dist = 0; # track lifted distance for toolchanges and extruders with different retract_lift values
  25. my $changed_tool = 0;
  26. my $wait_for_toolchange = 0;
  27. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  28. my ($self, $cmd, $args, $info) = @_;
  29. if ($cmd =~ /^T(\d+)/) {
  30. $tool = $1;
  31. $changed_tool = 1;
  32. $wait_for_toolchange = 0;
  33. $toolchange_count[$tool] //= 0;
  34. $toolchange_count[$tool]++;
  35. } elsif ($cmd =~ /^G[01]$/ && !$args->{Z}) { # ignore lift taking place after retraction
  36. fail 'toolchange happens right after retraction' if $wait_for_toolchange;
  37. }
  38. if ($info->{dist_Z}) {
  39. # lift move or lift + change layer
  40. if (_eq($info->{dist_Z}, $print->print->config->get_at('retract_lift', $tool))
  41. || (_eq($info->{dist_Z}, $conf->layer_height + $print->print->config->get_at('retract_lift', $tool)) && $print->print->config->get_at('retract_lift', $tool) > 0)) {
  42. fail 'only lifting while retracted' if !$retracted[$tool];
  43. fail 'double lift' if $lifted;
  44. $lifted = 1;
  45. $lift_dist = $info->{dist_Z};
  46. }
  47. if ($info->{dist_Z} < 0) {
  48. fail 'going down only after lifting' if !$lifted;
  49. fail 'going down by the same amount of the lift or by the amount needed to get to next layer'
  50. if !_eq($info->{dist_Z}, -$lift_dist)
  51. && !_eq($info->{dist_Z}, -lift_dist + $conf->layer_height);
  52. $lift_dist = 0;
  53. $lifted = 0;
  54. }
  55. fail 'move Z at travel speed' if ($args->{F} // $self->F) != $conf->travel_speed * 60;
  56. }
  57. if ($info->{retracting}) {
  58. $retracted[$tool] = 1;
  59. $retracted_length[$tool] += -$info->{dist_E};
  60. if (_eq($retracted_length[$tool], $print->print->config->get_at('retract_length', $tool))) {
  61. # okay
  62. } elsif (_eq($retracted_length[$tool], $print->print->config->get_at('retract_length_toolchange', $tool))) {
  63. $wait_for_toolchange = 1;
  64. } else {
  65. fail 'retracted by the correct amount';
  66. }
  67. }
  68. if ($info->{extruding}) {
  69. fail 'only extruding while not lifted' if $lifted;
  70. if ($retracted[$tool]) {
  71. my $expected_amount = $retracted_length[$tool] + $print->print->config->get_at('retract_restart_extra', $tool);
  72. if ($changed_tool && $toolchange_count[$tool] > 1) {
  73. $expected_amount = $print->print->config->get_at('retract_length_toolchange', $tool) + $print->print->config->get_at('retract_restart_extra_toolchange', $tool);
  74. $changed_tool = 0;
  75. }
  76. fail 'unretracted by the correct amount' && exit
  77. if !_eq($info->{dist_E}, $expected_amount);
  78. $retracted[$tool] = 0;
  79. $retracted_length[$tool] = 0;
  80. }
  81. }
  82. if ($info->{travel} && $info->{dist_XY} >= $print->print->config->get_at('retract_before_travel', $tool)) {
  83. fail 'retracted before long travel move' if !$retracted[$tool];
  84. }
  85. });
  86. 1;
  87. };
  88. $config->set('first_layer_height', $config->layer_height);
  89. $config->set('first_layer_speed', '100%');
  90. $config->set('start_gcode', ''); # to avoid dealing with the nozzle lift in start G-code
  91. $config->set('retract_length', [1.5]);
  92. $config->set('retract_before_travel', [3]);
  93. $config->set('only_retract_when_crossing_perimeters', 0);
  94. my $retract_tests = sub {
  95. my ($descr) = @_;
  96. ok $test->(), "retraction$descr";
  97. my $conf = $config->clone;
  98. $conf->set('retract_restart_extra', [1]);
  99. ok $test->($conf), "restart extra length$descr";
  100. $conf->set('retract_restart_extra', [-1]);
  101. ok $test->($conf), "negative restart extra length$descr";
  102. $conf->set('retract_lift', [1, 2]);
  103. ok $test->($conf), "lift$descr";
  104. };
  105. $retract_tests->('');
  106. $duplicate = 2;
  107. $retract_tests->(' (duplicate)');
  108. $duplicate = 1;
  109. $config->set('infill_extruder', 2);
  110. $config->set('skirts', 4);
  111. $config->set('skirt_height', 3);
  112. $retract_tests->(' (dual extruder with multiple skirt layers)');
  113. }
  114. {
  115. my $config = Slic3r::Config::new_from_defaults;
  116. $config->set('start_gcode', ''); # prevent any default priming Z move from affecting our lift detection
  117. $config->set('retract_length', [0]);
  118. $config->set('retract_layer_change', [0]);
  119. $config->set('retract_lift', [0.2]);
  120. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  121. my $retracted = 0;
  122. my $layer_changes_with_retraction = 0;
  123. my $retractions = my $z_restores = 0;
  124. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  125. my ($self, $cmd, $args, $info) = @_;
  126. if ($info->{retracting}) {
  127. $retracted = 1;
  128. $retractions++;
  129. } elsif ($info->{extruding} && $retracted) {
  130. $retracted = 0;
  131. }
  132. if ($info->{dist_Z} && $retracted) {
  133. $layer_changes_with_retraction++;
  134. }
  135. if ($info->{dist_Z} && $args->{Z} < $self->Z) {
  136. $z_restores++;
  137. }
  138. });
  139. is $layer_changes_with_retraction, 0, 'no retraction on layer change';
  140. is $retractions, 0, 'no retractions';
  141. is $z_restores, 0, 'no lift';
  142. }
  143. {
  144. my $config = Slic3r::Config::new_from_defaults;
  145. $config->set('use_firmware_retraction', 1);
  146. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  147. my $retracted = 0;
  148. my $double_retractions = my $double_unretractions = 0;
  149. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  150. my ($self, $cmd, $args, $info) = @_;
  151. if ($cmd eq 'G10') {
  152. $double_retractions++ if $retracted;
  153. $retracted = 1;
  154. } elsif ($cmd eq 'G11') {
  155. $double_unretractions++ if !$retracted;
  156. $retracted = 0;
  157. }
  158. });
  159. is $double_retractions, 0, 'no double retractions';
  160. is $double_unretractions, 0, 'no double unretractions';
  161. }
  162. {
  163. my $config = Slic3r::Config::new_from_defaults;
  164. $config->set('use_firmware_retraction', 1);
  165. $config->set('retract_length', [0]);
  166. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  167. my $retracted = 0;
  168. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  169. my ($self, $cmd, $args, $info) = @_;
  170. if ($cmd eq 'G10') {
  171. $retracted = 1;
  172. }
  173. });
  174. ok $retracted, 'retracting also when --retract-length is 0 but --use-firmware-retraction is enabled';
  175. }
  176. {
  177. my $config = Slic3r::Config::new_from_defaults;
  178. $config->set('start_gcode', '');
  179. $config->set('retract_lift', [3, 4]);
  180. my @lifted_at = ();
  181. my $test = sub {
  182. my $print = Slic3r::Test::init_print('20mm_cube', config => $config, duplicate => 2);
  183. @lifted_at = ();
  184. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  185. my ($self, $cmd, $args, $info) = @_;
  186. if ($cmd eq 'G1' && $info->{dist_Z} < 0) {
  187. push @lifted_at, $info->{new_Z};
  188. }
  189. });
  190. };
  191. $config->set('retract_lift_above', [0, 0]);
  192. $config->set('retract_lift_below', [0, 0]);
  193. $test->();
  194. ok !!@lifted_at, 'lift takes place when above/below == 0';
  195. $config->set('retract_lift_above', [5, 6]);
  196. $config->set('retract_lift_below', [15, 13]);
  197. $test->();
  198. ok !!@lifted_at, 'lift takes place when above/below != 0';
  199. ok !(any { $_ < $config->get_at('retract_lift_above', 0) } @lifted_at),
  200. 'Z is not lifted below the configured value';
  201. ok !(any { $_ > $config->get_at('retract_lift_below', 0) } @lifted_at),
  202. 'Z is not lifted above the configured value';
  203. # check lifting with different values for 2. extruder
  204. $config->set('perimeter_extruder', 2);
  205. $config->set('infill_extruder', 2);
  206. $config->set('retract_lift_above', [0, 0]);
  207. $config->set('retract_lift_below', [0, 0]);
  208. $test->();
  209. ok !!@lifted_at, 'lift takes place when above/below == 0 for 2. extruder';
  210. $config->set('retract_lift_above', [5, 6]);
  211. $config->set('retract_lift_below', [15, 13]);
  212. $test->();
  213. ok !!@lifted_at, 'lift takes place when above/below != 0 for 2. extruder';
  214. ok !(any { $_ < $config->get_at('retract_lift_above', 1) } @lifted_at),
  215. 'Z is not lifted below the configured value for 2. extruder';
  216. ok !(any { $_ > $config->get_at('retract_lift_below', 1) } @lifted_at),
  217. 'Z is not lifted above the configured value for 2. extruder';
  218. }
  219. __END__