Simple Power Loss Recovery

I am currently working on a more complex approach of Power Loss Recovery, but while working on that, I made this simple power loss recovery script in Python, that can recover any print by the last recorded Z height. It works as follows:

  1. Slicer’s start G-Code ends with ; POWER_PANIC_PRESERVE
  2. Slicer’s layer change G-Code will invoke UPDATE_Z Z= to set the z height
  3. UPDATE_Z will save the variable to variables.cfg
  4. When power is lost, you still have to manually heat up the bed
  5. You can get the last recorded z from variables.cfg
  6. Run the script: python3 /path/to/script.py <Z HEIGHT FROM PREVIOUS STEP> <NAME OF GCODE FILE>
  7. Look in your G-Codes folder, and there should be a G-Code file that starts with RECOVER-SOFT-
  8. Use SET_KINEMATIC_POSITION Z= to set the z height without homing Z
  9. Use the move panel to move Z 10mm up
  10. Run G28 X Y to home X and Y
  11. For now, you will still need to edit your recovered G-Code to remove any homing G-Code from the start G-Code.
  12. Set your velocity limit to something slow e.g. 20mm
  13. Start the recovery G-Code and make sure your print doesn’t fall off the bed.
  14. Increase print speed slowly to avoid knocking the print off the bed.

Config:

[save_variables]
filename: ~/printer_data/config/variables.cfg

[gcode_macro UPDATE_Z]
gcode:
    SAVE_VARIABLE VARIABLE=z VALUE={params.Z | float}

Python Script:

import sys
import pathlib

nums = []
gcodes = pathlib.Path('/home/pi/printer_data/gcodes/')

with open(gcodes / sys.argv[2]) as gfile:
    ulines = []
    gcode = ''
    i = 0
    for line in gfile.readlines():
        ulines.append(line)
        if line.startswith(f';Z:{sys.argv[1]}'):
            nums.append(i)
        i += 1

num = nums[0]
preserve_line = 0
for uline in ulines:
    if 'POWER_PANIC_PRESERVE' in uline:
        break
    preserve_line += 1
if preserve_line >= len(ulines) - 1:
    preserve_line = 200

print(preserve_line, len(ulines), nums, num)
final = '\n'.join(ulines[0:preserve_line]) + '\n'.join(ulines[num:])
with open(gcodes / ('RECOVER-SOFT-'+str(sys.argv[2])), 'w') as file:
    file.write(final)

Add the following to the end of your slicer’s start G-Code (NOT in your PRINT_START macro if you have one)

; POWER_PANIC_PRESERVE

Add the following to your slicer’s after layer change G-Code (this syntax is for PrusaSlicer)

UPDATE_Z Z=[layer_z]

If anyone finds this helpful or has ideas to improve it, please let me know.

GitHub Link: https://github.com/3DCoded/klipper-simple-power-loss-recovery/tree/main

What do you think about this?

1 Like

Hi,

I downloaded script above changed the path to corresponding variables and files but it seems it does not use variables even requires some permision to find these files.

cp /home/lukaso1/printer_data/gcodes/.thumbs/{4}.png /home/lukaso1/printer_data/gcodes/.thumbs/plr.png cp /home/lukaso1/printer_data/gcodes/.thumbs/{4}-32x32.png /home/lukaso1/printer_data/gcodes/.thumbs/plr-32x32.png
cp /home/lukaso1/printer_data/gcodes/.thumbs/${4}-400x400.png /home/lukaso1/printer_data/gcodes/.thumbs/plr-400x400.png

Can you help me? I uploaded PLR.gcode what it creates.

plr.gcode (546.8 KB)

Where did you put the Python script? I put it in a folder in ~. Can you try that to see if it fixes path errors?

I put it in folder /home/printer_data/config/scripts/plr_z.sh

I would try moving it to your home directory. Also, did you download the python script? It should be downloaded as a .py, not a .sh

HI,

there was a little misunderstanding. I downloaded the files from video but now i am trying your link.

I downloaded both .cfg and .py files.
.cfg is in
“home/user/printer_data/config/”

and the .py file is in
“home/user/klipper/klippy/extras”

but i am not sure how to run that .py script. Variabless are correctly saving.

Oh. I was confused about the script accessing Gcode thumbnails. For the script I uploaded:

The config file is in the right place. The python file doesn’t go in the klippy extras folder (I should make that clear in the instructions for the future). The python file goes in your home directory (or a folder in it). In my case, I have it in ~/klipper-simple-powerloss-recovery/powerloss.py.

Okey i am in work till 22:00(CET) but will try at arriving. And how can run that script? Trough Putty or terminal? Or can you send some manual? I would like create simple macro for that.

You run the script through Putty (I use SSH on my Mac). On the Github page, I put some instructions for running the script.

GitHub - 3DCoded/klipper-simple-power-loss-recovery: Simple power loss recovery for Klipper Firmware

1 Like

Hi i am getting this line.

obrázok

okay now some progress

Did you follow the steps? Step 2 at the top of this page is very important. The error you’re facing is what would be expected if step 2 wasn’t followed.

Yes and wasn’t successfull. i made my own makro between the layers and then just edit gcode a have different start makro for continuing. Just now I need find out how to make acceleration lower for start and the higher for next layers.

Dňa po 15. 7. 2024, 14:15 3dcoded via Klipper <notifications@klipper.discoursemail.com> napísal(a):

I am also not able to recover a file after shutdown

Can you explain what happened when you tried to recover the file? The most common source of failure in a power loss is the bed cooling down, releasing the print.

No
After Shutdown when we start the machine.
This file is not available in G codes : 1. Look in your G-Codes folder, and there should be a G-Code file that starts with RECOVER-SOFT-
Only last file with sign showing interruption is there.

Did you alter your start GCode properly? The ; POWER_PANIC_PRESERVE line should be after your heating commands, after G28 XY, and before G28 Z. It can’t be in your Klipper PRINT_START. It has to be in your slicer gcode.