Prusa MINI Firmware overview
power_loss_recovery.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4  *
5  * Based on Sprinter and grbl.
6  * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 #pragma once
23 
24 /**
25  * power_loss_recovery.h - Resume an SD print after power-loss
26  */
27 
28 #include "../sd/cardreader.h"
29 #include "../inc/MarlinConfig.h"
30 
31 #if ENABLED(MIXING_EXTRUDER)
32  #include "../feature/mixing.h"
33 #endif
34 
35 #if !defined(POWER_LOSS_STATE) && PIN_EXISTS(POWER_LOSS)
36  #define POWER_LOSS_STATE HIGH
37 #endif
38 
39 //#define DEBUG_POWER_LOSS_RECOVERY
40 //#define SAVE_EACH_CMD_MODE
41 //#define SAVE_INFO_INTERVAL_MS 0
42 
43 typedef struct {
45 
46  // Machine state
48 
49  #if HAS_HOME_OFFSET
51  #endif
52  #if HAS_POSITION_SHIFT
54  #endif
55 
56  uint16_t feedrate;
57 
58  #if EXTRUDERS > 1
60  #endif
61 
62  #if HOTENDS
63  int16_t target_temperature[HOTENDS];
64  #endif
65 
66  #if HAS_HEATED_BED
67  int16_t target_temperature_bed;
68  #endif
69 
70  #if FAN_COUNT
71  uint8_t fan_speed[FAN_COUNT];
72  #endif
73 
74  #if HAS_LEVELING
75  bool leveling;
76  float fade;
77  #endif
78 
79  #if ENABLED(FWRETRACT)
80  float retract[EXTRUDERS], retract_hop;
81  #endif
82 
83  // Mixing extruder and gradient
84  #if ENABLED(MIXING_EXTRUDER)
85  //uint_fast8_t selected_vtool;
86  //mixer_comp_t color[NR_MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
87  #if ENABLED(GRADIENT_MIX)
88  gradient_t gradient;
89  #endif
90  #endif
91 
92  // Relative axis modes
94 
95  // SD Filename and position
96  char sd_filename[MAXPATHNAMELENGTH];
97  volatile uint32_t sdpos;
98 
99  // Job elapsed time
101 
103 
105 
107  public:
108  static const char filename[5];
109 
110  static SdFile file;
112 
113  static uint8_t queue_index_r; //!< Queue index of the active command
114  static uint32_t cmd_sdpos, //!< SD position of the next command
115  sdpos[BUFSIZE]; //!< SD positions of queued commands
116 
117  static void init();
118  static void prepare();
119 
120  static inline void setup() {
121  #if PIN_EXISTS(POWER_LOSS)
122  #if ENABLED(POWER_LOSS_PULL)
123  #if POWER_LOSS_STATE == LOW
125  #else
127  #endif
128  #else
130  #endif
131  #endif
132  }
133 
134  // Track each command's file offsets
135  static inline uint32_t command_sdpos() { return sdpos[queue_index_r]; }
136  static inline void commit_sdpos(const uint8_t index_w) { sdpos[index_w] = cmd_sdpos; }
137 
138  static bool enabled;
139  static void enable(const bool onoff);
140  static void changed();
141 
142  static void check();
143  static void resume();
144 
145  static inline bool exists() { return card.jobRecoverFileExists(); }
146  static inline void open(const bool read) { card.openJobRecoveryFile(read); }
147  static inline void close() { file.close(); }
148 
149  static void purge();
150  static void load();
151  static void save(const bool force=
152  #if ENABLED(SAVE_EACH_CMD_MODE)
153  true
154  #else
155  false
156  #endif
157  , const bool save_queue=true
158  );
159 
160  #if PIN_EXISTS(POWER_LOSS)
161  static inline void outage() {
163  _outage();
164  }
165  #endif
166 
167  static inline bool valid() { return info.valid_head && info.valid_head == info.valid_foot; }
168 
169  #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
170  static void debug(PGM_P const prefix);
171  #else
172  static inline void debug(PGM_P const) {}
173  #endif
174 
175  private:
176  static void write();
177 
178  #if PIN_EXISTS(POWER_LOSS)
179  static void _outage();
180  #endif
181 };
182 
SdBaseFile::seekSet
bool seekSet(const uint32_t pos)
Definition: Marlin_CardReader.cpp:505
PrintJobRecovery::close
static void close()
Definition: power_loss_recovery.h:147
GET_TEXT
#define GET_TEXT(MSG)
Definition: multi_language.h:72
power_loss_recovery.h
PrintJobRecovery
Definition: power_loss_recovery.h:106
XYZEval< float >
XYZEval::z
T z
Definition: types.h:383
mixer
Mixer mixer
SET_INPUT_PULLDOWN
#define SET_INPUT_PULLDOWN(IO)
set pin as input with pulldown wrapper
Definition: fastio.h:95
PrintJobRecovery::info
static job_recovery_info_t info
Definition: power_loss_recovery.h:111
PrintJobRecovery::exists
static bool exists()
Definition: power_loss_recovery.h:145
PrintJobRecovery::cmd_sdpos
static uint32_t cmd_sdpos
SD position of the next command.
Definition: power_loss_recovery.h:114
queue
GCodeQueue queue
Definition: queue.cpp:28
g29_auto.gcode
list gcode
Definition: g29_auto.py:44
DEBUG_EOL
#define DEBUG_EOL()
Definition: debug_out.h:86
DEBUG_ECHOLNPGM
#define DEBUG_ECHOLNPGM(...)
Definition: debug_out.h:79
PrintJobRecovery::command_sdpos
static uint32_t command_sdpos()
Definition: power_loss_recovery.h:135
IS_SD_PRINTING
#define IS_SD_PRINTING()
Definition: cardreader.h:295
print_job_timer
Stopwatch print_job_timer
Definition: printcounter.cpp:63
axis_homed
uint8_t axis_homed
Definition: motion.cpp:91
Planner::leveling_active
static bool leveling_active
Definition: planner.h:276
DEBUG_PRINT_P
#define DEBUG_PRINT_P(P)
Definition: debug_out.h:71
PrintJobRecovery::sdpos
static uint32_t sdpos[BUFSIZE]
SD positions of queued commands.
Definition: power_loss_recovery.h:114
PrintJobRecovery::commit_sdpos
static void commit_sdpos(const uint8_t index_w)
Definition: power_loss_recovery.h:136
PrintJobRecovery::resume
static void resume()
Z_HOME_DIR
#define Z_HOME_DIR
Definition: Configuration_A3ides_2209_MINI.h:975
dtostrf
char * dtostrf(double __val, signed char __width, unsigned char __prec, char *__s)
SdBaseFile::close
bool close()
Definition: Marlin_CardReader.cpp:535
PGM_P
#define PGM_P
Definition: pgmspace.h:30
XYZEval::e
T e
Definition: types.h:383
i
uint8_t i
Definition: screen_test_graph.c:72
STRINGIFY
#define STRINGIFY(M)
Definition: macros.h:73
AxisEnum
AxisEnum
Definition: types.h:36
kill
void kill(PGM_P const lcd_error, PGM_P const lcd_component, const bool steppers_off)
Definition: Marlin.cpp:718
millis
uint32_t millis(void)
Definition: wiring_time.c:29
PrintJobRecovery::file
static SdFile file
Definition: power_loss_recovery.h:110
PrintJobRecovery::queue_index_r
static uint8_t queue_index_r
Queue index of the active command.
Definition: power_loss_recovery.h:113
PrintJobRecovery::purge
static void purge()
job_recovery_info_t::axis_relative
uint8_t axis_relative
Definition: power_loss_recovery.h:93
DEBUG_ECHOLNPAIR
#define DEBUG_ECHOLNPAIR(...)
Definition: debug_out.h:82
IS_KINEMATIC
#define IS_KINEMATIC
Definition: Conditionals_LCD.h:545
recovery
PrintJobRecovery recovery
sprintf_P
#define sprintf_P(s,...)
Definition: pgmspace.h:72
update_workspace_offset
#define update_workspace_offset(x)
Definition: motion.h:127
PrintJobRecovery::filename
static const char filename[5]
Definition: power_loss_recovery.h:108
DEBUG_ECHOPGM
#define DEBUG_ECHOPGM(...)
Definition: debug_out.h:78
job_recovery_info_t::feedrate
uint16_t feedrate
Definition: power_loss_recovery.h:56
job_recovery_info_t::position_shift
xyz_pos_t position_shift
Definition: power_loss_recovery.h:53
SdFile::write
void write(uint8_t b)
SdBaseFile::read
int16_t read()
Definition: Marlin_CardReader.cpp:523
SET_INPUT_PULLUP
#define SET_INPUT_PULLUP(IO)
Definition: fastio.h:100
PrintJobRecovery::valid
static bool valid()
Definition: power_loss_recovery.h:167
POWER_LOSS_STATE
#define POWER_LOSS_STATE
Definition: pins_GT2560_V3.h:90
job_recovery_info_t::sdpos
volatile uint32_t sdpos
Definition: power_loss_recovery.h:97
PSTR
#define PSTR(str)
Definition: pgmspace.h:31
current_position
xyze_pos_t current_position
Definition: motion.cpp:102
SET_INPUT
#define SET_INPUT(IO)
Definition: fastio.h:99
COPY
Definition: inflate.h:36
LOOP_XYZ
#define LOOP_XYZ(VAR)
Definition: types.h:60
DISABLED
#define DISABLED(V...)
Definition: macros.h:178
Stopwatch::duration
static millis_t duration()
Get the running time.
Definition: stopwatch.cpp:108
void
void
Definition: png.h:1083
XYZEval::x
T x
Definition: types.h:383
SdFile
SdBaseFile with Print.
Definition: SdFile.h:45
feedrate_mm_s
feedRate_t feedrate_mm_s
Definition: motion.cpp:138
PrintJobRecovery::load
static void load()
HOTEND_LOOP
#define HOTEND_LOOP()
Definition: Conditionals_LCD.h:436
job_recovery_info_t::sd_filename
char sd_filename[MAXPATHNAMELENGTH]
Definition: power_loss_recovery.h:96
axis_known_position
uint8_t axis_known_position
Definition: motion.cpp:91
PrintJobRecovery::check
static void check()
ELAPSED
#define ELAPSED(NOW, SOON)
Definition: millis_t.h:29
POWER_LOSS_PIN
#define POWER_LOSS_PIN
Definition: pins_GT2560_V3.h:89
uint8_t
const uint8_t[]
Definition: 404_html.c:3
job_recovery_info_t::valid_head
uint8_t valid_head
Definition: power_loss_recovery.h:44
DEBUG_ECHO
#define DEBUG_ECHO(...)
Definition: debug_out.h:75
PrintJobRecovery::prepare
static void prepare()
job_recovery_info_t::current_position
xyze_pos_t current_position
Definition: power_loss_recovery.h:47
PrintJobRecovery::save
static void save(const bool force=false, const bool save_queue=true)
xyz_bits
constexpr uint8_t xyz_bits
Definition: motion.h:43
read
static int read(struct _reent *_r, void *pv, char *pc, int n)
Definition: gcode_file.cpp:9
PrintJobRecovery::debug
static void debug(PGM_P const)
Definition: power_loss_recovery.h:172
MAXPATHNAMELENGTH
#define MAXPATHNAMELENGTH
Definition: screen_filebrowser.cpp:26
job_recovery_info_t
Definition: power_loss_recovery.h:43
EXTRUDERS
#define EXTRUDERS
Definition: Configuration_A3ides_2209_MINI.h:148
FAN_COUNT
#define FAN_COUNT
Definition: Conditionals_post.h:1292
PrintJobRecovery::setup
static void setup()
Definition: power_loss_recovery.h:120
job_recovery_info_t::print_job_elapsed
millis_t print_job_elapsed
Definition: power_loss_recovery.h:100
MAX_CMD_SIZE
#define MAX_CMD_SIZE
Definition: Configuration_A3ides_2209_MINI_adv.h:1173
job_recovery_info_t::home_offset
xyz_pos_t home_offset
Definition: power_loss_recovery.h:50
HOTENDS
#define HOTENDS
Definition: Conditionals_LCD.h:425
XYZval< float >
BUFSIZE
#define BUFSIZE
Definition: Configuration_A3ides_2209_MINI_adv.h:1174
GCodeQueue::inject_P
static void inject_P(PGM_P const pgcode)
Definition: queue.cpp:206
DEBUG_CHAR
#define DEBUG_CHAR(...)
Definition: debug_out.h:74
LOOP_XYZE
#define LOOP_XYZE(VAR)
Definition: types.h:61
READ
#define READ(IO)
Definition: fastio.h:95
job_recovery_info_t::valid_foot
uint8_t valid_foot
Definition: power_loss_recovery.h:102
PrintJobRecovery::open
static void open(const bool read)
Definition: power_loss_recovery.h:146
thermalManager
Temperature thermalManager
Definition: temperature.cpp:89
millis_t
uint32_t millis_t
Definition: millis_t.h:26
PrintJobRecovery::enabled
static bool enabled
Definition: power_loss_recovery.h:138
active_extruder
constexpr uint8_t active_extruder
Definition: motion.h:107
ENABLED
#define ENABLED(V...)
Definition: macros.h:177
planner
Planner planner
Definition: planner.cpp:111
XYZEval::y
T y
Definition: types.h:383
fwretract.h
PrintJobRecovery::init
static void init()
PrintJobRecovery::enable
static void enable(const bool onoff)
PrintJobRecovery::changed
static void changed()