protoloft / klipper_z_calibration

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

Fail gracefully #58

Closed NyxCode closed 2 years ago

NyxCode commented 2 years ago

I think it'd be great if the z calibration could be retried if the resulting z offset is outside of the configured max_deviation. For me, this happens once in a while when the nozzle was not properly cleaned by my wipe procedure. Currently, I have to manually restart the print, waiting for QGL/bed mesh/heatup etc.
Ideally, when z calibration fails, i'd like to re-do my wipe procedure, followed by a new z calibration.
This is currently not possible, since z-calibration aborts the print in that case.

Could the macro somehow indicate that it failed, other than canceling the print?

NyxCode commented 2 years ago

I tried implementing this myself, but getting the z offset immediately after running CALIBRATE_Z always gives me 0.0.
I tried printer.gcode_move.homing_origin.z and printer['z_calibration'].last_z_offset, both just return 0.0

NyxCode commented 2 years ago

Alright, finally got my solution working. It cleans the nozzle and does the z calibration - If the resulting offset is outside of a configured maximum deviation, it tries again.

[gcode_macro WIPE_AND_CALIBRATE_Z]
gcode:
  {% set max_deviation = 1.5 %}
  {% set try = params.TRY|default(1)|int %}
  {% set offset = printer['z_calibration'].last_z_offset %}
  {% if try == 1 or offset|abs > max_deviation %}
    M118 Calibrating Z (try {try})..
    CLEAN_NOZZLE
    CALIBRATE_Z
  {% endif %}

[gcode_macro ENSURE_Z_OFFSET_OK]
gcode:
  {% set offset = printer['z_calibration'].last_z_offset %}
  {% if offset|abs > 1.5 %}
    RESPOND TYPE=error MSG="Z offset doesn't seem right"
    CANCEL_PRINT
  {% endif %}

Then, in my PRINT_START macro, I added

{% for try in range(1, 4) %}
  WIPE_AND_CALIBRATE_Z TRY={try}
{% endfor %}
ENSURE_Z_OFFSET_OK

The macros use M118 and RESPOND to print something to the console. If you want to try these macros, you'll have to either enable [respond] in your config or remove these two lines.