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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. if (force
  111. #if DISABLED(SAVE_EACH_CMD_MODE) // Always save state when enabled
  112. #if PIN_EXISTS(POWER_LOSS) // Save if power loss pin is triggered
  113. || READ(POWER_LOSS_PIN) == POWER_LOSS_STATE
  114. #endif
  115. #if SAVE_INFO_INTERVAL_MS > 0 // Save if interval is elapsed
  116. || ELAPSED(ms, next_save_ms)
  117. #endif
  118. // Save every time Z is higher than the last call
  119. || current_position[Z_AXIS] > info.current_position[Z_AXIS]
  120. #endif
  121. ) {
  122. #if SAVE_INFO_INTERVAL_MS > 0
  123. next_save_ms = ms + SAVE_INFO_INTERVAL_MS;
  124. #endif
  125. // Set Head and Foot to matching non-zero values
  126. if (!++info.valid_head) ++info.valid_head; // non-zero in sequence
  127. //if (!IS_SD_PRINTING()) info.valid_head = 0;
  128. info.valid_foot = info.valid_head;
  129. // Machine state
  130. COPY(info.current_position, current_position);
  131. info.feedrate = uint16_t(feedrate_mm_s * 60.0f);
  132. #if HOTENDS > 1
  133. info.active_hotend = active_extruder;
  134. #endif
  135. COPY(info.target_temperature, thermalManager.target_temperature);
  136. #if HAS_HEATED_BED
  137. info.target_temperature_bed = thermalManager.target_temperature_bed;
  138. #endif
  139. #if FAN_COUNT
  140. COPY(info.fan_speed, thermalManager.fan_speed);
  141. #endif
  142. #if HAS_LEVELING
  143. info.leveling = planner.leveling_active;
  144. info.fade = (
  145. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  146. planner.z_fade_height
  147. #else
  148. 0
  149. #endif
  150. );
  151. #endif
  152. #if ENABLED(GRADIENT_MIX)
  153. memcpy(&info.gradient, &mixer.gradient, sizeof(info.gradient));
  154. #endif
  155. #if ENABLED(FWRETRACT)
  156. COPY(info.retract, fwretract.current_retract);
  157. info.retract_hop = fwretract.current_hop;
  158. #endif
  159. // Commands in the queue
  160. info.commands_in_queue = save_queue ? commands_in_queue : 0;
  161. info.cmd_queue_index_r = cmd_queue_index_r;
  162. COPY(info.command_queue, command_queue);
  163. // Elapsed print job time
  164. info.print_job_elapsed = print_job_timer.duration();
  165. // SD file position
  166. card.getAbsFilename(info.sd_filename);
  167. info.sdpos = card.getIndex();
  168. write();
  169. // KILL now if the power-loss pin was triggered
  170. #if PIN_EXISTS(POWER_LOSS)
  171. if (READ(POWER_LOSS_PIN) == POWER_LOSS_STATE) kill(PSTR(MSG_OUTAGE_RECOVERY));
  172. #endif
  173. }
  174. }
  175. /**
  176. * Save the recovery info the recovery file
  177. */
  178. void PrintJobRecovery::write() {
  179. #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
  180. debug(PSTR("Write"));
  181. #endif
  182. open(false);
  183. file.seekSet(0);
  184. const int16_t ret = file.write(&info, sizeof(info));
  185. #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
  186. if (ret == -1) SERIAL_ECHOLNPGM("Power-loss file write failed.");
  187. #else
  188. UNUSED(ret);
  189. #endif
  190. }
  191. /**
  192. * Resume the saved print job
  193. */
  194. void PrintJobRecovery::resume() {
  195. #define RECOVERY_ZRAISE 2
  196. #if HAS_LEVELING
  197. // Make sure leveling is off before any G92 and G28
  198. gcode.process_subcommands_now_P(PSTR("M420 S0 Z0"));
  199. #endif
  200. // Set Z to 0, raise Z by 2mm, and Home (XY only for Cartesian) with no raise
  201. // (Only do simulated homing in Marlin Dev Mode.)
  202. gcode.process_subcommands_now_P(PSTR("G92.0 Z0\nG1 Z" STRINGIFY(RECOVERY_ZRAISE) "\nG28 R0"
  203. #if ENABLED(MARLIN_DEV_MODE)
  204. " S"
  205. #elif !IS_KINEMATIC
  206. " X Y"
  207. #endif
  208. ));
  209. // Pretend that all axes are homed
  210. axis_homed = axis_known_position = xyz_bits;
  211. char cmd[40], str_1[16], str_2[16];
  212. // Select the previously active tool (with no_move)
  213. #if EXTRUDERS > 1
  214. sprintf_P(cmd, PSTR("T%i S"), info.active_hotend);
  215. gcode.process_subcommands_now(cmd);
  216. #endif
  217. #if HAS_HEATED_BED
  218. const int16_t bt = info.target_temperature_bed;
  219. if (bt) {
  220. // Restore the bed temperature
  221. sprintf_P(cmd, PSTR("M190 S%i"), bt);
  222. gcode.process_subcommands_now(cmd);
  223. }
  224. #endif
  225. // Restore all hotend temperatures
  226. HOTEND_LOOP() {
  227. const int16_t et = info.target_temperature[e];
  228. if (et) {
  229. #if HOTENDS > 1
  230. sprintf_P(cmd, PSTR("T%i"), e);
  231. gcode.process_subcommands_now(cmd);
  232. #endif
  233. sprintf_P(cmd, PSTR("M109 S%i"), et);
  234. gcode.process_subcommands_now(cmd);
  235. }
  236. }
  237. // Restore print cooling fan speeds
  238. FANS_LOOP(i) {
  239. uint8_t f = info.fan_speed[i];
  240. if (f) {
  241. sprintf_P(cmd, PSTR("M106 P%i S%i"), i, f);
  242. gcode.process_subcommands_now(cmd);
  243. }
  244. }
  245. // Restore retract and hop state
  246. #if ENABLED(FWRETRACT)
  247. for (uint8_t e = 0; e < EXTRUDERS; e++) {
  248. if (info.retract[e] != 0.0)
  249. fwretract.current_retract[e] = info.retract[e];
  250. fwretract.retracted[e] = true;
  251. }
  252. fwretract.current_hop = info.retract_hop;
  253. #endif
  254. #if HAS_LEVELING
  255. // Restore leveling state before 'G92 Z' to ensure
  256. // the Z stepper count corresponds to the native Z.
  257. if (info.fade || info.leveling) {
  258. dtostrf(info.fade, 1, 1, str_1);
  259. sprintf_P(cmd, PSTR("M420 S%i Z%s"), int(info.leveling), str_1);
  260. gcode.process_subcommands_now(cmd);
  261. }
  262. #endif
  263. #if ENABLED(GRADIENT_MIX)
  264. memcpy(&mixer.gradient, &info.gradient, sizeof(info.gradient));
  265. #endif
  266. // Restore Z (plus raise) and E positions with G92.0
  267. dtostrf(info.current_position[Z_AXIS] + RECOVERY_ZRAISE, 1, 3, str_1);
  268. dtostrf(info.current_position[E_AXIS]
  269. #if ENABLED(SAVE_EACH_CMD_MODE)
  270. - 5 // Extra extrusion on restart
  271. #endif
  272. , 1, 3, str_2
  273. );
  274. sprintf_P(cmd, PSTR("G92.0 Z%s E%s"), str_1, str_2);
  275. gcode.process_subcommands_now(cmd);
  276. // Move back to the saved XY
  277. dtostrf(info.current_position[X_AXIS], 1, 3, str_1);
  278. dtostrf(info.current_position[Y_AXIS], 1, 3, str_2);
  279. sprintf_P(cmd, PSTR("G1 X%s Y%s F3000"), str_1, str_2);
  280. gcode.process_subcommands_now(cmd);
  281. // Move back to the saved Z
  282. dtostrf(info.current_position[Z_AXIS], 1, 3, str_1);
  283. sprintf_P(cmd, PSTR("G1 Z%s F200"), str_1);
  284. gcode.process_subcommands_now(cmd);
  285. // Restore the feedrate
  286. sprintf_P(cmd, PSTR("G1 F%d"), info.feedrate);
  287. gcode.process_subcommands_now(cmd);
  288. // Process commands from the old pending queue
  289. uint8_t c = info.commands_in_queue, r = info.cmd_queue_index_r;
  290. for (; c--; r = (r + 1) % BUFSIZE)
  291. gcode.process_subcommands_now(info.command_queue[r]);
  292. // Resume the SD file from the last position
  293. char *fn = info.sd_filename;
  294. while (*fn == '/') fn++;
  295. sprintf_P(cmd, PSTR("M23 %s"), fn);
  296. gcode.process_subcommands_now(cmd);
  297. sprintf_P(cmd, PSTR("M24 S%ld T%ld"), info.sdpos, info.print_job_elapsed);
  298. gcode.process_subcommands_now(cmd);
  299. }
  300. #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
  301. void PrintJobRecovery::debug(PGM_P const prefix) {
  302. serialprintPGM(prefix);
  303. SERIAL_ECHOLNPAIR(" Job Recovery Info...\nvalid_head:", int(info.valid_head), " valid_foot:", int(info.valid_foot));
  304. if (info.valid_head) {
  305. if (info.valid_head == info.valid_foot) {
  306. SERIAL_ECHOPGM("current_position: ");
  307. LOOP_XYZE(i) {
  308. SERIAL_ECHO(info.current_position[i]);
  309. if (i < E_AXIS) SERIAL_CHAR(',');
  310. }
  311. SERIAL_EOL();
  312. SERIAL_ECHOLNPAIR("feedrate: ", info.feedrate);
  313. #if HOTENDS > 1
  314. SERIAL_ECHOLNPAIR("active_hotend: ", int(info.active_hotend));
  315. #endif
  316. SERIAL_ECHOPGM("target_temperature: ");
  317. HOTEND_LOOP() {
  318. SERIAL_ECHO(info.target_temperature[e]);
  319. if (e < HOTENDS - 1) SERIAL_CHAR(',');
  320. }
  321. SERIAL_EOL();
  322. #if HAS_HEATED_BED
  323. SERIAL_ECHOLNPAIR("target_temperature_bed: ", info.target_temperature_bed);
  324. #endif
  325. #if FAN_COUNT
  326. SERIAL_ECHOPGM("fan_speed: ");
  327. for (int8_t i = 0; i < FAN_COUNT; i++) {
  328. SERIAL_ECHO(int(info.fan_speed[i]));
  329. if (i < FAN_COUNT - 1) SERIAL_CHAR(',');
  330. }
  331. SERIAL_EOL();
  332. #endif
  333. #if HAS_LEVELING
  334. SERIAL_ECHOLNPAIR("leveling: ", int(info.leveling), "\n fade: ", int(info.fade));
  335. #endif
  336. #if ENABLED(FWRETRACT)
  337. SERIAL_ECHOPGM("retract: ");
  338. for (int8_t e = 0; e < EXTRUDERS; e++) {
  339. SERIAL_ECHO(info.retract[e]);
  340. if (e < EXTRUDERS - 1) SERIAL_CHAR(',');
  341. }
  342. SERIAL_EOL();
  343. SERIAL_ECHOLNPAIR("retract_hop: ", info.retract_hop);
  344. #endif
  345. SERIAL_ECHOLNPAIR("cmd_queue_index_r: ", int(info.cmd_queue_index_r));
  346. SERIAL_ECHOLNPAIR("commands_in_queue: ", int(info.commands_in_queue));
  347. for (uint8_t i = 0; i < info.commands_in_queue; i++) SERIAL_ECHOLNPAIR("> ", info.command_queue[i]);
  348. SERIAL_ECHOLNPAIR("sd_filename: ", info.sd_filename);
  349. SERIAL_ECHOLNPAIR("sdpos: ", info.sdpos);
  350. SERIAL_ECHOLNPAIR("print_job_elapsed: ", info.print_job_elapsed);
  351. }
  352. else
  353. SERIAL_ECHOLNPGM("INVALID DATA");
  354. }
  355. SERIAL_ECHOLNPGM("---");
  356. }
  357. #endif // DEBUG_POWER_LOSS_RECOVERY
  358. #endif // POWER_LOSS_RECOVERY