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

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