g29_auto.py 4.8 KB

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