Skip to content

bugfix/z offset#204

Merged
HugoCLSC merged 8 commits intodevfrom
bugfix/z-offset
Apr 2, 2026
Merged

bugfix/z offset#204
HugoCLSC merged 8 commits intodevfrom
bugfix/z-offset

Conversation

@gmmcosta15
Copy link
Copy Markdown
Collaborator

@gmmcosta15 gmmcosta15 commented Apr 2, 2026

Description

Select the type:

  • Feature
  • Bug fix
  • Code refactor
  • Documentation

Fixes the Z-offset babystep save flow in PrintTab and refactors BabystepPage. Babystep changes made during a print were silently not persisting across Klipper restarts.

  • Changed hardcoded Z_OFFSET_APPLY_PROBE → dynamic command resolved at runtime from stepper_z.endstop_pin config (Z_OFFSET_APPLY_ENDSTOP for physical endstop,Z_OFFSET_APPLY_PROBE for virtual endstop).
  • Added _on_stepper_z_config callback queried via printer.on_subscribe_config on each Klipper ready event.
  • Added _pending_save_offset captured at dialog-open time; re-applied via SET_GCODE_OFFSET before the apply command to guard against END_PRINT race condition.
  • Removed erroneous pre-dialog Z_OFFSET_APPLY_PROBE emission in save_config().
  • Replaced permanent accepted.connect in init with per-call connect/disconnect (guarded try/except) to prevent duplicate save callbacks.
  • Fixed bare .disconnect() calls (crash-prone) → scoped .accepted.disconnect() with try/except in _on_delete_file_confirmed and delete_file.
  • Added len(value) > 2 bounds check in activate_save_button.
  • Moved _z_offset, _active_z_offset,_finish_print_handled from class-level defaults to init; added _pending_save_offset and_z_apply_command.
  • BabystepPage: live offset display updates immediately on button press without waiting for Klipper's homing_origin diff; synced back to confirmed value on homing_origin update.
  • BabystepPage: hardened handle_z_offset_change with sender null/type guards and try/except on float parse.
  • BabystepPage: table-driven button creation (_OFFSET_STEPS), extracted _create_offset_button helper, consolidated toggled connections into helper.

Motivation

Two bugs made the Z-offset save silently fail:

  1. Wrong Klipper command. Z_OFFSET_APPLY_PROBE adjusts the probe's calibration z_offset — used for bed leveling accuracy. For a printer homing Z with a physical endstop, the babystep offset must be persisted into stepper_z.position_endstop, which is what Z_OFFSET_APPLY_ENDSTOP does. SAVE_CONFIG was restarting Klipper but writing to the wrong config key, so the home position was unchanged on the next boot. The correct command is now resolved dynamically at runtime by reading stepper_z.endstop_pin from the printer config, matching Mainsail's behaviour for both physical and virtual endstop setups.

  2. Race condition. The printer's END_PRINT macro calls SET_GCODE_OFFSET Z=0, zeroing homing_origin[2] before the user confirms the dialog. Z_OFFSET_APPLY_ENDSTOP would then receive 0, Klipper would raise "Nothing to do", and SAVE_CONFIG would have nothing staged — completing silently with no effect. Capturing _pending_save_offset at dialog-open time and re-applying it with SET_GCODE_OFFSET Z={pending} MOVE=0 immediately before the apply command eliminates this window.

@gmmcosta15 gmmcosta15 marked this pull request as ready for review April 2, 2026 11:22
@gmmcosta15 gmmcosta15 requested a review from HugoCLSC April 2, 2026 11:22
@gmmcosta15 gmmcosta15 self-assigned this Apr 2, 2026
@HugoCLSC HugoCLSC merged commit ef8d728 into dev Apr 2, 2026
8 checks passed
@HugoCLSC HugoCLSC deleted the bugfix/z-offset branch April 2, 2026 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants