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.

powerloss.cpp 15KB

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