Berta_CNC.py 8.1 KB

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