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

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