My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

power_loss_recovery.cpp 15KB

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