Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad gcode produced when interpreting {code1, code2} in M140, M109, M190 #17501

Closed
bellzw opened this issue Dec 3, 2023 · 14 comments · Fixed by #18025
Closed

Bad gcode produced when interpreting {code1, code2} in M140, M109, M190 #17501

bellzw opened this issue Dec 3, 2023 · 14 comments · Fixed by #18025
Labels
Status: On Backlog The issue / feature has been reproduced and is deemed important enough to be fixed. Type: Bug The code does not produce the intended behavior.

Comments

@bellzw
Copy link

bellzw commented Dec 3, 2023

Cura Version

5.6.0

Operating System

Windows 10

Printer

BIBO 2

Reproduction steps

I have created my own definition for this printer and it has been working for me since before 5.0. Below is the startup code copied from the Machine Settings; it correctly reflects what's in the json file:
; Start of BIBO starutp code
M140 S{material_bed_temperature, initial_extruder_nr}
M104 T0 S{material_standby_temperature, 0}
M104 T1 S{material_standby_temperature, 1}
G21 ;metric values
G90 ;absolute positioning
M107 ;start with the fan off
T0
G92 E0
G28
G1 Y0 F1200 E0
T1
G92 E0 ;set extruder 2 position to 0
M109 T0 S{material_standby_temperature, 0} ; now wait for E1 to reach temperature
M109 T1 S{material_standby_temperature,1} ; now wait for E2 to reach temperature
M109 T{initial_extruder_nr} S{material_print_temperature_layer_0, initial_extruder_nr} ; now set the correct extruder to the correct initial temperature
T{initial_extruder_nr}
M190 S{material_bed_temperature, initial_extruder_nr}
M117 BIBO Printing...

Actual results

This is the gcode generated by 5.6.0 (installed on 2 Dec 2023):
; Start of BIBO starutp code
M140 S(60, 0) <------
M104 T0 S175.0
M104 T1 S175
G21 ;metric values
G90 ;absolute positioning
M107 ;start with the fan off
T0
G92 E0
G28
G1 Y0 F1200 E0
T1
G92 E0 ;set extruder 2 position to 0
M109 T0 S175.0 ; now wait for E1 to reach temperature
M109 T1 S175 ; now wait for E2 to reach temperature
M109 T0 S(200.0, 0) ; now set the correct extruder to the correct initial temperature <--------
T0
M190 S(60, 0) <------
M117 BIBO Printing...

I have marked the bad code with <------ on the right.

Expected results

; Start of BIBO starutp code
M140 S60 <------
M104 T0 S175.0
M104 T1 S175
G21 ;metric values
G90 ;absolute positioning
M107 ;start with the fan off
T0
G92 E0
G28
G1 Y0 F1200 E0
T1
G92 E0 ;set extruder 2 position to 0
M109 T0 S175.0 ; now wait for E1 to reach temperature
M109 T1 S175 ; now wait for E2 to reach temperature
M109 T0 S200.0 ; now set the correct extruder to the correct initial temperature <-------
T0
M190 S60 <-------
M117 BIBO Printing...

Add your .zip and screenshots here ⬇️

bibo2_dual_improved.def.zip
These are the printer and extruder definitions that have worked in 5.0 and earlier versions.

@bellzw bellzw added Status: Triage This ticket requires input from someone of the Cura team Type: Bug The code does not produce the intended behavior. labels Dec 3, 2023
@photodude
Copy link
Contributor

Is there a reason to include an "extruder number" in the bed heatup?

M140 S{material_bed_temperature, initial_extruder_nr}

Maybe try removing 'initial_extruder_nr'

And see if just M140 S{material_bed_temperature} will allow the start gcode to work as expected.

@bellzw
Copy link
Author

bellzw commented Dec 3, 2023

@photodude That's a reasonable question, and is probably the answer! The reason for initial_extruder_nr was, because, as far as I knew, Cura picks up the bed temperature from the build plate temperature parameter in the profile of the material assigned to whatever tool chain is currently selected. Now that I look at the github version of fdmprinter.def.json, I see that this is NOT settable per extruder, and that Cura resolves the issue of different bed temps because different materials specify different temps by using the maximum of all extruders.

This does not resolve the issue with the M109 T{initial_extruder_nr} S{material_print_temperature_layer_0, initial_extruder_nr}. material_print_temperature_layer_0 IS supposed to be settable per extruder, so that should have worked.

Anyway, I removed the initial_extruder_nr in all 3 places, and that results in
Start of BIBO starutp code
M140 S60 <---------
M104 T0 S175.0
M104 T1 S175
G21 ;metric values
G90 ;absolute positioning
M107 ;start with the fan off
T0
G92 E0
G28
G1 Y0 F1200 E0
T1
G92 E0 ;set extruder 2 position to 0
M109 T0 S175.0 ; now wait for E1 to reach temperature
M109 T1 S175 ; now wait for E2 to reach temperature
M109 T0 S200.0 ; now set the correct extruder to the correct initial temperature <---------
T0
M190 S60 <-------
M117 BIBO Printing...

which is correct. Arrows are mine. So it seems that the issue is related to S{code1, code2}, when code2 is a replacement pattern. The M109s with S{code, 0} and S{code, 1} worked correctly.

Fixing the M140 and M190 substitutions, but leaving the last M109 as
M109 T{initial_extruder_nr} S{material_print_temperature_layer_0, initial_extruder_nr}
in the startup, resulted in
; Start of BIBO starutp code
M140 S60
M104 T0 S175.0
M104 T1 S175
G21 ;metric values
G90 ;absolute positioning
M107 ;start with the fan off
T0
G92 E0
G28
G1 Y0 F1200 E0
T1
G92 E0 ;set extruder 2 position to 0
M109 T0 S175.0 ; now wait for E1 to reach temperature
M109 T1 S175 ; now wait for E2 to reach temperature
M109 T0 S(200.0, 0) ; now set the correct extruder to the correct initial temperature <------
T0
M190 S60
M117 BIBO Printing...
That M109 T0 S(200, 0) is not interpreted by the printer correctly.

@GregValiant GregValiant added Status: Needs Info Needs more information before action can be taken. Status: Under Investigation The issue has been confirmed or is assumed to be likely to be a real issue. It's pending discussion. and removed Status: Triage This ticket requires input from someone of the Cura team labels Dec 7, 2023
@GregValiant
Copy link
Collaborator

Can this be a simple syntax error? You are explicitly calling out T{initial_extruder_nr} and then adding the S parameter with the extruder number added a second time. Why bother? It's already stated that this is for the initial extruder.
M109 T{initial_extruder_nr} S{material_print_temperature_layer_0, initial_extruder_nr}

It would seem that this should be sufficient:
M109 T{initial_extruder_nr} S{material_print_temperature_layer_0}

Separating the two replacement patterns and explicitly calling for the comma might also work:
M109 T{initial_extruder_nr} S{material_print_temperature_layer_0}, {initial_extruder_nr}
That would result in M109 T0 S210, 0 (which still looks to be redundant)

I don't see a Cura bug here and this does appear to be firmware specific. What firmware flavor are you running?

@bellzw
Copy link
Author

bellzw commented Dec 8, 2023

@GregValiant Thank you for the suggestions, but I don't think they quite worked; details are immediately below. In addition, I found a different but possibly related problem with replacement patterns; look for the Bolded "However" below.

Firmware is Marlin 1.something Shaqfoo fork. It doesn't like to see parentheses in the gcode. It doesn't crash, but it causes the heater for the extruder to shut off, so it seems to interpret S(200, 0) as S0.

This code worked in versions up to 5.5, so something changed in 5.6. I'm merely a user, and don't know what's actually in the code.

I've never been able to find documentation of the syntax of replacement patterns, other than a few examples at https://github.com/Ghostkeeper/SettingsGuide/blob/master/resources/articles/machine_settings/machine_start_gcode.md. There are comments about what happens if a per extruder setting does not references an extruder, and they guided me to include the initial_extruder_nr in the M109.

M109 T{initial_extruder_nr} S{material_print_temperature_layer_0, initial_extruder_nr} should NOT be a syntax error because, per fdmprinter.def.json, material_print_temperature_layer_0 IS settable per extruder. The M140 was a syntax error, and I've corrected that in my 5.6.0 and 5.1.0 installations.

I did try separating the two patterns per your suggestion and got M109 T0 S180, 0 . I don't think the , 0 will fly. I also tried S{{material_print_temperature_layer_0}, {initial_extruder_nr}}, but that resulted in Cura not correctly interpreting all replacement patterns, so I'm guessing that nesting braces is not permitted and not expected.

However, I tried your suggestion to omit initial_extruder_nr from the M109 and got the correct gcode. Being hopeful, I moved the model from T0 to T1 to make sure of things, but, much to my surprise, I got
;Generated with Cura_SteamEngine 5.6.0
T1 <------ THIS IS CORRECT; NO EXTRUDERS ARE OVERRIDEN, NO SUPPORTS OR PRIME TOWER, AND THERE IS A SINGLE MODEL ON T1
M82 ;absolute extrusion mode
; Start of BIBO starutp code
M140 S40
M104 T0 S175.0
M104 T1 S175
G21 ;metric values
G90 ;absolute positioning
M107 ;start with the fan off
T0
G92 E0
G28
G1 Y0 F1200 E0
T1
G92 E0 ;set extruder 2 position to 0
M109 T0 S175.0 ; now wait for E1 to reach temperature
M109 T1 S175 ; now wait for E2 to reach temperature
M109 T0 S180 ; now set the correct extruder to the correct initial temperature <---- INCORRECT. SHOULD BE M109 T1 S200
T0 <----- INCORRECT. SHOULD BE T1
M190 S40
M117 BIBO Printing...

Apparently, Cura thinks initial_extruder_nr is 0, despite putting T1 correctly at the top of the code. T references above the M109 use constants; the only lines in the startup code containing initial_extruder_nr are the two incorrect lines above, This error also occurs when I use the BIBO definition distributed with Cura 5.6.0.

When I use the startup code with Cura 5.1.0, the generated code is correct, both with the distribution BIBO definition and my definition. Again, I come to the conclusion that there is a bug related to replacement patterns.

Would it help if I post 3mf files for some of these cases?

@github-actions github-actions bot removed the Status: Needs Info Needs more information before action can be taken. label Dec 8, 2023
@GregValiant GregValiant added the Status: Duplicate Duplicate of another issue. label Dec 8, 2023
@GregValiant
Copy link
Collaborator

GregValiant commented Dec 8, 2023

Looking back I found one of my old posts.
Take a look at #15673 , #16715 (for a BIBO) and #11985 .
On Sept 18 @MariMakes added a ticket to the backlog CURA-11053. It would appear that this still hasn't been addressed.

"initial_extruder_nr" gets replaced but always with a "0" rather than the number of the initial extruder.

I'll mark this as a duplicate report. It appears that it's on the list of things to fix but it hasn't been repaired yet.

@bellzw
Copy link
Author

bellzw commented Dec 8, 2023 via email

@GregValiant GregValiant removed the Status: Duplicate Duplicate of another issue. label Dec 9, 2023
@GregValiant
Copy link
Collaborator

I'll remove the duplicate tag.

I think it's time to get a professional involved.

@fieldOfView the OP would like to get something like this to work in the startup gcode to return the proper Initial Temp for the Initial Extruder.
T{initial_extruder_nr} S{material_print_temperature_layer_0}, {initial_extruder_nr}
Is there a solution to this?

@bellzw
Copy link
Author

bellzw commented Dec 9, 2023

The exact replacements that we BIBO owners would like to have working are
M109 T{initial_extruder_nr} S{material_print_temperature_layer_0, initial_extruder_nr}
T{initial_extruder_nr}
regardless of whether the initial extruder is T1 or T0.

I appreciate @GregValiant's help with this issue.

@MariMakes
Copy link
Contributor

Hey all,

Sorry, it took us a while to get back to you 😞

I have to be honest, that this discussion on Gcode formula level is outside my field of expertise.
But I do know that we introduced changes in Cura 5.6, hoping to improve this behavior here: #16962

When I'm reading these comments, it seems to either introduce new bugs or is not resolved.
Thanks for your report 👍

I'll bring it up to the team to see what they can do to improve it.
Fingers crossed 🤞

@casperlamboo
Copy link
Contributor

casperlamboo commented Jan 15, 2024

Hi,

Thanks for the bug report. We recently changed some of the behavior regarding start/end extruder/machine gcodes (#16962). With this change formula's are now accepted with in the curly brackets. With this change apparently an oversight was made regarding the optional extruder argument. It was assumed that only patterns like {[formula], [extruder_nr]} are valid, e.g M140 S{material_bed_temperature, 0} will work but M140 S{material_bed_temperature, initial_extruder_nr} won't. It is valid to pass digits, other expressions such as the initial_extruder_nr variable were not seen as a valid extruder nr argument. This was an oversight when implementing the feature.

Should be relatively easy to fix by changing this regex to accept any expression after the comma.

_extruder_regex = re.compile(r"^\s*(?P<expression>.*)\s*,\s*(?P<extruder_nr>\d+)\s*$")

@MariMakes MariMakes added Status: On Backlog The issue / feature has been reproduced and is deemed important enough to be fixed. and removed Status: Under Investigation The issue has been confirmed or is assumed to be likely to be a real issue. It's pending discussion. labels Jan 15, 2024
@bellzw
Copy link
Author

bellzw commented Jan 15, 2024

Thanks for the update, @casperlamboo.

One comment on the M140: I was wrong to expect M140 S{material_bed_temperature, initial_extruder_nr} to work. According to fdmprinter.def.json (my BIBO inherits from this definition), material_bed_temperature is NOT settable per extruder. None of the bed_temperature parameters are settable per extruder, and this makes sense because printers don’t (as far as I know!) have more than one bed. So, M140 should always be M140 S{material_bed_temperature}, or the like; I’ve corrected my startup code accordingly as the BIBO only has one bed. As I understand fdmprinter.def.json, resolution of different materials on different extruders having different bed temperatures is by using the maximum of the bed temperatures: "resolve": "max(extruderValues('material_bed_temperature'))".

There are 2 issues:

  1. At the end of the startup code, initial_extruder_nr might be replaced by 0, even if the actual initial extruder is 1
  2. M109 T{initial_extruder_nr} S{material_print_temperature_layer_0, initial_extruder_nr} is replaced by M109 T0 S(200, 0). It should be M109 T0 S200; the () and ,0 don’t belong.

Recently, I’ve found that issue 1, above, appears may be related to Skirt/Brim Extruder and occurs in 5.6.0 and 5.5.0. When a print is entirely on E2, and brim is the selected adhesion, these combinations of Build Plate Adhesion Extruder, Skirt/Brim Extruder result in incorrect substitutions: (Not overridden, not overridden) and (E2, Not overridden). The combinations (E2, E2) and (Not overridden, E2) result in correct substitutions. It appears that 5.5.0 and 5.6.0 take Skirt/Brim Extruder to be the one to start printing, but there seems to be a disconnect between the initial T1 inserted into the gcode, and the extruder Cura thinks the printing should start with.

Issue 1, above, happens in 5.1.0 in an understandable way if the build plate adhesion extruder is different from the skirt/brim extruder. Is there a philosophical difference in the way Skirt/Brim Extruder is treated in 5.5.0 and 5.6.0? In 5.1.0, there is no Not overridden option, Cura interprets Skirt/Brim Extruder to be the extruder that will print the brim, and so it starts with that extruder. 5.1.0 does this when the adhesion extruder is E2 and Skirt/Brim Extruder is E1; Skirt/Brim Extruder seems to override Build Plate Adhesion Extruder, printing starts with T0 doing the brim, and then switches to T1 to do the model. The actual initial extruder matches the substitution in 5.1.0.

I have attached a pdf with examples of good and bad behavior.
Issue #17501 examples.pdf

@MariMakes
Copy link
Contributor

Quick update from our side 👋

We released a special Alpha to help with the slicing crashes, but since it's a Work In Progress build it contains the fix that has been proposed. You can download the build yourself here and see if it fixes your problem: #18080

Can you let us know if the problem you described in your wonderful PDF is solved or if you would like us to take another look?

@bellzw
Copy link
Author

bellzw commented Jan 22, 2024

The issue of M109 T0 S(200, 0) appearing seems to be fixed; I see M109 T1 S200.0 where I should. However, the issue of initial_extruder_nr being wrong at the end of the startup code remains.

This is what I see when slicing for BIBO2 dual (as distributed with Cura), all extruders are set to Not overridden with the model assigned to E2, no supports, no prime tower, Normal 25% infill profile, my materials Sunlu White PLA and Green Silk PLA assigned to E1 and E2, respectively:
;Generated with Cura_SteamEngine 5.7.0-alpha.0
T1 <-----Correct
M140 S60
M105
M190 S60
M82 ;absolute extrusion mode
M104 T0 165
M104 T1 165
M109 T0 S200.0 <----- Now fixed in 5.7.0 alpha
G21 ;metric values
G90 ;absolute positioning
M107 ;start with the fan off
G28 X0 Y0 ;move X/Y to min endstops
G28 Z0 ;move Z to min endstops
G1 Z2.0 F400 ;move the platform down 2mm
T0
G92 E0
G28
G1 Y0 F1200 E0
G92 E0
T0 <-------- WRONG! Should be T1 Startup code has T{initial_extruder_nr}. Printing starts with E1
M117 BIBO Printing...
G92 E0
G92 E0
G1 F1500 E-6.5
;LAYER_COUNT:84
;LAYER:0
M107
G0 F3600 X-16.97 Y3.485 Z0.3
;TYPE:SKIRT
G1 F1500 E0

Gcode above starts printing with E1 and never selects E2. It appears that Cura thinks it's supposed to use E1 for something, despite there not being any mention of E1, except that there is material assigned to the extruder.

When I change only the Skirt/Brim Extruder to E2, I get:
;Generated with Cura_SteamEngine 5.7.0-alpha.0
T1 <---- Still correct
M140 S60
M105
M190 S60
M82 ;absolute extrusion mode
M104 T0 165
M104 T1 165
M109 T1 S215.0 <----- Still correct
G21 ;metric values
G90 ;absolute positioning
M107 ;start with the fan off
G28 X0 Y0 ;move X/Y to min endstops
G28 Z0 ;move Z to min endstops
G1 Z2.0 F400 ;move the platform down 2mm
T0
G92 E0
G28
G1 Y0 F1200 E0
G92 E0
T1 <----- Correct. Printing will start with E2, as expected.
M117 BIBO Printing...
G92 E0
G92 E0
G1 F1500 E-6.5
;LAYER_COUNT:84
;LAYER:0
M107
G0 F3600 X-16.97 Y3.485 Z0.3
;TYPE:SKIRT
G1 F1500 E0

If I disable E1, I get:
;Generated with Cura_SteamEngine 5.7.0-alpha.0
T1 <---- CORRECT
M140 S60
M105
M190 S60
M82 ;absolute extrusion mode
M104 T0 165
M104 T1 165
M109 T1 S215.0 <---- STILL CORRECT
G21 ;metric values
G90 ;absolute positioning
M107 ;start with the fan off
G28 X0 Y0 ;move X/Y to min endstops
G28 Z0 ;move Z to min endstops
G1 Z2.0 F400 ;move the platform down 2mm
T0
G92 E0
G28
G1 Y0 F1200 E0
G92 E0
T1 <----- NOW CORRECT. Printing will start with E2, as expected.
M117 BIBO Printing...
G92 E0
G92 E0
G1 F1500 E-6.5
;LAYER_COUNT:84
;LAYER:0
M107
G0 F3600 X-16.97 Y3.485 Z0.3
;TYPE:SKIRT
G1 F1500 E0

The option to set Skirt/Brim extruder disappears, as expected, and now the substitution for initial_extruder_nr is correct. Apparently, if E1 is enabled and not, as far as I can tell mentioned in any setting, a model entirely on E2 will cause an incorrect substitution of initial_extruder_nr.

I searched for "extruder" in the settings window and enabled every setting that I though referenced an extruder, but did not see anything when slicing to indicate that E1 was referenced.

Let me know if you want me to try other combinations of settings and extruder to which the model is assigned.

casperlamboo added a commit that referenced this issue Jan 23, 2024
For the `skirt_brim_extruder_nr` setting it is possible for the setting to be -1; this means that we have no preference for extruder. This allowed us to implement the "multi-material brim". When we were requesting the initial extruder, and this value was set to -1 we were default to the 0-th extruder. However, this was incorrect in the situation where the first extruder is not used.

Fixes #17501
@casperlamboo
Copy link
Contributor

casperlamboo commented Jan 23, 2024

@bellzw, thank your response. The second issue you mention is caused by a feature we implement called multi-material brim (Ultimaker/CuraEngine#1613). To enable multi-material brim the skirt-brim extruder is set to extruder_nr -1 (this is now the default value for this setting). However, there was an oversight made when implementing the feature that, if the extruder_nr was set to -1 the initial extruder nr defaulted to 0, even when the first extruder is not used.

Made a partial fix in 8af3de2. Implementation will not solve every case, we cannot always correctly determine the initial extruder in the frontend without slicing the model. Read commit message for more info. However, for the specific case stated in this issue the fix will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: On Backlog The issue / feature has been reproduced and is deemed important enough to be fixed. Type: Bug The code does not produce the intended behavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants