printrun-src/printrun/laser.py

changeset 43
f7e9bd735ce1
parent 42
ea4c43494a19
--- a/printrun-src/printrun/laser.py	Thu May 30 19:02:52 2019 +0200
+++ b/printrun-src/printrun/laser.py	Tue Jan 19 20:25:47 2021 +0100
@@ -1,6 +1,6 @@
 """
 Lasercutter library
-2015-2017 by NeoSoft, Malte Di Donato
+2015-2019 by NeoSoft, Malte Di Donato
 Intended to use standalone or implemented in Pronterface/Printrun
 """
 
@@ -18,7 +18,31 @@
 # GENERAL HEADER AND FOOTER GCODE
 GCODE_HEAD = """
 ; GCode generated by laser.py pronterface library (marlin code flavour)
-; 2015-2017 by NeoSoft - Malte Di Donato
+; 2015-2019 by NeoSoft - Malte Di Donato
+
+G21 ; Metric
+; We assume Z is in focus height and laser head is focus at bottom left of image!
+G92 X0 Y0 E0; set zero position - new origin
+G90 ; absolute positioning
+M201 X1000 Y1000 ; Set acceleration
+M203 X1000 Y1000 ; Set max feedrate
+M209 S0 ; disable firmware retraction, we dont want to burn holes...
+;M85 S0 ; Disable idle hold timeout (BUG!)
+M84 ; enable motors
+
+"""
+
+GCODE_FOOT = """
+M400 ; Wait for all moves to finish
+M5 ; Force laser off!
+G0 X0 Y0 F%.4f ; Move back to origin
+; M501 ; undo all settings made
+""" % (100*60)
+
+
+GCODE_HEAD_MELZI = """
+; GCode generated by laser.py pronterface library (marlin code flavour)
+; 2015-2019 by NeoSoft - Malte Di Donato
 
 G21 ; Metric
 ; We assume Z is in focus height and laser head is focus at bottom left of image!
@@ -35,7 +59,7 @@
 
 """
 
-GCODE_FOOT = """
+GCODE_FOOT_MELZI = """
 M400 ; Wait for all moves to finish
 M42 P28 S0 ; Force laser off!
 ;M85 S30 ; re-enable idle hold timeout (BUG!)
@@ -56,6 +80,9 @@
         # 10mm/sec for black anodized aluminum to get maximum possible engraving! @ 100% power
         self.lc_travel_speed = 120
 
+        # insert config option to enable the Melzi Marlin FW Hack (M571)
+        self.lc_melzi_hack = False
+
         # BITMAP:
         self.lc_bitmap_speed_factor = 1.0
         self.lc_dpi = 300
@@ -151,7 +178,10 @@
         pix = im.load()
 
         fo = open(filename + ".g", "w")
-        fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
+        if self.settings.lc_melzi_hack:
+            fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD_MELZI))
+        else:
+            fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
 
         fo.write(";Start engraving the raster image: %dx%d points @ %d DPI = %.0fx%.0f mm\n\n" % (
             im.size[0], im.size[1], self.settings.lc_dpi,
@@ -237,7 +267,10 @@
             if self.settings.lc_change_dir:
                 DIR = DIR * (-1) # change y direction on every X
 
-        fo.write(GCODE_FOOT)
+        if self.settings.lc_melzi_hack:
+            fo.write(GCODE_FOOT_MELZI)
+        else:
+            fo.write(GCODE_FOOT)
         fo.close()
 
         if self.pronterwindow:
@@ -255,7 +288,10 @@
     
         fi = open(filename, "r")
         fo = open(filename + ".g", "w")
-        fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
+        if self.settings.lc_melzi_hack:
+            fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD_MELZI))
+        else:
+            fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
 
         G = "0"
         LASER_STATE = 0
@@ -303,7 +339,10 @@
                         print "UNKNOWN: %s" % action
                     last_cmd = cmd            
     
-        fo.write(GCODE_FOOT)
+        if self.settings.lc_melzi_hack:
+            fo.write(GCODE_FOOT_MELZI)
+        else:
+            fo.write(GCODE_FOOT)
         fi.close()
         fo.close()    
 
@@ -423,8 +462,11 @@
         self.log("Scaling factor: %.2f, %.2f" % (scale_x,scale_y))
 
         fo = open(filename + ".g", "w")
-        fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
-        fo.write("M571 S0 E1 ; On SVG we control the laser by ourself\n")
+        if self.settings.lc_melzi_hack:
+            fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD_MELZI))
+            fo.write("M571 S0 E1 ; On SVG we control the laser by ourself\n")
+        else:
+            fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
 
         travel_speed = self.settings.lc_travel_speed * 60
         engrave_speed = self.settings.lc_engrave_speed * 60 * self.settings.lc_svg_speed_factor
@@ -453,6 +495,7 @@
                             self.log("Ignoring shape %s (%i) by stroke color" % (tag_suffix, elemidx))
                             skip = True
                             break
+                # TODO: do a config option to enter the CUT color or something
                 if skip:
                     continue
 
@@ -463,8 +506,9 @@
                 mat = shape_obj.transformation_matrix()
 
                 if d:
-                    fo.write("M400 ; start %s\n" % (tag_suffix))
-                    fo.write("G92 E0\n") 
+                    if self.settings.lc_melzi_hack:
+                        fo.write("M400 ; start %s\n" % (tag_suffix))
+                        fo.write("G92 E0\n") 
                     E = 0
                     xo = 0
                     yo = 0
@@ -489,12 +533,19 @@
                             y = height - y # invert the bed
                             xs = scale_x * x
                             ys = scale_y * y
-                            fo.write("M400 ; Wait for all moves to finish\n")
-                            fo.write("M42 P28 S0 ; Turn off laser\n")
-                            fo.write("G0 X%0.4f Y%0.4f F%.4f ; Move to start of shape\n" % (
-                                xs, ys, travel_speed))
-                            fo.write("M400 ; Wait for all moves to finish\n")
-                            fo.write("M42 P28 S255 ; Turn on laser\n")
+                            if self.settings.lc_melzi_hack:
+                                fo.write("M400 ; Wait for all moves to finish\n")
+                                fo.write("M42 P28 S0 ; Turn off laser\n")
+                                fo.write("G0 X%0.4f Y%0.4f F%.4f ; Move to start of shape\n" % (
+                                    xs, ys, travel_speed))
+                                fo.write("M400 ; Wait for all moves to finish\n")
+                                fo.write("M42 P28 S255 ; Turn on laser\n")
+                            else:
+                                fo.write("M5 ; Turn off laser\n")
+                                fo.write("G0 X%0.4f Y%0.4f F%.4f ; Move to start of shape\n" % (
+                                    xs, ys, travel_speed))
+                                # todo: laser power as parameter?
+                                fo.write("M3 S100 ; Turn on laser\n")
 
                             xo = xs
                             yo = ys 
@@ -521,11 +572,22 @@
                                 E = E + (e_distance)
 
                                 if xs >= 0 and xs <= bed_max_x+0.1 and ys >= 0 and ys <= bed_max_y+0.1:
-                                    fo.write("G1 X%0.4f Y%0.4f E%.4f F%.4f\n" % (
-                                        xs, ys, E * E_FACTOR, engrave_speed))
+                                    if self.settings.lc_melzi_hack:
+                                        fo.write("G1 X%0.4f Y%0.4f E%.4f F%.4f\n" % (
+                                            xs, ys, E * E_FACTOR, engrave_speed))
+                                    else:
+                                        fo.write("G1 X%0.4f Y%0.4f F%.4f\n" % (
+                                            xs, ys, engrave_speed))
                                 else:
-                                    fo.write("G0 X%0.4f Y%0.4f F%.4f\n" % (
-                                        xs, ys, travel_speed))
+                                    if self.settings.lc_melzi_hack:
+                                        fo.write("G0 X%0.4f Y%0.4f F%.4f\n" % (
+                                            xs, ys, travel_speed))
+                                    else:
+                                        fo.write("M5 ; Turn off laser\n")
+                                        fo.write("G0 X%0.4f Y%0.4f F%.4f\n" % (
+                                            xs, ys, travel_speed))
+                                        # todo: laser power as parameter?
+                                        fo.write("M3 S100 ; Turn on laser\n")
                                     errors += 1
                                     if errors < 10:
                                         self.log("Position outside print dimension: %d, %d" % (xs, ys)) 
@@ -533,15 +595,23 @@
                                 #print "   Point: ", end_pt[0], end_pt[1], pen
                             #self.log("Laser OFF at: " + repr(sp[-1][-1]))
 
-                    if shape_obj.xml_node.get('fill'):
+                    #if shape_obj.xml_node.get('fill'):
+                    if tag_suffix == "polygon":
                         # Close the polygon
-                        e_distance = math.hypot(object_xs - xo, object_ys - yo)
-                        E = E + (e_distance)
-                        fo.write("G1 X%0.4f Y%0.4f E%.4f F%.4f ; Close the object polygon\n" % (
-                            object_xs, object_ys, E * E_FACTOR, engrave_speed))
-                        print "connecting filled path end to start"
+                        if self.settings.lc_melzi_hack:
+                            e_distance = math.hypot(object_xs - xo, object_ys - yo)
+                            E = E + (e_distance)
+                            fo.write("G1 X%0.4f Y%0.4f E%.4f F%.4f ; Close the object polygon\n" % (
+                                object_xs, object_ys, E * E_FACTOR, engrave_speed))
+                        else:
+                            fo.write("G1 X%0.4f Y%0.4f F%.4f ; Close the object polygon\n" % (
+                                object_xs, object_ys, engrave_speed))
+                        print "connecting filled polygon path end to start"
 
-        fo.write(GCODE_FOOT)
+        if self.settings.lc_melzi_hack:
+            fo.write(GCODE_FOOT_MELZI)
+        else:
+            fo.write(GCODE_FOOT)
         fo.close()
 
         if errors > 0:

mercurial