My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

g29_auto.py 3.9KB

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