Marlin.py 8.0 KB

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