Improve Power-loss Recovery (#15135)

This commit is contained in:
Scott Lahteine
2019-09-10 18:52:41 -05:00
committed by GitHub
parent 75927e17dd
commit c590e8ac05
9 changed files with 87 additions and 32 deletions

View File

@ -36,6 +36,9 @@ bool PrintJobRecovery::enabled; // Initialized by settings.load()
SdFile PrintJobRecovery::file;
job_recovery_info_t PrintJobRecovery::info;
const char PrintJobRecovery::filename[5] = "/PLR";
uint8_t PrintJobRecovery::queue_index_r;
uint32_t PrintJobRecovery::cmd_sdpos, // = 0
PrintJobRecovery::sdpos[BUFSIZE];
#include "../sd/cardreader.h"
#include "../lcd/ultralcd.h"
@ -124,6 +127,14 @@ void PrintJobRecovery::load() {
debug(PSTR("Load"));
}
/**
* Set info fields that won't change
*/
void PrintJobRecovery::prepare() {
card.getAbsFilename(info.sd_filename); // SD filename
cmd_sdpos = 0;
}
/**
* Save the current machine state to the power-loss recovery file
*/
@ -141,9 +152,6 @@ void PrintJobRecovery::save(const bool force/*=false*/, const bool save_queue/*=
// Did Z change since the last call?
if (force
#if DISABLED(SAVE_EACH_CMD_MODE) // Always save state when enabled
#if PIN_EXISTS(POWER_LOSS) // Save if power loss pin is triggered
|| READ(POWER_LOSS_PIN) == POWER_LOSS_STATE
#endif
#if SAVE_INFO_INTERVAL_MS > 0 // Save if interval is elapsed
|| ELAPSED(ms, next_save_ms)
#endif
@ -211,27 +219,20 @@ void PrintJobRecovery::save(const bool force/*=false*/, const bool save_queue/*=
info.relative_mode = relative_mode;
info.relative_modes_e = gcode.axis_relative_modes[E_AXIS];
// Commands in the queue
info.queue_length = save_queue ? queue.length : 0;
info.queue_index_r = queue.index_r;
COPY(info.queue_buffer, queue.command_buffer);
// Elapsed print job time
info.print_job_elapsed = print_job_timer.duration();
// SD file position
card.getAbsFilename(info.sd_filename);
info.sdpos = card.getIndex();
write();
// KILL now if the power-loss pin was triggered
#if PIN_EXISTS(POWER_LOSS)
if (READ(POWER_LOSS_PIN) == POWER_LOSS_STATE) kill(PSTR(MSG_OUTAGE_RECOVERY));
#endif
}
}
#if PIN_EXISTS(POWER_LOSS)
void PrintJobRecovery::_outage() {
save(true);
kill(PSTR(MSG_OUTAGE_RECOVERY));
}
#endif
/**
* Save the recovery info the recovery file
*/
@ -253,6 +254,8 @@ void PrintJobRecovery::resume() {
#define RECOVERY_ZRAISE 2
const uint32_t resume_sdpos = info.sdpos; // Get here before the stepper ISR overwrites it
#if HAS_LEVELING
// Make sure leveling is off before any G92 and G28
gcode.process_subcommands_now_P(PSTR("M420 S0 Z0"));
@ -405,16 +408,11 @@ void PrintJobRecovery::resume() {
}
#endif
// Process commands from the old pending queue
uint8_t c = info.queue_length, r = info.queue_index_r;
for (; c--; r = (r + 1) % BUFSIZE)
gcode.process_subcommands_now(info.queue_buffer[r]);
// Resume the SD file from the last position
char *fn = info.sd_filename;
sprintf_P(cmd, PSTR("M23 %s"), fn);
gcode.process_subcommands_now(cmd);
sprintf_P(cmd, PSTR("M24 S%ld T%ld"), info.sdpos, info.print_job_elapsed);
sprintf_P(cmd, PSTR("M24 S%ld T%ld"), resume_sdpos, info.print_job_elapsed);
gcode.process_subcommands_now(cmd);
}
@ -490,9 +488,6 @@ void PrintJobRecovery::resume() {
DEBUG_EOL();
DEBUG_ECHOLNPAIR("retract_hop: ", info.retract_hop);
#endif
DEBUG_ECHOLNPAIR("queue_index_r: ", int(info.queue_index_r));
DEBUG_ECHOLNPAIR("queue_length: ", int(info.queue_length));
for (uint8_t i = 0; i < info.queue_length; i++) DEBUG_ECHOLNPAIR("> ", info.queue_buffer[i]);
DEBUG_ECHOLNPAIR("sd_filename: ", info.sd_filename);
DEBUG_ECHOLNPAIR("sdpos: ", info.sdpos);
DEBUG_ECHOLNPAIR("print_job_elapsed: ", info.print_job_elapsed);

View File

@ -92,13 +92,9 @@ typedef struct {
// Relative mode
bool relative_mode, relative_modes_e;
// Command queue
uint8_t queue_length, queue_index_r;
char queue_buffer[BUFSIZE][MAX_CMD_SIZE];
// SD Filename and position
char sd_filename[MAXPATHNAMELENGTH];
uint32_t sdpos;
volatile uint32_t sdpos;
// Job elapsed time
millis_t print_job_elapsed;
@ -114,7 +110,12 @@ class PrintJobRecovery {
static SdFile file;
static job_recovery_info_t info;
static uint8_t queue_index_r; //!< Queue index of the active command
static uint32_t cmd_sdpos, //!< SD position of the next command
sdpos[BUFSIZE]; //!< SD positions of queued commands
static void init();
static void prepare();
static inline void setup() {
#if PIN_EXISTS(POWER_LOSS)
@ -130,6 +131,10 @@ class PrintJobRecovery {
#endif
}
// Track each command's file offsets
static inline uint32_t command_sdpos() { return sdpos[queue_index_r]; }
static inline void commit_sdpos(const uint8_t index_w) { sdpos[index_w] = cmd_sdpos; }
static bool enabled;
static void enable(const bool onoff);
static void changed();
@ -152,6 +157,13 @@ class PrintJobRecovery {
, const bool save_queue=true
);
#if PIN_EXISTS(POWER_LOSS)
static inline void outage() {
if (enabled && IS_SD_PRINTING() && READ(POWER_LOSS_PIN) == POWER_LOSS_STATE)
_outage();
}
#endif
static inline bool valid() { return info.valid_head && info.valid_head == info.valid_foot; }
#if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
@ -162,6 +174,10 @@ class PrintJobRecovery {
private:
static void write();
#if PIN_EXISTS(POWER_LOSS)
static void _outage();
#endif
};
extern PrintJobRecovery recovery;