Berta_CNC.py 8.1 KB

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