Skip to content

Commit

Permalink
Improve Power-Loss Recovery (MarlinFirmware#19540)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored Sep 28, 2020
1 parent 6d4c82f commit 9142f54
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
28 changes: 24 additions & 4 deletions Marlin/src/feature/powerloss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ void PrintJobRecovery::check() {
if (card.isMounted()) {
load();
if (!valid()) return cancel();
queue.inject_P(PSTR("M1000 S"));
TERN_(DWIN_CREALITY_LCD, dwin_flag = true);
queue.inject_P(PSTR("M1000S"));
}
}

Expand Down Expand Up @@ -227,6 +226,10 @@ void PrintJobRecovery::save(const bool force/*=false*/, const float zraise/*=0*/
// Elapsed print job time
info.print_job_elapsed = print_job_timer.duration();

// Misc. Marlin flags
info.flag.dryrun = !!(marlin_debug_flags & MARLIN_DEBUG_DRYRUN);
info.flag.allow_cold_extrusion = TERN0(PREVENT_COLD_EXTRUSION, thermalManager.allow_cold_extrude);

write();
}
}
Expand Down Expand Up @@ -326,6 +329,12 @@ void PrintJobRecovery::resume() {

const uint32_t resume_sdpos = info.sdpos; // Get here before the stepper ISR overwrites it

// Apply the dry-run flag if enabled
if (info.flag.dryrun) marlin_debug_flags |= MARLIN_DEBUG_DRYRUN;

// Restore cold extrusion permission
TERN_(PREVENT_COLD_EXTRUSION, thermalManager.allow_cold_extrude = info.flag.allow_cold_extrusion);

#if HAS_LEVELING
// Make sure leveling is off before any G92 and G28
gcode.process_subcommands_now_P(PSTR("M420 S0 Z0"));
Expand All @@ -337,7 +346,7 @@ void PrintJobRecovery::resume() {
// If Z homing goes to max, just reset E and home all
gcode.process_subcommands_now_P(PSTR(
"G92.9 E0\n"
"G28R0" TERN_(MARLIN_DEV_MODE, "S")
"G28R0"
));

#else // "G92.9 E0 ..."
Expand All @@ -358,7 +367,6 @@ void PrintJobRecovery::resume() {

gcode.process_subcommands_now_P(PSTR(
"G28R0" // No raise during G28
TERN_(MARLIN_DEV_MODE, "S") // Simulated Homing
TERN_(IS_CARTESIAN, "XY") // Don't home Z on Cartesian
));

Expand Down Expand Up @@ -498,13 +506,23 @@ void PrintJobRecovery::resume() {
LOOP_XYZ(i) update_workspace_offset((AxisEnum)i);
#endif

#if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
const uint8_t old_flags = marlin_debug_flags;
marlin_debug_flags |= MARLIN_DEBUG_ECHO;
#endif

// Continue to apply PLR when a file is resumed!
enable(true);

// Resume the SD file from the last position
char *fn = info.sd_filename;
extern const char M23_STR[];
sprintf_P(cmd, M23_STR, fn);
gcode.process_subcommands_now(cmd);
sprintf_P(cmd, PSTR("M24 S%ld T%ld"), resume_sdpos, info.print_job_elapsed);
gcode.process_subcommands_now(cmd);

TERN_(DEBUG_POWER_LOSS_RECOVERY, marlin_debug_flags = old_flags);
}

#if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
Expand Down Expand Up @@ -584,6 +602,8 @@ void PrintJobRecovery::resume() {
DEBUG_ECHOLNPAIR("sd_filename: ", info.sd_filename);
DEBUG_ECHOLNPAIR("sdpos: ", info.sdpos);
DEBUG_ECHOLNPAIR("print_job_elapsed: ", info.print_job_elapsed);
DEBUG_ECHOLNPAIR("dryrun: ", int(info.flag.dryrun));
DEBUG_ECHOLNPAIR("allow_cold_extrusion: ", int(info.flag.allow_cold_extrusion));
}
else
DEBUG_ECHOLNPGM("INVALID DATA");
Expand Down
11 changes: 10 additions & 1 deletion Marlin/src/feature/powerloss.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ typedef struct {
// Job elapsed time
millis_t print_job_elapsed;

// Misc. Marlin flags
struct {
bool dryrun:1; // M111 S8
bool allow_cold_extrusion:1; // M302 P1
} flag;

uint8_t valid_foot;

bool valid() { return valid_head && valid_head == valid_foot; }
Expand Down Expand Up @@ -173,7 +179,10 @@ class PrintJobRecovery {
}
#endif

static inline bool valid() { return info.valid(); }
// The referenced file exists
static inline bool interrupted_file_exists() { return card.fileExists(info.sd_filename); }

static inline bool valid() { return info.valid() && interrupted_file_exists(); }

#if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
static void debug(PGM_P const prefix);
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/gcode/feature/powerloss/M1000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ void GcodeSuite::M1000() {
if (parser.seen('S')) {
#if HAS_LCD_MENU
ui.goto_screen(menu_job_recovery);
#elif ENABLED(DWIN_CREALITY_LCD)
recovery.dwin_flag = true;
#elif ENABLED(EXTENSIBLE_UI)
ExtUI::onPowerLossResume();
#else
Expand Down
1 change: 1 addition & 0 deletions Marlin/src/gcode/feature/powerloss/M413.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void GcodeSuite::M413() {
if (parser.seen("RL")) recovery.load();
if (parser.seen('W')) recovery.save(true);
if (parser.seen('P')) recovery.purge();
if (parser.seen('D')) recovery.debug(PSTR("M413"));
#if PIN_EXISTS(POWER_LOSS)
if (parser.seen('O')) recovery._outage();
#endif
Expand Down

0 comments on commit 9142f54

Please sign in to comment.