How to set Z-Offset based on used build plate and filament with simple klipper macro

Wanted to share again some of ‘tools’ I use when printing with K2 (this macro with other values I use also on other printers).

I had to change my nozzle because after 500+ hours of printing with it it was starting to clog every 2nd print, even with some PLA and always with PETG.
After replacing the nozzle and running all printer calibration I realized that I need to increase my Z-Offset by +0.02 mm now because even PLA was pushed too much into print plate.
On top of that I bought a 3rd party print plate (on AliExpress) which has one one side a golden textured PEI and on the other side a black flat carbon laser texture. For the flat side I user the ‘high temp plate’ settings in slice but it needs also some increased Z-offset of additional +0.06mm.
Most of my PETG needs also +0.025 Z-Offset so therefore to automatically set the right Z-Offset I use the the below macro:

#[gcode_macro MY_PRINT_START] sets Z-Offset based on print bed and filament type 

[respond]
# the [respond] is needed to make m118 working and echo output! You need it only once in you macro file....

[gcode_macro MY_PRINT_START]
description: Add this to the "Machine start G-Code" section after the line "START_PRINT ..." in your special printer config file.
  Usage: MY_PRINT_START FILAMENT_TYPE="{filament_type[initial_no_support_extruder]}" CURR_BED_TYPE="{curr_bed_type}" N_ZO=<your default offset>
gcode:
    {% set CURR_BED_TYPE = params.CURR_BED_TYPE|default('none')|string %}
    {% set FILAMENT_TYPE = params.FILAMENT_TYPE|default('none')|string %}
    {% set N_ZO = params.N_ZO|default(0.0)|float %}
    M118 My_PRINT_START with {FILAMENT_TYPE} and {CURR_BED_TYPE} @{N_ZO}

    {% if FILAMENT_TYPE == 'PETG' %}
      M118 PETG filament detected, increasing Z-Offset by 0.025!
      {% set N_ZO = N_ZO + 0.025 %}
    {% elif FILAMENT_TYPE == 'TPU' %}
      {% set N_ZO = N_ZO + 0.025 %}
#    {% else %}
#      {% set N_ZO = N_ZO + 0.0 %}
    {% endif %}

    {% if CURR_BED_TYPE == 'High Temp Plate' %}
      M118 High Temp Plate (flat)  detected, increasing Z-Offset by 0.09!
      {% set N_ZO = N_ZO + 0.06 %}
#    {% elif CURR_BED_TYPE == ['Textured PEI Plate'] %}
#        {% set N_ZO = N_ZO + 0.0 %}
#    {% else %}
#        {% set N_ZO = N_ZO + 0.09 %}
    {% endif %}
    M118 The new ZO is: {N_ZO}
    SET_GCODE_OFFSET Z={N_ZO} MOVE=1

You can put the macro directly in the printer.cfg with fluidd but I have it in my ‘my_macros.cfg’ file which I include in ‘printer.cfg’ with

[include my_macros.cfg]

In this way I need to upload after firmware updates only my_macros.cfg and edit into printer.cfg only above line if firmware update changes printer.cfg.

Anyhow, to make the above macro working you need to run the macro also in your printer start code in slicer.

You need to enter in the ‘Machine start G-Code’ section after the ‘START_PRINT’ line

MY_PRINT_START FILAMENT_TYPE="{filament_type[initial_no_support_extruder]}" CURR_BED_TYPE="{curr_bed_type}" N_ZO=0.0

Where N_ZO will be the Z-Offset you would need with PLA on the textured print plate, before I changed my nozzle it was 0.0 but now it is 0.02!

In ‘Machine end G-Code’ you need to enter before the ‘END_PRINT’ line

SET_GCODE_OFFSET Z=0.0 MOVE=1

In this way the macro will be called just before loading filament and printing the purge line (which is important that purge line is also affected). At print end the Z-Offset is reset to 0 (or whatever you want or need).

The MY_PRINT_START macro itself is (in the listed version) only checking if filament = ‘PETG’ and if yes adding 0.025mm (you may change this for your filament needs) and then it’s checking if print plate = ‘High temp Plate’ and if yess adding my 0.06mm for this plate.

So in my case when I print PLA on textured plate the Z-Offset will be 0.02, for PETG it would be 0.045 and on High temp plate it would be 0.08 for PLA and 0.105 for PETG.

You may extend the macro for other filament or bed types as well, I use for TPU for example also the PETG settings.

All the best for your print optimization!

4 Likes

Hello frankjoke,

Many thanks for this.

You are a genius as usual :+1:

Cheers.

I changed the macro a bit to allow also to change TPU or PETG adjustments in slicers printer profile.

#[gcode_macro MY_PRINT_START] sets Z-Offset based on print bed and filament type 

[respond]
# the [respond] is needed to make m118 working and echo output! You need it only once in you macro file....

[gcode_macro MY_PRINT_START]
description: Add this to the "Machine start G-Code" section after the line "START_PRINT ..." in your special printer config file.
  Usage: MY_PRINT_START FILAMENT_TYPE="{filament_type[initial_no_support_extruder]}" CURR_BED_TYPE="{curr_bed_type}" ZO=<your default offset> ZG=<diff for PETG> ZT=<diff for TPU> ZB=<diff for flat bed>
gcode:
    {% set CURR_BED_TYPE = params.CURR_BED_TYPE|default('none')|string %}
    {% set FILAMENT_TYPE = params.FILAMENT_TYPE|default('none')|string %}
    {% set ZO = params.ZO|default(0.0)|float %}
    {% set ZG = params.ZG|default(0.025)|float %}
    {% set ZT = params.ZT|default(0.025)|float %}
    {% set ZB = params.ZB|default(0.06)|float %}
    M118 My_PRINT_START with {FILAMENT_TYPE} and {CURR_BED_TYPE} @{ZO} ZB={ZB} ZG={ZG} ZT={ZT}

    {% if FILAMENT_TYPE == 'PETG' %}
      M118 PETG filament detected, increasing Z-Offset by ZG={ZG}!
      {% set ZO = ZO + ZG %}
    {% elif FILAMENT_TYPE == 'TPU' %}
      M118 TPU filament detected, increasing Z-Offset by ZT={ZT}!
      {% set ZO = ZO + ZT %}
#    {% else %}
#      {% set ZO = ZO + 0.0 %}
    {% endif %}

    {% if CURR_BED_TYPE == 'High Temp Plate' %}
      M118 High Temp Plate (flat)  detected, increasing Z-Offset by ZB={ZB}!
      {% set ZO = ZO + ZB %}
#    {% elif CURR_BED_TYPE == ['Textured PEI Plate'] %}
#        {% set ZO = ZO + 0.0 %}
#    {% else %}
#        {% set ZO = ZO + 0.09 %}
    {% endif %}
    M118 The new ZO is: {ZO}
    SET_GCODE_OFFSET Z={ZO} MOVE=1

What I changed are the parameters for the macro call in the printer profile:

  • ZO replaces N_ZO and is the basic z-offset for all prints (default 0.0mm)
  • ZG is the z-offset to add for PETG (default 0.025mm)
  • ZT is the z-offset to add for TPU (default 0.025mm)
  • ZB is the z-offset to be added for a flat bed (default 0.06mm)

If you omit any of parameters the defaults will be used but if you have different PETG which need different Z-offset you may vary now.

The line to write into printer profile machine start code changes now to

MY_PRINT_START FILAMENT_TYPE="{filament_type[initial_no_support_extruder]}" CURR_BED_TYPE="{curr_bed_type}" ZO=0.02

if you have 0.02mm basic z-offset for PLA and for flat bed 0.08 (and not the default 0.06) as an example

2 Likes

Dear all!
As I copied over the macro from a non-CFS/AMS printer I had to change the call in the printer settings to make it work also on CFS when first filament used is not in first slot!

I reflected the changes needed in above postings but you need to change only the printer config settings line to:

MY_PRINT_START FILAMENT_TYPE="{filament_type[initial_no_support_extruder]}" CURR_BED_TYPE="{curr_bed_type}" ZO=0.01

where ZO=0.01 is you Z-offset when using PLA, most probably can be removed or set to 0.0 for you (I need it for new nozzle).

Here is again the picture where you need to enter the line:

1 Like

I don’t think this macro has gotten the respect it deserves! This is awesome!

Absolutely brilliant. The macro I’ve been using is one-size-fits-all… one Z-offset for every material. :joy: This is next-level!

Oh, and flat bed? :laughing: I managed to get mine leveled down to a 0.0600mm deviation after a full heat soak at 50°C for my PLA prints. Took some serious time and patience! Most of the deviation happens toward the outer edges due to expansion at the center, but I’m totally fine with that.

I actually designed a handy dial indicator jig in Fusion360 that attaches to the print head for more precise adjustments. Made bed leveling way smoother! Suppose I should throw that into Creality Cloud for others to utilize.

Thanks @frankjoke !