Using gcode variables doesn't work

Hi. I am working on a set of macros, were I set a gcode variable in one macro (read as parameter of the macro call and stored in a gcode variable using the SET_GCODE_VARIABLE command), so that it can be used in a set of other macros. The intention is that the value of the gcode variable is temporarily saved for use in other macros until I call the macro to set the gcose variable again and potentially change it. After the call to change the gcode variable, the other macros should consider the new value once called.

I attach my code snippet here:

[gcode_macro SET_PRINTER_PROFILE]
description: Choose Printer Profile at run time (0:Silent, 1:Standard, 2:Sport, 3:Ludricous)
variable_printer_profile: 0
gcode:
        # Read Profile parameter, standard profile is default
        {% set P = params.Profile|default(1)|int %}

        # Save current printer profile variable to be used across macros
        SET_GCODE_VARIABLE MACRO=SET_PRINTER_PROFILE VARIABLE=printer_profile VALUE={P}

        # Call IMPLEMENT_PRINTER_PROFILE macro to set all printer profile values
        IMPLEMENT_PRINTER_PROFILE

        
[gcode_macro IMPLEMENT_PRINTER_PROFILE]
description: Macro called after printer profile was chosen
gcode:
        # Retrieve active printer profile
        {% set Profile = printer["gcode_macro SET_PRINTER_PROFILE"].printer_profile %}

       .... and so on....

Unfortunately, the gcode variable doesn’t change and stays the same as the default value. Just wondering if I misinterpreted how gcode variable work or if my syntax is wrong or if you simply cannot set a gcode variable via a macro parameter. I hope the klipper gods can help :slight_smile:

Cheers
Florian

You may have a look on this thread:

1 Like

I didn’t answer the question directly as I would have thought the best/most proper answer for this question was when IMPLEMENT_PRINTER_PROFILE was invoked, parameters from SET_PRINTER_PROFILE were passed to it and customize the execution of the gcode_macro.

Unfortunately, I’m still pretty shaky on writing my own macros, so I didn’t want to put down something that wouldn’t work and my printers are all doing long jobs so I couldn’t test it out before posting. Sorry.

2 Likes

But maybe it helps to find a solution…

Thanks for your comments, very appreciated and great to know that this community sticks together. I guess the main difference to your previous discussions/topics is that I want to assign a parameter of the SET_PRINTER_PROFILE gcode macro as printer variable. If I hard code the variable in the macro, I can change the value without issue. passing a parameter to be stored as printer variable is the issue…I am not even sure if this is possible at all. Maybe the data type or default value assignment is the issue…???

So, the mystery is solved…Turns out, parameters of macros need to be defined in ALLCAPS.
I just changed the parameter name from “Profile” to “PROFILE” and, voila…works.

[gcode_macro SET_PRINTER_PROFILE]
description: Choose Printer Profile at run time (0:Silent, 1:Standard, 2:Sport, 3:Ludricous)
variable_printer_profile: None
gcode:
        # Read Profile parameter, standard profile is default
        {% set P = params.PROFILE|default(1) %}

        # Save current printer profile variable to be used across macros
        SET_GCODE_VARIABLE MACRO=SET_PRINTER_PROFILE VARIABLE=printer_profile VALUE={P}
                  
        # Call IMPLEMENT_PRINTER_PROFILE macro to set all printer profile values
        IMPLEMENT_PRINTER_PROFILE

        
[gcode_macro IMPLEMENT_PRINTER_PROFILE]
gcode:
        # Retrieve active printer profile
        {% set Profile = printer["gcode_macro SET_PRINTER_PROFILE"].printer_profile %}

        .... and so on....

In case any of you have a similar use case, beware of capitals :stuck_out_tongue:

1 Like

One of my print jobs finished, so I took a few moments to look at this.

This is the way that I would implement the functionality set out in the operation of the SET_PRINTER_PROFILE/IMPLEMENT_PRINTER_PROFILE macros:

# 2023.02.09 - Macro Testing
[respond]

[gcode_macro SET_PRINTER_PROFILE]
description: Choose Printer Profile at run time (0:Silent, 1:Standard, 2:Sport, 3:Ludricous)
gcode:
    # Read Profile parameter, standard profile is default
    {% set P = params.PROFILE|default(1) %}
                  
    # Call IMPLEMENT_PRINTER_PROFILE macro to set all printer profile values
    IMPLEMENT_PRINTER_PROFILE PROFILE={P}

        
[gcode_macro IMPLEMENT_PRINTER_PROFILE]
gcode:
    # Read Profile parameter
    {% set P = params.PROFILE|int %}

    {% if   0 == P %}
        RESPOND TYPE=echo MSG="Silent Profile"
    {% elif 1 == P %}
        RESPOND TYPE=echo MSG="Standard Profile"
    {% elif 2 == P %}
        RESPOND TYPE=echo MSG="Sport Profile"
    {% elif 3 == P %}
        RESPOND TYPE=echo MSG="Ludricous Profile"
    {% else %}
        RESPOND TYPE=echo MSG="UNKNOWN Profile"
    {% endif %}

Now, there may be the requirement for saving the “printer_profile” variable in other macros, but for what’s outlined here in the question, I would think that passing the PROFILE parameter would be more appropriate as it more closely models what is considered the “better” programming model of:

SET_PRINTER_PROFILE( PROFILE Profile ) {

  if ( undefined Profile ) {
    Profile = 1;
  }

  IMPLEMENT_PRINTER_PROFILE( Profile );

}

IMPLEMENT_PRINTER_PROFILE( PROFILE Profile ) {

//  Do something with "Profile"

}

Rather than the model which is followed by @Florian 's macros and is somewhat “old school”:

PROFILE Printer_Profile = 1;

SET_PRINTER_PROFILE( PROFILE Profile ) {

  if ( !undefined Profile ) {
    Printer_Profile = Profile;
  }

  IMPLEMENT_PRINTER_PROFILE();

}

IMPLEMENT_PRINTER_PROFILE() {

//  Do something with "Printer_Profile"

}

The pseudo-code here is a bit clumsy as I’m trying to illustrate how the macros work but I think the point comes across.

Thanks @mykepredko. I absolutely agree with you that, using parameters for passing one or several variables from one to another macro (or to many other macros), is the cleaner and more straight forward way.

The reason why I went down the SET_GCODE_VARIABLE route is because I am using the Profile-Variable to send/alter M220 and M204 commands during runtime. This way, I will have one default speed slicer profile and the print speed is managed on the printer.

Once the whole bunch of macros is finished, it shall allow switching speed profiles at runtime, similar to what my Bambu Labs X1 Carbin does. My aim is to share all of this once it properly works. Now I am working on switching stealthChop on and off depending on the profile chosen…

Cheers
Florian

1 Like