My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

g29_auto.py 4.7KB

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