Snapmaker/Snapmaker2-Controller

Inline laser power problem lightburn/maybe others

quazzie opened this issue · 12 comments

To enable inline laser power commands you need to send first M3 SXXX I and then also a M3 SXXX before G1 SXXX works.
I don't think that's how i should work. Also if a M5 is sent, inline is disabled again.

If I check lightburns output for inline it just sends G1 SXXX commands.

So to get it to work i have to export the gcode from lightburn and manually add:

M3 S0 I
M3 S0

before all move commands.

And you're compiling this current code or at least one since inline power was merged in #153? If you are using the prebuilt binary from the forum it does not contain inline power. And if you have the 10W laser you at least need #223 also.

At least when I tested on the 1.6W the movement complies with the Marlin specifications. G1 X10 SXXX would be the simplest command to use inline power. M3 is more verbose but functions the same (M3 SXXX I which does nothing until a G10 XXXX)

Compiled from this main branch 2 hours ago.
10W laser.
G1 X10 SXXX does not work without running M3 S0 I and M3 S0 first.
And after a M5 it's disabled again and i have to run M3 S0 I and M3 S0 again.

This does not work, no laser turns on

;LightBurn 1.1.04
; Marlin device profile, current position
; Bounds: X-0.1 Y0.25 to X40.2 Y39.75

G21
G91
; Image @ 1500 mm/min, 100% power
M9
M05
G0 X-0.1 Y0.25 F3000
; Layer C02
G1 X0.5 F1500 I S0
G1 X0.1 I S224.9
G1 X0.1 I S227.9
G1 X0.1 I S230.9
.....

This does work, laser turns on correctly:

; LightBurn 1.1.04
; Marlin device profile, current position
; Bounds: X-0.1 Y0.25 to X40.2 Y39.75

G21
G91
; Image @ 1500 mm/min, 100% power
M9
M05
M03 S0 I
M03 S0
G0 X-0.1 Y0.25 F3000
; Layer C02
G1 X0.5 F1500 I S0
G1 X0.1 I S224.9
G1 X0.1 I S227.9
G1 X0.1 I S230.9
....

Weird, I'm skeptical it's working correctly in the second example as unless you know what you're looking for it's hard to distinguish "correct" behavior. Wish I had a 10W laser to test. Unfortunately I don't, maybe @nivekmai could try and reproduce.

It's much easier to test with synthetic commands rather than real project files, as the main difference for inline power when it's enabled and working correctly is when the laser is engaged and motion stops the power should be at the minimum "safe" level.

Something like:

M3 S255 I
G1 X10 ; laser full brightness when moving
; after motion stops minimum brightness

Well in the first example it does not turn on laser at all let alone change the intensity. In the second example it turns it on and changes its intesity and i'm able to etch/engrave :-D

I'll try to run some manual commands like your example and report behavior.
But it will probably be in a couple of hours, need to go to sleep.

In the second example it turns it on and changes its intesity

Sounds promising. That would indicate something is blocking engagement of inline power until a power command is sent to the laser. That could be related to the 10W specific "safety" features that were added, which is what #223 was intending to fix. Hopefully nivemaki can pop in to help troubleshoot, as I don't think I will be able to reproduce this on the 1.6W laser module as it's missing the additional "safety" features.

I think I may have made a mistake in #223. In that, I was making the gcode parsing work per what the other forum members was saying was the correct gcode output from lightburn (in that they needed to swap some gcode to get things working).

The changes they were making were to ensure that you have first an M3 S0 at the beginning of the script, then swap the M5's for M5 I (except the last one), and remove M6, M7, and M8.

I had written a bash script to fix a file from lightburn:

#! /bin/bash

FILE=$1
OUT=`echo "$FILE" |  sed 's/\(.*\)\(\..*\)/\1 - fixed\2/'`
sed '/M[678]/d' "$FILE" \
	| sed 's/M05/M05 I/g' \
	| tac \
	| sed '0,/M05 I/{s/M05 I/M05/}' \
	| tac \
	> "$OUT"
echo "$OUT"

Combining those 2, you end up changing a file that was:

; LightBurn 1.0.06
; Marlin device profile, user origin
; Bounds: X0.04 Y0.02 to X31.95 Y41.55

G21
G90
G0 X0 Y0 F6000
G91
; Image @ 1000 mm/min, 15% power
M8
M05
G0X15.639 Y0.983 F6000
; Layer Plywood Engrave
G1X0.417 F1000 I S0
G1X0.08 I S6.9
G1X0.319 I S38.3
...
G1X-0.08 I S38.3
G1X-0.08 I S25.4
G1X-0.417 I S0
; Cut @ 200 mm/min, 100% power
M8
M05
G0X5.983 Y-3.094 F6000
; Layer Plywood cut
G1X0.088 Y0.066 F200 I S255
G1X0.095 Y0.024
...
G1X0.07 Y0.14
G1X0.08 Y0.106
M05
G0X0.871 Y2.023 F6000
; Layer Plywood cut
G1X-0.202 Y0.045 F200 I S255
G1X-0.197 Y0.07
...
G1X-0.336 Y-0.378
G1X-0.208 Y-0.009
G1X-0.207 Y0.017
M9
M05
G90
; return to user-defined finish pos
G0 X0 Y0 F6000

To instead be:

; LightBurn 1.0.06
; Marlin device profile, user origin
; Bounds: X0.04 Y0.02 to X31.95 Y41.55

;USER START SCRIPT
M3 S0
;USER START SCRIPT

G21
G90
G0 X0 Y0 F6000
G91
; Image @ 1000 mm/min, 15% power
M05 I
G0X15.639 Y0.983 F6000
; Layer Plywood Engrave
G1X0.417 F1000 I S0
G1X0.08 I S6.9
G1X0.319 I S38.3
...
G1X-0.08 I S38.3
G1X-0.08 I S25.4
G1X-0.417 I S0
; Cut @ 200 mm/min, 100% power
M05 I
G0X5.983 Y-3.094 F6000
; Layer Plywood cut
G1X0.088 Y0.066 F200 I S255
G1X0.095 Y0.024
...
G1X0.07 Y0.14
G1X0.08 Y0.106
M05 I
G0X0.871 Y2.023 F6000
; Layer Plywood cut
G1X-0.202 Y0.045 F200 I S255
G1X-0.197 Y0.07
G1X-0.54 Y0.031
...
G1X-0.208 Y-0.009
G1X-0.207 Y0.017
M9
M05
G90
; return to user-defined finish pos
G0 X0 Y0 F6000

However, now I'm looking at the code from the base marlin, and reading on the lightburn forums about how the inline code is supposed to work, I don't think that you should need to send M5 I to only temporarily disable the laser.

tl;dr: I don't think that #223 correctly fixed the issue, but it also wasn't fixed before.

I'll try to do some more testing, and see if I can get things working properly.

@brent113 could you supply a file that correctly works with the 1.6W laser (maybe, just something that draws 2 parallel lines) directly from lightburn with the inline setting enabled, and I can start playing with seeing if that works correctly w/ the 10W laser too (without any modifications to the gcode), and then from there modify firmware again to figure out what's going on w/ the safety stuff?

Yep, looking again, it's not right, I now think I see another problem that's probably the root of the issue:
https://github.com/Snapmaker/Snapmaker2-Controller/blob/main/snapmaker/src/module/toolhead_laser.cpp#L228-L233

This section here will set the laser to be disabled after 200 ticks of ToolheadLaser::Process:
https://github.com/Snapmaker/Snapmaker2-Controller/blob/main/snapmaker/src/module/toolhead_laser.cpp#L994-L995
https://github.com/Snapmaker/Snapmaker2-Controller/blob/main/snapmaker/src/module/toolhead_laser.cpp#L1014

And it only does that for the 10w laser.

What I guess is happening is that the M5 is received, and then somehow there's 200 ticks (is that just 200ms? IDK, it's just a loop) before the next G1 I command is received, so the laser is disabled in ToolHeadLaser::TurnoffLaserIfNeeded:
https://github.com/Snapmaker/Snapmaker2-Controller/blob/main/snapmaker/src/module/toolhead_laser.cpp#L179-L190

Alternatively, it could also be from the HMI sending down the security status 0, since that'll also turn off the laser:
https://github.com/Snapmaker/Snapmaker2-Controller/blob/main/snapmaker/src/module/toolhead_laser.cpp#L91-L92
but that doesn't make sense to only happen after M5.

I'm gonna start looking into fixing the firmware the right way. In the meantime, I've created https://github.com/nivekmai/Fix-Lightburn-for-Snapmaker-2.0 which should alleviate the pain I've caused with #223 (sorry about that)

We have fixed this issue and a new firmware will be released soon

Any update or pull I can view/compile myself?