Marlin.py 8.1 KB

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