power_loss_recovery.cpp 9.8 KB

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