Skip to content

Instantly share code, notes, and snippets.

@Jomik
Created November 8, 2025 13:23
Show Gist options
  • Select an option

  • Save Jomik/74ba0bf306b9930127f41885596504b7 to your computer and use it in GitHub Desktop.

Select an option

Save Jomik/74ba0bf306b9930127f41885596504b7 to your computer and use it in GitHub Desktop.
[gcode_macro CARTO_CALIBRATE_TOOL_Z]
description: Macro to calibrate Z offsets for all tools using the cartographer touch probe.
variable_z_offsets: {}
gcode:
{% if printer.toolhead.homed_axes != 'xyz' %}
{ action_raise_error("Please home the printer before running this macro.") }
{% endif }
{% set zrp = printer.configfile.bed_mesh.zero_reference_position | tuple | default((50,50))}
{action_respond_info("Calibrating Z offsets for all tools")}
{% set z_offsets = {} }
{% set tools = printer.toolchanger.tool_numbers %}
{% for tool_num in tools %}
RESPOND TYPE=command MSG="Starting calibration for tool {tool}"
M104 T{tool} S150
SELECT_TOOL T={tool}
M109 S150
G0 X{zrp[0]} Y{zrp[1]}
CARTOGRAPHER_TOUCH_PROBE
M104 S0
_CARTO_CALIBRATE_TOOL_Z_SAVE
{% endfor %}
[gcode_macro _CARTO_CALIBRATE_TOOL_Z_SAVE]
gcode:
{% set active_tool = printer.toolchanger.tool_number|int %}
{% set z_result = printer.cartographer.touch.last_z_result %}
{% if z_result is none %}
{ action_raise_error("No probe result found for %s" % active_tool) }
{% endif }
{% set z_result = printer.probe.last_z_result %}
{% set z_offsets = printer["gcode_macro CARTO_CALIBRATE_TOOL_Z"].z_offsets %}
{% set z_offsets = z_offsets.update({active_tool: z_result}) %}
SET_GCODE_VARIABLE MACRO=CARTO_CALIBRATE_TOOL_Z VARIABLE=z_offsets VALUE="{ z_offsets }"
{ action_respond_info("Saved T%d => %0.3f" % (active_tool, -z_result)) }
[gcode_macro _CARTO_CALIBRATE_TOOL_EVALUATE]
gcode:
{% set tools = printer.toolchanger.tool_numbers %}
{% set z_offsets = printer["gcode_macro CARTO_CALIBRATE_TOOL_Z"].z_offsets %}
RESPOND TYPE=command MSG="--- Z-Offsets from Cartographer Saved ---"
RESPOND TYPE=command MSG="------------------------------------"
{% for tool_num in tools %}
{% set saved_offset = z_offsets[tool_num] }
RESPOND TYPE=command MSG="gcode_z_offset for tool T{tool_num}: {saved_offset}"
{% endfor %}
RESPOND TYPE=command MSG="------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment