2018-04-22 00:41:26 +00:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 14:00:57 +00:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2018-04-22 00:41:26 +00:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 04:57:50 +00:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2018-04-22 00:41:26 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-07-23 03:20:14 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-04-22 00:41:26 +00:00
|
|
|
*
|
|
|
|
*/
|
2018-10-28 03:48:28 +00:00
|
|
|
#pragma once
|
2018-04-22 00:41:26 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-08 23:21:44 +00:00
|
|
|
* feature/powerloss.h - Resume an SD print after power-loss
|
2018-04-22 00:41:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../sd/cardreader.h"
|
2020-07-15 06:14:03 +00:00
|
|
|
#include "../gcode/gcode.h"
|
|
|
|
|
2019-08-20 07:40:44 +00:00
|
|
|
#include "../inc/MarlinConfig.h"
|
2018-04-22 00:41:26 +00:00
|
|
|
|
2020-11-27 03:18:40 +00:00
|
|
|
#if ENABLED(GCODE_REPEAT_MARKERS)
|
|
|
|
#include "../feature/repeat.h"
|
|
|
|
#endif
|
|
|
|
|
2019-02-10 10:54:23 +00:00
|
|
|
#if ENABLED(MIXING_EXTRUDER)
|
|
|
|
#include "../feature/mixing.h"
|
|
|
|
#endif
|
|
|
|
|
2019-08-20 07:40:44 +00:00
|
|
|
#if !defined(POWER_LOSS_STATE) && PIN_EXISTS(POWER_LOSS)
|
|
|
|
#define POWER_LOSS_STATE HIGH
|
|
|
|
#endif
|
|
|
|
|
2021-05-08 03:36:36 +00:00
|
|
|
#ifndef POWER_LOSS_ZRAISE
|
|
|
|
#define POWER_LOSS_ZRAISE 2
|
|
|
|
#endif
|
|
|
|
|
2018-04-22 00:41:26 +00:00
|
|
|
//#define DEBUG_POWER_LOSS_RECOVERY
|
2019-05-08 01:38:40 +00:00
|
|
|
//#define SAVE_EACH_CMD_MODE
|
|
|
|
//#define SAVE_INFO_INTERVAL_MS 0
|
2018-04-22 00:41:26 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t valid_head;
|
|
|
|
|
|
|
|
// Machine state
|
2019-09-29 09:25:39 +00:00
|
|
|
xyze_pos_t current_position;
|
2020-11-21 03:25:51 +00:00
|
|
|
uint16_t feedrate;
|
2021-05-08 03:36:36 +00:00
|
|
|
|
2020-07-15 06:14:03 +00:00
|
|
|
float zraise;
|
2018-11-17 02:47:07 +00:00
|
|
|
|
2020-11-27 03:18:40 +00:00
|
|
|
// Repeat information
|
2021-03-24 10:40:28 +00:00
|
|
|
#if ENABLED(GCODE_REPEAT_MARKERS)
|
|
|
|
Repeat stored_repeat;
|
|
|
|
#endif
|
2019-04-15 23:53:39 +00:00
|
|
|
|
2021-07-09 22:09:58 +00:00
|
|
|
#if HAS_HOME_OFFSET
|
2021-03-24 10:40:28 +00:00
|
|
|
xyz_pos_t home_offset;
|
|
|
|
#endif
|
2021-07-09 22:09:58 +00:00
|
|
|
#if HAS_POSITION_SHIFT
|
2021-03-24 10:40:28 +00:00
|
|
|
xyz_pos_t position_shift;
|
|
|
|
#endif
|
2021-07-09 22:09:58 +00:00
|
|
|
#if HAS_MULTI_EXTRUDER
|
2021-03-24 10:40:28 +00:00
|
|
|
uint8_t active_extruder;
|
|
|
|
#endif
|
2018-07-03 04:21:28 +00:00
|
|
|
|
2019-10-31 19:09:37 +00:00
|
|
|
#if DISABLED(NO_VOLUMETRICS)
|
2020-05-04 21:37:20 +00:00
|
|
|
float filament_size[EXTRUDERS];
|
2019-10-31 19:09:37 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-09 22:09:58 +00:00
|
|
|
#if HAS_HOTEND
|
2021-03-24 10:40:28 +00:00
|
|
|
celsius_t target_temperature[HOTENDS];
|
|
|
|
#endif
|
2021-07-09 22:09:58 +00:00
|
|
|
#if HAS_HEATED_BED
|
2021-03-24 10:40:28 +00:00
|
|
|
celsius_t target_temperature_bed;
|
|
|
|
#endif
|
2021-07-09 22:09:58 +00:00
|
|
|
#if HAS_FAN
|
2021-03-24 10:40:28 +00:00
|
|
|
uint8_t fan_speed[FAN_COUNT];
|
|
|
|
#endif
|
2018-07-02 21:42:13 +00:00
|
|
|
|
2021-06-10 07:05:04 +00:00
|
|
|
#if HAS_LEVELING
|
2021-03-24 10:40:28 +00:00
|
|
|
float fade;
|
|
|
|
#endif
|
2018-04-22 00:41:26 +00:00
|
|
|
|
2018-09-17 02:24:15 +00:00
|
|
|
#if ENABLED(FWRETRACT)
|
|
|
|
float retract[EXTRUDERS], retract_hop;
|
|
|
|
#endif
|
|
|
|
|
2019-02-10 10:54:23 +00:00
|
|
|
// Mixing extruder and gradient
|
|
|
|
#if ENABLED(MIXING_EXTRUDER)
|
|
|
|
//uint_fast8_t selected_vtool;
|
|
|
|
//mixer_comp_t color[NR_MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
|
2021-03-24 10:40:28 +00:00
|
|
|
#if ENABLED(GRADIENT_MIX)
|
|
|
|
gradient_t gradient;
|
|
|
|
#endif
|
2019-02-10 10:54:23 +00:00
|
|
|
#endif
|
|
|
|
|
2018-06-30 03:41:02 +00:00
|
|
|
// SD Filename and position
|
2018-11-20 11:36:19 +00:00
|
|
|
char sd_filename[MAXPATHNAMELENGTH];
|
2019-09-10 23:52:41 +00:00
|
|
|
volatile uint32_t sdpos;
|
2018-04-22 00:41:26 +00:00
|
|
|
|
|
|
|
// Job elapsed time
|
|
|
|
millis_t print_job_elapsed;
|
|
|
|
|
2020-11-21 03:25:51 +00:00
|
|
|
// Relative axis modes
|
|
|
|
uint8_t axis_relative;
|
|
|
|
|
2020-09-28 20:52:21 +00:00
|
|
|
// Misc. Marlin flags
|
|
|
|
struct {
|
2021-05-10 12:24:35 +00:00
|
|
|
bool raised:1; // Raised before saved
|
2020-09-28 20:52:21 +00:00
|
|
|
bool dryrun:1; // M111 S8
|
|
|
|
bool allow_cold_extrusion:1; // M302 P1
|
2021-06-10 07:05:04 +00:00
|
|
|
#if HAS_LEVELING
|
2021-05-04 01:55:05 +00:00
|
|
|
bool leveling:1; // M420 S
|
|
|
|
#endif
|
|
|
|
#if DISABLED(NO_VOLUMETRICS)
|
|
|
|
bool volumetric_enabled:1; // M200 S D
|
2021-03-24 10:40:28 +00:00
|
|
|
#endif
|
2020-09-28 20:52:21 +00:00
|
|
|
} flag;
|
|
|
|
|
2018-04-22 00:41:26 +00:00
|
|
|
uint8_t valid_foot;
|
2018-11-17 02:47:07 +00:00
|
|
|
|
2020-06-01 22:02:49 +00:00
|
|
|
bool valid() { return valid_head && valid_head == valid_foot; }
|
|
|
|
|
2018-04-22 00:41:26 +00:00
|
|
|
} job_recovery_info_t;
|
|
|
|
|
2018-11-17 02:47:07 +00:00
|
|
|
class PrintJobRecovery {
|
|
|
|
public:
|
2019-08-02 10:21:44 +00:00
|
|
|
static const char filename[5];
|
2019-08-02 07:22:09 +00:00
|
|
|
|
2018-11-17 02:47:07 +00:00
|
|
|
static SdFile file;
|
|
|
|
static job_recovery_info_t info;
|
2018-04-22 00:41:26 +00:00
|
|
|
|
2019-09-10 23:52:41 +00:00
|
|
|
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
|
|
|
|
|
2021-09-07 07:15:24 +00:00
|
|
|
#if HAS_DWIN_E3V2_BASIC
|
2020-06-16 06:45:27 +00:00
|
|
|
static bool dwin_flag;
|
|
|
|
#endif
|
|
|
|
|
2018-11-17 02:47:07 +00:00
|
|
|
static void init();
|
2019-09-10 23:52:41 +00:00
|
|
|
static void prepare();
|
2018-11-17 02:47:07 +00:00
|
|
|
|
2019-08-20 07:40:44 +00:00
|
|
|
static inline void setup() {
|
|
|
|
#if PIN_EXISTS(POWER_LOSS)
|
2020-11-21 23:56:56 +00:00
|
|
|
#if ENABLED(POWER_LOSS_PULLUP)
|
|
|
|
SET_INPUT_PULLUP(POWER_LOSS_PIN);
|
|
|
|
#elif ENABLED(POWER_LOSS_PULLDOWN)
|
|
|
|
SET_INPUT_PULLDOWN(POWER_LOSS_PIN);
|
2019-08-20 07:40:44 +00:00
|
|
|
#else
|
|
|
|
SET_INPUT(POWER_LOSS_PIN);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:52:41 +00:00
|
|
|
// 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; }
|
|
|
|
|
2018-11-17 02:47:07 +00:00
|
|
|
static bool enabled;
|
|
|
|
static void enable(const bool onoff);
|
|
|
|
static void changed();
|
|
|
|
|
|
|
|
static inline bool exists() { return card.jobRecoverFileExists(); }
|
|
|
|
static inline void open(const bool read) { card.openJobRecoveryFile(read); }
|
|
|
|
static inline void close() { file.close(); }
|
2018-04-22 00:41:26 +00:00
|
|
|
|
2020-01-14 02:52:24 +00:00
|
|
|
static void check();
|
|
|
|
static void resume();
|
2018-11-17 02:47:07 +00:00
|
|
|
static void purge();
|
2020-01-14 02:52:24 +00:00
|
|
|
|
2020-12-07 11:53:15 +00:00
|
|
|
static inline void cancel() { purge(); IF_DISABLED(NO_SD_AUTOSTART, card.autofile_begin()); }
|
2020-01-14 02:52:24 +00:00
|
|
|
|
2018-11-17 02:47:07 +00:00
|
|
|
static void load();
|
2021-05-10 12:24:35 +00:00
|
|
|
static void save(const bool force=ENABLED(SAVE_EACH_CMD_MODE), const float zraise=POWER_LOSS_ZRAISE, const bool raised=false);
|
2018-11-17 02:47:07 +00:00
|
|
|
|
2020-07-15 06:14:03 +00:00
|
|
|
#if PIN_EXISTS(POWER_LOSS)
|
|
|
|
static inline void outage() {
|
2021-05-08 03:37:31 +00:00
|
|
|
static constexpr uint8_t OUTAGE_THRESHOLD = 3;
|
|
|
|
static uint8_t outage_counter = 0;
|
|
|
|
if (enabled && READ(POWER_LOSS_PIN) == POWER_LOSS_STATE) {
|
|
|
|
outage_counter++;
|
|
|
|
if (outage_counter >= OUTAGE_THRESHOLD) _outage();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
outage_counter = 0;
|
2020-07-15 06:14:03 +00:00
|
|
|
}
|
|
|
|
#endif
|
2019-09-10 23:52:41 +00:00
|
|
|
|
2020-09-28 20:52:21 +00:00
|
|
|
// 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(); }
|
2018-11-17 02:47:07 +00:00
|
|
|
|
2020-07-15 06:14:03 +00:00
|
|
|
#if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
|
2021-09-27 18:46:42 +00:00
|
|
|
static void debug(FSTR_P const prefix);
|
2020-07-15 06:14:03 +00:00
|
|
|
#else
|
2021-09-27 18:46:42 +00:00
|
|
|
static inline void debug(FSTR_P const) {}
|
2020-07-15 06:14:03 +00:00
|
|
|
#endif
|
2018-11-17 02:47:07 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static void write();
|
2019-09-10 23:52:41 +00:00
|
|
|
|
2020-07-15 06:14:03 +00:00
|
|
|
#if ENABLED(BACKUP_POWER_SUPPLY)
|
2021-04-01 22:59:57 +00:00
|
|
|
static void retract_and_lift(const_float_t zraise);
|
2020-07-15 06:14:03 +00:00
|
|
|
#endif
|
2019-11-20 23:40:21 +00:00
|
|
|
|
2020-07-15 06:14:03 +00:00
|
|
|
#if PIN_EXISTS(POWER_LOSS)
|
|
|
|
friend class GcodeSuite;
|
|
|
|
static void _outage();
|
|
|
|
#endif
|
2018-11-17 02:47:07 +00:00
|
|
|
};
|
2018-04-22 00:41:26 +00:00
|
|
|
|
2018-11-17 02:47:07 +00:00
|
|
|
extern PrintJobRecovery recovery;
|