| 
15
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 | # File 'lib/rotor/gcode.rb', line 15
def simulate
  @x = 0
  @y = 0
  @z = 0
  line_num = 0
  last_parsed_line = nil
  @file.each_line do |line|
            
    parsed_line = parse_line(line)
    
    line_num += 1
    if parsed_line[:g]
            if parsed_line[:g] == 0
        if parsed_line[:z] && parsed_line[:f] && parsed_line[:x].nil? && parsed_line[:y].nil? 
          puts "Lowering marker"
          @servo.rotate(:down) if @servo
        elsif parsed_line[:z] && parsed_line[:f].nil? && parsed_line[:x].nil? && parsed_line[:y].nil? 
          puts "Raising marker::#{parsed_line}"
          @servo.rotate(:up) if @servo
        else
          puts "Move Stepper::#{parsed_line}"
          move_stepper(parsed_line,1)
        end
      elsif parsed_line[:g] == 1
        if parsed_line[:z] && parsed_line[:f] && parsed_line[:x].nil? && parsed_line[:y].nil? 
          puts "Lowering marker"
          @servo.rotate(:down) if @servo
        elsif parsed_line[:z] && parsed_line[:f].nil? && parsed_line[:x].nil? && parsed_line[:y].nil? 
          puts "Raising marker::#{parsed_line}"
          @servo.rotate(:up) if @servo
        elsif parsed_line[:z].nil? && parsed_line[:f] && parsed_line[:x].nil? && parsed_line[:y].nil? 
                  else
          puts "Move Stepper::#{parsed_line}"
          move_stepper(parsed_line,1)
        end
      elsif parsed_line[:g] == 2 || parsed_line[:g] == 3
                        ignore = false
        x_start = @x
        x_end = parsed_line[:x]
        y_start = @y
        y_end = parsed_line[:y]
        if parsed_line[:i] && parsed_line[:j] && parsed_line[:r].nil?
          x_offset = parsed_line[:i]
                              y_offset = parsed_line[:j]
                    
          x_origin = x_offset + x_start
          y_origin = y_offset + y_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[: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,4].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
                      elsif start_angle - end_angle < -Math::PI
            end_angle *= -1
                      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,1)
        end unless ignore
      else
                puts "DEBUG::GLINE - Something else::#{parsed_line}"
      end
    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
    last_parsed_line = parsed_line
  end
end |