# See if the position should be restored after the attach
{% set goback=params.BACK|default(0) %}
# Get probe attach status
{% set probe_attached=printer["gcode_macro _Probe_Variables"].probe_attached %}
{% set probe_lock=printer["gcode_macro _Probe_Variables"].probe_lock %}
{% set verbose=printer["gcode_macro _User_Variables"].verbose %}
# Get Docking location
{% set dockmove_x=printer["gcode_macro _User_Variables"].dockmove_x|default(0)|float %}
{% set dockmove_y=printer["gcode_macro _User_Variables"].dockmove_y|default(0)|float %}
{% set dockmove_z=printer["gcode_macro _User_Variables"].dockmove_z|default(0)|float %}
{% set docklocation_x=printer["gcode_macro _User_Variables"].docklocation_x|float %}
{% set docklocation_y=printer["gcode_macro _User_Variables"].docklocation_y|float %}
{% set docklocation_z=printer["gcode_macro _User_Variables"].docklocation_z|float %}
{% set attachmove_x=printer["gcode_macro _User_Variables"].attachmove_x|default(0)|float %}
{% set attachmove_y=printer["gcode_macro _User_Variables"].attachmove_y|default(0)|float %}
{% set attachmove_z=printer["gcode_macro _User_Variables"].attachmove_z|default(0)|float %}
{% set attachmove2_x=printer["gcode_macro _User_Variables"].attachmove2_x|default(0)|float %}
{% set attachmove2_y=printer["gcode_macro _User_Variables"].attachmove2_y|default(0)|float %}
{% set attachmove2_z=printer["gcode_macro _User_Variables"].attachmove2_z|default(0)|float %}
# Safe Z for travel
{% set safe_z=printer["gcode_macro _User_Variables"].safe_z %}
{% set enable_z_hop=printer["gcode_macro _User_Variables"].enable_z_hop %}
# Set feedrates
{% set travel_feedrate=printer["gcode_macro _User_Variables"].travel_speed * 60 %}
{% set dock_feedrate=printer["gcode_macro _User_Variables"].dock_speed * 60 %}
{% set release_feedrate=printer["gcode_macro _User_Variables"].release_speed * 60 %}
{% set z_drop_feedrate=printer["gcode_macro _User_Variables"].z_drop_speed * 60 %}
{% set bypass_probe_docking=printer["gcode_macro _User_Variables"].bypass_probe_docking|default(False) %}
_entry_point function=Attach_Probe
{% if bypass_probe_docking== False %}
# If x and y are not homed
{% if not 'xy' in printer.toolhead.homed_axes %}
{ action_raise_error("Must Home X and Y Axis First!") }
_KlickyDebug msg="Attach_Probe Axis homed"
# If probe not attached and locked
{% elif not probe_attached and not probe_lock %}
_KlickyDebug msg="Attach_Probe going to attach probe"
{% if verbose %}
{ action_respond_info("Attaching Probe") }
{% endif %}
_KLICKY_STATUS_BUSY
{% if not 'z' in printer.toolhead.homed_axes %}
{% if verbose %}
{ action_respond_info("Resetting Z position to zero") }
{% endif %}
_KlickyDebug msg="Attach_Probe Z not homed, setting position as X=Y=Z=0"
SET_KINEMATIC_POSITION Z=0
{% if not enable_z_hop %} # Disables safe_z
_KlickyDebug msg="Attach_Probe z_hop disabled"
{% set safe_z=0 %}
{% endif %}
{% endif %}
# Prior to saving actual position, check if its necessary to move to a safe Z
# that has enought overhead for the attached probe
{% if printer.gcode_move.gcode_position.z < safe_z %}
_KlickyDebug msg="Attach_Probe toolhead too low, raising it to {safe_z}mm from {printer.gcode_move.gcode_position.z}mm"
{% if verbose %}
{ action_respond_info("moving to a safe Z distance") }
{% endif %}
G0 Z{safe_z} F{z_drop_feedrate}
{% endif %}
{% if not 'z' in printer.toolhead.homed_axes %} #duplicate??
{% if verbose %}
{ action_respond_info("Resetting Z position to zero, duplicate?") }
{% endif %}
_KlickyDebug msg="Attach_Probe Z not homed, setting position as X=Y=Z=0"
SET_KINEMATIC_POSITION Z=0
{% endif %}
{% if printer.gcode_move.gcode_position.z < safe_z %} #duplicate??
_KlickyDebug msg="Attach_Probe toolhead too low, raising it to {safe_z}mm from {printer.gcode_move.gcode_position.z}mm"
G0 Z{safe_z} F{z_drop_feedrate}
{% endif %}
_Umbilical_Path
_entry_point function=Attach_Probe_intern
# Probe entry location
_KlickyDebug msg="Attach_Probe moving near the dock with G0 X{docklocation_x - attachmove_x - attachmove2_x} Y{docklocation_y - attachmove_y - attachmove2_y} F{travel_feedrate}"
_KlickyDebug msg="Dock_Probe moving away from the dock to G0 X{docklocation_x + dockmove_x - attachmove_x} Y{docklocation_y + dockmove_y - attachmove_y} F{release_feedrate}"
{% set verbose=printer["gcode_macro _User_Variables"].verbose %}
{% set safe_z=printer["gcode_macro _User_Variables"].safe_z|float %}
# Safe Z for travel
{% set safe_z=printer["gcode_macro _User_Variables"].safe_z %}
{% set enable_z_hop=printer["gcode_macro _User_Variables"].enable_z_hop %}
{% set kinematic_z=0 %}
{% set dock_on_zhome=printer["gcode_macro _User_Variables"].dock_on_zhome|default(True) %}
{% set attachmove_y=printer["gcode_macro _User_Variables"].attachmove_y|default(0)|float %}
{% set z_drop_feedrate=printer["gcode_macro _User_Variables"].z_drop_speed * 60 %}
{% set travel_feedrate=printer["gcode_macro _User_Variables"].travel_speed * 60 %}
{% set home_backoff_x=printer["gcode_macro _User_Variables"].home_backoff_x|default(0)|float %}
{% set home_backoff_y=printer["gcode_macro _User_Variables"].home_backoff_y|default(0)|float %}
{% set override_homing=printer["gcode_macro _User_Variables"].override_homing|default('') %}
#checks if the variable definitions are up to date
_klicky_check_variables_version
_CheckProbe action=query
# reset parameters
{% set home_x, home_y, home_z, leave_probe_attached=False, False, False, False %}
{% if 'PROBE_LOCK' in params%}
{% if verbose %}
{ action_respond_info("PROBE_LOCK=True") }
{% endif %}
{% set leave_probe_attached=True %}
{% endif %}
# which axes have been requested for homing
{% if not 'X' in params
and not 'Y' in params
and not 'Z' in params %}
{% set home_x, home_y, home_z=True, True, True %}
_KlickyDebug msg="homing_override going to home all axes"
{% else %}
{% if 'X' in params %}
{% set home_x=True %}
_KlickyDebug msg="homing_override going to home X"
{% endif %}
{% if 'Y' in params %}
{% set home_y=True %}
_KlickyDebug msg="homing_override going to home Y"
{% endif %}
{% if 'Z' in params %}
{% set home_z=True %}
_KlickyDebug msg="homing_override going to home Z"
{% 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
_KlickyDebug msg="homing_override going to home all axes"
{% endif %}
{% endif %}
_entry_point function=homing_override
_KLICKY_STATUS_HOMING
# if Z is not homed, do not move the bed if it goes down
{% if 'z' not in printer.toolhead.homed_axes %}
{% if enable_z_hop== False %} # Disables safe_z
_KlickyDebug msg="homing_override z_hop disabled"
#preserve safe_z to use as the SET KINEMATIC Z position, so that the toolhead does not move to pick up the probe
{% set kinematic_z=safe_z %}
{% set safe_z=safe_z %}
{% endif %}
{% endif %}
#On the first G28 after motors losing power, moves the Z to safe_z distance, if z_hop is enabled
{% if 'x' not in printer.toolhead.homed_axes and 'y' not in printer.toolhead.homed_axes and 'z' not in printer.toolhead.homed_axes%}
{% if verbose %}
{ action_respond_info("No axis homed") }
{% endif %}
_KlickyDebug msg="homing_override no axis homed, setting position as X=Y=0 Z={kinematic_z}"
SET_KINEMATIC_POSITION X=0 Y=0 Z={kinematic_z}
M400
_KlickyDebug msg="homing_override moving toolhead to {safe_z}mm from {printer.gcode_move.gcode_position.z}mm"
{% if verbose %}
{ action_respond_info("moving to a safe Z distance") }
{% endif %}
G0 Z{safe_z} F{z_drop_feedrate}
{% if home_z !=True %}
_KlickyDebug msg="homing_override clearing axis homed state if not already homing Z"
M84
{% endif %}
{% else %}
_KlickyDebug msg="All axis homed"
{% if printer.gcode_move.gcode_position.z > safe_z %}
{% set safe_z=printer.gcode_move.gcode_position.z|float %}
_KlickyDebug msg="Setting Safe_z to {printer.gcode_move.gcode_position.z}mm as Z is now above configured safe_z"
{% endif %}
{% endif %}
{% if home_z %}
{% if 'x' not in printer.toolhead.homed_axes and 'y' not in printer.toolhead.homed_axes%}
{% if verbose %}
{ action_respond_info("X or Y not homed, forcing full G28") }
{% endif %}
{% set home_x, home_y, home_z=True, True, True %}
{% endif %}
{% endif %}
# if the dock is oriented on the Y, first do Y endstop
{% if ((attachmove_y== 0 and override_homing == '' ) or (override_homing == 'Y'))%}
# Home y
{% if home_y %}
{% if override_homing== 'Y' %}
_KlickyDebug msg="homing_override Y homing first override, due to override_homing = Y"
{% else %}
_KlickyDebug msg="homing_override Y homing first override, due to attachmove_y = 0"
{% endif %}
{% if verbose %}
{ action_respond_info("Homing Y") }
{% endif %}
{% if 'z' in printer.toolhead.homed_axes and printer.gcode_move.gcode_position.z < safe_z %}
_KlickyDebug msg="homing_override moving toolhead to {safe_z}mm from {printer.gcode_move.gcode_position.z}mm in Y homing seq"
{% if verbose %}
{ action_respond_info("moving to a safe Z distance") }
{% endif %}
G0 Z{safe_z} F{z_drop_feedrate}
{% endif %}
{% if printer["gcode_macro _HOME_Y"] is defined %}
_KlickyDebug msg="homing_override calling _HOME_Y external script to handle the Y homing"
_HOME_Y
{% else %}
_KlickyDebug msg="homing_override Homing Y G28 Y0"
G28 Y0
# does it need to back away from the home position
{% if home_backoff_y !=0 %}
{% if (printer.configfile.settings.stepper_y.position_endstop > (printer.configfile.settings.stepper_y.position_min|default(0) + printer.configfile.settings.stepper_y.position_max)/2) %}
_KlickyDebug msg="homing_override backing off Y endstop, G0 Y{printer.configfile.settings.stepper_y.position_endstop-home_backoff_y} F{travel_feedrate}"
# This macro was provided by discord user Garrettwp to whom i give my thanks for sharing it with me.
# I have tweaked it a lot.
#
# this macro is based on the great Annex magprobe dockable probe macros "#Originally developed by Mental, modified for better use on K-series printers by RyanG and Trails"
# that macro as since evolved into a klipper plugin that currently is pending inclusion in klipper
# more information here https://github.com/Annex-Engineering/Quickdraw_Probe/tree/main/Klipper_Macros
#
# by standing on the shoulders of giants, lets see if we can see further
# User richardjm revised the macro variables and added some functions, thanks a lot
# This macro home is https://github.com/jlas1/Klicky-Probe
###################
## Z Tilt Adjust
[gcode_macro Z_TILT_ADJUST]
rename_existing: _Z_TILT_ADJUST
description:
gcode:
{% set V=printer["gcode_macro _User_Variables"].verbose %}