Browse Source

Creality3D Power-Loss Recovery

Scott Lahteine 6 years ago
parent
commit
11ab017dd0

+ 1
- 1
.travis.yml View File

@@ -67,7 +67,7 @@ script:
67 67
   - opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER SDSUPPORT EEPROM_SETTINGS
68 68
   - opt_enable BLINKM PCA9632 RGB_LED NEOPIXEL_LED AUTO_POWER_CONTROL NOZZLE_PARK_FEATURE FILAMENT_RUNOUT_SENSOR
69 69
   - opt_enable AUTO_BED_LEVELING_LINEAR Z_MIN_PROBE_REPEATABILITY_TEST DEBUG_LEVELING_FEATURE SKEW_CORRECTION SKEW_CORRECTION_FOR_Z SKEW_CORRECTION_GCODE
70
-  - opt_enable_adv ARC_P_CIRCLES ADVANCED_PAUSE_FEATURE CNC_WORKSPACE_PLANES CNC_COORDINATE_SYSTEMS
70
+  - opt_enable_adv ARC_P_CIRCLES ADVANCED_PAUSE_FEATURE CNC_WORKSPACE_PLANES CNC_COORDINATE_SYSTEMS POWER_LOSS_RECOVERY
71 71
   - opt_enable_adv FWRETRACT MAX7219_DEBUG LED_CONTROL_MENU CASE_LIGHT_ENABLE CASE_LIGHT_USE_NEOPIXEL CODEPENDENT_XY_HOMING
72 72
   - opt_set GRID_MAX_POINTS_X 16
73 73
   - opt_set_adv FANMUX0_PIN 53

+ 10
- 0
Marlin/Configuration_adv.h View File

@@ -556,6 +556,16 @@
556 556
   //#define MENU_ADDAUTOSTART
557 557
 
558 558
   /**
559
+   * Continue after Power-Loss (Creality3D)
560
+   *
561
+   * Store the current state to the SD Card at the start of each layer
562
+   * during SD printing. If the recovery file is found at boot time, present
563
+   * an option on the LCD screen to continue the print from the last-known
564
+   * point in the file.
565
+   */
566
+  //#define POWER_LOSS_RECOVERY
567
+
568
+  /**
559 569
    * Sort SD file listings in alphabetical order.
560 570
    *
561 571
    * With this option enabled, items on SD cards will be sorted

+ 8
- 0
Marlin/src/Marlin.cpp View File

@@ -126,6 +126,10 @@
126 126
   #include "feature/pause.h"
127 127
 #endif
128 128
 
129
+#if ENABLED(POWER_LOSS_RECOVERY)
130
+  #include "feature/power_loss_recovery.h"
131
+#endif
132
+
129 133
 #if ENABLED(FILAMENT_RUNOUT_SENSOR)
130 134
   #include "feature/runout.h"
131 135
 #endif
@@ -876,6 +880,10 @@ void setup() {
876 880
     pe_magnet_init();
877 881
   #endif
878 882
 
883
+  #if ENABLED(POWER_LOSS_RECOVERY)
884
+    do_print_job_recovery();
885
+  #endif
886
+
879 887
   #if ENABLED(USE_WATCHDOG) // Reinit watchdog after HAL_get_reset_source call
880 888
     watchdog_init();
881 889
   #endif

+ 10
- 0
Marlin/src/config/default/Configuration_adv.h View File

@@ -556,6 +556,16 @@
556 556
   //#define MENU_ADDAUTOSTART
557 557
 
558 558
   /**
559
+   * Continue after Power-Loss (Creality3D)
560
+   *
561
+   * Store the current state to the SD Card at the start of each layer
562
+   * during SD printing. If the recovery file is found at boot time, present
563
+   * an option on the LCD screen to continue the print from the last-known
564
+   * point in the file.
565
+   */
566
+  //#define POWER_LOSS_RECOVERY
567
+
568
+  /**
559 569
    * Sort SD file listings in alphabetical order.
560 570
    *
561 571
    * With this option enabled, items on SD cards will be sorted

+ 235
- 0
Marlin/src/feature/power_loss_recovery.cpp View File

@@ -0,0 +1,235 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 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
+
23
+/**
24
+ * power_loss_recovery.cpp - Resume an SD print after power-loss
25
+ */
26
+
27
+#include "../inc/MarlinConfigPre.h"
28
+
29
+#if ENABLED(POWER_LOSS_RECOVERY)
30
+
31
+#include "power_loss_recovery.h"
32
+
33
+#include "../lcd/ultralcd.h"
34
+#include "../gcode/queue.h"
35
+#include "../module/planner.h"
36
+#include "../module/printcounter.h"
37
+#include "../module/temperature.h"
38
+#include "../sd/cardreader.h"
39
+#include "../core/serial.h"
40
+
41
+// Recovery data
42
+job_recovery_info_t job_recovery_info;
43
+JobRecoveryPhase job_recovery_phase = JOB_RECOVERY_IDLE;
44
+uint8_t job_recovery_commands_count; //=0
45
+char job_recovery_commands[BUFSIZE + APPEND_CMD_COUNT][MAX_CMD_SIZE];
46
+
47
+// Private
48
+static char sd_filename[MAXPATHNAMELENGTH];
49
+
50
+#if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
51
+  void debug_print_job_recovery(const bool recovery) {
52
+    SERIAL_PROTOCOLPAIR("valid_head:", (int)job_recovery_info.valid_head);
53
+    SERIAL_PROTOCOLLNPAIR(" valid_foot:", (int)job_recovery_info.valid_foot);
54
+    if (job_recovery_info.valid_head) {
55
+      if (job_recovery_info.valid_head == job_recovery_info.valid_foot) {
56
+        SERIAL_PROTOCOLPGM("current_position");
57
+        LOOP_XYZE(i) SERIAL_PROTOCOLPAIR(": ", job_recovery_info.current_position[i]);
58
+        SERIAL_EOL();
59
+        SERIAL_PROTOCOLLNPAIR("feedrate: ", job_recovery_info.feedrate);
60
+        SERIAL_PROTOCOLPGM("target_temperature");
61
+        HOTEND_LOOP() SERIAL_PROTOCOLPAIR(": ", job_recovery_info.target_temperature[e]);
62
+        SERIAL_EOL();
63
+        SERIAL_PROTOCOLPGM("fanSpeeds");
64
+        for(uint8_t i = 0; i < FAN_COUNT; i++) SERIAL_PROTOCOLPAIR(": ", job_recovery_info.fanSpeeds[i]);
65
+        SERIAL_EOL();
66
+        #if HAS_LEVELING
67
+          SERIAL_PROTOCOLPAIR("leveling: ", int(job_recovery_info.leveling));
68
+          SERIAL_PROTOCOLLNPAIR(" fade: ", int(job_recovery_info.fade));
69
+        #endif
70
+        SERIAL_PROTOCOLLNPAIR("target_temperature_bed: ", job_recovery_info.target_temperature_bed);
71
+        SERIAL_PROTOCOLLNPAIR("cmd_queue_index_r: ", job_recovery_info.cmd_queue_index_r);
72
+        SERIAL_PROTOCOLLNPAIR("commands_in_queue: ", job_recovery_info.commands_in_queue);
73
+        if (recovery)
74
+          for (uint8_t i = 0; i < job_recovery_commands_count; i++) SERIAL_PROTOCOLLNPAIR("> ", job_recovery_commands[i]);
75
+        else
76
+          for (uint8_t i = 0; i < job_recovery_info.commands_in_queue; i++) SERIAL_PROTOCOLLNPAIR("> ", job_recovery_info.command_queue[i]);
77
+        SERIAL_PROTOCOLLNPAIR("sd_filename: ", sd_filename);
78
+        SERIAL_PROTOCOLLNPAIR("sdpos: ", job_recovery_info.sdpos);
79
+        SERIAL_PROTOCOLLNPAIR("print_job_elapsed: ", job_recovery_info.print_job_elapsed);
80
+      }
81
+      else
82
+        SERIAL_PROTOCOLLNPGM("INVALID DATA");
83
+    }
84
+  }
85
+#endif // DEBUG_POWER_LOSS_RECOVERY
86
+
87
+/**
88
+ * Check for Print Job Recovery
89
+ * If the file has a saved state, populate the job_recovery_commands queue
90
+ */
91
+void do_print_job_recovery() {
92
+  //if (job_recovery_commands_count > 0) return;
93
+  memset(&job_recovery_info, 0, sizeof(job_recovery_info));
94
+  ZERO(job_recovery_commands);
95
+
96
+  if (!card.cardOK) card.initsd();
97
+
98
+  if (card.cardOK) {
99
+
100
+    #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
101
+      SERIAL_PROTOCOLLNPAIR("Init job recovery info. Size: ", (int)sizeof(job_recovery_info));
102
+    #endif
103
+
104
+    if (card.jobRecoverFileExists()) {
105
+      card.openJobRecoveryFile(true);
106
+      card.loadJobRecoveryInfo();
107
+      card.closeJobRecoveryFile();
108
+      //card.removeJobRecoveryFile();
109
+
110
+      if (job_recovery_info.valid_head && job_recovery_info.valid_head == job_recovery_info.valid_foot) {
111
+
112
+        uint8_t ind = 0;
113
+
114
+        #if HAS_LEVELING
115
+          strcpy_P(job_recovery_commands[ind++], PSTR("M420 S0 Z0"));               // Leveling off before G92 or G28
116
+        #endif
117
+
118
+        strcpy_P(job_recovery_commands[ind++], PSTR("G92.0 Z0"));                   // Ensure Z is equal to 0
119
+        strcpy_P(job_recovery_commands[ind++], PSTR("G1 Z2"));                      // Raise Z by 2mm (we hope!)
120
+        strcpy_P(job_recovery_commands[ind++], PSTR("G28 R0"
121
+          #if !IS_KINEMATIC
122
+            " X Y"                                                                  // Home X and Y for Cartesian
123
+          #endif
124
+        ));
125
+
126
+        #if HAS_LEVELING
127
+          // Restore leveling state before G92 sets Z
128
+          // This ensures the steppers correspond to the native Z
129
+          sprintf_P(job_recovery_commands[ind++], PSTR("M420 S%i Z%s"), int(job_recovery_info.leveling), job_recovery_info.fade);
130
+        #endif
131
+
132
+        char str_1[16], str_2[16];
133
+        dtostrf(job_recovery_info.current_position[Z_AXIS] + 2, 1, 3, str_1);
134
+        dtostrf(job_recovery_info.current_position[E_AXIS]
135
+          #if ENABLED(SAVE_EACH_CMD_MODE)
136
+            - 5
137
+          #endif
138
+          , 1, 3, str_2
139
+        );
140
+        sprintf_P(job_recovery_commands[ind++], PSTR("G92.0 Z%s E%s"), str_1, str_2); // Current Z + 2 and E
141
+
142
+        strcpy_P(job_recovery_commands[ind++], PSTR("M117 Continuing..."));
143
+
144
+        uint8_t r = job_recovery_info.cmd_queue_index_r;
145
+        while (job_recovery_info.commands_in_queue) {
146
+          strcpy(job_recovery_commands[ind++], job_recovery_info.command_queue[r]);
147
+          job_recovery_info.commands_in_queue--;
148
+          r = (r + 1) % BUFSIZE;
149
+        }
150
+
151
+        job_recovery_commands_count = ind;
152
+
153
+        #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
154
+          debug_print_job_recovery(true);
155
+        #endif
156
+
157
+        card.openFile(sd_filename, true);
158
+        card.setIndex(job_recovery_info.sdpos);
159
+      }
160
+      else {
161
+        if (job_recovery_info.valid_head != job_recovery_info.valid_foot)
162
+          LCD_ALERTMESSAGEPGM("INVALID DATA");
163
+        memset(&job_recovery_info, 0, sizeof(job_recovery_info));
164
+      }
165
+    }
166
+  }
167
+}
168
+
169
+/**
170
+ * Save the current machine state to the "bin" file
171
+ */
172
+void save_job_recovery_info() {
173
+  #if SAVE_INFO_INTERVAL_MS > 0
174
+    static millis_t next_save_ms; // = 0;  // Init on reset
175
+    millis_t ms = millis();
176
+  #endif
177
+  if (
178
+    #if SAVE_INFO_INTERVAL_MS > 0
179
+      ELAPSED(ms, next_save_ms) ||
180
+    #endif
181
+    #if ENABLED(SAVE_EACH_CMD_MODE)
182
+      true
183
+    #else
184
+      (current_position[Z_AXIS] > 0 && current_position[Z_AXIS] > job_recovery_info.current_position[Z_AXIS])
185
+    #endif
186
+  ) {
187
+    #if SAVE_INFO_INTERVAL_MS > 0
188
+      next_save_ms = ms + SAVE_INFO_INTERVAL_MS;
189
+    #endif
190
+
191
+    // Head and foot will match if valid data was saved
192
+    if (!++job_recovery_info.valid_head) ++job_recovery_info.valid_head; // non-zero in sequence
193
+    job_recovery_info.valid_foot = job_recovery_info.valid_head;
194
+
195
+    // Machine state
196
+    COPY(job_recovery_info.current_position, current_position);
197
+    job_recovery_info.feedrate = feedrate_mm_s;
198
+    COPY(job_recovery_info.target_temperature, thermalManager.target_temperature);
199
+    job_recovery_info.target_temperature_bed = thermalManager.target_temperature_bed;
200
+    COPY(job_recovery_info.fanSpeeds, fanSpeeds);
201
+
202
+    #if HAS_LEVELING
203
+      job_recovery_info.leveling = planner.leveling_active;
204
+      job_recovery_info.fade = (
205
+        #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
206
+          planner.z_fade_height
207
+        #else
208
+          0
209
+        #endif
210
+      );
211
+    #endif
212
+
213
+    // Commands in the queue
214
+    job_recovery_info.cmd_queue_index_r = cmd_queue_index_r;
215
+    job_recovery_info.commands_in_queue = commands_in_queue;
216
+    COPY(job_recovery_info.command_queue, command_queue);
217
+
218
+    // Elapsed print job time
219
+    job_recovery_info.print_job_elapsed = print_job_timer.duration() * 1000UL;
220
+
221
+    // SD file position
222
+    card.getAbsFilename(sd_filename);
223
+    job_recovery_info.sdpos = card.getIndex();
224
+
225
+    #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
226
+      SERIAL_PROTOCOLLNPGM("Saving job_recovery_info");
227
+      debug_print_job_recovery(false);
228
+    #endif
229
+
230
+    card.openJobRecoveryFile(false);
231
+    (void)card.saveJobRecoveryInfo();
232
+  }
233
+}
234
+
235
+#endif // POWER_LOSS_RECOVERY

+ 86
- 0
Marlin/src/feature/power_loss_recovery.h View File

@@ -0,0 +1,86 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 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
+
23
+/**
24
+ * power_loss_recovery.h - Resume an SD print after power-loss
25
+ */
26
+
27
+#ifndef _POWER_LOSS_RECOVERY_H_
28
+#define _POWER_LOSS_RECOVERY_H_
29
+
30
+#include "../sd/cardreader.h"
31
+#include "../core/types.h"
32
+#include "../inc/MarlinConfigPre.h"
33
+
34
+#define SAVE_INFO_INTERVAL_MS 0
35
+//#define SAVE_EACH_CMD_MODE
36
+//#define DEBUG_POWER_LOSS_RECOVERY
37
+
38
+typedef struct {
39
+  uint8_t valid_head;
40
+
41
+  // Machine state
42
+  float current_position[NUM_AXIS], feedrate;
43
+  int16_t target_temperature[HOTENDS],
44
+          target_temperature_bed,
45
+          fanSpeeds[FAN_COUNT];
46
+
47
+  #if HAS_LEVELING
48
+    bool leveling;
49
+    float fade;
50
+  #endif
51
+
52
+  // Command queue
53
+  uint8_t cmd_queue_index_r, commands_in_queue;
54
+  char command_queue[BUFSIZE][MAX_CMD_SIZE];
55
+
56
+  // SD File position
57
+  uint32_t sdpos;
58
+
59
+  // Job elapsed time
60
+  millis_t print_job_elapsed;
61
+
62
+  uint8_t valid_foot;
63
+} job_recovery_info_t;
64
+
65
+extern job_recovery_info_t job_recovery_info;
66
+
67
+enum JobRecoveryPhase : unsigned char {
68
+  JOB_RECOVERY_IDLE,
69
+  JOB_RECOVERY_MAYBE,
70
+  JOB_RECOVERY_YES
71
+};
72
+extern JobRecoveryPhase job_recovery_phase;
73
+
74
+#if HAS_LEVELING
75
+  #define APPEND_CMD_COUNT 7
76
+#else
77
+  #define APPEND_CMD_COUNT 5
78
+#endif
79
+
80
+extern char job_recovery_commands[BUFSIZE + APPEND_CMD_COUNT][MAX_CMD_SIZE];
81
+extern uint8_t job_recovery_commands_count;
82
+
83
+void do_print_job_recovery();
84
+void save_job_recovery_info();
85
+
86
+#endif // _POWER_LOSS_RECOVERY_H_

+ 1
- 2
Marlin/src/gcode/calibrate/M100.cpp View File

@@ -25,6 +25,7 @@
25 25
 #if ENABLED(M100_FREE_MEMORY_WATCHER)
26 26
 
27 27
 #include "../gcode.h"
28
+#include "../queue.h"
28 29
 #include "../../libs/hex_print_routines.h"
29 30
 
30 31
 #include "../../Marlin.h" // for idle()
@@ -59,8 +60,6 @@
59 60
 
60 61
 #define TEST_BYTE ((char) 0xE5)
61 62
 
62
-extern char command_queue[BUFSIZE][MAX_CMD_SIZE];
63
-
64 63
 extern char* __brkval;
65 64
 extern size_t  __heap_start, __heap_end, __flp;
66 65
 extern char __bss_end;

+ 33
- 4
Marlin/src/gcode/queue.cpp View File

@@ -37,6 +37,10 @@
37 37
   #include "../feature/leds/leds.h"
38 38
 #endif
39 39
 
40
+#if ENABLED(POWER_LOSS_RECOVERY)
41
+  #include "../feature/power_loss_recovery.h"
42
+#endif
43
+
40 44
 /**
41 45
  * GCode line number handling. Hosts may opt to include line numbers when
42 46
  * sending commands to Marlin, and lines will be checked for sequentiality.
@@ -115,7 +119,7 @@ inline void _commit_command(bool say_ok
115 119
  * Return true if the command was successfully added.
116 120
  * Return false for a full buffer, or if the 'command' is a comment.
117 121
  */
118
-inline bool _enqueuecommand(const char* cmd, bool say_ok
122
+inline bool _enqueuecommand(const char* cmd, bool say_ok=false
119 123
   #if NUM_SERIAL > 1
120 124
     , int16_t port = -1
121 125
   #endif
@@ -133,8 +137,8 @@ inline bool _enqueuecommand(const char* cmd, bool say_ok
133 137
 /**
134 138
  * Enqueue with Serial Echo
135 139
  */
136
-bool enqueue_and_echo_command(const char* cmd, bool say_ok/*=false*/) {
137
-  if (_enqueuecommand(cmd, say_ok)) {
140
+bool enqueue_and_echo_command(const char* cmd) {
141
+  if (_enqueuecommand(cmd)) {
138 142
     SERIAL_ECHO_START();
139 143
     SERIAL_ECHOPAIR(MSG_ENQUEUEING, cmd);
140 144
     SERIAL_CHAR('"');
@@ -486,6 +490,22 @@ inline void get_serial_commands() {
486 490
     }
487 491
   }
488 492
 
493
+  #if ENABLED(POWER_LOSS_RECOVERY)
494
+
495
+    inline bool drain_job_recovery_commands() {
496
+      static uint8_t job_recovery_commands_index = 0; // Resets on reboot
497
+      if (job_recovery_commands_count) {
498
+        if (_enqueuecommand(job_recovery_commands[job_recovery_commands_index])) {
499
+          ++job_recovery_commands_index;
500
+          if (!--job_recovery_commands_count) job_recovery_phase = JOB_RECOVERY_IDLE;
501
+        }
502
+        return true;
503
+      }
504
+      return false;
505
+    }
506
+
507
+  #endif
508
+
489 509
 #endif // SDSUPPORT
490 510
 
491 511
 /**
@@ -501,6 +521,11 @@ void get_available_commands() {
501 521
 
502 522
   get_serial_commands();
503 523
 
524
+  #if ENABLED(POWER_LOSS_RECOVERY)
525
+    // Commands for power-loss recovery take precedence
526
+    if (job_recovery_phase == JOB_RECOVERY_YES && drain_job_recovery_commands()) return;
527
+  #endif
528
+
504 529
   #if ENABLED(SDSUPPORT)
505 530
     get_sdcard_commands();
506 531
   #endif
@@ -543,8 +568,12 @@ void advance_command_queue() {
543 568
           ok_to_send();
544 569
       }
545 570
     }
546
-    else
571
+    else {
547 572
       gcode.process_next_command();
573
+      #if ENABLED(POWER_LOSS_RECOVERY)
574
+        if (card.cardOK && card.sdprinting) save_job_recovery_info();
575
+      #endif
576
+    }
548 577
 
549 578
   #else
550 579
 

+ 1
- 1
Marlin/src/gcode/queue.h View File

@@ -95,7 +95,7 @@ void enqueue_and_echo_commands_P(const char * const pgcode);
95 95
 /**
96 96
  * Enqueue with Serial Echo
97 97
  */
98
-bool enqueue_and_echo_command(const char* cmd, bool say_ok=false);
98
+bool enqueue_and_echo_command(const char* cmd);
99 99
 
100 100
 #define HAS_LCD_QUEUE_NOW (ENABLED(MALYAN_LCD) || (ENABLED(ULTIPANEL) && (ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(PID_AUTOTUNE_MENU) || ENABLED(ADVANCED_PAUSE_FEATURE))))
101 101
 #define HAS_QUEUE_NOW (ENABLED(SDSUPPORT) || HAS_LCD_QUEUE_NOW)

+ 10
- 3
Marlin/src/gcode/sdcard/M20-M30_M32-M34_M928.cpp View File

@@ -29,13 +29,16 @@
29 29
 #include "../../module/printcounter.h"
30 30
 #include "../../module/stepper.h"
31 31
 
32
+#if ENABLED(POWER_LOSS_RECOVERY)
33
+  #include "../../feature/power_loss_recovery.h"
34
+#endif
35
+
32 36
 #if ENABLED(PARK_HEAD_ON_PAUSE)
33 37
   #include "../../feature/pause.h"
34
-  #include "../queue.h"
35 38
 #endif
36 39
 
37
-#if NUM_SERIAL > 1
38
-  #include "../../gcode/queue.h"
40
+#if ENABLED(PARK_HEAD_ON_PAUSE) || NUM_SERIAL > 1
41
+  #include "../queue.h"
39 42
 #endif
40 43
 
41 44
 /**
@@ -78,6 +81,10 @@ void GcodeSuite::M23() {
78 81
  * M24: Start or Resume SD Print
79 82
  */
80 83
 void GcodeSuite::M24() {
84
+  #if ENABLED(POWER_LOSS_RECOVERY)
85
+    card.removeJobRecoveryFile();
86
+  #endif
87
+
81 88
   #if ENABLED(PARK_HEAD_ON_PAUSE)
82 89
     resume_print();
83 90
   #endif

+ 1
- 1
Marlin/src/inc/Conditionals_post.h View File

@@ -1334,7 +1334,7 @@
1334 1334
 #define GRID_MAX_POINTS ((GRID_MAX_POINTS_X) * (GRID_MAX_POINTS_Y))
1335 1335
 
1336 1336
 // Add commands that need sub-codes to this list
1337
-#define USE_GCODE_SUBCODES ENABLED(G38_PROBE_TARGET) || ENABLED(CNC_COORDINATE_SYSTEMS)
1337
+#define USE_GCODE_SUBCODES ENABLED(G38_PROBE_TARGET) || ENABLED(CNC_COORDINATE_SYSTEMS) || ENABLED(POWER_LOSS_RECOVERY)
1338 1338
 
1339 1339
 // Parking Extruder
1340 1340
 #if ENABLED(PARKING_EXTRUDER)

+ 5
- 1
Marlin/src/inc/SanityCheck.h View File

@@ -462,7 +462,7 @@ static_assert(X_MAX_LENGTH >= X_BED_SIZE && Y_MAX_LENGTH >= Y_BED_SIZE,
462 462
   #elif ENABLED(BABYSTEP_ZPROBE_OFFSET) && !HAS_BED_PROBE
463 463
     #error "BABYSTEP_ZPROBE_OFFSET requires a probe."
464 464
   #elif ENABLED(BABYSTEP_ZPROBE_GFX_OVERLAY) && !ENABLED(DOGLCD)
465
-    #error "BABYSTEP_ZPROBE_GFX_OVERLAY requires a DOGLCD."
465
+    #error "BABYSTEP_ZPROBE_GFX_OVERLAY requires a Graphical LCD."
466 466
   #elif ENABLED(BABYSTEP_ZPROBE_GFX_OVERLAY) && !ENABLED(BABYSTEP_ZPROBE_OFFSET)
467 467
     #error "BABYSTEP_ZPROBE_GFX_OVERLAY requires a BABYSTEP_ZPROBE_OFFSET."
468 468
   #endif
@@ -1708,4 +1708,8 @@ static_assert(COUNT(sanity_arr_3) <= XYZE_N, "DEFAULT_MAX_ACCELERATION has too m
1708 1708
   #endif
1709 1709
 #endif
1710 1710
 
1711
+#if ENABLED(POWER_LOSS_RECOVERY) && !ENABLED(ULTIPANEL)
1712
+  #error "POWER_LOSS_RECOVERY currently requires an LCD Controller."
1713
+#endif
1714
+
1711 1715
 #endif // _SANITYCHECK_H_

+ 1
- 1
Marlin/src/lcd/malyanlcd.cpp View File

@@ -302,7 +302,7 @@ void process_lcd_s_command(const char* command) {
302 302
 
303 303
     case 'H':
304 304
       // Home all axis
305
-      enqueue_and_echo_command("G28", false);
305
+      enqueue_and_echo_commands_P(PSTR("G28"));
306 306
       break;
307 307
 
308 308
     case 'L': {

+ 71
- 0
Marlin/src/lcd/ultralcd.cpp View File

@@ -43,6 +43,10 @@
43 43
   #include "../feature/pause.h"
44 44
 #endif
45 45
 
46
+#if ENABLED(POWER_LOSS_RECOVERY)
47
+  #include "../feature/power_loss_recovery.h"
48
+#endif
49
+
46 50
 #if ENABLED(PRINTCOUNTER) && ENABLED(LCD_INFO_MENU)
47 51
   #include "../libs/duration_t.h"
48 52
 #endif
@@ -842,10 +846,70 @@ void kill_screen(const char* lcd_msg) {
842 846
       abort_sd_printing = true;
843 847
       lcd_setstatusPGM(PSTR(MSG_PRINT_ABORTED), -1);
844 848
       lcd_return_to_status();
849
+
850
+      #if ENABLED(POWER_LOSS_RECOVERY)
851
+        card.openJobRecoveryFile(false);
852
+        job_recovery_info.valid_head = job_recovery_info.valid_foot = 0;
853
+        (void)card.saveJobRecoveryInfo();
854
+        card.closeJobRecoveryFile();
855
+        job_recovery_commands_count = 0;
856
+      #endif
845 857
     }
846 858
 
847 859
   #endif // SDSUPPORT
848 860
 
861
+  #if ENABLED(POWER_LOSS_RECOVERY)
862
+
863
+    static void lcd_sdcard_recover_job() {
864
+      char cmd[20];
865
+
866
+      // Return to status now
867
+      lcd_return_to_status();
868
+
869
+      // Turn leveling off and home
870
+      enqueue_and_echo_commands_P(PSTR("M420 S0\nG28"
871
+        #if !IS_KINEMATIC
872
+          " X Y"
873
+        #endif
874
+      ));
875
+
876
+      // Restore the bed temperature
877
+      sprintf_P(cmd, PSTR("M190 S%i"), job_recovery_info.target_temperature_bed);
878
+      enqueue_and_echo_command(cmd);
879
+
880
+      // Restore all hotend temperatures
881
+      HOTEND_LOOP() {
882
+        sprintf_P(cmd, PSTR("M109 S%i"), job_recovery_info.target_temperature[e]);
883
+        enqueue_and_echo_command(cmd);
884
+      }
885
+
886
+      // Restore print cooling fan speeds
887
+      for (uint8_t i = 0; i < FAN_COUNT; i++) {
888
+        sprintf_P(cmd, PSTR("M106 P%i S%i"), i, job_recovery_info.fanSpeeds[i]);
889
+        enqueue_and_echo_command(cmd);
890
+      }
891
+
892
+      // Start draining the job recovery command queue
893
+      job_recovery_phase = JOB_RECOVERY_YES;
894
+
895
+      // Resume the print job timer
896
+      if (job_recovery_info.print_job_elapsed)
897
+        print_job_timer.resume(job_recovery_info.print_job_elapsed);
898
+
899
+      // Start getting commands from SD
900
+      card.startFileprint();
901
+    }
902
+
903
+    static void lcd_job_recovery_menu() {
904
+      defer_return_to_status = true;
905
+      START_MENU();
906
+      MENU_ITEM(function, MSG_RESUME_PRINT, lcd_sdcard_recover_job);
907
+      MENU_ITEM(function, MSG_STOP_PRINT, lcd_sdcard_stop);
908
+      END_MENU();
909
+    }
910
+
911
+  #endif // POWER_LOSS_RECOVERY
912
+
849 913
   #if ENABLED(MENU_ITEM_CASE_LIGHT)
850 914
 
851 915
     #include "../feature/caselight.h"
@@ -5047,6 +5111,13 @@ void lcd_update() {
5047 5111
 
5048 5112
   #endif // SDSUPPORT && SD_DETECT_PIN
5049 5113
 
5114
+  #if ENABLED(POWER_LOSS_RECOVERY)
5115
+    if (job_recovery_commands_count && job_recovery_phase == JOB_RECOVERY_IDLE) {
5116
+      lcd_goto_screen(lcd_job_recovery_menu);
5117
+      job_recovery_phase = JOB_RECOVERY_MAYBE; // Waiting for a response
5118
+    }
5119
+  #endif
5120
+
5050 5121
   const millis_t ms = millis();
5051 5122
   if (ELAPSED(ms, next_lcd_update_ms)
5052 5123
     #if ENABLED(DOGLCD)

+ 55
- 0
Marlin/src/sd/cardreader.cpp View File

@@ -33,6 +33,10 @@
33 33
 #include "../core/language.h"
34 34
 #include "../gcode/queue.h"
35 35
 
36
+#if ENABLED(POWER_LOSS_RECOVERY)
37
+  #include "../feature/power_loss_recovery.h"
38
+#endif
39
+
36 40
 #if ENABLED(ADVANCED_PAUSE_FEATURE)
37 41
   #include "../feature/pause.h"
38 42
 #endif
@@ -968,6 +972,15 @@ void CardReader::printingHasFinished() {
968 972
   }
969 973
   else {
970 974
     sdprinting = false;
975
+
976
+    #if ENABLED(POWER_LOSS_RECOVERY)
977
+      openJobRecoveryFile(false);
978
+      job_recovery_info.valid_head = job_recovery_info.valid_foot = 0;
979
+      (void)saveJobRecoveryInfo();
980
+      closeJobRecoveryFile();
981
+      job_recovery_commands_count = 0;
982
+    #endif
983
+
971 984
     #if ENABLED(SD_FINISHED_STEPPERRELEASE) && defined(SD_FINISHED_RELEASECOMMAND)
972 985
       stepper.cleaning_buffer_counter = 1; // The command will fire from the Stepper ISR
973 986
     #endif
@@ -1006,4 +1019,46 @@ void CardReader::printingHasFinished() {
1006 1019
   }
1007 1020
 #endif // AUTO_REPORT_SD_STATUS
1008 1021
 
1022
+#if ENABLED(POWER_LOSS_RECOVERY)
1023
+
1024
+  char job_recovery_file_name[4] = "bin";
1025
+
1026
+  void CardReader::openJobRecoveryFile(const bool read) {
1027
+    if (!cardOK) return;
1028
+    if (jobRecoveryFile.isOpen()) return;
1029
+    if (!jobRecoveryFile.open(&root, job_recovery_file_name, read ? O_READ : O_CREAT | O_WRITE | O_TRUNC | O_SYNC)) {
1030
+      SERIAL_PROTOCOLPAIR(MSG_SD_OPEN_FILE_FAIL, job_recovery_file_name);
1031
+      SERIAL_PROTOCOLCHAR('.');
1032
+      SERIAL_EOL();
1033
+    }
1034
+    else
1035
+      SERIAL_PROTOCOLLNPAIR(MSG_SD_WRITE_TO_FILE, job_recovery_file_name);
1036
+  }
1037
+
1038
+  void CardReader::closeJobRecoveryFile() { jobRecoveryFile.close(); }
1039
+
1040
+  bool CardReader::jobRecoverFileExists() {
1041
+    return jobRecoveryFile.open(&root, job_recovery_file_name, O_READ);
1042
+  }
1043
+
1044
+  int16_t CardReader::saveJobRecoveryInfo() {
1045
+    jobRecoveryFile.seekSet(0);
1046
+    const int16_t ret = jobRecoveryFile.write(&job_recovery_info, sizeof(job_recovery_info));
1047
+    if (ret == -1) SERIAL_PROTOCOLLNPGM("Power-loss file write failed.");
1048
+    return ret;
1049
+  }
1050
+
1051
+  int16_t CardReader::loadJobRecoveryInfo() {
1052
+    return jobRecoveryFile.read(&job_recovery_info, sizeof(job_recovery_info));
1053
+  }
1054
+
1055
+  void CardReader::removeJobRecoveryFile() {
1056
+    if (jobRecoveryFile.remove(&root, job_recovery_file_name))
1057
+      SERIAL_PROTOCOLLNPGM("Power-loss file deleted.");
1058
+    else
1059
+      SERIAL_PROTOCOLLNPGM("Power-loss file delete failed.");
1060
+  }
1061
+
1062
+#endif // POWER_LOSS_RECOVERY
1063
+
1009 1064
 #endif // SDSUPPORT

+ 15
- 1
Marlin/src/sd/cardreader.h View File

@@ -103,11 +103,21 @@ public:
103 103
     #endif
104 104
   #endif
105 105
 
106
+  #if ENABLED(POWER_LOSS_RECOVERY)
107
+    void openJobRecoveryFile(const bool read);
108
+    void closeJobRecoveryFile();
109
+    bool jobRecoverFileExists();
110
+    int16_t saveJobRecoveryInfo();
111
+    int16_t loadJobRecoveryInfo();
112
+    void removeJobRecoveryFile();
113
+  #endif
114
+
106 115
   FORCE_INLINE void pauseSDPrint() { sdprinting = false; }
107 116
   FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
108 117
   FORCE_INLINE bool eof() { return sdpos >= filesize; }
109 118
   FORCE_INLINE int16_t get() { sdpos = file.curPosition(); return (int16_t)file.read(); }
110
-  FORCE_INLINE void setIndex(long index) { sdpos = index; file.seekSet(index); }
119
+  FORCE_INLINE void setIndex(const uint32_t index) { sdpos = index; file.seekSet(index); }
120
+  FORCE_INLINE uint32_t getIndex() { return sdpos; }
111 121
   FORCE_INLINE uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
112 122
   FORCE_INLINE char* getWorkDirName() { workDir.getFilename(filename); return filename; }
113 123
 
@@ -191,6 +201,10 @@ private:
191 201
   SdVolume volume;
192 202
   SdFile file;
193 203
 
204
+  #if ENABLED(POWER_LOSS_RECOVERY)
205
+    SdFile jobRecoveryFile;
206
+  #endif
207
+
194 208
   #define SD_PROCEDURE_DEPTH 1
195 209
   #define MAXPATHNAMELENGTH (FILENAME_LENGTH*MAX_DIR_DEPTH + MAX_DIR_DEPTH + 1)
196 210
   uint8_t file_subcall_ctr;

Loading…
Cancel
Save