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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. #define DEBUG_OUT ENABLED(DEBUG_POWER_LOSS_RECOVERY)
  45. #include "../core/debug_out.h"
  46. PrintJobRecovery recovery;
  47. #ifndef POWER_LOSS_PURGE_LEN
  48. #define POWER_LOSS_PURGE_LEN 0
  49. #endif
  50. #ifndef POWER_LOSS_RETRACT_LEN
  51. #define POWER_LOSS_RETRACT_LEN 0
  52. #endif
  53. /**
  54. * Clear the recovery info
  55. */
  56. void PrintJobRecovery::init() { memset(&info, 0, sizeof(info)); }
  57. /**
  58. * Enable or disable then call changed()
  59. */
  60. void PrintJobRecovery::enable(const bool onoff) {
  61. enabled = onoff;
  62. changed();
  63. }
  64. /**
  65. * The enabled state was changed:
  66. * - Enabled: Purge the job recovery file
  67. * - Disabled: Write the job recovery file
  68. */
  69. void PrintJobRecovery::changed() {
  70. if (!enabled)
  71. purge();
  72. else if (IS_SD_PRINTING())
  73. save(true);
  74. }
  75. /**
  76. * Check for Print Job Recovery during setup()
  77. *
  78. * If a saved state exists send 'M1000 S' to initiate job recovery.
  79. */
  80. void PrintJobRecovery::check() {
  81. if (enabled) {
  82. if (!card.isDetected()) card.initsd();
  83. if (card.isDetected()) {
  84. load();
  85. if (!valid()) return purge();
  86. queue.inject_P(PSTR("M1000 S"));
  87. }
  88. }
  89. }
  90. /**
  91. * Delete the recovery file and clear the recovery data
  92. */
  93. void PrintJobRecovery::purge() {
  94. init();
  95. card.removeJobRecoveryFile();
  96. }
  97. /**
  98. * Load the recovery data, if it exists
  99. */
  100. void PrintJobRecovery::load() {
  101. if (exists()) {
  102. open(true);
  103. (void)file.read(&info, sizeof(info));
  104. close();
  105. }
  106. debug(PSTR("Load"));
  107. }
  108. /**
  109. * Save the current machine state to the power-loss recovery file
  110. */
  111. void PrintJobRecovery::save(const bool force/*=false*/, const bool save_queue/*=true*/) {
  112. #if SAVE_INFO_INTERVAL_MS > 0
  113. static millis_t next_save_ms; // = 0
  114. millis_t ms = millis();
  115. #endif
  116. #ifndef POWER_LOSS_MIN_Z_CHANGE
  117. #define POWER_LOSS_MIN_Z_CHANGE 0.05 // Vase-mode-friendly out of the box
  118. #endif
  119. // Did Z change since the last call?
  120. if (force
  121. #if DISABLED(SAVE_EACH_CMD_MODE) // Always save state when enabled
  122. #if PIN_EXISTS(POWER_LOSS) // Save if power loss pin is triggered
  123. || READ(POWER_LOSS_PIN) == POWER_LOSS_STATE
  124. #endif
  125. #if SAVE_INFO_INTERVAL_MS > 0 // Save if interval is elapsed
  126. || ELAPSED(ms, next_save_ms)
  127. #endif
  128. // Save if Z is above the last-saved position by some minimum height
  129. || current_position[Z_AXIS] > info.current_position[Z_AXIS] + POWER_LOSS_MIN_Z_CHANGE
  130. #endif
  131. ) {
  132. #if SAVE_INFO_INTERVAL_MS > 0
  133. next_save_ms = ms + SAVE_INFO_INTERVAL_MS;
  134. #endif
  135. // Set Head and Foot to matching non-zero values
  136. if (!++info.valid_head) ++info.valid_head; // non-zero in sequence
  137. //if (!IS_SD_PRINTING()) info.valid_head = 0;
  138. info.valid_foot = info.valid_head;
  139. // Machine state
  140. COPY(info.current_position, current_position);
  141. #if HAS_HOME_OFFSET
  142. COPY(info.home_offset, home_offset);
  143. #endif
  144. #if HAS_POSITION_SHIFT
  145. COPY(info.position_shift, position_shift);
  146. #endif
  147. info.feedrate = uint16_t(feedrate_mm_s * 60.0f);
  148. #if EXTRUDERS > 1
  149. info.active_extruder = active_extruder;
  150. #endif
  151. HOTEND_LOOP() info.target_temperature[e] = thermalManager.temp_hotend[e].target;
  152. #if HAS_HEATED_BED
  153. info.target_temperature_bed = thermalManager.temp_bed.target;
  154. #endif
  155. #if FAN_COUNT
  156. COPY(info.fan_speed, thermalManager.fan_speed);
  157. #endif
  158. #if HAS_LEVELING
  159. info.leveling = planner.leveling_active;
  160. info.fade = (
  161. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  162. planner.z_fade_height
  163. #else
  164. 0
  165. #endif
  166. );
  167. #endif
  168. #if ENABLED(GRADIENT_MIX)
  169. memcpy(&info.gradient, &mixer.gradient, sizeof(info.gradient));
  170. #endif
  171. #if ENABLED(FWRETRACT)
  172. COPY(info.retract, fwretract.current_retract);
  173. info.retract_hop = fwretract.current_hop;
  174. #endif
  175. // Relative mode
  176. info.relative_mode = relative_mode;
  177. info.relative_modes_e = gcode.axis_relative_modes[E_AXIS];
  178. // Commands in the queue
  179. info.queue_length = save_queue ? queue.length : 0;
  180. info.queue_index_r = queue.index_r;
  181. COPY(info.queue_buffer, queue.buffer);
  182. // Elapsed print job time
  183. info.print_job_elapsed = print_job_timer.duration();
  184. // SD file position
  185. card.getAbsFilename(info.sd_filename);
  186. info.sdpos = card.getIndex();
  187. write();
  188. // KILL now if the power-loss pin was triggered
  189. #if PIN_EXISTS(POWER_LOSS)
  190. if (READ(POWER_LOSS_PIN) == POWER_LOSS_STATE) kill(PSTR(MSG_OUTAGE_RECOVERY));
  191. #endif
  192. }
  193. }
  194. /**
  195. * Save the recovery info the recovery file
  196. */
  197. void PrintJobRecovery::write() {
  198. debug(PSTR("Write"));
  199. open(false);
  200. file.seekSet(0);
  201. const int16_t ret = file.write(&info, sizeof(info));
  202. if (ret == -1) DEBUG_ECHOLNPGM("Power-loss file write failed.");
  203. if (!file.close()) DEBUG_ECHOLNPGM("Power-loss file close failed.");
  204. }
  205. /**
  206. * Resume the saved print job
  207. */
  208. void PrintJobRecovery::resume() {
  209. #define RECOVERY_ZRAISE 2
  210. #if HAS_LEVELING
  211. // Make sure leveling is off before any G92 and G28
  212. gcode.process_subcommands_now_P(PSTR("M420 S0 Z0"));
  213. #endif
  214. // Reset E, raise Z, home XY...
  215. gcode.process_subcommands_now_P(PSTR("G92.9 E0"
  216. #if Z_HOME_DIR > 0
  217. // If Z homing goes to max, reset E and home all
  218. "\nG28R0"
  219. #if ENABLED(MARLIN_DEV_MODE)
  220. "S"
  221. #endif
  222. #else
  223. // Set Z to 0, raise Z by RECOVERY_ZRAISE, and Home (XY only for Cartesian)
  224. // with no raise. (Only do simulated homing in Marlin Dev Mode.)
  225. "Z0\nG1Z" STRINGIFY(RECOVERY_ZRAISE) "\nG28R0"
  226. #if ENABLED(MARLIN_DEV_MODE)
  227. "S"
  228. #elif !IS_KINEMATIC
  229. "XY"
  230. #endif
  231. #endif
  232. ));
  233. // Pretend that all axes are homed
  234. axis_homed = axis_known_position = xyz_bits;
  235. char cmd[MAX_CMD_SIZE+16], str_1[16], str_2[16];
  236. // Select the previously active tool (with no_move)
  237. #if EXTRUDERS > 1
  238. sprintf_P(cmd, PSTR("T%i S"), info.active_extruder);
  239. gcode.process_subcommands_now(cmd);
  240. #endif
  241. #if HAS_HEATED_BED
  242. const int16_t bt = info.target_temperature_bed;
  243. if (bt) {
  244. // Restore the bed temperature
  245. sprintf_P(cmd, PSTR("M190 S%i"), bt);
  246. gcode.process_subcommands_now(cmd);
  247. }
  248. #endif
  249. // Restore all hotend temperatures
  250. HOTEND_LOOP() {
  251. const int16_t et = info.target_temperature[e];
  252. if (et) {
  253. #if HOTENDS > 1
  254. sprintf_P(cmd, PSTR("T%i"), e);
  255. gcode.process_subcommands_now(cmd);
  256. #endif
  257. sprintf_P(cmd, PSTR("M109 S%i"), et);
  258. gcode.process_subcommands_now(cmd);
  259. }
  260. }
  261. // Restore print cooling fan speeds
  262. FANS_LOOP(i) {
  263. uint8_t f = info.fan_speed[i];
  264. if (f) {
  265. sprintf_P(cmd, PSTR("M106 P%i S%i"), i, f);
  266. gcode.process_subcommands_now(cmd);
  267. }
  268. }
  269. // Restore retract and hop state
  270. #if ENABLED(FWRETRACT)
  271. for (uint8_t e = 0; e < EXTRUDERS; e++) {
  272. if (info.retract[e] != 0.0)
  273. fwretract.current_retract[e] = info.retract[e];
  274. fwretract.retracted[e] = true;
  275. }
  276. fwretract.current_hop = info.retract_hop;
  277. #endif
  278. #if HAS_LEVELING
  279. // Restore leveling state before 'G92 Z' to ensure
  280. // the Z stepper count corresponds to the native Z.
  281. if (info.fade || info.leveling) {
  282. dtostrf(info.fade, 1, 1, str_1);
  283. sprintf_P(cmd, PSTR("M420 S%i Z%s"), int(info.leveling), str_1);
  284. gcode.process_subcommands_now(cmd);
  285. }
  286. #endif
  287. #if ENABLED(GRADIENT_MIX)
  288. memcpy(&mixer.gradient, &info.gradient, sizeof(info.gradient));
  289. #endif
  290. // Extrude and retract to clean the nozzle
  291. #if POWER_LOSS_PURGE_LEN
  292. //sprintf_P(cmd, PSTR("G1 E%d F200"), POWER_LOSS_PURGE_LEN);
  293. //gcode.process_subcommands_now(cmd);
  294. gcode.process_subcommands_now_P(PSTR("G1 E" STRINGIFY(POWER_LOSS_PURGE_LEN) " F200"));
  295. #endif
  296. #if POWER_LOSS_RETRACT_LEN
  297. sprintf_P(cmd, PSTR("G1 E%d F3000"), POWER_LOSS_PURGE_LEN - (POWER_LOSS_RETRACT_LEN));
  298. gcode.process_subcommands_now(cmd);
  299. #endif
  300. // Move back to the saved XY
  301. dtostrf(info.current_position[X_AXIS], 1, 3, str_1);
  302. dtostrf(info.current_position[Y_AXIS], 1, 3, str_2);
  303. sprintf_P(cmd, PSTR("G1 X%s Y%s F3000"), str_1, str_2);
  304. gcode.process_subcommands_now(cmd);
  305. // Move back to the saved Z
  306. dtostrf(info.current_position[Z_AXIS], 1, 3, str_1);
  307. #if Z_HOME_DIR > 0
  308. sprintf_P(cmd, PSTR("G1 Z%s F200"), str_1);
  309. #else
  310. gcode.process_subcommands_now_P(PSTR("G1 Z0 F200"));
  311. sprintf_P(cmd, PSTR("G92.9 Z%s"), str_1);
  312. #endif
  313. gcode.process_subcommands_now(cmd);
  314. // Un-retract
  315. #if POWER_LOSS_PURGE_LEN
  316. //sprintf_P(cmd, PSTR("G1 E%d F3000"), POWER_LOSS_PURGE_LEN);
  317. //gcode.process_subcommands_now(cmd);
  318. gcode.process_subcommands_now_P(PSTR("G1 E" STRINGIFY(POWER_LOSS_PURGE_LEN) " F3000"));
  319. #endif
  320. // Restore the feedrate
  321. sprintf_P(cmd, PSTR("G1 F%d"), info.feedrate);
  322. gcode.process_subcommands_now(cmd);
  323. // Restore E position with G92.9
  324. dtostrf(info.current_position[E_AXIS], 1, 3, str_1);
  325. sprintf_P(cmd, PSTR("G92.9 E%s"), str_1);
  326. gcode.process_subcommands_now(cmd);
  327. // Relative mode
  328. relative_mode = info.relative_mode;
  329. gcode.axis_relative_modes[E_AXIS] = info.relative_modes_e;
  330. #if HAS_HOME_OFFSET || HAS_POSITION_SHIFT
  331. LOOP_XYZ(i) {
  332. #if HAS_HOME_OFFSET
  333. home_offset[i] = info.home_offset[i];
  334. #endif
  335. #if HAS_POSITION_SHIFT
  336. position_shift[i] = info.position_shift[i];
  337. #endif
  338. update_workspace_offset((AxisEnum)i);
  339. }
  340. #endif
  341. // Process commands from the old pending queue
  342. uint8_t c = info.queue_length, r = info.queue_index_r;
  343. for (; c--; r = (r + 1) % BUFSIZE)
  344. gcode.process_subcommands_now(info.queue_buffer[r]);
  345. // Resume the SD file from the last position
  346. char *fn = info.sd_filename;
  347. sprintf_P(cmd, PSTR("M23 %s"), fn);
  348. gcode.process_subcommands_now(cmd);
  349. sprintf_P(cmd, PSTR("M24 S%ld T%ld"), info.sdpos, info.print_job_elapsed);
  350. gcode.process_subcommands_now(cmd);
  351. }
  352. #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
  353. void PrintJobRecovery::debug(PGM_P const prefix) {
  354. DEBUG_PRINT_P(prefix);
  355. DEBUG_ECHOLNPAIR(" Job Recovery Info...\nvalid_head:", int(info.valid_head), " valid_foot:", int(info.valid_foot));
  356. if (info.valid_head) {
  357. if (info.valid_head == info.valid_foot) {
  358. DEBUG_ECHOPGM("current_position: ");
  359. LOOP_XYZE(i) {
  360. if (i) DEBUG_CHAR(',');
  361. DEBUG_ECHO(info.current_position[i]);
  362. }
  363. DEBUG_EOL();
  364. #if HAS_HOME_OFFSET
  365. DEBUG_ECHOPGM("home_offset: ");
  366. LOOP_XYZ(i) {
  367. if (i) DEBUG_CHAR(',');
  368. DEBUG_ECHO(info.home_offset[i]);
  369. }
  370. DEBUG_EOL();
  371. #endif
  372. #if HAS_POSITION_SHIFT
  373. DEBUG_ECHOPGM("position_shift: ");
  374. LOOP_XYZ(i) {
  375. if (i) DEBUG_CHAR(',');
  376. DEBUG_ECHO(info.position_shift[i]);
  377. }
  378. DEBUG_EOL();
  379. #endif
  380. DEBUG_ECHOLNPAIR("feedrate: ", info.feedrate);
  381. #if EXTRUDERS > 1
  382. DEBUG_ECHOLNPAIR("active_extruder: ", int(info.active_extruder));
  383. #endif
  384. DEBUG_ECHOPGM("target_temperature: ");
  385. HOTEND_LOOP() {
  386. DEBUG_ECHO(info.target_temperature[e]);
  387. if (e < HOTENDS - 1) DEBUG_CHAR(',');
  388. }
  389. DEBUG_EOL();
  390. #if HAS_HEATED_BED
  391. DEBUG_ECHOLNPAIR("target_temperature_bed: ", info.target_temperature_bed);
  392. #endif
  393. #if FAN_COUNT
  394. DEBUG_ECHOPGM("fan_speed: ");
  395. FANS_LOOP(i) {
  396. DEBUG_ECHO(int(info.fan_speed[i]));
  397. if (i < FAN_COUNT - 1) DEBUG_CHAR(',');
  398. }
  399. DEBUG_EOL();
  400. #endif
  401. #if HAS_LEVELING
  402. DEBUG_ECHOLNPAIR("leveling: ", int(info.leveling), "\n fade: ", int(info.fade));
  403. #endif
  404. #if ENABLED(FWRETRACT)
  405. DEBUG_ECHOPGM("retract: ");
  406. for (int8_t e = 0; e < EXTRUDERS; e++) {
  407. DEBUG_ECHO(info.retract[e]);
  408. if (e < EXTRUDERS - 1) DEBUG_CHAR(',');
  409. }
  410. DEBUG_EOL();
  411. DEBUG_ECHOLNPAIR("retract_hop: ", info.retract_hop);
  412. #endif
  413. DEBUG_ECHOLNPAIR("queue_index_r: ", int(info.queue_index_r));
  414. DEBUG_ECHOLNPAIR("queue_length: ", int(info.queue_length));
  415. for (uint8_t i = 0; i < info.queue_length; i++) DEBUG_ECHOLNPAIR("> ", info.queue_buffer[i]);
  416. DEBUG_ECHOLNPAIR("sd_filename: ", info.sd_filename);
  417. DEBUG_ECHOLNPAIR("sdpos: ", info.sdpos);
  418. DEBUG_ECHOLNPAIR("print_job_elapsed: ", info.print_job_elapsed);
  419. }
  420. else
  421. DEBUG_ECHOLNPGM("INVALID DATA");
  422. }
  423. DEBUG_ECHOLNPGM("---");
  424. }
  425. #endif // DEBUG_POWER_LOSS_RECOVERY
  426. #endif // POWER_LOSS_RECOVERY