grbl_11.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # File Author: Matthieu Berthomé #
  5. # Date: 5/26/2017 #
  6. # MIT Licence #
  7. # ##########################################################
  8. from appPreProcessor import *
  9. class grbl_11(PreProc):
  10. include_header = True
  11. coordinate_format = "%.*f"
  12. feedrate_format = '%.*f'
  13. def start_code(self, p):
  14. units = ' ' + str(p['units']).lower()
  15. coords_xy = p['xy_toolchange']
  16. end_coords_xy = p['xy_end']
  17. gcode = '(This preprocessor is used with a motion controller loaded with GRBL firmware.)\n'
  18. gcode += '(It is configured to be compatible with almost any version of GRBL firmware.)\n\n'
  19. xmin = '%.*f' % (p.coords_decimals, p['options']['xmin'])
  20. xmax = '%.*f' % (p.coords_decimals, p['options']['xmax'])
  21. ymin = '%.*f' % (p.coords_decimals, p['options']['ymin'])
  22. ymax = '%.*f' % (p.coords_decimals, p['options']['ymax'])
  23. if str(p['options']['type']) == 'Geometry':
  24. gcode += '(TOOL DIAMETER: ' + str(p['options']['tool_dia']) + units + ')\n'
  25. gcode += '(Feedrate_XY: ' + str(p['feedrate']) + units + '/min' + ')\n'
  26. gcode += '(Feedrate_Z: ' + str(p['z_feedrate']) + units + '/min' + ')\n'
  27. gcode += '(Feedrate rapids ' + str(p['feedrate_rapid']) + units + '/min' + ')\n' + '\n'
  28. gcode += '(Z_Cut: ' + str(p['z_cut']) + units + ')\n'
  29. if p['multidepth'] is True:
  30. gcode += '(DepthPerCut: ' + str(p['z_depthpercut']) + units + ' <=>' + \
  31. str(math.ceil(abs(p['z_cut']) / p['z_depthpercut'])) + ' passes' + ')\n'
  32. gcode += '(Z_Move: ' + str(p['z_move']) + units + ')\n'
  33. elif str(p['options']['type']) == 'Excellon' and p['use_ui'] is True:
  34. gcode += '\n(TOOLS DIAMETER: )\n'
  35. for tool, val in p['exc_tools'].items():
  36. gcode += '(Tool: %s -> ' % str(tool) + 'Dia: %s' % str(val["tooldia"]) + ')\n'
  37. gcode += '\n(FEEDRATE Z: )\n'
  38. for tool, val in p['exc_tools'].items():
  39. gcode += '(Tool: %s -> ' % str(tool) + 'Feedrate: %s' % \
  40. str(val['data']["tools_drill_feedrate_z"]) + ')\n'
  41. gcode += '\n(FEEDRATE RAPIDS: )\n'
  42. for tool, val in p['exc_tools'].items():
  43. gcode += '(Tool: %s -> ' % str(tool) + 'Feedrate Rapids: %s' % \
  44. str(val['data']["tools_drill_feedrate_rapid"]) + ')\n'
  45. gcode += '\n(Z_CUT: )\n'
  46. for tool, val in p['exc_tools'].items():
  47. gcode += '(Tool: %s -> ' % str(tool) + 'Z_Cut: %s' % str(val['data']["tools_drill_cutz"]) + ')\n'
  48. gcode += '\n(Tools Offset: )\n'
  49. for tool, val in p['exc_cnc_tools'].items():
  50. gcode += '(Tool: %s -> ' % str(val['tool']) + 'Offset Z: %s' % \
  51. str(val['data']["tools_drill_offset"]) + ')\n'
  52. if p['multidepth'] is True:
  53. gcode += '\n(DEPTH_PER_CUT: )\n'
  54. for tool, val in p['exc_tools'].items():
  55. gcode += '(Tool: %s -> ' % str(tool) + 'DeptPerCut: %s' % \
  56. str(val['data']["tools_drill_depthperpass"]) + ')\n'
  57. gcode += '\n(Z_MOVE: )\n'
  58. for tool, val in p['exc_tools'].items():
  59. gcode += '(Tool: %s -> ' % str(tool) + 'Z_Move: %s' % str(val['data']["tools_drill_travelz"]) + ')\n'
  60. gcode += '\n'
  61. if p['toolchange'] is True:
  62. gcode += '(Z Toolchange: ' + str(p['z_toolchange']) + units + ')\n'
  63. if coords_xy is not None:
  64. gcode += '(X,Y Toolchange: ' + "%.*f, %.*f" % (p.decimals, coords_xy[0],
  65. p.decimals, coords_xy[1]) + units + ')\n'
  66. else:
  67. gcode += '(X,Y Toolchange: ' + "None" + units + ')\n'
  68. gcode += '(Z Start: ' + str(p['startz']) + units + ')\n'
  69. gcode += '(Z End: ' + str(p['z_end']) + units + ')\n'
  70. if end_coords_xy is not None:
  71. gcode += '(X,Y End: ' + "%.*f, %.*f" % (p.decimals, end_coords_xy[0],
  72. p.decimals, end_coords_xy[1]) + units + ')\n'
  73. else:
  74. gcode += '(X,Y End: ' + "None" + units + ')\n'
  75. gcode += '(Steps per circle: ' + str(p['steps_per_circle']) + ')\n'
  76. gcode += '(Steps per circle: ' + str(p['steps_per_circle']) + ')\n'
  77. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  78. gcode += '(Preprocessor Excellon: ' + str(p['pp_excellon_name']) + ')\n' + '\n'
  79. else:
  80. gcode += '(Preprocessor Geometry: ' + str(p['pp_geometry_name']) + ')\n' + '\n'
  81. gcode += '(X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + ')\n'
  82. gcode += '(Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + ')\n\n'
  83. gcode += '(Spindle Speed: %s RPM)\n' % str(p['spindlespeed'])
  84. gcode += ('G20' if p.units.upper() == 'IN' else 'G21') + "\n"
  85. gcode += 'G90\n'
  86. gcode += 'G17\n'
  87. gcode += 'G94\n'
  88. return gcode
  89. def startz_code(self, p):
  90. if p.startz is not None:
  91. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  92. else:
  93. return ''
  94. def lift_code(self, p):
  95. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.z_move)
  96. def down_code(self, p):
  97. return 'G01 Z' + self.coordinate_format % (p.coords_decimals, p.z_cut)
  98. def toolchange_code(self, p):
  99. z_toolchange = p.z_toolchange
  100. toolchangexy = p.xy_toolchange
  101. f_plunge = p.f_plunge
  102. if toolchangexy is not None:
  103. x_toolchange = toolchangexy[0]
  104. y_toolchange = toolchangexy[1]
  105. else:
  106. x_toolchange = 0.0
  107. y_toolchange = 0.0
  108. no_drills = 1
  109. if int(p.tool) == 1 and p.startz is not None:
  110. z_toolchange = p.startz
  111. toolC_formatted = '%.*f' % (p.decimals, p.toolC)
  112. if str(p['options']['type']) == 'Excellon':
  113. for i in p['options']['Tools_in_use']:
  114. if i[0] == p.tool:
  115. no_drills = i[2]
  116. if toolchangexy is not None:
  117. gcode = """
  118. M5
  119. G00 Z{z_toolchange}
  120. G00 X{x_toolchange} Y{y_toolchange}
  121. T{tool}
  122. M6
  123. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  124. M0
  125. G00 Z{z_toolchange}
  126. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  127. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  128. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  129. tool=int(p.tool),
  130. t_drills=no_drills,
  131. toolC=toolC_formatted)
  132. else:
  133. gcode = """
  134. M5
  135. G00 Z{z_toolchange}
  136. T{tool}
  137. M6
  138. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  139. M0
  140. G00 Z{z_toolchange}
  141. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  142. tool=int(p.tool),
  143. t_drills=no_drills,
  144. toolC=toolC_formatted)
  145. if f_plunge is True:
  146. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  147. return gcode
  148. else:
  149. if toolchangexy is not None:
  150. gcode = """
  151. M5
  152. G00 Z{z_toolchange}
  153. G00 X{x_toolchange} Y{y_toolchange}
  154. T{tool}
  155. M6
  156. (MSG, Change to Tool Dia = {toolC})
  157. M0
  158. G00 Z{z_toolchange}
  159. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  160. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  161. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  162. tool=int(p.tool),
  163. toolC=toolC_formatted)
  164. else:
  165. gcode = """
  166. M5
  167. G00 Z{z_toolchange}
  168. T{tool}
  169. M6
  170. (MSG, Change to Tool Dia = {toolC})
  171. M0
  172. G00 Z{z_toolchange}
  173. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  174. tool=int(p.tool),
  175. toolC=toolC_formatted)
  176. if f_plunge is True:
  177. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  178. return gcode
  179. def up_to_zero_code(self, p):
  180. return 'G01 Z0'
  181. def position_code(self, p):
  182. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  183. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  184. def rapid_code(self, p):
  185. return ('G00 ' + self.position_code(p)).format(**p)
  186. def linear_code(self, p):
  187. return ('G01 ' + self.position_code(p)).format(**p) + \
  188. ' F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  189. def end_code(self, p):
  190. coords_xy = p['xy_end']
  191. gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n")
  192. if coords_xy and coords_xy != '':
  193. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  194. return gcode
  195. def feedrate_code(self, p):
  196. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  197. def z_feedrate_code(self, p):
  198. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  199. def spindle_code(self, p):
  200. sdir = {'CW': 'M03', 'CCW': 'M04'}[p.spindledir]
  201. if p.spindlespeed:
  202. return '%s S%s' % (sdir, str(p.spindlespeed))
  203. else:
  204. return sdir
  205. def dwell_code(self, p):
  206. if p.dwelltime:
  207. return 'G4 P' + str(p.dwelltime)
  208. def spindle_stop_code(self, p):
  209. return 'M05'