Notifications
Clear all

Using variables in start gcode  

  RSS
Peace
(@peace)
Member
Using variables in start gcode

Hello!

I'm using PrusaSlicer for my Ender3 V3 SE. However, in comparision to Cura the metadata about print time/filament used is missing, so I wanted to add that as a part of custom gcode.

Here's how the metadata part looks after generating gcode from Cura:

;FLAVOR:Marlin
;TIME:7036
;Filament used: 1.20047m
;Layer height: 0.12
;MINX:86.48
;MINY:83.008
;MINZ:0.12
;MAXX:140.087
;MAXY:133.43
;MAXZ:34.68
;TARGET_MACHINE.NAME:Creality Ender-3 V3 SE
;Generated with Cura_SteamEngine 5.6.0

I've tried to do the same by adding this to `Printer Settings -> Custom G-code`:

;FLAVOR:Marlin
;TIME:[print_time]
;Filament used: [used_filament]
;Layer height: [layer_height]m
;TARGET_MACHINE.NAME:Creality Ender-3 V3 SE

However, this ends up with throwing this error:

G-code export to C:\Users\xxxx\AppData\Local\Temp\.45260.gcode failed due to invalid custom G-code sections:
start_gcode
Parsing error at line 2: Variable does not exist
;TIME:[print_time]

I suspect that these values aren't generated yet at the time I'm trying to use them (before slicing) thus the error. What would be the correct way of getting this data in the gcode?

Best Answer by Peace:

Again thanks for your suggestion. I was able to solve the issue using post process script written in python.

For anyone interested, I shared the code here: https://github.com/MaciejWanat/ender-v3-prusa-preview-data

Napsal : 09/01/2024 9:54 am
Neophyl
(@neophyl)
Illustrious Member
RE: Using variables in start gcode

You can't use Cura specific placeholders in other slicers. They just wont work as they have no idea what they are.  

What are you trying to do ?  If its just add the total print time then the list of Prusa sepecific place holders and where they can be used is detailed here https://help.prusa3d.com/article/list-of-placeholders_205643#custom-g-code-specific-placeholders

Also in Prusa G-code output that sort of data is usually placed below the actual gcode, just before the list of settings used to generate it and not at the top.

M73 P100 R0M73 Q100 S0 <-- last gcode command from actual print
; filament used [mm] = 1590.68; filament used [cm3] = 3.83
; filament used [g] = 4.74
; filament cost = 0.13
; total filament used [g] = 4.74; total filament cost = 0.13
; total filament used for wipe tower [g] = 0.00
; estimated printing time (normal mode) = 25m 30s
; estimated printing time (silent mode) = 25m 36s
; estimated first layer printing time (normal mode) = 1m 40s
; estimated first layer printing time (silent mode) = 1m 41s

; prusaslicer_config = begin <- list of all the settings then goes after this.

 

If you are trying to display the remaining time as you are printing then that is a little more difficult. Prusa uses a tick box for Supports Remaining Time, that is located under Printer Settings>General and is a red/Expert level setting.

However that option enables the output of M73 commands into the g-code (see the tooltip by the setting).  The format is M73 P1 R38 ( so percentage currently is 1 percent and there are 38 minutes remaining). If your firmware does not support M73 then you have to do some post processing to convert the M73  Rxx to something else like a M117 xx. 

There are python scripts on github that can be used with the Post Processing Script option (Print Settings>Output Options) such as https://github.com/scheric/Prusaslicer-M73-to-M117-macro

Recent versions of PS also have a built in G-code substitutions function on the same page.  With that you could use regex to do the same thing.  https://help.prusa3d.com/article/g-code-substitutions_301694

Hope that helps you figure out further.

Napsal : 10/01/2024 6:29 am
Peace
(@peace)
Member
Topic starter answered:
RE: Using variables in start gcode

Thanks for an answer! So, first things first.

What I'm trying to achieve exactly is for my machine to display estimated print time/filament used/layer height in preview menu. The estimated priting time works correctly once I start printing, its just 0 on the preview. This works when gcode comes from cura, doesn't when it comes from prusa slicer. Like this:

After slicing in Prusa all these values are 0. So what I did is I compared the gcodes from Cura and Prusa, and noticed that there is metadata attatched in cura one - which I assumed is used by my machine to display these:

;FLAVOR:Marlin
;TIME:7036
;Filament used: 1.20047m
;Layer height: 0.12
;MINX:86.48
;MINY:83.008
;MINZ:0.12
;MAXX:140.087
;MAXY:133.43
;MAXZ:34.68
;TARGET_MACHINE.NAME:Creality Ender-3 V3 SE
;Generated with Cura_SteamEngine 5.6.0

What I wanted to achive then was for Prusa to generate the same header for the gcode, so supposingly my machine would display the values correctly. You mentioned that I can't use Cura specific placeholders which is clear to me, but I used placeholders provided by prusa (all of ones I've used are listed in the document you've linked), specifically these ones:

;TIME:[print_time]
;Filament used: [used_filament]
;Layer height: [layer_height]m

But trying to attatch this results in an error I've attached in initial post.

I don't think there's an issue with some gcode conversion that you've linked to me, since as I mentioned displaying time remaining works correctly, just preview doesn't work. Is there any way I can generate this same header in prusa gcode files?

Hope this clears out my question!

Napsal : 10/01/2024 10:10 am
Peace
(@peace)
Member
Topic starter answered:
RE: Using variables in start gcode

Just to confirm, I pasted this header into some gcode file:

;FLAVOR:Marlin
;TIME:7036
;Filament used: 1.20047m
;Layer height: 0.12
;MINX:86.48
;MINY:83.008
;MINZ:0.12
;MAXX:140.087
;MAXY:133.43
;MAXZ:34.68
;TARGET_MACHINE.NAME:Creality Ender-3 V3 SE

And it displayed the preview values correctly (even though it doesn't represent the real file times):

Comparing to other cura-generated gcode there's only some weird padding issues, not sure why is that. Cura example:

What I'm trying to achieve is to generate the second preview info automatically when slicing in Prusa. Attatching both files for reference: https://we.tl/t-IGb69flHKB

Napsal : 10/01/2024 12:18 pm
Neophyl
(@neophyl)
Illustrious Member
RE: Using variables in start gcode

Sorry not downloading files from some random place on the internet.   

The print_time is usable for generating the file name as by that point its generated the gcode.  That prusa knowledge base article tells you where they are usable.  Not in the place you want unfortunately.  Also its done in hours and minutes and the ones your machine looks to be using is a value in seconds.

As mentioned the prusa gcode should contain the estimates in the format I mentioned, however you need them in a different format.  So you need to post process that to get them. Either using a post process script with python or using a regex lookup and conversion.  So your post process script should be looking for the ; estimated printing time (normal mode) = 25m 30s line, converting the 25m and 30 seconds to a value in seconds and then adding a line in the format that you require.

Napsal : 10/01/2024 3:01 pm
Peace
(@peace)
Member
Topic starter answered:
RE: Using variables in start gcode

Again thanks for your suggestion. I was able to solve the issue using post process script written in python.

For anyone interested, I shared the code here: https://github.com/MaciejWanat/ender-v3-prusa-preview-data

Napsal : 10/01/2024 6:44 pm
Share: