grbl_11.py 7.6 KB

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