grbl_11.py 9.4 KB

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