homing.cfg modified - 07/20/2025 - 03:44:34 PM

This commit is contained in:
macocha 2025-07-20 15:44:37 +00:00
parent c0a5a8c314
commit 1a8336a687

View file

@ -1,84 +1,277 @@
# This configuration file contains variables and macros specific for homing X and Y using stallguard rather than
# physical switches. You will likely need to have DIAG jumpers installed on your mainboard and Klipper configured
# correctly for this to work.
#
# More information can be found here: https://www.klipper3d.org/TMC_Drivers.html?h=sens#sensorless-homing
#
# Be sure to [include] this .cfg file in printer.cfg **BEFORE** you have working SGTHRS/SGT values for
# [tmcXXXX stepper_x] and [tmcXXXX stepper_y] as that will simplify this process. If you already have SGTHRS/SGT
# values in your config, they will likely need adjusting after enabling this configuration.
#
### Default motor current and homing speed are configured per axis in printer.cfg ###
#
##################################
## XY Sensorless Homing Variables:
[gcode_macro _Sensorless_Homing_Variables]
description: Variables for sensorless homing X and Y
variable_homing_current: 0.49 # The desired motor current for homing the X and Y axes. Leave as '0.0' to use run_current values instead.
variable_clear_time: 1 # Time in seconds to wait for stallguard registers to clear, default is 1 second.
variable_x_backoff_distance: 5 # Distance in mm to back off from the X axis after homing.
variable_y_backoff_distance: 5 # Distance in mm to back off from the Y axis after homing.
variable_z_hop_distance: 10 # Distance to move Z axis prior to homing, and after homing.
variable_first_homed_axis: 'X' # First axis to home when 'G28' is called. Some prefer homing Y before X.
## The following variables are used for moving the printhead to a certain part of the bed before homing the Z axis:
variable_safe_z_enable: False # Enables/disables moving the printhead before homing the Z axis.
variable_safe_x: -128 # Safe X position to home the Z axis, leave at -128 to home to the center of the X axis.
variable_safe_y: -128 # Safe Y position to home the Z axis, leave at -128 to home to the center of the Y axis.
# Do not modify below
gcode:
[gcode_macro _HOME_X]
gcode:
# Home
{% set RUN_CURRENT_X = printer.configfile.settings['tmc2209 stepper_x'].run_current|float %}
{% set RUN_CURRENT_Y = printer.configfile.settings['tmc2209 stepper_y'].run_current|float %}
{% set HOME_CURRENT = 0.49 %}
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CURRENT}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={HOME_CURRENT}
SET_KINEMATIC_POSITION X=15 SET_HOMED=X
G91
G1 X-15 F1200
#G4 P2000
#M400 to finish all pending moves/process the buffer
M400
G28 X
# Move away
G91
G1 X-15 F1200
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={RUN_CURRENT_X}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={RUN_CURRENT_Y}
{% set sensorless_variables = printer["gcode_macro _Sensorless_Homing_Variables"] %} # Pull variables from _Sensorless_Homing_Variables
{% set homing_current = sensorless_variables.homing_current | float %} #
{% set backoff_distance = sensorless_variables.x_backoff_distance | float %} #
{% set clear_time = (sensorless_variables.clear_time * 1000) | float %} #
{% set positive_dir = printer.configfile.settings.stepper_x.homing_positive_dir | abs %} # Pull X axis homing speed from config
{% set homing_speed = (printer.configfile.settings['stepper_x'].homing_speed * 60) | float %} #
{% if homing_current != 0.0 %} # If a homing_current is defined, do the following:
{% if 'y' not in printer.toolhead.homed_axes %} # Check if the Y axis is not homed, if Y is not homed,
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={homing_current} # set the homing_current
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={homing_current} #
{% elif 'x' in printer.toolhead.homed_axes %} #
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={homing_current} # If X is already homed but is requested to home again,
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={homing_current} # set the homing_current
{% endif %} #
{% endif %} #
G4 P{clear_time} # Wait for stallguard registers to clear
G28 X # Home the X axis
{% if backoff_distance > 0 %} # Check if variable_backoff_distance is greater than 0, and back off from axis
{% if positive_dir == True %} # Check if the axis is homed in the positive direction
G91 # If it is, back away in the negative direction
G0 X-{backoff_distance} F{homing_speed} #
G90 #
{% else %} #
G91 # If the axis is not homed in the positive direction,
G0 X{backoff_distance} F{homing_speed} # back away in the positive direction
G90 #
{% endif %} #
{% endif %} #
{% if positive_dir == True %} # Check if homing direction is positive
G0 X{printer.configfile.settings.stepper_x.position_endstop - backoff_distance} # Set X axis position to remove offset incurred from sensorless homing
{% else %} #
G0 X{printer.configfile.settings.stepper_x.position_endstop + backoff_distance} # Set X axis position to remove offset incurred from sensorless homing
{% endif %} #
{% if homing_current != 0.0 %} # Check if a homing_current was configured
{% if 'tmc2209 stepper_x' in printer %} # If a homing current was configured, figure out the driver type for X
{% set driver_x = 'tmc2209' %} # and define them as driver_x
{% elif 'tmc5160 stepper_x' in printer %} #
{% set driver_x = 'tmc5160' %} #
{% elif 'tmc2130 stepper_x' in printer %} #
{% set driver_x = 'tmc2130' %} # https://www.klipper3d.org/Config_Reference.html#tmc-stepper-driver-configuration
{% elif 'tmc2660 stepper_x' in printer %} # All drivers at the address above containing SGTHRS or SGT configurations
{% set driver_x = 'tmc2660' %} # are checked here
{% elif 'tmc2240 stepper_x' in printer %} #
{% set driver_x = 'tmc2240' %} #
{% endif %} #
{% if 'tmc2209 stepper_y' in printer %} # If a homing current was configured, figure out the driver type for Y
{% set driver_y = 'tmc2209' %} # and define them as driver_y
{% elif 'tmc5160 stepper_y' in printer %} #
{% set driver_y = 'tmc5160' %} #
{% elif 'tmc2130 stepper_y' in printer %} #
{% set driver_y = 'tmc2130' %} # https://www.klipper3d.org/Config_Reference.html#tmc-stepper-driver-configuration
{% elif 'tmc2660 stepper_y' in printer %} # All drivers at the address above containing SGTHRS or SGT configurations
{% set driver_y = 'tmc2660' %} # are checked here
{% elif 'tmc2240 stepper_y' in printer %} #
{% set driver_y = 'tmc2240' %} #
{% endif %} #
{% if 'y' in printer.toolhead.homed_axes %} # If the Y axis is already homed, do the following:
{% set default_current_x = printer.configfile.settings[driver_x +' stepper_x'].run_current %} # Set default_current_x to stepper_x's run_current
{% set default_current_y = printer.configfile.settings[driver_y +' stepper_y'].run_current %} # Set default_current_y to stepper_y's run_current
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={default_current_x} # Revert stepper_x's current to stepper_x's run_current
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={default_current_y} # Revert stepper_y's current to stepper_y's run_current
{% endif %}
{% endif %} # End of homing_current section
G4 P{clear_time} # Wait for stallguard registers to clear
[gcode_macro _HOME_Y]
gcode:
{% set RUN_CURRENT_X = printer.configfile.settings['tmc2209 stepper_x'].run_current|float %}
{% set RUN_CURRENT_Y = printer.configfile.settings['tmc2209 stepper_y'].run_current|float %}
{% set HOME_CURRENT = 0.49 %}
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CURRENT}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={HOME_CURRENT}
SET_KINEMATIC_POSITION Y=15 SET_HOMED=Y
G91
G1 Y-15 F1200
#G4 P2000
#M400 to finish all pending moves/process the buffer
M400
# Home
G28 Y
# Move away
G91
G1 Y-15 F1200
# Wait just a second… (give StallGuard registers time to clear)
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={RUN_CURRENT_X}
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={RUN_CURRENT_Y}
{% set sensorless_variables = printer["gcode_macro _Sensorless_Homing_Variables"] %} # Pull variables from _Sensorless_Homing_Variables
{% set homing_current = sensorless_variables.homing_current | float %} #
{% set backoff_distance = sensorless_variables.y_backoff_distance | float %} #
{% set clear_time = (sensorless_variables.clear_time * 1000) | float %} #
{% set positive_dir = printer.configfile.settings.stepper_y.homing_positive_dir | abs %} # Pull Y axis homing speed from config
{% set homing_speed = (printer.configfile.settings['stepper_y'].homing_speed * 60) | float %} #
{% if homing_current != 0.0 %} # If a homing_current is defined, do the following:
{% if 'x' not in printer.toolhead.homed_axes %} # Check if the X axis is not homed, if X is not homed,
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={homing_current} # set the homing_current
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={homing_current} #
{% elif 'y' in printer.toolhead.homed_axes %} #
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={homing_current} # If Y is already homed but is requested to home again,
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={homing_current} # set the homing_current
{% endif %} #
{% endif %} #
G4 P{clear_time} # Wait for stallguard registers to clear
G28 Y # Home the Y axis
{% if backoff_distance > 0 %} # Check if variable_backoff_distance is greater than 0, and back off from axis
{% if positive_dir == True %} # Check if the axis is homed in the positive direction
G91 # If it is, back away in the negative direction
G0 Y-{backoff_distance} F{homing_speed} #
G90 #
{% else %} #
G91 # If the axis is not homed in the positive direction,
G0 Y{backoff_distance} F{homing_speed} # back away in the positive direction
G90 #
{% endif %} #
{% endif %} #
{% if positive_dir == True %} # Check if homing direction is positive
G0 Y{printer.configfile.settings.stepper_y.position_endstop - backoff_distance} # Set Y axis position to remove offset incurred from sensorless homing
{% else %} #
G0 Y{printer.configfile.settings.stepper_y.position_endstop + backoff_distance} # Set Y axis position to remove offset incurred from sensorless homing
{% endif %} #
{% if homing_current != 0.0 %} # Check if a homing_current is configured
{% if 'tmc2209 stepper_x' in printer %} # If a homing current was configured, figure out the driver type for X
{% set driver_x = 'tmc2209' %} # and define them as driver_x
{% elif 'tmc5160 stepper_x' in printer %} #
{% set driver_x = 'tmc5160' %} #
{% elif 'tmc2130 stepper_x' in printer %} #
{% set driver_x = 'tmc2130' %} # https://www.klipper3d.org/Config_Reference.html#tmc-stepper-driver-configuration
{% elif 'tmc2660 stepper_x' in printer %} # All drivers at the address above containing SGTHRS or SGT configurations
{% set driver_x = 'tmc2660' %} # are checked here
{% elif 'tmc2240 stepper_x' in printer %} #
{% set driver_x = 'tmc2240' %} #
{% endif %} #
{% if 'tmc2209 stepper_y' in printer %} # If a homing current was configured, figure out the driver type for Y
{% set driver_y = 'tmc2209' %} # and define them as driver_y
{% elif 'tmc5160 stepper_y' in printer %} #
{% set driver_y = 'tmc5160' %} #
{% elif 'tmc2130 stepper_y' in printer %} #
{% set driver_y = 'tmc2130' %} # https://www.klipper3d.org/Config_Reference.html#tmc-stepper-driver-configuration
{% elif 'tmc2660 stepper_y' in printer %} # All drivers at the address above containing SGTHRS or SGT configurations
{% set driver_y = 'tmc2660' %} # are checked here
{% elif 'tmc2240 stepper_y' in printer %} #
{% set driver_y = 'tmc2240' %} #
{% endif %} #
{% if 'x' in printer.toolhead.homed_axes %} # If the X axis is already homed, do the following:
{% set default_current_x = printer.configfile.settings[driver_x +' stepper_x'].run_current %} # Set default_current_x to stepper_x's run_current
{% set default_current_y = printer.configfile.settings[driver_y +' stepper_y'].run_current %} # Set default_current_y to stepper_y's run_current
SET_TMC_CURRENT STEPPER=stepper_x CURRENT={default_current_x} # Revert stepper_x's current to stepper_x's run_current
SET_TMC_CURRENT STEPPER=stepper_y CURRENT={default_current_y} # Revert stepper_y's current to stepper_y's run_current
{% endif %}
{% endif %} # End of homing_current section
G4 P{clear_time} # Wait for stallguard registers to clear
[homing_override]
axes: xyz
set_position_z: 0
gcode:
{% set home_all = 'X' not in params and 'Y' not in params and 'Z' not in params %}
SET_KINEMATIC_POSITION Z=1 SET_HOMED=Z
G1 Z4 F1200
{% set home_all = 'X' not in params and 'Y' not in params and 'Z' not in params %} #
{% set z_hop_speed = (printer.configfile.settings['stepper_z'].homing_speed * 60) | float %} #
{% set travel_speed = (printer.toolhead.max_velocity * 60) | float %} #
{% set sensorless_variables = printer["gcode_macro _Sensorless_Homing_Variables"] %} #
{% set z_hop_distance = sensorless_variables.z_hop_distance | float %} # Collect all variables needed for sensorless homing
{% set first_homed_axis = sensorless_variables.first_homed_axis | string %} # from machine config file and _Sensorless_Homing_Variables
{% set safe_x = sensorless_variables.safe_x | float %} #
{% set safe_y = sensorless_variables.safe_y | float %} #
{% set safe_z = sensorless_variables.safe_z_enable | abs %} #
{% if home_all or 'X' in params %}
_HOME_X
{% endif %}
{% if home_all or 'Y' in params %}
_HOME_Y
{% endif %}
{% if home_all or 'Z' in params %}
{% if printer.configfile.settings.beacon is defined %} # Check if a third-party [probe] definiton is used
{% set probe_name = printer.configfile.settings.beacon %} # If [beacon] is found in config, set 'probe_name' as [beacon] config string
{% elif printer.configfile.settings.probe is defined %} #
{% set probe_name = printer.configfile.settings.probe %} # If [probe] is found in config, set 'probe_name' as [probe] config string
{% elif printer.configfile.settings.dockable_probe is defined %} #
{% set probe_name = printer.configfile.settings.dockable_probe %} # If [dockable_probe] is found in config, set 'probe_name' as [dockable_probe] config string
{% elif printer.configfile.settings.bltouch is defined %} #
{% set probe_name = printer.configfile.settings.bltouch %} # If [bltouch] is found in config, set 'probe_name' as [bltouch] config string
{% endif %} #
G90
G1 X175 Y175 F15000
{% if 'probe' in printer.configfile.settings.stepper_z.endstop_pin %} # Check if Z is configured to home with a probe and pull config values for
{% set probe_x_offset = probe_name.x_offset | float %} # X and Y offsets
{% set probe_y_offset = probe_name.y_offset | float %} #
{% else %} #
{% set probe_x_offset = 0 | float %} #
{% set probe_y_offset = 0 | float %} # If Z if not homed with a probe, set offsets to 0 (do not apply an offset)
{% endif %} #
G28 Z
{% if safe_x == -128 %} #
{% set safe_x = (printer.configfile.settings.stepper_x.position_max) /2 %} # If safe_x is '-128', set safe_x to the center of the X axis
{% endif %} #
G1 Z10 F1500
{% endif %}
{% if probe_x_offset < 0 %} #
{% set safe_x = safe_x + probe_x_offset %} #
{% elif probe_x_offset > 0 %} # Depending on if probe_x_offset is a positive or negative value, adjust safe_x
{% set safe_x = safe_x - probe_x_offset %} # If the machine does not home Z with a probe, an offset is not applied.
{% endif %} #
{% if safe_y == -128 %} #
{% set safe_y = (printer.configfile.settings.stepper_y.position_max) /2 %} # If safe_y is '-128', set safe_y to the center of the Y axis
{% endif %} #
[gcode_macro _CG28]
description: Homing only if necessary
gcode:
{% if "xyz" not in printer.toolhead.homed_axes %}
G28
{% endif %}
{% if probe_y_offset < 0 %} #
{% set safe_y = safe_y + probe_y_offset %} #
{% elif probe_y_offset > 0 %} # Depending on if probe_y_offset is a positive or negative value, adjust safe_y
{% set safe_y = safe_y - probe_y_offset %} # If the machine does not home Z with a probe, an offset is not applied.
{% endif %} #
{% if z_hop_distance > 0 %} # Check if z_hop_distance is greater than zero
{% if 'x' not in printer.toolhead.homed_axes and 'y' not in printer.toolhead.homed_axes %} # If X and Y are not homed, move Z to z_hop_distance
G0 Z{z_hop_distance} F{z_hop_speed} #
{% endif %} #
{% endif %} #
{% if first_homed_axis == 'X' %} # If first_homed_axis is 'X', begin G28 param check
{% if home_all or 'X' in params %} #
_HOME_X # If home_all or 'X' is in params, home X
{% endif %} #
{% if home_all or 'Y' in params %} # If home_all or 'Y' in params, home Y
_HOME_Y #
{% endif %} #
{% endif %} #
{% if first_homed_axis == 'Y' %} # If first_homed_axis is 'Y', begin G28 param check
{% if home_all or 'Y' in params %} #
_HOME_Y # if home_all or 'Y' is in params, home Y
{% endif %} #
{% if home_all or 'X' in params %} # If home_all or 'X' in params, home X
_HOME_X #
{% endif %} #
{% endif %} #
{% if safe_z == True and (home_all or 'Z' in params) %} # If safe_z is true and home_all or 'Z' is in params,
G0 X{safe_x} Y{safe_y} F{travel_speed} # Move to the defined safe XY location
{% endif %} #
{% if home_all or 'Z' in params %} #
G28 Z # Home the Z axis
G0 Z{z_hop_distance} F{z_hop_speed} # Move Z to z_hop_distance
{% endif %} #