Berta_CNC.py 11 KB

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