~ubuntu-branches/ubuntu/utopic/slic3r/utopic

« back to all changes in this revision

Viewing changes to lib/Slic3r/GCode/VibrationLimit.pm

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2014-06-17 01:27:26 UTC
  • Revision ID: package-import@ubuntu.com-20140617012726-2wrs4zdo251nr4vg
Tags: upstream-1.1.4+dfsg
ImportĀ upstreamĀ versionĀ 1.1.4+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Slic3r::GCode::VibrationLimit;
 
2
use Moo;
 
3
 
 
4
extends 'Slic3r::GCode::Reader';
 
5
 
 
6
has 'config'    => (is => 'ro', required => 1);
 
7
has '_min_time' => (is => 'lazy');
 
8
has '_last_dir' => (is => 'ro', default => sub { [0,0] });
 
9
has '_dir_time' => (is => 'ro', default => sub { [0,0] });
 
10
 
 
11
# inspired by http://hydraraptor.blogspot.it/2010/12/frequency-limit.html
 
12
 
 
13
use List::Util qw(max);
 
14
use Slic3r::Geometry qw(X Y);
 
15
 
 
16
sub _build__min_time {
 
17
    my ($self) = @_;
 
18
    return 1 / ($self->config->vibration_limit * 60);  # in minutes
 
19
}
 
20
 
 
21
sub process {
 
22
    my $self = shift;
 
23
    my ($gcode) = @_;
 
24
    
 
25
    my $new_gcode = "";
 
26
    $self->parse($gcode, sub {
 
27
        my ($reader, $cmd, $args, $info) = @_;
 
28
        
 
29
        if ($cmd eq 'G1' && $info->{dist_XY} > 0) {
 
30
            my $point = Slic3r::Point->new($args->{X} // $reader->X, $args->{Y} // $reader->Y);
 
31
            my @dir = (
 
32
                ($point->x <=> $reader->X),
 
33
                ($point->y <=> $reader->Y),   #$
 
34
            );
 
35
            my $time = $info->{dist_XY} / ($args->{F} // $reader->F);  # in minutes
 
36
            if ($time > 0) {
 
37
                my @pause = ();
 
38
                foreach my $axis (X,Y) {
 
39
                    if ($dir[$axis] != 0 && $self->_last_dir->[$axis] != $dir[$axis]) {
 
40
                        if ($self->_last_dir->[$axis] != 0) {
 
41
                            # this axis is changing direction: check whether we need to pause
 
42
                            if ($self->_dir_time->[$axis] < $self->_min_time) {
 
43
                                push @pause, ($self->_min_time - $self->_dir_time->[$axis]);
 
44
                            }
 
45
                        }
 
46
                        $self->_last_dir->[$axis] = $dir[$axis];
 
47
                        $self->_dir_time->[$axis] = 0;
 
48
                    }
 
49
                    $self->_dir_time->[$axis] += $time;
 
50
                }
 
51
                
 
52
                if (@pause) {
 
53
                    $new_gcode .= sprintf "G4 P%d\n", max(@pause) * 60 * 1000;
 
54
                }
 
55
            }
 
56
        }
 
57
        
 
58
        $new_gcode .= $info->{raw} . "\n";
 
59
    });
 
60
    
 
61
    return $new_gcode;
 
62
}
 
63
 
 
64
1;