grbl_laser.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # File Author: Matthieu Berthomé #
  5. # Date: 5/26/2017 #
  6. # MIT Licence #
  7. # ##########################################################
  8. from FlatCAMPostProc import *
  9. # This post processor is configured to output code that
  10. # is compatible with almost any version of Grbl.
  11. class grbl_laser(FlatCAMPostProc):
  12. include_header = True
  13. coordinate_format = "%.*f"
  14. feedrate_format = '%.*f'
  15. def start_code(self, p):
  16. units = ' ' + str(p['units']).lower()
  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. gcode += '(Feedrate: ' + str(p['feedrate']) + units + '/min' + ')\n'
  23. gcode += '(Feedrate rapids ' + str(p['feedrate_rapid']) + units + '/min' + ')\n' + '\n'
  24. gcode += '(Steps per circle: ' + str(p['steps_per_circle']) + ')\n'
  25. if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  26. gcode += '(Preprocessor Excellon: ' + str(p['pp_excellon_name']) + ')\n'
  27. else:
  28. gcode += '(Preprocessor Geometry: ' + str(p['pp_geometry_name']) + ')\n' + '\n'
  29. gcode += '(X range: ' + '{: >9s}'.format(xmin) + ' ... ' + '{: >9s}'.format(xmax) + ' ' + units + ')\n'
  30. gcode += '(Y range: ' + '{: >9s}'.format(ymin) + ' ... ' + '{: >9s}'.format(ymax) + ' ' + units + ')\n\n'
  31. gcode += ('G20' if p.units.upper() == 'IN' else 'G21') + "\n"
  32. gcode += 'G90\n'
  33. gcode += 'G17\n'
  34. gcode += 'G94\n'
  35. return gcode
  36. def startz_code(self, p):
  37. return ''
  38. def lift_code(self, p):
  39. return 'M05 S0'
  40. def down_code(self, p):
  41. if p.spindlespeed:
  42. return 'M03 S%d' % p.spindlespeed
  43. else:
  44. return 'M03'
  45. def toolchange_code(self, p):
  46. return ''
  47. def up_to_zero_code(self, p):
  48. return 'M05'
  49. def position_code(self, p):
  50. return ('X' + self.coordinate_format + ' Y' + self.coordinate_format) % \
  51. (p.coords_decimals, p.x, p.coords_decimals, p.y)
  52. def rapid_code(self, p):
  53. return ('G00 ' + self.position_code(p)).format(**p)
  54. def linear_code(self, p):
  55. return ('G01 ' + self.position_code(p)).format(**p) + \
  56. ' F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  57. def end_code(self, p):
  58. coords_xy = p['xy_toolchange']
  59. gcode = ('G00 Z' + self.feedrate_format % (p.fr_decimals, p.z_end) + "\n")
  60. if coords_xy is not None:
  61. gcode += 'G00 X{x} Y{y}'.format(x=coords_xy[0], y=coords_xy[1]) + "\n"
  62. return gcode
  63. def feedrate_code(self, p):
  64. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.feedrate))
  65. def z_feedrate_code(self, p):
  66. return 'G01 F' + str(self.feedrate_format % (p.fr_decimals, p.z_feedrate))
  67. def spindle_code(self, p):
  68. sdir = {'CW': 'M03', 'CCW': 'M04'}[p.spindledir]
  69. if p.spindlespeed:
  70. return '%s S%s' % (sdir, str(p.spindlespeed))
  71. else:
  72. return sdir
  73. def dwell_code(self, p):
  74. return ''
  75. def spindle_stop_code(self, p):
  76. return 'M05'