marlin.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. coordinate_format = "%.*f"
  11. feedrate_format = '%.*f'
  12. feedrate_rapid_format = feedrate_format
  13. def start_code(self, p):
  14. units = ' ' + str(p['units']).lower()
  15. coords_xy = p['xy_toolchange']
  16. gcode = ''
  17. xmin = '%.*f' % (p.coords_decimals, p['options']['xmin'])
  18. xmax = '%.*f' % (p.coords_decimals, p['options']['xmax'])
  19. ymin = '%.*f' % (p.coords_decimals, p['options']['ymin'])
  20. ymax = '%.*f' % (p.coords_decimals, p['options']['ymax'])
  21. if str(p['options']['type']) == 'Geometry':
  22. gcode += ';TOOL DIAMETER: ' + str(p['options']['tool_dia']) + units + '\n' + '\n'
  23. gcode += ';Feedrate: ' + str(p['feedrate']) + units + '/min' + '\n'
  24. if str(p['options']['type']) == 'Geometry':
  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 str(p['options']['type']) == 'Geometry':
  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: ' + "%.4f, %.4f" % (coords_xy[0], coords_xy[1]) + units + '\n'
  36. else:
  37. gcode += ';X,Y Toolchange: ' + "None" + units + '\n'
  38. gcode += ';Z Start: ' + str(p['startz']) + units + '\n'
  39. gcode += ';Z End: ' + str(p['z_end']) + units + '\n'
  40. gcode += ';Steps per circle: ' + str(p['steps_per_circle']) + '\n'
  41. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  42. gcode += ';Postprocessor Excellon: ' + str(p['pp_excellon_name']) + '\n'
  43. else:
  44. gcode += ';Postprocessor Geometry: ' + str(p['pp_geometry_name']) + '\n' + '\n'
  45. gcode += ';X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + '\n'
  46. gcode += ';Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + '\n\n'
  47. gcode += ';Spindle Speed: ' + str(p['spindlespeed']) + ' RPM' + '\n' + '\n'
  48. gcode += ('G20' if p.units.upper() == 'IN' else 'G21') + "\n"
  49. gcode += 'G90\n'
  50. return gcode
  51. def startz_code(self, p):
  52. if p.startz is not None:
  53. return 'G0 Z' + self.coordinate_format % (p.coords_decimals, p.startz)
  54. else:
  55. return ''
  56. def lift_code(self, p):
  57. return 'G0 Z' + self.coordinate_format%(p.coords_decimals, p.z_move) + " " + self.feedrate_rapid_code(p)
  58. def down_code(self, p):
  59. return 'G1 Z' + self.coordinate_format%(p.coords_decimals, p.z_cut) + " " + self.end_feedrate_code(p)
  60. def toolchange_code(self, p):
  61. z_toolchange = p.z_toolchange
  62. toolchangexy = p.xy_toolchange
  63. f_plunge = p.f_plunge
  64. gcode = ''
  65. if toolchangexy is not None:
  66. x_toolchange = toolchangexy[0]
  67. y_toolchange = toolchangexy[1]
  68. no_drills = 1
  69. if int(p.tool) == 1 and p.startz is not None:
  70. z_toolchange = p.startz
  71. if p.units.upper() == 'MM':
  72. toolC_formatted = format(p.toolC, '.2f')
  73. else:
  74. toolC_formatted = format(p.toolC, '.4f')
  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""".format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  88. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  89. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  90. tool=int(p.tool),
  91. t_drills=no_drills,
  92. toolC=toolC_formatted)
  93. else:
  94. gcode = """
  95. M5
  96. G0 Z{z_toolchange}
  97. T{tool}
  98. M6
  99. ;MSG, Change to Tool Dia = {toolC}, Total drills for tool T{tool} = {t_drills}
  100. M0""".format(z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  101. tool=int(p.tool),
  102. t_drills=no_drills,
  103. toolC=toolC_formatted)
  104. if f_plunge is True:
  105. gcode += '\nG0 Z%.*f' % (p.coords_decimals, p.z_move)
  106. return gcode
  107. else:
  108. if toolchangexy is not None:
  109. gcode = """
  110. M5
  111. G0 Z{z_toolchange}
  112. G0 X{x_toolchange} Y{y_toolchange}
  113. T{tool}
  114. M6
  115. ;MSG, Change to Tool Dia = {toolC}
  116. M0""".format(x_toolchange=self.coordinate_format % (p.coords_decimals, x_toolchange),
  117. y_toolchange=self.coordinate_format % (p.coords_decimals, y_toolchange),
  118. z_toolchange=self.coordinate_format % (p.coords_decimals, z_toolchange),
  119. tool=int(p.tool),
  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}
  128. M0""".format(z_toolchange=self.coordinate_format%(p.coords_decimals, z_toolchange),
  129. tool=int(p.tool),
  130. toolC=toolC_formatted)
  131. if f_plunge is True:
  132. gcode += '\nG0 Z%.*f' % (p.coords_decimals, p.z_move)
  133. return gcode
  134. def up_to_zero_code(self, p):
  135. return 'G1 Z0' + " " + self.feedrate_code(p)
  136. def position_code(self, p):
  137. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  138. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  139. def rapid_code(self, p):
  140. return ('G0 ' + self.position_code(p)).format(**p) + " " + self.feedrate_rapid_code(p)
  141. def linear_code(self, p):
  142. return ('G1 ' + self.position_code(p)).format(**p) + " " + self.end_feedrate_code(p)
  143. def end_code(self, p):
  144. coords_xy = p['xy_toolchange']
  145. gcode = ('G0 Z' + self.feedrate_format %(p.fr_decimals, p.z_end) + " " + self.feedrate_rapid_code(p) + "\n")
  146. if coords_xy is not None:
  147. gcode += 'G0 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + " " + self.feedrate_rapid_code(p) + "\n"
  148. return gcode
  149. def feedrate_code(self, p):
  150. return 'G1 F' + str(self.feedrate_format %(p.fr_decimals, p.feedrate))
  151. def end_feedrate_code(self, p):
  152. return 'F' + self.feedrate_format %(p.fr_decimals, p.feedrate)
  153. def z_feedrate_code(self, p):
  154. return 'G1 F' + str(self.feedrate_format %(p.fr_decimals, p.z_feedrate))
  155. def feedrate_rapid_code(self, p):
  156. return 'F' + self.feedrate_rapid_format % (p.fr_decimals, p.feedrate_rapid)
  157. def spindle_code(self, p):
  158. sdir = {'CW': 'M3', 'CCW': 'M4'}[p.spindledir]
  159. if p.spindlespeed:
  160. return '%s S%s' % (sdir, str(p.spindlespeed))
  161. else:
  162. return sdir
  163. def dwell_code(self, p):
  164. if p.dwelltime:
  165. return 'G4 P' + str(p.dwelltime)
  166. def spindle_stop_code(self,p):
  167. return 'M5'