Save/restore gcode state in updated pause/resume macros

I’ve had these macros in my printer.cfg since I started playing with klipper and I don’t even remember which config example I copied them from.

[gcode_macro PAUSE]
rename_existing: BASE_PAUSE
gcode:
  {% set E = params.E|default(1.7)|float %}
  {% set x_park = printer.toolhead.axis_maximum.x|float - 5.0 %}
  {% set y_park = printer.toolhead.axis_maximum.y|float - 5.0 %}
  {% set max_z = printer.toolhead.axis_maximum.z|float %}
  {% set act_z = printer.toolhead.position.z|float %}
  {% if act_z < (max_z - 2.0) %}
      {% set z_safe = 2.0 %}
  {% else %}
      {% set z_safe = max_z - act_z %}
  {% endif %}
  SAVE_GCODE_STATE NAME=PAUSE_state
  BASE_PAUSE
  G91
  G1 E-{E} F2100
  G1 Z{z_safe} F900
  G90
  G0 X{x_park} Y{y_park} F6000
  G91

[gcode_macro RESUME]
rename_existing: BASE_RESUME
gcode:
    {% set E = params.E|default(1)|float %} # edit to your preferred retract length
    G91
    G1 E{E} F2100
    G90
    RESTORE_GCODE_STATE NAME=PAUSE_state MOVE=1
    BASE_RESUME

While testing a filament runout switch, I started re-thinking of the whole M600, load/unload, pause/resume topic and revisited the above macros.

There is one thing I would like to clarify. If the “base” pause/resume commands already save & restore the current position , why do the new pause/resume macros save & restore a second time?

1 Like

Just to confirm my suspicion, here are the snippets from ~/klipper/klippy/extras/pause_resume.py that show that SAVE_GCODE_STATE and RESTORE_GCODE_STATE are already present in the base gcode commands.

    def cmd_PAUSE(self, gcmd):
        if self.is_paused:
            gcmd.respond_info("Print already paused")
            return
        self.send_pause_command()
        self.gcode.run_script_from_command("SAVE_GCODE_STATE NAME=PAUSE_STATE")
        self.is_paused = True
    def cmd_RESUME(self, gcmd):
        if not self.is_paused:
            gcmd.respond_info("Print is not paused, resume aborted")
            return
        velocity = gcmd.get_float('VELOCITY', self.recover_velocity)
        self.gcode.run_script_from_command(
            "RESTORE_GCODE_STATE NAME=PAUSE_STATE MOVE=1 MOVE_SPEED=%.4f"
            % (velocity))
        self.send_resume_command()
        self.is_paused = False
2 Likes