Marlin.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # File Author: Marius Adrian Stanciu (c) #
  5. # Date: 3/10/2019 #
  6. # MIT Licence #
  7. # ##########################################################
  8. from AppPreProcessor import *
  9. class Marlin(PreProc):
  10. include_header = True
  11. coordinate_format = "%.*f"
  12. feedrate_format = '%.*f'
  13. feedrate_rapid_format = feedrate_format
  14. def start_code(self, p):
  15. units = ' ' + str(p['units']).lower()
  16. coords_xy = p['xy_toolchange']
  17. gcode = ''
  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'
  24. gcode += ';Feedrate_XY: ' + str(p['feedrate']) + units + '/min' + '\n'
  25. gcode += ';Feedrate_Z: ' + str(p['z_feedrate']) + units + '/min' + '\n'
  26. gcode += ';Feedrate rapids ' + str(p['feedrate_rapid']) + units + '/min' + '\n' + '\n'
  27. gcode += ';Z_Cut: ' + str(p['z_cut']) + units + '\n'
  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. elif str(p['options']['type']) == 'Excellon' and p['use_ui'] is True:
  33. gcode += '\n;TOOLS DIAMETER: \n'
  34. for tool, val in p['exc_tools'].items():
  35. gcode += ';Tool: %s -> ' % str(tool) + 'Dia: %s' % str(val["C"]) + '\n'
  36. gcode += '\n;FEEDRATE Z: \n'
  37. for tool, val in p['exc_tools'].items():
  38. gcode += ';Tool: %s -> ' % str(tool) + 'Feedrate: %s' % str(val['data']["feedrate_z"]) + '\n'
  39. gcode += '\n;FEEDRATE RAPIDS: \n'
  40. for tool, val in p['exc_tools'].items():
  41. gcode += ';Tool: %s -> ' % str(tool) + 'Feedrate Rapids: %s' % \
  42. str(val['data']["feedrate_rapid"]) + '\n'
  43. gcode += '\n;Z_CUT: \n'
  44. for tool, val in p['exc_tools'].items():
  45. gcode += ';Tool: %s -> ' % str(tool) + 'Z_Cut: %s' % str(val['data']["cutz"]) + '\n'
  46. gcode += '\n;Tools Offset: \n'
  47. for tool, val in p['exc_cnc_tools'].items():
  48. gcode += ';Tool: %s -> ' % str(val['tool']) + 'Offset Z: %s' % str(val['offset_z']) + '\n'
  49. if p['multidepth'] is True:
  50. gcode += '\n;DEPTH_PER_CUT: \n'
  51. for tool, val in p['exc_tools'].items():
  52. gcode += ';Tool: %s -> ' % str(tool) + 'DeptPerCut: %s' % \
  53. str(val['data']["depthperpass"]) + '\n'
  54. gcode += '\n;Z_MOVE: \n'
  55. for tool, val in p['exc_tools'].items():
  56. gcode += ';Tool: %s -> ' % str(tool) + 'Z_Move: %s' % str(val['data']["travelz"]) + '\n'
  57. gcode += '\n'
  58. if p['toolchange'] is True:
  59. gcode += ';Z Toolchange: ' + str(p['z_toolchange']) + units + '\n'
  60. if coords_xy is not None:
  61. gcode += ';X,Y Toolchange: ' + "%.*f, %.*f" % (p.decimals, coords_xy[0],
  62. p.decimals, coords_xy[1]) + units + '\n'
  63. else:
  64. gcode += ';X,Y Toolchange: ' + "None" + units + '\n'
  65. gcode += ';Z Start: ' + str(p['startz']) + units + '\n'
  66. gcode += ';Z End: ' + str(p['z_end']) + units + '\n'
  67. gcode += ';Steps per circle: ' + str(p['steps_per_circle']) + '\n'
  68. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  69. gcode += ';Preprocessor Excellon: ' + str(p['pp_excellon_name']) + '\n' + '\n'
  70. else:
  71. gcode += ';Preprocessor Geometry: ' + str(p['pp_geometry_name']) + '\n' + '\n'
  72. gcode += ';X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + '\n'
  73. gcode += ';Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + '\n\n'
  74. gcode += ';Spindle Speed: %s RPM)\n' % str(p['spindlespeed'])
  75. gcode += ('G20' if p.units.upper() == 'IN' else 'G21') + "\n"
  76. gcode += 'G90'
  77. return gcode
  78. def startz_code(self, p):
  79. if p.startz is not None:
  80. return 'G0 Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  81. else:
  82. return ''
  83. def lift_code(self, p):
  84. return 'G0 Z' + self.coordinate_format % (p.coords_decimals, p.z_move) + " " + self.feedrate_rapid_code(p)
  85. def down_code(self, p):
  86. return 'G1 Z' + self.coordinate_format % (p.coords_decimals, p.z_cut) + " " + self.inline_z_feedrate_code(p)
  87. def toolchange_code(self, p):
  88. z_toolchange = p.z_toolchange
  89. toolchangexy = p.xy_toolchange
  90. f_plunge = p.f_plunge
  91. if toolchangexy is not None:
  92. x_toolchange = toolchangexy[0]
  93. y_toolchange = toolchangexy[1]
  94. else:
  95. x_toolchange = 0.0
  96. y_toolchange = 0.0
  97. no_drills = 1
  98. if int(p.tool) == 1 and p.startz is not None:
  99. z_toolchange = p.startz
  100. toolC_formatted = '%.*f' % (p.decimals, p.toolC)
  101. if str(p['options']['type']) == 'Excellon':
  102. for i in p['options']['Tools_in_use']:
  103. if i[0] == p.tool:
  104. no_drills = i[2]
  105. if toolchangexy is not None:
  106. gcode = """
  107. M5
  108. G0 Z{z_toolchange}
  109. G0 X{x_toolchange} Y{y_toolchange}
  110. T{tool}
  111. M6
  112. ;MSG, Change to Tool Dia = {toolC}, Total drills for tool T{tool} = {t_drills}
  113. M0
  114. G0 Z{z_toolchange}
  115. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  116. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  117. 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. else:
  122. gcode = """
  123. M5
  124. G0 Z{z_toolchange}
  125. T{tool}
  126. M6
  127. ;MSG, Change to Tool Dia = {toolC}, Total drills for tool T{tool} = {t_drills}
  128. M0
  129. G0 Z{z_toolchange}
  130. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  131. tool=int(p.tool),
  132. t_drills=no_drills,
  133. toolC=toolC_formatted)
  134. if f_plunge is True:
  135. gcode += '\nG0 Z%.*f' % (p.coords_decimals, p.z_move)
  136. return gcode
  137. else:
  138. if toolchangexy is not None:
  139. gcode = """
  140. M5
  141. G0 Z{z_toolchange}
  142. G0 X{x_toolchange} Y{y_toolchange}
  143. T{tool}
  144. M6
  145. ;MSG, Change to Tool Dia = {toolC}
  146. M0
  147. G0 Z{z_toolchange}
  148. """.format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  149. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  150. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  151. tool=int(p.tool),
  152. toolC=toolC_formatted)
  153. else:
  154. gcode = """
  155. M5
  156. G0 Z{z_toolchange}
  157. T{tool}
  158. M6
  159. ;MSG, Change to Tool Dia = {toolC}
  160. M0
  161. G0 Z{z_toolchange}
  162. """.format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  163. tool=int(p.tool),
  164. toolC=toolC_formatted)
  165. if f_plunge is True:
  166. gcode += '\nG0 Z%.*f' % (p.coords_decimals, p.z_move)
  167. return gcode
  168. def up_to_zero_code(self, p):
  169. return 'G1 Z0' + " " + self.feedrate_code(p)
  170. def position_code(self, p):
  171. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  172. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  173. def rapid_code(self, p):
  174. return ('G0 ' + self.position_code(p)).format(**p) + " " + self.feedrate_rapid_code(p)
  175. def linear_code(self, p):
  176. return ('G1 ' + self.position_code(p)).format(**p) + " " + self.inline_feedrate_code(p)
  177. def end_code(self, p):
  178. coords_xy = p['xy_end']
  179. gcode = ('G0 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + " " + self.feedrate_rapid_code(p) + "\n")
  180. if coords_xy and coords_xy != '':
  181. gcode += 'G0 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + " " + self.feedrate_rapid_code(p) + "\n"
  182. return gcode
  183. def feedrate_code(self, p):
  184. return 'G1 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  185. def inline_feedrate_code(self, p):
  186. return 'F' + self.feedrate_format % (p.fr_decimals, p.feedrate)
  187. def inline_z_feedrate_code(self, p):
  188. return 'F' + self.feedrate_format % (p.fr_decimals, p.z_feedrate)
  189. def z_feedrate_code(self, p):
  190. return 'G1 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  191. def feedrate_rapid_code(self, p):
  192. return 'F' + self.feedrate_rapid_format % (p.fr_decimals, p.feedrate_rapid)
  193. def spindle_code(self, p):
  194. sdir = {'CW': 'M3', 'CCW': 'M4'}[p.spindledir]
  195. if p.spindlespeed:
  196. return '%s S%s' % (sdir, str(p.spindlespeed))
  197. else:
  198. return sdir
  199. def dwell_code(self, p):
  200. gcode = 'G4 P' + str(p.dwelltime)
  201. if p.dwelltime:
  202. return gcode
  203. def spindle_stop_code(self, p):
  204. gcode = 'M400\n'
  205. gcode += 'M5'
  206. return gcode