Class: Rotor::Gcode

Inherits:
Object
  • Object
show all
Defined in:
lib/rotor/gcode.rb

Instance Method Summary collapse

Constructor Details

#initialize(stepper_x = nil, stepper_y = nil, stepper_z = nil, scale = 1, servo = nil, fast_move = 1) ⇒ Gcode

Returns a new instance of Gcode.



3
4
5
6
7
8
9
10
# File 'lib/rotor/gcode.rb', line 3

def initialize(stepper_x=nil,stepper_y=nil,stepper_z=nil,scale=1,servo=nil,fast_move=1)
  @stepper_x = stepper_x
  @stepper_y = stepper_y
  @stepper_z = stepper_z
  @fast_move = fast_move
  @scale = scale
  @servo = servo
end

Instance Method Details

#open(file) ⇒ Object



12
13
14
# File 'lib/rotor/gcode.rb', line 12

def open(file)
  @file = File.open(file)
end

#simulate(accuracy = 8, speed = 1) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rotor/gcode.rb', line 16

def simulate(accuracy=8,speed=1)
  @x = 0
  @y = 0
  @z = 0

  line_num = 0

  @file.each_line do |line|
    line[0..2] = @previous_command if line[0..2] == "   "
    parsed_line = parse_line(line)
    
    line_num += 1
    if parsed_line[:g]
      #Move to this origin point.
      if parsed_line[:g] == 0
        puts "Move Stepper (#{line_num})::#{parsed_line}"
        move_stepper(parsed_line,@fast_move)
      elsif parsed_line[:g] == 1
        puts "Move Stepper (#{line_num})::#{parsed_line}"
        move_stepper(parsed_line,speed)
      elsif parsed_line[:g] == 2 || parsed_line[:g] == 3
        # Get my ARC on
        ignore = false

        x_start = @x
        x_end = parsed_line[:x]

        y_start = @y
        y_end = parsed_line[:y]

        z_start = @z
        z_end = parsed_line[:z]

        if (parsed_line[:i] || parsed_line[:j] || parsed_line[:k]) && parsed_line[:r].nil?
          x_offset = parsed_line[:i]
          y_offset = parsed_line[:j]
          z_offset = parsed_line[:k]

          x_origin = x_offset + x_start
          y_origin = y_offset + y_start
          # z_origin = z_offset + z_start

          radius = Math.sqrt((x_start - x_origin) ** 2 + (y_start - y_origin) ** 2)
   
        elsif parsed_line[:i].nil? && parsed_line[:j].nil? && parsed_line[:k].nil? && parsed_line[:r]
          ignore = true
        end

        unless ignore
          distance = Math.sqrt((x_start - x_end) ** 2 + (y_start - y_end) ** 2)
          number_of_precision = [distance.to_i,8].max
          start_angle = Math.atan2((y_start - y_origin),(x_start - x_origin))
          end_angle = Math.atan2((y_end - y_origin),(x_end - x_origin))

          if start_angle - end_angle > Math::PI
            end_angle += Math::PI * 2
            # puts "Move Arc Stepper (#{line_num})::Insert Fix"
          elsif start_angle - end_angle < -Math::PI
            end_angle *= -1
            # puts "Move Arc Stepper (#{line_num})::Insert Fix 2"
          end

          steps = (end_angle - start_angle) / number_of_precision
          current_degrees = start_angle
        end

        number_of_precision.times do |i|
          current_degrees += steps
          arc_line = {}
          arc_line[:g] = parsed_line[:g]
          arc_line[:x] = radius * Math.cos(current_degrees) + x_origin
          arc_line[:y] = radius * Math.sin(current_degrees) + y_origin
          arc_line[:z] = nil
          # puts "Move Arc Stepper (#{line_num})::#{arc_line}::#{start_angle},#{end_angle}"
          move_stepper(arc_line,speed)
        end unless ignore

      else
        # puts "DEBUG::GLINE - Something else::#{parsed_line}"
      end
      @previous_command = line[0..2]
    elsif parsed_line[:m]
      if line[0..2] == "M03"
        puts "Lowering marker"
        @servo.rotate(:down) if @servo
      elsif line[0..2] == "M05"
        puts "Lifting marker"
        @servo.rotate(:up) if @servo
      else
        # puts "DEBUG::MLINE - Something else::#{parsed_line}"
      end
    else
      # puts "DEBUG::????? - Something else::#{parsed_line}"
    end        
  end
end