My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

power_loss_recovery.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. /**
  23. * power_loss_recovery.cpp - Resume an SD print after power-loss
  24. */
  25. #include "../inc/MarlinConfigPre.h"
  26. #if ENABLED(POWER_LOSS_RECOVERY)
  27. #include "power_loss_recovery.h"
  28. #include "../core/macros.h"
  29. bool PrintJobRecovery::enabled; // Initialized by settings.load()
  30. SdFile PrintJobRecovery::file;
  31. job_recovery_info_t PrintJobRecovery::info;
  32. #include "../sd/cardreader.h"
  33. #include "../lcd/ultralcd.h"
  34. #include "../gcode/queue.h"
  35. #include "../gcode/gcode.h"
  36. #include "../module/motion.h"
  37. #include "../module/planner.h"
  38. #include "../module/printcounter.h"
  39. #include "../module/temperature.h"
  40. #include "../core/serial.h"
  41. #if ENABLED(FWRETRACT)
  42. #include "fwretract.h"
  43. #endif
  44. PrintJobRecovery recovery;
  45. /**
  46. * Clear the recovery info
  47. */
  48. void PrintJobRecovery::init() { memset(&info, 0, sizeof(info)); }
  49. /**
  50. * Enable or disable then call changed()
  51. */
  52. void PrintJobRecovery::enable(const bool onoff) {
  53. enabled = onoff;
  54. changed();
  55. }
  56. /**
  57. * The enabled state was changed:
  58. * - Enabled: Purge the job recovery file
  59. * - Disabled: Write the job recovery file
  60. */
  61. void PrintJobRecovery::changed() {
  62. if (!enabled)
  63. purge();
  64. else if (IS_SD_PRINTING())
  65. save(true);
  66. }
  67. /**
  68. * Check for Print Job Recovery during setup()
  69. *
  70. * If a saved state exists send 'M1000 S' to initiate job recovery.
  71. */
  72. void PrintJobRecovery::check() {
  73. if (enabled) {
  74. if (!card.isDetected()) card.initsd();
  75. if (card.isDetected()) {
  76. load();
  77. if (!valid()) return purge();
  78. enqueue_and_echo_commands_P(PSTR("M1000 S"));
  79. }
  80. }
  81. }
  82. /**
  83. * Delete the recovery file and clear the recovery data
  84. */
  85. void PrintJobRecovery::purge() {
  86. init();
  87. card.removeJobRecoveryFile();
  88. }
  89. /**
  90. * Load the recovery data, if it exists
  91. */
  92. void PrintJobRecovery::load() {
  93. if (exists()) {
  94. open(true);
  95. (void)file.read(&info, sizeof(info));
  96. close();
  97. }
  98. #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
  99. debug(PSTR("Load"));
  100. #endif
  101. }
  102. /**
  103. * Save the current machine state to the power-loss recovery file
  104. */
  105. void PrintJobRecovery::save(const bool force/*=false*/, const bool save_queue/*=true*/) {
  106. #if SAVE_INFO_INTERVAL_MS > 0
  107. static millis_t next_save_ms; // = 0
  108. millis_t ms = millis();
  109. #endif
  110. // Did Z change since the last call?
  111. if (force
  112. #if DISABLED(SAVE_EACH_CMD_MODE) // Always save state when enabled
  113. #if PIN_EXISTS(POWER_LOSS) // Save if power loss pin is triggered
  114. || READ(POWER_LOSS_PIN) == POWER_LOSS_STATE
  115. #endif
  116. #if SAVE_INFO_INTERVAL_MS > 0 // Save if interval is elapsed
  117. || ELAPSED(ms, next_save_ms)
  118. #endif
  119. // Save every time Z is higher than the last call
  120. || current_position[Z_AXIS] > info.current_position[Z_AXIS]
  121. #endif
  122. ) {
  123. #if SAVE_INFO_INTERVAL_MS > 0
  124. next_save_ms = ms + SAVE_INFO_INTERVAL_MS;
  125. #endif
  126. // Set Head and Foot to matching non-zero values
  127. if (!++info.valid_head) ++info.valid_head; // non-zero in sequence
  128. //if (!IS_SD_PRINTING()) info.valid_head = 0;
  129. info.valid_foot = info.valid_head;
  130. // Machine state
  131. COPY(info.current_position, current_position);
  132. info.feedrate = uint16_t(feedrate_mm_s * 60.0f);
  133. #if HOTENDS > 1
  134. info.active_hotend = active_extruder;
  135. #endif
  136. HOTEND_LOOP() info.target_temperature[e] = thermalManager.temp_hotend[e].target;
  137. #if HAS_HEATED_BED
  138. info.target_temperature_bed = thermalManager.temp_bed.target;
  139. #endif
  140. #if FAN_COUNT
  141. COPY(info.fan_speed, thermalManager.fan_speed);
  142. #endif
  143. #if HAS_LEVELING
  144. info.leveling = planner.leveling_active;
  145. info.fade = (
  146. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  147. planner.z_fade_height
  148. #else
  149. 0
  150. #endif
  151. );
  152. #endif
  153. #if ENABLED(GRADIENT_MIX)
  154. memcpy(&info.gradient, &mixer.gradient, sizeof(info.gradient));
  155. #endif
  156. #if ENABLED(FWRETRACT)
  157. COPY(info.retract, fwretract.current_retract);
  158. info.retract_hop = fwretract.current_hop;
  159. #endif
  160. //relative mode
  161. info.relative_mode = relative_mode;
  162. info.relative_modes_e = gcode.axis_relative_modes[E_AXIS];
  163. // Commands in the queue
  164. info.commands_in_queue = save_queue ? commands_in_queue : 0;
  165. info.cmd_queue_index_r = cmd_queue_index_r;
  166. COPY(info.command_queue, command_queue);
  167. // Elapsed print job time
  168. info.print_job_elapsed = print_job_timer.duration();
  169. // SD file position
  170. card.getAbsFilename(info.sd_filename);
  171. info.sdpos = card.getIndex();
  172. write();
  173. // KILL now if the power-loss pin was triggered
  174. #if PIN_EXISTS(POWER_LOSS)
  175. if (READ(POWER_LOSS_PIN) == POWER_LOSS_STATE) kill(PSTR(MSG_OUTAGE_RECOVERY));
  176. #endif
  177. }
  178. }
  179. /**
  180. * Save the recovery info the recovery file
  181. */
  182. void PrintJobRecovery::write() {
  183. #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
  184. debug(PSTR("Write"));
  185. #endif
  186. open(false);
  187. file.seekSet(0);
  188. const int16_t ret = file.write(&info, sizeof(info));
  189. close();
  190. #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
  191. if (ret == -1) SERIAL_ECHOLNPGM("Power-loss file write failed.");
  192. #else
  193. UNUSED(ret);
  194. #endif
  195. }
  196. /**
  197. * Resume the saved print job
  198. */
  199. void PrintJobRecovery::resume() {
  200. #define RECOVERY_ZRAISE 2
  201. #if HAS_LEVELING
  202. // Make sure leveling is off before any G92 and G28
  203. gcode.process_subcommands_now_P(PSTR("M420 S0 Z0"));
  204. #endif
  205. // Set Z to 0, raise Z by 2mm, and Home (XY only for Cartesian) with no raise
  206. // (Only do simulated homing in Marlin Dev Mode.)
  207. gcode.process_subcommands_now_P(PSTR("G92.0 Z0\nG1 Z" STRINGIFY(RECOVERY_ZRAISE) "\nG28 R0"
  208. #if ENABLED(MARLIN_DEV_MODE)
  209. " S"
  210. #elif !IS_KINEMATIC
  211. " X Y"
  212. #endif
  213. ));
  214. // Pretend that all axes are homed
  215. axis_homed = axis_known_position = xyz_bits;
  216. char cmd[40], str_1[16], str_2[16];
  217. // Select the previously active tool (with no_move)
  218. #if EXTRUDERS > 1
  219. sprintf_P(cmd, PSTR("T%i S"), info.active_hotend);
  220. gcode.process_subcommands_now(cmd);
  221. #endif
  222. #if HAS_HEATED_BED
  223. const int16_t bt = info.target_temperature_bed;
  224. if (bt) {
  225. // Restore the bed temperature
  226. sprintf_P(cmd, PSTR("M190 S%i"), bt);
  227. gcode.process_subcommands_now(cmd);
  228. }
  229. #endif
  230. // Restore all hotend temperatures
  231. HOTEND_LOOP() {
  232. const int16_t et = info.target_temperature[e];
  233. if (et) {
  234. #if HOTENDS > 1
  235. sprintf_P(cmd, PSTR("T%i"), e);
  236. gcode.process_subcommands_now(cmd);
  237. #endif
  238. sprintf_P(cmd, PSTR("M109 S%i"), et);
  239. gcode.process_subcommands_now(cmd);
  240. }
  241. }
  242. // Restore print cooling fan speeds
  243. FANS_LOOP(i) {
  244. uint8_t f = info.fan_speed[i];
  245. if (f) {
  246. sprintf_P(cmd, PSTR("M106 P%i S%i"), i, f);
  247. gcode.process_subcommands_now(cmd);
  248. }
  249. }
  250. // Restore retract and hop state
  251. #if ENABLED(FWRETRACT)
  252. for (uint8_t e = 0; e < EXTRUDERS; e++) {
  253. if (info.retract[e] != 0.0)
  254. fwretract.current_retract[e] = info.retract[e];
  255. fwretract.retracted[e] = true;
  256. }
  257. fwretract.current_hop = info.retract_hop;
  258. #endif
  259. #if HAS_LEVELING
  260. // Restore leveling state before 'G92 Z' to ensure
  261. // the Z stepper count corresponds to the native Z.
  262. if (info.fade || info.leveling) {
  263. dtostrf(info.fade, 1, 1, str_1);
  264. sprintf_P(cmd, PSTR("M420 S%i Z%s"), int(info.leveling), str_1);
  265. gcode.process_subcommands_now(cmd);
  266. }
  267. #endif
  268. #if ENABLED(GRADIENT_MIX)
  269. memcpy(&mixer.gradient, &info.gradient, sizeof(info.gradient));
  270. #endif
  271. // Restore Z (plus raise) and E positions with G92.0
  272. dtostrf(info.current_position[Z_AXIS] + RECOVERY_ZRAISE, 1, 3, str_1);
  273. dtostrf(info.current_position[E_AXIS]
  274. #if ENABLED(SAVE_EACH_CMD_MODE)
  275. - 5 // Extra extrusion on restart
  276. #endif
  277. , 1, 3, str_2
  278. );
  279. sprintf_P(cmd, PSTR("G92.0 Z%s E%s"), str_1, str_2);
  280. gcode.process_subcommands_now(cmd);
  281. // Move back to the saved XY
  282. dtostrf(info.current_position[X_AXIS], 1, 3, str_1);
  283. dtostrf(info.current_position[Y_AXIS], 1, 3, str_2);
  284. sprintf_P(cmd, PSTR("G1 X%s Y%s F3000"), str_1, str_2);
  285. gcode.process_subcommands_now(cmd);
  286. // Move back to the saved Z
  287. dtostrf(info.current_position[Z_AXIS], 1, 3, str_1);
  288. sprintf_P(cmd, PSTR("G1 Z%s F200"), str_1);
  289. gcode.process_subcommands_now(cmd);
  290. // Restore the feedrate
  291. sprintf_P(cmd, PSTR("G1 F%d"), info.feedrate);
  292. gcode.process_subcommands_now(cmd);
  293. //relative mode
  294. if (info.relative_mode) relative_mode = true;
  295. if (info.relative_modes_e) gcode.axis_relative_modes[E_AXIS] = true;
  296. // Process commands from the old pending queue
  297. uint8_t c = info.commands_in_queue, r = info.cmd_queue_index_r;
  298. for (; c--; r = (r + 1) % BUFSIZE)
  299. gcode.process_subcommands_now(info.command_queue[r]);
  300. // Resume the SD file from the last position
  301. char *fn = info.sd_filename;
  302. while (*fn == '/') fn++;
  303. sprintf_P(cmd, PSTR("M23 %s"), fn);
  304. gcode.process_subcommands_now(cmd);
  305. sprintf_P(cmd, PSTR("M24 S%ld T%ld"), info.sdpos, info.print_job_elapsed);
  306. gcode.process_subcommands_now(cmd);
  307. }
  308. #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
  309. void PrintJobRecovery::debug(PGM_P const prefix) {
  310. serialprintPGM(prefix);
  311. SERIAL_ECHOLNPAIR(" Job Recovery Info...\nvalid_head:", int(info.valid_head), " valid_foot:", int(info.valid_foot));
  312. if (info.valid_head) {
  313. if (info.valid_head == info.valid_foot) {
  314. SERIAL_ECHOPGM("current_position: ");
  315. LOOP_XYZE(i) {
  316. if (i) SERIAL_CHAR(',');
  317. SERIAL_ECHO(info.current_position[i]);
  318. }
  319. SERIAL_EOL();
  320. SERIAL_ECHOLNPAIR("feedrate: ", info.feedrate);
  321. #if HOTENDS > 1
  322. SERIAL_ECHOLNPAIR("active_hotend: ", int(info.active_hotend));
  323. #endif
  324. SERIAL_ECHOPGM("target_temperature: ");
  325. HOTEND_LOOP() {
  326. SERIAL_ECHO(info.target_temperature[e]);
  327. if (e < HOTENDS - 1) SERIAL_CHAR(',');
  328. }
  329. SERIAL_EOL();
  330. #if HAS_HEATED_BED
  331. SERIAL_ECHOLNPAIR("target_temperature_bed: ", info.target_temperature_bed);
  332. #endif
  333. #if FAN_COUNT
  334. SERIAL_ECHOPGM("fan_speed: ");
  335. FANS_LOOP(i) {
  336. SERIAL_ECHO(int(info.fan_speed[i]));
  337. if (i < FAN_COUNT - 1) SERIAL_CHAR(',');
  338. }
  339. SERIAL_EOL();
  340. #endif
  341. #if HAS_LEVELING
  342. SERIAL_ECHOLNPAIR("leveling: ", int(info.leveling), "\n fade: ", int(info.fade));
  343. #endif
  344. #if ENABLED(FWRETRACT)
  345. SERIAL_ECHOPGM("retract: ");
  346. for (int8_t e = 0; e < EXTRUDERS; e++) {
  347. SERIAL_ECHO(info.retract[e]);
  348. if (e < EXTRUDERS - 1) SERIAL_CHAR(',');
  349. }
  350. SERIAL_EOL();
  351. SERIAL_ECHOLNPAIR("retract_hop: ", info.retract_hop);
  352. #endif
  353. SERIAL_ECHOLNPAIR("cmd_queue_index_r: ", int(info.cmd_queue_index_r));
  354. SERIAL_ECHOLNPAIR("commands_in_queue: ", int(info.commands_in_queue));
  355. for (uint8_t i = 0; i < info.commands_in_queue; i++) SERIAL_ECHOLNPAIR("> ", info.command_queue[i]);
  356. SERIAL_ECHOLNPAIR("sd_filename: ", info.sd_filename);
  357. SERIAL_ECHOLNPAIR("sdpos: ", info.sdpos);
  358. SERIAL_ECHOLNPAIR("print_job_elapsed: ", info.print_job_elapsed);
  359. }
  360. else
  361. SERIAL_ECHOLNPGM("INVALID DATA");
  362. }
  363. SERIAL_ECHOLNPGM("---");
  364. }
  365. #endif // DEBUG_POWER_LOSS_RECOVERY
  366. #endif // POWER_LOSS_RECOVERY