protoloft / klipper_z_calibration

Klipper plugin for self-calibrating z-offset
GNU General Public License v3.0
1.06k stars 152 forks source link

Do I need to move xy to a safe position myself after G28? #123

Closed rushairer closed 1 year ago

rushairer commented 1 year ago

# 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.
# They are 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", kudos to them.
# 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
# User richardjm revised the macro variables and added some functions, thanks a lot
# by standing on the shoulders of giants, lets see if we can see further
#
# the current home for this version is https://github.com/jlas1/Klicky-Probe
# the 1000 values below is to give an error instead of doing something wrong, hopefully, this won't be used is a printer larger than 1 meter

[gcode_macro _User_Variables]
variable_verbose:             True    # Enable verbose output
variable_debug:              False    # Enable Debug output
variable_travel_speed:         200    # how fast all other travel moves will be performed when running these macros
variable_move_accel:          1000    # how fast should the toolhead accelerate when moving
variable_dock_speed:            50    # how fast should the toolhead move when docking the probe for the final movement
variable_release_speed:         75    # how fast should the toolhead move to release the hold of the magnets after docking
variable_z_drop_speed:          10    # how fast the z will lower when moving to the z location to clear the probe

variable_safe_z:                10    # Minimum Z for attach/dock and homing functions
# if true it will move the bed away from the nozzle when Z is not homed
variable_enable_z_hop:        True    # set this to false for beds that fall significantly under gravity (almost to Z max)

variable_max_bed_y:            310    # maximum Bed size avoids doing a probe_accuracy outside the bed
variable_max_bed_x:            310    # maximum Bed size avoids doing a probe_accuracy outside the bed

# if a separate Z endstop switch is in
# use, specify the coordinates of the switch here (Voron).
# Set to 0 to have the probe move to center of bed
variable_z_endstop_x:         254
variable_z_endstop_y:         313

#Check the printer specific documentation on klipper Dock/Undock configuration, these are dummy values
#dock location
variable_docklocation_x:      178    # X Dock position
variable_docklocation_y:      303    # Y Dock position
variable_docklocation_z:      18.96    # Z dock position (-128 for a gantry/frame mount)

#The following variables are used if the dock is deployed and retracted via a servo motor
variable_enable_dock_servo:  True    # Set to true if your klicky dock is servo-controlled
variable_servo_name:         'probe_servo'    # The name of the dock servo defined in printer.cfg under [servo]
variable_servo_deploy:          0    # This EXAMPLE is the value used to deploy the servo fully
variable_servo_retract:         90    # This EXAMPLE is the value used to retract the servo fully (initial_angle in [servo] config)
variable_servo_delay:          250    # This is a delay to wait the servo to reach the requested position, be carefull with high values

#Dock move, final toolhead movement to release the probe on the dock
#it's a relative move
Variable_dockmove_x:             40
Variable_dockmove_y:             10
Variable_dockmove_z:             20

#Attach move. final toolhead movement to attach the probe on the mount
#it's a relative move
Variable_attachmove_x:           0
Variable_attachmove_y:          40
Variable_attachmove_z:           0

#Umbilical to help untangle the umbilical in difficult situations
variable_umbilical:          False    # should we untangle the umbilical
variable_umbilical_x:           15    # X umbilical position
variable_umbilical_y:           15    # Y umbilical position

# location to park the toolhead
variable_park_toolhead:      False    # Enable toolhead parking
variable_parkposition_x:       125
variable_parkposition_y:       125
variable_parkposition_z:        30    # -128 excludes Z - Park only on X-Y

variable_version:                1    # Helps users to update the necessary variables, do not update if the variables above are not updated

#Below this remark, you normally do not need to configure
#Attach move2
Variable_attachmove2_x:          0    # intermediate toolhead movement to attach
Variable_attachmove2_y:          0    # the probe on the dock
Variable_attachmove2_z:          0    # (can be negative)

variable_home_backoff_x:        10    # how many mm to move away from the X endstop after homing X
                                      # this is useful for the voron v0 to enable the toolhead to move out of the way to allow an unstricted Y homing
variable_home_backoff_y:        10    # how many mm to move away from the Y endstop after homing Y

variable_override_homing:       ''    # configures what axis to home first
                                      #  '' = default klicky behavior (tries to avoid the hitting the dock)
                                      # 'X' = forces X to home first
                                      # 'Y' = forces Y to home first

variable_dock_on_zhome:       True    # docks the probe on Z Homing if not necessary (avoids hitting the bed on some printers       

variable_bypass_probe_docking:        False

variable_after_homing_safe_x:  155
variable_after_homing_safe_y:  155

# Do not modify below
gcode:
    {% set Mx = printer['configfile'].config["stepper_x"]["position_max"]|float %}
    {% set My = printer['configfile'].config["stepper_y"]["position_max"]|float %}
    {% set Ox = printer['configfile'].config["probe"]["x_offset"]|float %}
    {% set Oy = printer['configfile'].config["probe"]["y_offset"]|float %}
    {% set Oz = printer['configfile'].config["probe"]["z_offset"]|float %}

    # If x, y coordinates are set for z endstop, assign them
    {% if z_endstop_x != 0 or z_endstop_y != 0 %}
        SET_GCODE_VARIABLE MACRO=_Probe_Variables VARIABLE=z_endstop_x VALUE={ z_endstop_x }
        SET_GCODE_VARIABLE MACRO=_Probe_Variables VARIABLE=z_endstop_y VALUE={ z_endstop_y }

    # if no x, y coordinates for z endstop, assume probe is endstop and move toolhead to center of bed
    {% else %}
        SET_GCODE_VARIABLE MACRO=_Probe_Variables VARIABLE=z_endstop_x VALUE={ (Mx * 0.5) - Ox }
        SET_GCODE_VARIABLE MACRO=_Probe_Variables VARIABLE=z_endstop_y VALUE={ (My * 0.5) - Oy }
    {% endif %}
[z_calibration]
nozzle_xy_position: 254, 313
#   A X, Y coordinate (e.g. 100,100) of the nozzle, clicking on the z endstop.
switch_xy_position: 225, 313
#   A X, Y coordinate (e.g. 100,100) of the probe's switch body, clicking on
#   the z endstop.
switch_xy_offsets: -29, 0
#   Instead of an absolute position (switch_xy_position), this configuration
#   adds an X, Y offset (e.g. -6,-18) to the nozzle position.
bed_xy_position: 155, 155
#   A X, Y coordinate (e.g. 100,100) where the print surface (e.g. the center
#   point) is probed. These coordinates are adjusted by the
#   probe's X and Y offsets. The default is the zero_reference_position which
#   replaces the deprecated relative_reference_index
#   of the configured bed_mesh, if configured. It's possible to change the zero
#   reference position at runtime or use the GCode argument BED_POSITION of CALIBRATE_Z.
#wiggle_xy_offsets: 0,0
#   After probing the nozzle and retracting, move x some distance away and
#   back. Useful to prevent the z endstop pin sticking to the nozzle and
#   being pulled out of the assembly. Can be negative. Defaults to zero to
#   disable it. Can be combined in x and y to move diagonally. Be careful
#   to not move your nozzle out of range!
switch_offset: 0.45
#   The trigger point offset of the used mag-probe switch.
#   A larger value will position the nozzle closer to the bed.
#   This must be determined manually. More on this later
#   in this section..
offset_margins: -2.0,2.0
#   The minimum and maximum margins allowed for the calculated offset.
#   If the offset is outside these values, it will stop!
#   The margin can be defined as "min,max" e.g. "-0.5,1.5" or by just one
#   value e.g. "1.0" which translates to "-1.0,1.0" (which is also the default).
#max_deviation: DEPRECATED - please use offset_margins instead!
#   The maximum allowed deviation of the calculated offset.
#   If the offset exceeds this value, it will stop!
#   The default is 1.0 mm.
#samples: default from "probe:samples" section
#   The number of times to probe each point. The probed z-values
#   will be averaged. The default is from the probe's configuration.
#samples_tolerance: default from "probe:samples_tolerance" section
#   The maximum Z distance (in mm) that a sample may differ from other
#   samples. The default is from the probe's configuration.
#samples_tolerance_retries: default from "probe:samples_tolerance_retries" section
#   The number of times to retry if a sample is found that exceeds
#   samples_tolerance. The default is from the probe's configuration.
#samples_result: default from "probe:samples_result" section
#   The calculation method when sampling more than once - either
#   "median" or "average". The default is from the probe's configuration.
#safe_z_height: default is 2 * z_offset from the "probe:z_offset" section
#   The absolute z position in mm to move to before moving to the next
#   position. The default is two times the z_offset from the probe's
#   configuration. The minimum safe z height is 3mm.
#clearance: DEPRECATED - please use safe_z_height instead!
#   The distance in mm to move up before moving to the next
#   position. The default is two times the z_offset from the probe's
#   configuration.
#position_min: default from "stepper_z:position_min" section.
#   Minimum valid distance (in mm) used for probing move. The
#   default is from the Z rail configuration.
#speed: 50
#   The moving speed in X and Y. The default is 50 mm/s.
#lift_speed: default from "probe:lift_speed" section
#   Speed (in mm/s) of the Z axis when lifting the probe between
#   samples and clearance moves. The default is from the probe's
#   configuration.
#probing_speed: default from "stepper_z:homing_speed" section.
#   The fast probing speed (in mm/s) used, when probing_first_fast
#   is enabled. The default is from the Z rail configuration.
#probing_second_speed: default from "stepper_z:second_homing_speed" section.
#   The slower speed (in mm/s) for probing the recorded samples.
#   The default is second_homing_speed of the Z rail configuration.
#probing_retract_dist: default from "stepper_z:homing_retract_dist" section.
#   Distance to retract (in mm) before probing the next sample.
#   The default is homing_retract_dist from the Z rail configuration.
#   Caution: if sensorless homing is used on the Z axis with
#   homing_retract_dist set to zero, this must be set to a value greater zero.
#probing_first_fast: false
#   If true, the first probing will be faster by the probing speed.
#   This is to get down faster and not record the result as a
#   probing sample. The default is false.
start_gcode: Attach_Probe
#   A list of G-Code commands to run before each calibration command.
#   See docs/Command_Templates.md for the G-Code format. This can be used to
#   attach the probe.
before_switch_gcode: Attach_Probe
#   A list of G-Code commands to run before to each probing on the
#   mag-probe. See docs/Command_Templates.md for the G-Code format. This can
#   be used to attach the probe after probing on the nozzle and before probing
#   on the mag-probe.
end_gcode: Dock_Probe
#   A list of G-Code commands to run after each calibration command.
#   See docs/Command_Templates.md for the G-Code format. This can be used to
#   detach the probe afterwards.

[gcode_macro CALIBRATE_Z]
rename_existing: BASE_CALIBRATE_Z
gcode:
    {% set bed_position = params.BED_POSITION|default('None') %}
    G28                         # can also be a conditional homing macro
    M117 Z-Calibration..
    Attach_Probe                # a macro for fetching the probe first
    {% if bed_position != 'None' %}
      BASE_CALIBRATE_Z BED_POSITION={bed_position}
    {% else %}
      BASE_CALIBRATE_Z
    {% endif %}
    Dock_Probe                # and parking it afterwards (or DOCK_PROBE in klicky macros)
    M117

After I ran the G28 command, the nozzle stayed at the z endstop switch xy position. Is not safe before print.

so i add this gcode in [homing_override] of klicky


[homing_override]
...
        _Home_Z_

        #!!!!!!!!!!!!!!!!!!! insert this code after _Home_Z_
        # move xy to safe position after homing
        {% if after_homing_safe_x != 0 %}
           G0 X{after_homing_safe_x} F{travel_feedrate}
        {% endif %}
        {% if after_homing_safe_y != 0 %}
           G0 Y{after_homing_safe_y} F{travel_feedrate}
        {% endif %}

Am I doing duplicate work? Is there any other place where similar functionality can be configured?

Thanks!

TitusLabs commented 1 year ago

With safe_z_home you have only the option of the homing retract setting for Z direction. If you need a different position in X and Y, you can use the option move_to_previous=true - but, this depends on your previous position and might be unpredictable. So, the only and correct place to do it is the homing_override :+1: Only there you can make sure that it is always called afterwards.

rushairer commented 1 year ago

thanks @TitusLabs