Browse Source

g29 auto-mode for the new G29 LRFB

short:
this script scans the first few lines from a gcode. If the line between 2 different z are greater than min_g1 this is our first layer. On this layer we search the min and max values of X and Y. With an offset we write that in a new file.
Wurstnase 9 years ago
parent
commit
0620267ebb
1 changed files with 164 additions and 0 deletions
  1. 164
    0
      Marlin/scripts/g29_auto.py

+ 164
- 0
Marlin/scripts/g29_auto.py View File

@@ -0,0 +1,164 @@
1
+# This file is for preprocessing gcode and the new G29 Autobedleveling from Marlin
2
+# It will analyse the first 2 Layer and return the maximum size for this part
3
+# After this it will replace with g29_keyword = ';MarlinG29Script' with the new G29 LRFB
4
+# the new file will be created in the same folder.
5
+
6
+# your gcode-file/folder
7
+folder = './'
8
+my_file = 'test.gcode'
9
+
10
+# this is the minimum of G1 instructions which should be between 2 different heights
11
+min_g1 = 3
12
+
13
+#  maximum number of lines to parse, I don't want to parse the complete file
14
+# only the first plane is we are interested in
15
+max_g1 = 1000
16
+
17
+# g29 keyword
18
+g29_keyword = ';MarlinG29Script'
19
+
20
+# output filename
21
+output_file = folder + 'g29_' + my_file
22
+
23
+# offset makes the plane a little bit bigger
24
+offset_x = 10
25
+offset_y = 10
26
+probing_points = 3  # points x points
27
+
28
+# other stuff
29
+min_x = 500
30
+min_y = min_x
31
+max_x = -500
32
+max_y = max_x
33
+last_z = 0.001
34
+
35
+layer = 0
36
+lines_of_g1 = 0
37
+
38
+gcode = []
39
+
40
+
41
+def found_g1(line):
42
+    return line[:2].upper() == "G1"
43
+
44
+
45
+def find(line, axis):
46
+    found = False
47
+    number = ""
48
+    for char in line:
49
+        if found:
50
+            if char == ".":
51
+                number += char
52
+            else:
53
+                try:
54
+                    int(char)
55
+                    number += char
56
+                except ValueError:
57
+                    break
58
+        else:
59
+            found = char.upper() == axis.upper()
60
+    try:
61
+        return float(number)
62
+    except ValueError:
63
+        return None
64
+
65
+
66
+def set_mima(line):
67
+    global min_x, max_x, min_y, max_y, last_z
68
+
69
+    current_x = find(line, 'x')
70
+    current_y = find(line, 'y')
71
+
72
+    if current_x is not None:
73
+        min_x = min(current_x, min_x)
74
+        max_x = max(current_x, max_x)
75
+    if current_y is not None:
76
+        min_y = min(current_y, min_y)
77
+        max_y = max(current_y, max_y)
78
+
79
+    return min_x, max_x, min_y, max_y
80
+
81
+
82
+def find_z(gcode, start_at_line=0):
83
+    for i in range(start_at_line, len(gcode)):
84
+        my_z = find(gcode[i], 'Z')
85
+        if my_z is not None:
86
+            return my_z, i
87
+
88
+
89
+def z_parse(gcode, start_at_line=0, end_at_line=0):
90
+    i = start_at_line
91
+    all_z = []
92
+    line_between_z = []
93
+    z_at_line = []
94
+    # last_z = 0
95
+    last_i = -1
96
+
97
+    while len(gcode) > i:
98
+        try:
99
+            z, i = find_z(gcode, i + 1)
100
+        except TypeError:
101
+            break
102
+
103
+        all_z.append(z)
104
+        z_at_line.append(i)
105
+        line_between_z.append(i - last_i - 1)
106
+        # last_z = z
107
+        last_i = i
108
+        if 0 < end_at_line <= i:
109
+            break
110
+            # print('{}:{}'.format(last_z, last_i))
111
+
112
+    line_between_z = line_between_z[1:]
113
+    return all_z, line_between_z, z_at_line
114
+
115
+
116
+def get_lines(gcode, minimum):
117
+    i = 0
118
+    all_z, line_between_z, z_at_line = z_parse(gcode, end_at_line=max_g1)
119
+    for count in line_between_z:
120
+        i += 1
121
+        if count > minimum:
122
+            return z_at_line[i - 1], z_at_line[i]
123
+
124
+
125
+with open(my_file, 'r') as file:
126
+    lines = 0
127
+    for line in file:
128
+        lines += 1
129
+        if lines > 1000:
130
+            break
131
+        if found_g1(line):
132
+            gcode.append(line)
133
+file.close()
134
+
135
+start, end = get_lines(gcode, min_g1)
136
+for i in range(start, end):
137
+    set_mima(gcode[i])
138
+
139
+min_x = int(min_x) - offset_x
140
+max_x = int(max_x) + offset_x
141
+min_y = int(min_y) - offset_y
142
+max_y = int(max_y) + offset_y
143
+
144
+new_command = 'G29 L{0} R{1} F{2} B{3} P{4}\n'.format(min_x,
145
+                                                      max_x,
146
+                                                      min_y,
147
+                                                      max_y,
148
+                                                      probing_points)
149
+
150
+
151
+out_file = open(output_file, 'w')
152
+print('out_file open')
153
+input_file = open(my_file, 'r')
154
+print('input_file open')
155
+
156
+for line in input_file:
157
+    if line[:len(g29_keyword)] == g29_keyword:
158
+        out_file.write(new_command)
159
+        print('write new_command')
160
+    else:
161
+        out_file.write(line)
162
+
163
+file.close()
164
+out_file.close()

Loading…
Cancel
Save