Skip to content

Instantly share code, notes, and snippets.

@nmsmith22389
Last active April 7, 2024 00:56
Show Gist options
  • Save nmsmith22389/2ccca33d19da17f8a04f544b41d8d7cb to your computer and use it in GitHub Desktop.
Save nmsmith22389/2ccca33d19da17f8a04f544b41d8d7cb to your computer and use it in GitHub Desktop.
Klippain homing override macro
# Homing Override for dockable probe
[homing_override]
axes: xyz
gcode:
{% set verbose = printer["gcode_macro _USER_VARIABLES"].verbose %}
{% set kinematics = printer["configfile"].config["printer"]["kinematics"] %}
{% set probe_type_enabled = printer["gcode_macro _USER_VARIABLES"].probe_type_enabled %}
{% set homing_zhop = printer["gcode_macro _USER_VARIABLES"].homing_zhop|float|abs %}
{% set homing_travel_speed = printer["gcode_macro _USER_VARIABLES"].homing_travel_speed * 60 %}
{% set homing_travel_accel = printer["gcode_macro _USER_VARIABLES"].homing_travel_accel %}
{% set sensorless_homing_enabled = printer["gcode_macro _USER_VARIABLES"].sensorless_homing_enabled %}
{% set sensorless_current_factor = printer["gcode_macro _USER_VARIABLES"].sensorless_current_factor / 100 %}
{% set x_driver = printer["gcode_macro _USER_VARIABLES"].x_driver %}
{% set y_driver = printer["gcode_macro _USER_VARIABLES"].y_driver %}
{% set z_driver = printer["gcode_macro _USER_VARIABLES"].z_driver %}
{% set z_drop_speed = printer["gcode_macro _USER_VARIABLES"].z_drop_speed * 60 %}
{% set status_leds_enabled = printer["gcode_macro _USER_VARIABLES"].status_leds_enabled %}
{% set bed_mesh_enabled = printer["gcode_macro _USER_VARIABLES"].bed_mesh_enabled %}
{% set homing_first = printer["gcode_macro _USER_VARIABLES"].homing_first|string|upper %}
{% set x_homing_backoff, y_homing_backoff = printer["gcode_macro _USER_VARIABLES"].homing_backoff_distance_xy|map('float') %}
{% set x_position_endstop = printer["configfile"].config["stepper_x"]["position_endstop"]|float %}
{% set y_position_endstop = printer["configfile"].config["stepper_y"]["position_endstop"]|float %}
{% set x_position_center = printer.toolhead.axis_maximum.x|int/2 - printer.toolhead.axis_minimum.x|int/2 %}
{% set y_position_center = printer.toolhead.axis_maximum.y|int/2 - printer.toolhead.axis_minimum.y|int/2 %}
{% if probe_type_enabled == "dockable" or probe_type_enabled == "dockable_virtual" %}
_CHECK_PROBE action=query
{% endif %}
# reset parameters
{% set X, Y, Z = False, False, False %}
{% if status_leds_enabled %}
STATUS_LEDS COLOR="HOMING"
{% endif %}
# which axes have been requested for homing
{% if not 'X' in params
and not 'Y' in params
and not 'Z' in params %}
{% set X, Y, Z = True, True, True %}
{% else %}
{% if 'X' in params %}
{% set X = True %}
{% endif %}
{% if 'Y' in params %}
{% set Y = True %}
{% endif %}
{% if 'Z' in params %}
{% set Z = True %}
{% endif %}
{% if 'X' in params
and 'Y' in params
and 'Z' in params %}
# reset homing state variables
# if homing all axes
_HOMING_VARIABLES reset=1
{% endif %}
{% endif %}
{% if probe_type_enabled == "dockable" or probe_type_enabled == "dockable_virtual" %}
_ENTRY_POINT FUNCTION=homing_override
{% endif %}
# Set the homing acceleration prior to any movement
{% set saved_accel = printer.toolhead.max_accel %}
M204 S{homing_travel_accel}
{% if bed_mesh_enabled %}
BED_MESH_CLEAR
{% endif %}
G90
{% if Z %}
{% if ('z' in printer.toolhead.homed_axes) %}
{% if (printer.toolhead.position.z < homing_zhop) %}
{% if verbose %}
{ action_respond_info("Z too low, performing ZHOP to rehome Z") }
{% endif %}
G91
G0 Z{homing_zhop} F{z_drop_speed}
G90
{% else %}
{% if verbose %}
{ action_respond_info("Z already safe, no ZHOP needed to rehome Z") }
{% endif %}
{% endif %}
{% elif ('xy' in printer.toolhead.homed_axes) %}
{% if verbose %}
{ action_respond_info("X and Y already homed, no ZHOP needed to home Z") }
{% endif %}
{% else %}
{% if verbose %}
{ action_respond_info("X and Y not homed, forcing full G28 to home Z properly") }
{% endif %}
SET_KINEMATIC_POSITION X=0 Y=0 Z=0
G0 Z{homing_zhop} F{z_drop_speed}
{% if sensorless_homing_enabled and kinematics == "corexz" %}
# Wait for stallguard registers to clear
M400
{% endif %}
{% set X, Y, Z = True, True, True %}
{% endif %}
{% endif %}
{% if homing_first == "X" %}
{% if X %} # Home x
{% if verbose %}
{ action_respond_info("Homing X") }
{% endif %}
{% if sensorless_homing_enabled %}
{% if kinematics == "corexy" %}
{% set old_current_x = printer.configfile.config[x_driver ~ ' stepper_x'].run_current|float %}
{% set old_current_y = printer.configfile.config[y_driver ~ ' stepper_y'].run_current|float %}
{% set new_current_x = sensorless_current_factor * old_current_x %}
{% set new_current_y = sensorless_current_factor * old_current_y %}
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={new_current_x}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={new_current_y}
M400
{% elif kinematics == "corexz" %}
{% set old_current_x = printer.configfile.config[x_driver ~ ' stepper_x'].run_current|float %}
{% set old_current_z = printer.configfile.config[z_driver ~ ' stepper_z'].run_current|float %}
{% set new_current_x = sensorless_current_factor * old_current_x %}
{% set new_current_z = sensorless_current_factor * old_current_z %}
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={new_current_x}
SET_TMC_CURRENT STEPPER=stepper_z CURRENT={new_current_z}
M400
{% elif kinematics == "cartesian" %}
{% set old_current_x = printer.configfile.config[x_driver ~ ' stepper_x'].run_current|float %}
{% set new_current_x = sensorless_current_factor * old_current_x %}
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={new_current_x}
M400
{% endif %}
{% endif %}
G28 X0
G1 X{x_position_endstop + x_homing_backoff} F{homing_travel_speed}
{% if sensorless_homing_enabled %}
{% if kinematics == "corexy" %}
M400
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={old_current_x}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={old_current_y}
{% elif kinematics == "corexz" %}
M400
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={old_current_x}
SET_TMC_CURRENT STEPPER=stepper_z CURRENT={old_current_z}
{% elif kinematics == "cartesian" %}
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={old_current_x}
{% endif %}
{% endif %}
{% endif %}
{% if Y %} # Home y
{% if verbose %}
{ action_respond_info("Homing Y") }
{% endif %}
{% if sensorless_homing_enabled %}
{% if kinematics == "corexy" %}
{% set old_current_x = printer.configfile.config[x_driver ~ ' stepper_x'].run_current|float %}
{% set old_current_y = printer.configfile.config[y_driver ~ ' stepper_y'].run_current|float %}
{% set new_current_x = sensorless_current_factor * old_current_x %}
{% set new_current_y = sensorless_current_factor * old_current_y %}
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={new_current_x}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={new_current_y}
M400
{% elif kinematics == "corexz" %}
{% set old_current_y = printer.configfile.config[y_driver ~ ' stepper_y'].run_current|float %}
{% set new_current_y = sensorless_current_factor * old_current_y %}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={new_current_y}
M400
{% elif kinematics == "cartesian" %}
{% set old_current_y = printer.configfile.config[y_driver ~ ' stepper_y'].run_current|float %}
{% set new_current_y = sensorless_current_factor * old_current_y %}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={new_current_y}
M400
{% endif %}
{% endif %}
G28 Y0
G1 Y{y_position_endstop + y_homing_backoff} F{homing_travel_speed}
{% if sensorless_homing_enabled %}
{% if kinematics == "corexy" %}
M400
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={old_current_x}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={old_current_y}
{% elif kinematics == "corexz" %}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={old_current_y}
{% elif kinematics == "cartesian" %}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={old_current_y}
{% endif %}
{% endif %}
{% endif %}
{% elif homing_first == "Y" %}
{% if Y %} # Home y
{% if verbose %}
{ action_respond_info("Homing Y") }
{% endif %}
{% if sensorless_homing_enabled %}
{% if kinematics == "corexy" %}
{% set old_current_x = printer.configfile.config[x_driver ~ ' stepper_x'].run_current|float %}
{% set old_current_y = printer.configfile.config[y_driver ~ ' stepper_y'].run_current|float %}
{% set new_current_x = sensorless_current_factor * old_current_x %}
{% set new_current_y = sensorless_current_factor * old_current_y %}
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={new_current_x}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={new_current_y}
M400
{% elif kinematics == "corexz" %}
{% set old_current_y = printer.configfile.config[y_driver ~ ' stepper_y'].run_current|float %}
{% set new_current_y = sensorless_current_factor * old_current_y %}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={new_current_y}
M400
{% elif kinematics == "cartesian" %}
{% set old_current_y = printer.configfile.config[y_driver ~ ' stepper_y'].run_current|float %}
{% set new_current_y = sensorless_current_factor * old_current_y %}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={new_current_y}
M400
{% endif %}
{% endif %}
G28 Y0
G1 Y{y_position_endstop + y_homing_backoff} F{homing_travel_speed}
{% if sensorless_homing_enabled %}
{% if kinematics == "corexy" %}
M400
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={old_current_x}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={old_current_y}
{% elif kinematics == "corexz" %}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={old_current_y}
{% elif kinematics == "cartesian" %}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={old_current_y}
{% endif %}
{% endif %}
{% endif %}
{% if X %} # Home x
{% if verbose %}
{ action_respond_info("Homing X") }
{% endif %}
{% if sensorless_homing_enabled %}
{% if kinematics == "corexy" %}
{% set old_current_x = printer.configfile.config[x_driver ~ ' stepper_x'].run_current|float %}
{% set old_current_y = printer.configfile.config[y_driver ~ ' stepper_y'].run_current|float %}
{% set new_current_x = sensorless_current_factor * old_current_x %}
{% set new_current_y = sensorless_current_factor * old_current_y %}
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={new_current_x}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={new_current_y}
M400
{% elif kinematics == "corexz" %}
{% set old_current_x = printer.configfile.config[x_driver ~ ' stepper_x'].run_current|float %}
{% set old_current_z = printer.configfile.config[z_driver ~ ' stepper_z'].run_current|float %}
{% set new_current_x = sensorless_current_factor * old_current_x %}
{% set new_current_z = sensorless_current_factor * old_current_z %}
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={new_current_x}
SET_TMC_CURRENT STEPPER=stepper_z CURRENT={new_current_z}
M400
{% elif kinematics == "cartesian" %}
{% set old_current_x = printer.configfile.config[x_driver ~ ' stepper_x'].run_current|float %}
{% set new_current_x = sensorless_current_factor * old_current_x %}
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={new_current_x}
M400
{% endif %}
{% endif %}
G28 X0
G1 X{x_position_endstop + x_homing_backoff} F{homing_travel_speed}
{% if sensorless_homing_enabled %}
{% if kinematics == "corexy" %}
M400
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={old_current_x}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={old_current_y}
{% elif kinematics == "corexz" %}
M400
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={old_current_x}
SET_TMC_CURRENT STEPPER=stepper_z CURRENT={old_current_z}
{% elif kinematics == "cartesian" %}
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={old_current_x}
{% endif %}
{% endif %}
{% endif %}
{% else %}
{ action_respond_error("Axis homing order not valid. Choose either X or Y first in the variables.cfg file!") }
{% endif %}
# Home z
{% if Z %}
{% if verbose %}
{ action_respond_info("Homing Z") }
{% endif %}
# If there is a virtual probe endstop (ie. TAP or inductive as virtual), we go to the center of the bed
# If the Z endstop is not virtual, then we just move to it
{% if printer["configfile"].config["stepper_z"]["endstop_pin"]|lower == "probe:z_virtual_endstop" %}
# If our virtual endstop is a dockable probe, we need to activate it first
# If it's the voron tap, we put it to a safe temperature
{% if probe_type_enabled == "dockable_virtual" or probe_type_enabled == "vorontap" %}
ACTIVATE_PROBE
{% endif %}
# If there is a bed_mesh enabled and a zero_reference_position set, we retrieve it to home on it
# Else, we default to the center of the bed
{% if not bed_mesh_enabled or not printer["configfile"].config["bed_mesh"]["zero_reference_position"] %}
{% set probe_x_offset = 0 %} # Default x_offset to 0
{% set probe_y_offset = 0 %} # Default y_offset to 0
{% if probe_type_enabled == "bltouch" %}
{% if 'x_offset' in printer.configfile.config.bltouch %}
{% set probe_x_offset = printer.configfile.config.bltouch.x_offset %}
{% endif %}
{% if 'y_offset' in printer.configfile.config.bltouch %}
{% set probe_y_offset = printer.configfile.config.bltouch.y_offset %}
{% endif %}
{% else %}
{% if 'x_offset' in printer.configfile.config.probe %}
{% set probe_x_offset = printer.configfile.config.probe.x_offset %}
{% endif %}
{% if 'y_offset' in printer.configfile.config.probe %}
{% set probe_y_offset = printer.configfile.config.probe.y_offset %}
{% endif %}
{% endif %}
G0 X{x_position_center - probe_x_offset} Y{y_position_center - probe_y_offset} F{homing_travel_speed}
{% else %}
{% set ZRPx, ZRPy = printer["configfile"].config["bed_mesh"]["zero_reference_position"].split(',')|map('trim')|map('float') %}
G0 X{ZRPx} Y{ZRPy} F{homing_travel_speed}
{% endif %}
# Else, go to the Z endstop physical pin
{% else %}
_GOTO_Z_PROBE
{% endif %}
G28 Z0
G91
{% if printer["configfile"].settings["stepper_z"]["homing_positive_dir"] == False %}
G0 Z{homing_zhop} F{z_drop_speed} # small Z hop to avoid grinding the bed (as we should be close to Z0 right now)
{% elif printer["configfile"].settings["stepper_z"]["homing_positive_dir"] == True %}
G0 Z-{homing_zhop} F{z_drop_speed} # small Z move in the opposite direction to avoid staying on the endstop (not dangerous since we should be at Z max)
{% endif %}
G90
# if voron tap, restore original temperature
# if dockable probe as virtual endstop, then dock the probe
{% if probe_type_enabled == "vorontap" or probe_type_enabled == "dockable_virtual" %}
DEACTIVATE_PROBE
{% endif %}
{% endif %}
{% if probe_type_enabled == "dockable" or probe_type_enabled == "dockable_virtual" %}
_CHECK_PROBE action=query
{% endif %}
# Reset acceleration values to what it was before
SET_VELOCITY_LIMIT ACCEL={saved_accel}
{% if probe_type_enabled == "dockable" or probe_type_enabled == "dockable_virtual" %}
_EXIT_POINT FUNCTION=homing_override
{% endif %}
{% if status_leds_enabled %}
STATUS_LEDS COLOR="READY"
{% endif %}
[gcode_macro _GOTO_Z_PROBE]
description: Move to z probe avoiding the probe dock
gcode:
{% set verbose = printer["gcode_macro _USER_VARIABLES"].verbose %}
# If the auto z calibration plugin is enabled, then we use the z endstop coordinates set in its config section [z_calibration]
# Else we fall back on the variable_zendstop_position set in the variables.cfg file
{% set zcalib_plugin_enabled = printer["gcode_macro _USER_VARIABLES"].zcalib_plugin_enabled %}
{% if zcalib_plugin_enabled %}
{% set z_endstop_x, z_endstop_y = printer["configfile"].config["z_calibration"]["nozzle_xy_position"].split(',')|map('trim')|map('float') %}
{% else %}
{% set z_endstop_x, z_endstop_y = printer["gcode_macro _USER_VARIABLES"].zendstop_position|map('float') %}
{% endif %}
{% set homing_travel_speed = printer["gcode_macro _USER_VARIABLES"].homing_travel_speed|float * 60 %}
{% set probe_dock_location_x, probe_dock_location_y = printer["gcode_macro _USER_VARIABLES"].probe_dock_location_xy|map('float') %}
{% set probe_dock_margin_x, probe_dock_margin_y = printer["gcode_macro _USER_VARIABLES"].probe_dock_margin_xy|map('float') %}
{% set current_x = printer.toolhead.position.x|float %}
{% set current_y = printer.toolhead.position.y|float %}
SAVE_GCODE_STATE NAME=goto_ZProbe
G90
# check if toolhead have to avoid the dock
{% set avoid_dock = false %}
{% if probe_dock_location_x < z_endstop_x|float %}
#dock on left
#toolhead on right of dock ?
{% if current_x < (probe_dock_location_x + probe_dock_margin_x) %}
{% set avoid_dock = true %}
{% endif %}
{% else %}
#dock on right
#toolhead on left of dock ?
{% if current_x > (probe_dock_location_x - probe_dock_margin_x) %}
{% set avoid_dock = true %}
{% endif %}
{% endif %}
{% if avoid_dock == true %}
{% if verbose %}
{ action_respond_info("Avoiding probe dock to home Z...") }
{% endif %}
G0 Y{probe_dock_location_y - probe_dock_margin_y} F{homing_travel_speed}
G0 X{z_endstop_x} F{homing_travel_speed}
{% endif %}
G0 X{z_endstop_x} Y{z_endstop_y} F{homing_travel_speed}
RESTORE_GCODE_STATE NAME=goto_ZProbe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment