From 005f9f0d242b389906973e38995d2e2ec51433e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sat, 15 Jul 2023 17:51:22 +0000 Subject: [PATCH] power panic: Fix an error in saved position planner_abort_hard() calls planner_reset_position() which will set current_position vector to the machine position. We want to save this position when there is no position already saved (i.e. when there is no partial back-up or a saved print in RAM) When a power outage comes, the printer is in the middle of a gcode move. And at the moment a gcode is executed by the planner, the planner will update current_position vector to the final destination vector. This means current_position vector is invalidated during a power outage and so we must check what the actual machine position is instead and save it. This was working correctly before, this commit only fixes the regression in my pull request. Change in memory: Flash: -2 bytes SRAM: 0 bytes --- Firmware/power_panic.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Firmware/power_panic.cpp b/Firmware/power_panic.cpp index 8fe959467..b9f9dad47 100644 --- a/Firmware/power_panic.cpp +++ b/Firmware/power_panic.cpp @@ -73,14 +73,13 @@ void uvlo_() { saved_extruder_temperature = target_temperature[active_extruder]; saved_extruder_relative_mode = axis_relative_modes & E_AXIS_MASK; saved_fan_speed = fanSpeed; - memcpy(saved_pos, current_position, sizeof(saved_pos)); - if (pos_invalid) saved_pos[X_AXIS] = X_COORD_INVALID; } // Stop all heaters before continuing setTargetHotend(0); setTargetBed(0); + // Fetch data not included in a partial back-up if (!sd_print_saved_in_ram) { // Calculate the file position, from which to resume this print. save_print_file_state(); @@ -100,6 +99,14 @@ void uvlo_() { // are in action. planner_abort_hard(); + // When there is no position already saved, then we must grab whatever the current position is. + // This is most likely a position where the printer is in the middle of a G-code move + if (!sd_print_saved_in_ram && !isPartialBackupAvailable) + { + memcpy(saved_pos, current_position, sizeof(saved_pos)); + if (pos_invalid) saved_pos[X_AXIS] = X_COORD_INVALID; + } + // Store the print logical Z position, which we need to recover (a slight error here would be // recovered on the next Gcode instruction, while a physical location error would not) float logical_z = saved_pos[Z_AXIS];