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) ⇒ Gcode

Returns a new instance of Gcode.



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

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

Instance Method Details

#open(file) ⇒ Object



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

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

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



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
# File 'lib/rotor/gcode.rb', line 15

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

  line_num = 0

  @file.each_line do |line|
    parsed_line = parse_line(line)
    
    line_num += 1
    if parsed_line[:g]
      #Move to this origin point.
      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
          @stepper_z.forward(5,25) if @stepper_z
        elsif parsed_line[:z] && parsed_line[:f].nil? && parsed_line[:x].nil? && parsed_line[:y].nil? 
          puts "Raising marker::#{parsed_line}"
          @stepper_z.backwards(5,25) if @stepper_z
          @servo.rotate(:up) if @servo
        else
          puts "Move Stepper::#{parsed_line}"
          move_stepper(parsed_line,speed)
        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
          @stepper_z.forward(5,25) if @stepper_z
        elsif parsed_line[:z] && parsed_line[:f].nil? && parsed_line[:x].nil? && parsed_line[:y].nil? 
          puts "Raising marker::#{parsed_line}"
          @stepper_z.backwards(5,25) if @stepper_z
          @servo.rotate(:up) if @servo
        elsif parsed_line[:z].nil? && parsed_line[:f] && parsed_line[:x].nil? && parsed_line[:y].nil? 
          # Set feed/spin rate
        else
          puts "Move Stepper::#{parsed_line}"
          move_stepper(parsed_line,speed)
        end
      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]

        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,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 "GLINE - Something 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 "MLINE - Something else"
        puts "DEBUG::MLINE - Something else::#{parsed_line}"
      end
    else
      # puts "Something else"
      puts "DEBUG::????? - Something else::#{parsed_line}"
    end
  end
end