jlas1 / Klicky-Probe

Microswitch probe with magnetic attachement, primarily aimed at CoreXY 3d printers
GNU General Public License v3.0
1.22k stars 279 forks source link

Dock if probe is already connected before homing z #231

Open dderg opened 9 months ago

dderg commented 9 months ago

Problem:

If your probe signal wire gets disconnected, it will assume the probe is installed (in case of using klicky-pcb at least) and will try to home z without the probe ramming the nozzle into the bed.

Solution: If before homing the probe detected as attached, dock it and attach it again. This way docking will fail and the nozzle will not hit the bed.

dderg commented 9 months ago

I modified the macros as following, but I'm not sure if it's breaking some scenario that I'm not using.

######################
# Attach Probe Routine
[gcode_macro Attach_Probe]
description: Attaches Klicky Probe
gcode:
    # 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) %}
    {% set dockmove_y = printer["gcode_macro _User_Variables"].dockmove_y|default(0) %}
    {% set dockmove_z = printer["gcode_macro _User_Variables"].dockmove_z|default(0) %}
    {% set docklocation_x = printer["gcode_macro _User_Variables"].docklocation_x %}
    {% set docklocation_y = printer["gcode_macro _User_Variables"].docklocation_y %}
    {% set docklocation_z = printer["gcode_macro _User_Variables"].docklocation_z %}
    {% set attachmove_x = printer["gcode_macro _User_Variables"].attachmove_x|default(0) %}
    {% set attachmove_y = printer["gcode_macro _User_Variables"].attachmove_y|default(0) %}
    {% set attachmove_z = printer["gcode_macro _User_Variables"].attachmove_z|default(0) %}
    {% set attachmove2_x = printer["gcode_macro _User_Variables"].attachmove2_x|default(0) %}
    {% set attachmove2_y = printer["gcode_macro _User_Variables"].attachmove2_y|default(0) %}
    {% set attachmove2_z = printer["gcode_macro _User_Variables"].attachmove2_z|default(0) %}
    # 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"

        {% else %}
          {% if probe_attached %}
              {% if verbose %}
                  { action_respond_info("Probe already attached!") }
              {% endif %}

              # Probe attached, do nothing
              _KlickyDebug msg="Attach_Probe probe already attached, doing nothing"
              _CheckProbe action=query
              Dock_Probe
          {% endif %}
          # If probe not attached and locked
          {% if 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|int - attachmove_x|int - attachmove2_x|int} Y{docklocation_y|int - attachmove_y|int - attachmove2_y} F{travel_feedrate}"
              G0 X{docklocation_x|int - attachmove_x|int - attachmove2_x|int} Y{docklocation_y|int - attachmove_y|int - attachmove2_y} F{travel_feedrate}
              {% if docklocation_z != -128 %}
                  _KlickyDebug msg="Attach_Probe moving near the dock with G0 Z{docklocation_z|int - attachmove_z|int - attachmove2_z|int} F{dock_feedrate}"
                  G0 Z{docklocation_z|int - attachmove_z|int - attachmove2_z|int} F{dock_feedrate}
                  _KlickyDebug msg="Attach_Probe moving near the dock with G0 Z{docklocation_z|int - attachmove_z|int} F{dock_feedrate}"
                  G0 Z{docklocation_z|int - attachmove_z|int} F{dock_feedrate}
                {% endif %}
              # if necessary do some actions before moving the toolhead to dock
              _DeployKlickyDock

              # Drop Probe to Probe location
              {% if docklocation_z != -128 %}
                  _KlickyDebug msg="Attach_Probe moving to the dock with G0 Z{docklocation_z} F{dock_feedrate}"
                  G0 Z{docklocation_z} F{dock_feedrate}
              {% endif %}
              _KlickyDebug msg="Attach_Probe moving to the dock with G0 X{docklocation_x|int - attachmove2_x|int} Y{docklocation_y|int - attachmove2_y} F{dock_feedrate}"
              G0 X{docklocation_x|int - attachmove2_x|int} Y{docklocation_y|int - attachmove2_y} F{dock_feedrate}
              _KlickyDebug msg="Attach_Probe moving to the dock with G0 X{docklocation_x} Y{docklocation_y} F{dock_feedrate}"
              G0 X{docklocation_x} Y{docklocation_y} F{dock_feedrate}
              # Probe Attached
              {% if docklocation_z != -128 %}
                  _KlickyDebug msg="Attach_Probe moving from the dock to G0 Z{docklocation_z|int - attachmove_z|int} F{z_drop_feedrate}"
                  G0 Z{docklocation_z|int - attachmove_z|int} F{z_drop_feedrate}
              {% endif %}
              _KlickyDebug msg="Attach_Probe moving from the dock to G0 X{docklocation_x|int - attachmove_x|int} Y{docklocation_y|int - attachmove_y|int} F{release_feedrate}"
              G0 X{docklocation_x|int - attachmove_x|int} Y{docklocation_y|int - attachmove_y|int} F{release_feedrate}
              # if necessary do some actions after attaching the probe
              _RetractKlickyDock
              ## Go to Z safe distance
              {% if ((printer.gcode_move.gcode_position.z < safe_z) or (docklocation_z != -128 and docklocation_z < safe_z ))%}
                _KlickyDebug msg="Attach_Probe moving to a safe Z position: G0 Z{safe_z} F{z_drop_feedrate} from {printer.gcode_move.gcode_position.z}"
                G0 Z{safe_z} F{z_drop_feedrate}
              {% endif %}

              _Park_Toolhead

              _CheckProbe action=attach

              _exit_point function=Attach_Probe_intern move={goback}
              _KLICKY_STATUS_READY

          {% elif probe_lock %}
              {% if verbose %}
                  { action_respond_info("Probe locked!") }
              {% endif %}

              # Probe attached, do nothing
              _KlickyDebug msg="Attach_Probe probe locked not attaching probe"
              _CheckProbe action=query

          {% endif %}
        {% endif %}

        _exit_point function=Attach_Probe
    {% else %}
        _KlickyDebug msg="Attach_Probe probe docking bypassed, doing nothing"
    {% endif %}