I'd like to live better with Global Values

This is an offshoot of getting the homing working on my custom CoreXY printer. As I was working on and testing the macros I kept forgetting that with a CoreXY, you need to change both the A and B steppers with the same values (@Sineos pointed this out to me at least once). This was a pain when you’re working with sensorless homing.

So, after a bit of research and playing around, I figured out how to define what are essentially global values in an outside gcode macro and use them in the X and Y axis homing macros:

[gcode_macro global] 
variable_xy_run_current:  0.8 
variable_xy_home_current: 0.7
gcode:
    M115  ### Get Version/Basically a Nop


[gcode_macro _HOME_X]
gcode:
    SET_TMC_CURRENT STEPPER=stepper_x CURRENT={printer["gcode_macro global"].xy_home_current}
    SET_TMC_CURRENT STEPPER=stepper_y CURRENT={printer["gcode_macro global"].xy_home_current}

    G4 P500

    G28 X

    G91
    G1 X-120 F1200

    SET_TMC_CURRENT STEPPER=stepper_x CURRENT={printer["gcode_macro global"].xy_run_current}
    SET_TMC_CURRENT STEPPER=stepper_y CURRENT={printer["gcode_macro global"].xy_run_current}

#### _HOME_Y same except for "G28 Y" and "G1 Y110 F1200"

Now, for the life of me, I can’t figure out how to set run_current: in [tmc2209 stepper_x]/[tmc2209 stepper_y] with these values that I’ve created in the “global” gcode_macro. I’ve tried lots of permutations of printer["gcode_macro global"].xy_run_current with trying to set the value to a variable within the statement (no go), using the value but with no, one and two sets of braces (“{” & “}”).

Is it possible to set run_current with the xy_run_current value? I’m starting to suspect it isn’t. But, if it was, then I am setting the current value, which is used in multiple places once to ensure nothing gets forgotten to be changed later, during tuning and other operations. This would be good coding practice.

That macro should work. What error(s) are you getting?

You can simplify things slightly like this. The “set global” directive in the GLOBAL macro is just to replace the M115 so that it doesn’t print the firmware version to console whenever the globals are instantiated. I don’t think the capitalization of the macro names should make a difference but that’s the way I format mine and I’m using a ton of these kinds of portable macro variables so color me superstitious.

[gcode_macro GLOBAL] 
variable_xy_run_current:0.8 
variable_xy_home_current:0.7
gcode:
    {% set global = printer["gcode_macro GLOBAL"] %}

[gcode_macro _HOME_X]
gcode:
    {% set global = printer["gcode_macro GLOBAL"] %}
    SET_TMC_CURRENT STEPPER=stepper_x CURRENT={global.xy_home_current}
    SET_TMC_CURRENT STEPPER=stepper_y CURRENT={global.xy_home_current}

    G4 P500

    G28 X

    G91
    G1 X-120 F1200

    SET_TMC_CURRENT STEPPER=stepper_x CURRENT={global.xy_run_current}
    SET_TMC_CURRENT STEPPER=stepper_y CURRENT={global.xy_run_current}

#### _HOME_Y same except for "G28 Y" and "G1 Y110 F1200"

Sorry, I guess I wasn’t specific enough in regards to my problems. The problem is in getting "xy_run_current to be loaded into the [tmc2209 stepper_x] statement.

Here are some of the permutations I’ve tried (commented out):

[tmc2209 stepper_x]
###    {% set run_current  = printer["gcode_macro global"].xy_run_current %}
uart_pin: PC4
###    {% set RUN_CURRENT  = printer["gcode_macro global"].xy_run_current %}
run_current: 0.75
### run_current: RUN_CURRENT
### run_current: printer["gcode_macro global"].xy_run_current
### hold_current: 0.6  
sense_resistor: 0.110
stealthchop_threshold: 999999
driver_SGTHRS: 60   
diag_pin: PG6

So, I’m looking to load run_current: with the xy_run_current value. Each one I tried above (and a few others that were things like single and double braces around the jinja2 variable) resulted in an error at the run_current parameter when I did a save and restart.

Any ideas how to use the macro for setting run_current in [tmc2209 stepper_x] (and “y”)?

Thank you for your reply and the suggestion for the GLOBAL macro.

Oh. Yeah you can’t do that. But I think you could accomplish the same thing like this:

[tmc2209 stepper_x]
uart_pin: PC4
run_current: 0.8
hold_current: 0.6  
sense_resistor: 0.110
stealthchop_threshold: 999999
driver_SGTHRS: 60   
diag_pin: PG6
[gcode_macro GLOBAL] 
variable_xy_home_current:0.7
gcode:
    {% set global = printer["gcode_macro GLOBAL"] %}

[gcode_macro _HOME_X]
gcode:
    {% set home_current = printer["gcode_macro GLOBAL"].xy_home_current %}
    {% set run_current = printer.configfile.settings['tmc2209 stepper_x'].run_current %}
    SET_TMC_CURRENT STEPPER=stepper_x CURRENT={home_current}
    SET_TMC_CURRENT STEPPER=stepper_y CURRENT={home_current}

    G4 P500

    G28 X

    G91
    G1 X-120 F1200

    SET_TMC_CURRENT STEPPER=stepper_x CURRENT={run_current}
    SET_TMC_CURRENT STEPPER=stepper_y CURRENT={run_current}

#### _HOME_Y same except for "G28 Y" and "G1 Y110 F1200"

Not quite what I want to do - the issue is that there’s also the [tmc2209 stepper_y] statement - I wanted to have one location where I set all the current settings (and there are six of them; four for run_current and two for hold_current) all in one place.

@koconnor and @Sineos any ideas or this is as good as it’s going to get?

Ah. Well you can’t use jinja2 in the main config sections like that. But if you’re using the gcode macros to override the current settings anyway, it probably doesn’t matter what you put in the config sections.

1 Like

Hmmm… I think you’re right.

Every print will execute “G28” before starting the print and that will leave the printer at what ever current setting I want it to.

You’re a genius!

Thanx!