grbl_11.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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' + '\n'
  24. gcode += '(Feedrate: ' + str(p['feedrate']) + units + '/min' + ')\n'
  25. if str(p['options']['type']) == 'Geometry':
  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 str(p['options']['type']) == 'Geometry':
  30. if p['multidepth'] is True:
  31. gcode += '(DepthPerCut: ' + str(p['z_depthpercut']) + units + ' <=>' + \
  32. str(math.ceil(abs(p['z_cut']) / p['z_depthpercut'])) + ' passes' + ')\n'
  33. gcode += '(Z_Move: ' + str(p['z_move']) + units + ')\n'
  34. gcode += '(Z Toolchange: ' + str(p['z_toolchange']) + units + ')\n'
  35. if coords_xy is not None:
  36. gcode += '(X,Y Toolchange: ' + "%.*f, %.*f" % (p.decimals, coords_xy[0],
  37. p.decimals, coords_xy[1]) + units + ')\n'
  38. else:
  39. gcode += '(X,Y Toolchange: ' + "None" + units + ')\n'
  40. gcode += '(Z Start: ' + str(p['startz']) + units + ')\n'
  41. gcode += '(Z End: ' + str(p['z_end']) + units + ')\n'
  42. gcode += '(Steps per circle: ' + str(p['steps_per_circle']) + ')\n'
  43. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  44. gcode += '(Preprocessor Excellon: ' + str(p['pp_excellon_name']) + ')\n'
  45. else:
  46. gcode += '(Preprocessor Geometry: ' + str(p['pp_geometry_name']) + ')\n' + '\n'
  47. gcode += '(X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + ')\n'
  48. gcode += '(Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + ')\n\n'
  49. gcode += '(Spindle Speed: ' + str(p['spindlespeed']) + ' RPM' + ')\n' + '\n'
  50. gcode += ('G20' if p.units.upper() == 'IN' else 'G21') + "\n"
  51. gcode += 'G90\n'
  52. gcode += 'G17\n'
  53. gcode += 'G94\n'
  54. return gcode
  55. def startz_code(self, p):
  56. if p.startz is not None:
  57. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  58. else:
  59. return ''
  60. def lift_code(self, p):
  61. return 'G00 Z' + self.coordinate_format % (p.coords_decimals, p.z_move)
  62. def down_code(self, p):
  63. return 'G01 Z' + self.coordinate_format % (p.coords_decimals, p.z_cut)
  64. def toolchange_code(self, p):
  65. z_toolchange = p.z_toolchange
  66. toolchangexy = p.xy_toolchange
  67. f_plunge = p.f_plunge
  68. if toolchangexy is not None:
  69. x_toolchange = toolchangexy[0]
  70. y_toolchange = toolchangexy[1]
  71. else:
  72. x_toolchange = 0.0
  73. y_toolchange = 0.0
  74. no_drills = 1
  75. if int(p.tool) == 1 and p.startz is not None:
  76. z_toolchange = p.startz
  77. toolC_formatted = '%.*f' % (p.decimals, p.toolC)
  78. if str(p['options']['type']) == 'Excellon':
  79. for i in p['options']['Tools_in_use']:
  80. if i[0] == p.tool:
  81. no_drills = i[2]
  82. if toolchangexy is not None:
  83. gcode = """
  84. M5
  85. G00 Z{z_toolchange}
  86. G00 X{x_toolchange} Y{y_toolchange}
  87. T{tool}
  88. M6
  89. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  90. M0
  91. G00 Z{z_toolchange}
  92. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  93. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  94. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  95. tool=int(p.tool),
  96. t_drills=no_drills,
  97. toolC=toolC_formatted)
  98. else:
  99. gcode = """
  100. M5
  101. G00 Z{z_toolchange}
  102. T{tool}
  103. M6
  104. (MSG, Change to Tool Dia = {toolC} ||| Total drills for tool T{tool} = {t_drills})
  105. M0
  106. G00 Z{z_toolchange}
  107. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  108. tool=int(p.tool),
  109. t_drills=no_drills,
  110. toolC=toolC_formatted)
  111. if f_plunge is True:
  112. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  113. return gcode
  114. else:
  115. if toolchangexy is not None:
  116. gcode = """
  117. M5
  118. G00 Z{z_toolchange}
  119. G00 X{x_toolchange} Y{y_toolchange}
  120. T{tool}
  121. M6
  122. (MSG, Change to Tool Dia = {toolC})
  123. M0
  124. G00 Z{z_toolchange}
  125. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  126. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  127. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  128. tool=int(p.tool),
  129. toolC=toolC_formatted)
  130. else:
  131. gcode = """
  132. M5
  133. G00 Z{z_toolchange}
  134. T{tool}
  135. M6
  136. (MSG, Change to Tool Dia = {toolC})
  137. M0
  138. G00 Z{z_toolchange}
  139. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  140. tool=int(p.tool),
  141. toolC=toolC_formatted)
  142. if f_plunge is True:
  143. gcode += '\nG00 Z%.*f' % (p.coords_decimals, p.z_move)
  144. return gcode
  145. def up_to_zero_code(self, p):
  146. return 'G01 Z0'
  147. def position_code(self, p):
  148. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  149. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  150. def rapid_code(self, p):
  151. return ('G00 ' + self.position_code(p)).format(**p)
  152. def linear_code(self, p):
  153. return ('G01 ' + self.position_code(p)).format(**p) + \
  154. ' F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  155. def end_code(self, p):
  156. coords_xy = p['xy_toolchange']
  157. gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n")
  158. if coords_xy is not None:
  159. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  160. return gcode
  161. def feedrate_code(self, p):
  162. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  163. def z_feedrate_code(self, p):
  164. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  165. def spindle_code(self, p):
  166. sdir = {'CW': 'M03', 'CCW': 'M04'}[p.spindledir]
  167. if p.spindlespeed:
  168. return '%s S%s' % (sdir, str(p.spindlespeed))
  169. else:
  170. return sdir
  171. def dwell_code(self, p):
  172. if p.dwelltime:
  173. return 'G4 P' + str(p.dwelltime)
  174. def spindle_stop_code(self, p):
  175. return 'M05'