retraction.t 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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('nozzle_diameter', [0.6,0.6,0.6,0.6]);
  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('nozzle_diameter', [0.6,0.6,0.6,0.6]);
  180. $config->set('start_gcode', '');
  181. $config->set('retract_lift', [3, 4]);
  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. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  187. my ($self, $cmd, $args, $info) = @_;
  188. if ($cmd eq 'G1' && $info->{dist_Z} < 0) {
  189. push @lifted_at, $info->{new_Z};
  190. }
  191. });
  192. };
  193. $config->set('retract_lift_above', [0, 0]);
  194. $config->set('retract_lift_below', [0, 0]);
  195. $test->();
  196. ok !!@lifted_at, 'lift takes place when above/below == 0';
  197. $config->set('retract_lift_above', [5, 6]);
  198. $config->set('retract_lift_below', [15, 13]);
  199. $test->();
  200. ok !!@lifted_at, 'lift takes place when above/below != 0';
  201. ok !(any { $_ < $config->get_at('retract_lift_above', 0) } @lifted_at),
  202. 'Z is not lifted below the configured value';
  203. ok !(any { $_ > $config->get_at('retract_lift_below', 0) } @lifted_at),
  204. 'Z is not lifted above the configured value';
  205. # check lifting with different values for 2. extruder
  206. $config->set('perimeter_extruder', 2);
  207. $config->set('infill_extruder', 2);
  208. $config->set('retract_lift_above', [0, 0]);
  209. $config->set('retract_lift_below', [0, 0]);
  210. $test->();
  211. ok !!@lifted_at, 'lift takes place when above/below == 0 for 2. extruder';
  212. $config->set('retract_lift_above', [5, 6]);
  213. $config->set('retract_lift_below', [15, 13]);
  214. $test->();
  215. ok !!@lifted_at, 'lift takes place when above/below != 0 for 2. extruder';
  216. ok !(any { $_ < $config->get_at('retract_lift_above', 1) } @lifted_at),
  217. 'Z is not lifted below the configured value for 2. extruder';
  218. ok !(any { $_ > $config->get_at('retract_lift_below', 1) } @lifted_at),
  219. 'Z is not lifted above the configured value for 2. extruder';
  220. }
  221. __END__