vibrationlimit.t 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. use Test::More tests => 9;
  2. use strict;
  3. use warnings;
  4. BEGIN {
  5. use FindBin;
  6. use lib "$FindBin::Bin/../lib";
  7. }
  8. use Slic3r;
  9. use Slic3r::Geometry qw(epsilon);
  10. use Slic3r::Test;
  11. my $config = Slic3r::Config->new_from_defaults;
  12. # tolerance, in minutes
  13. # (our time estimation differs from the internal one because of decimals truncation)
  14. my $epsilon = 0.002;
  15. my $test = sub {
  16. my ($conf) = @_;
  17. $conf ||= $config;
  18. my $print = Slic3r::Test::init_print('2x20x10', config => $conf);
  19. my $min_time = 1 / ($conf->vibration_limit * 60); # minimum time between direction changes in minutes
  20. my %dir = (X => 0, Y => 0);
  21. my %dir_time = (X => 0, Y => 0);
  22. my %dir_sleep_time = (X => 0, Y => 0);
  23. my $last_cmd_pause = 0;
  24. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  25. my ($self, $cmd, $args, $info) = @_;
  26. if ($cmd !~ /^G[01]$/) {
  27. if ($cmd eq 'G4') {
  28. $last_cmd_pause = (($args->{P} // 0) / 1000 + ($args->{S} // 0)) / 60; # in minutes
  29. $dir_sleep_time{$_} += $last_cmd_pause for qw(X Y);
  30. $last_cmd_pause -= $epsilon; # error builds up
  31. }
  32. return;
  33. }
  34. # Z moves are slow enough that we can consider any vibration interrupted
  35. if ($info->{dist_Z}) {
  36. $dir_time{$_} += 99999 for qw(X Y);
  37. $last_cmd_pause = 0;
  38. return;
  39. } elsif ($info->{dist_E} != 0 && $info->{dist_XY} == 0) {
  40. my $time = abs($info->{dist_E}) / ($args->{F} // $self->F); # in minutes
  41. $dir_time{$_} += $time for qw(X Y);
  42. $last_cmd_pause = 0;
  43. return;
  44. }
  45. # compute move time (this assumes that the speed is XY-bound, which happens very likely)
  46. my $time = abs($info->{dist_XY}) / ($args->{F} // $self->F); # in minutes
  47. my $one_axis_would_trigger_limit_without_pause = 0;
  48. foreach my $axis (qw(X Y)) {
  49. # are we changing direction on this axis?
  50. my $dir = $info->{"dist_$axis"} <=> ($args->{$axis} // $self->$axis);
  51. if ($dir != 0 && $dir{$axis} != $dir) {
  52. # this move changes direction on this axis
  53. if ($dir{$axis} != 0) {
  54. if (($dir_time{$axis} + $dir_sleep_time{$axis}) < ($min_time - $epsilon)) {
  55. fail 'vibration limit exceeded';
  56. }
  57. $one_axis_would_trigger_limit_without_pause = 1
  58. if ($dir_time{$axis} - $last_cmd_pause) < $min_time;
  59. }
  60. $dir{$axis} = $dir;
  61. $dir_time{$axis} = 0;
  62. $dir_sleep_time{$axis} = 0;
  63. }
  64. $dir_time{$axis} += $time;
  65. }
  66. fail 'no unnecessary pauses are added'
  67. if $last_cmd_pause > $epsilon && !$one_axis_would_trigger_limit_without_pause;
  68. $last_cmd_pause = 0;
  69. });
  70. 1;
  71. };
  72. $config->set('gcode_comments', 1);
  73. $config->set('perimeters', 1);
  74. foreach my $frequency (5, 10, 15) {
  75. foreach my $gapfillspeed (20, 50, 100) {
  76. $config->set('vibration_limit', $frequency);
  77. ok $test->(), "vibrations limited to ${frequency}Hz (gap fill speed = ${gapfillspeed} mm/s)";
  78. }
  79. }
  80. __END__