retraction.t 12 KB

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