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.

pause.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. * feature/pause.cpp - Pause feature support functions
  24. * This may be combined with related G-codes if features are consolidated.
  25. */
  26. #include "../inc/MarlinConfig.h"
  27. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  28. #include "../Marlin.h"
  29. #include "../gcode/gcode.h"
  30. #include "../module/motion.h"
  31. #include "../module/planner.h"
  32. #include "../module/stepper.h"
  33. #include "../module/printcounter.h"
  34. #include "../module/temperature.h"
  35. #if ENABLED(FWRETRACT)
  36. #include "../feature/fwretract.h"
  37. #endif
  38. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  39. #include "../feature/runout.h"
  40. #endif
  41. #if ENABLED(ULTIPANEL)
  42. #include "../lcd/ultralcd.h"
  43. #endif
  44. #include "../libs/buzzer.h"
  45. #include "../libs/nozzle.h"
  46. #include "pause.h"
  47. // private:
  48. static float resume_position[XYZE];
  49. AdvancedPauseMenuResponse advanced_pause_menu_response;
  50. float filament_change_unload_length[EXTRUDERS],
  51. filament_change_load_length[EXTRUDERS];
  52. #if ENABLED(SDSUPPORT)
  53. #include "../sd/cardreader.h"
  54. #endif
  55. #if HAS_BUZZER
  56. static void filament_change_beep(const int8_t max_beep_count, const bool init=false) {
  57. static millis_t next_buzz = 0;
  58. static int8_t runout_beep = 0;
  59. if (init) next_buzz = runout_beep = 0;
  60. const millis_t ms = millis();
  61. if (ELAPSED(ms, next_buzz)) {
  62. if (max_beep_count < 0 || runout_beep < max_beep_count + 5) { // Only beep as long as we're supposed to
  63. next_buzz = ms + ((max_beep_count < 0 || runout_beep < max_beep_count) ? 1000 : 500);
  64. BUZZ(50, 880 - (runout_beep & 1) * 220);
  65. runout_beep++;
  66. }
  67. }
  68. }
  69. #endif
  70. /**
  71. * Ensure a safe temperature for extrusion
  72. *
  73. * - Fail if the TARGET temperature is too low
  74. * - Display LCD placard with temperature status
  75. * - Return when heating is done or aborted
  76. *
  77. * Returns 'true' if heating was completed, 'false' for abort
  78. */
  79. static bool ensure_safe_temperature(const AdvancedPauseMode mode=ADVANCED_PAUSE_MODE_PAUSE_PRINT) {
  80. #if ENABLED(PREVENT_COLD_EXTRUSION)
  81. if (!DEBUGGING(DRYRUN) && thermalManager.targetTooColdToExtrude(active_extruder)) {
  82. SERIAL_ERROR_START();
  83. SERIAL_ERRORLNPGM(MSG_HOTEND_TOO_COLD);
  84. return false;
  85. }
  86. #endif
  87. #if ENABLED(ULTIPANEL)
  88. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_WAIT_FOR_NOZZLES_TO_HEAT, mode);
  89. #else
  90. UNUSED(mode);
  91. #endif
  92. wait_for_heatup = true; // M108 will clear this
  93. while (wait_for_heatup && thermalManager.wait_for_heating(active_extruder)) idle();
  94. const bool status = wait_for_heatup;
  95. wait_for_heatup = false;
  96. return status;
  97. }
  98. static void do_pause_e_move(const float &length, const float &fr) {
  99. set_destination_from_current();
  100. destination[E_AXIS] += length / planner.e_factor[active_extruder];
  101. planner.buffer_line_kinematic(destination, fr, active_extruder);
  102. stepper.synchronize();
  103. set_current_from_destination();
  104. }
  105. /**
  106. * Load filament into the hotend
  107. *
  108. * - Fail if the a safe temperature was not reached
  109. * - If pausing for confirmation, wait for a click or M108
  110. * - Show "wait for load" placard
  111. * - Load and purge filament
  112. * - Show "Purge more" / "Continue" menu
  113. * - Return when "Continue" is selected
  114. *
  115. * Returns 'true' if load was completed, 'false' for abort
  116. */
  117. bool load_filament(const float &load_length/*=0*/, const float &purge_length/*=0*/, const int8_t max_beep_count/*=0*/,
  118. const bool show_lcd/*=false*/, const bool pause_for_user/*=false*/,
  119. const AdvancedPauseMode mode/*=ADVANCED_PAUSE_MODE_PAUSE_PRINT*/
  120. ) {
  121. #if DISABLED(ULTIPANEL)
  122. UNUSED(show_lcd);
  123. #endif
  124. if (!ensure_safe_temperature(mode)) {
  125. #if ENABLED(ULTIPANEL)
  126. if (show_lcd) // Show status screen
  127. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_STATUS);
  128. #endif
  129. return false;
  130. }
  131. if (pause_for_user) {
  132. #if ENABLED(ULTIPANEL)
  133. if (show_lcd) // Show "insert filament"
  134. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_INSERT, mode);
  135. #endif
  136. SERIAL_ECHO_START();
  137. SERIAL_ECHOLNPGM(MSG_FILAMENT_CHANGE_INSERT);
  138. #if HAS_BUZZER
  139. filament_change_beep(max_beep_count, true);
  140. #else
  141. UNUSED(max_beep_count);
  142. #endif
  143. KEEPALIVE_STATE(PAUSED_FOR_USER);
  144. wait_for_user = true; // LCD click or M108 will clear this
  145. while (wait_for_user) {
  146. #if HAS_BUZZER
  147. filament_change_beep(max_beep_count);
  148. #endif
  149. idle(true);
  150. }
  151. KEEPALIVE_STATE(IN_HANDLER);
  152. }
  153. #if ENABLED(ULTIPANEL)
  154. if (show_lcd) // Show "wait for load" message
  155. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_LOAD, mode);
  156. #endif
  157. // Load filament
  158. if (load_length) do_pause_e_move(load_length, FILAMENT_CHANGE_LOAD_FEEDRATE);
  159. do {
  160. if (purge_length > 0) {
  161. // "Wait for filament purge"
  162. #if ENABLED(ULTIPANEL)
  163. if (show_lcd)
  164. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_PURGE, mode);
  165. #endif
  166. // Extrude filament to get into hotend
  167. do_pause_e_move(purge_length, ADVANCED_PAUSE_EXTRUDE_FEEDRATE);
  168. }
  169. // Show "Purge More" / "Resume" menu and wait for reply
  170. #if ENABLED(ULTIPANEL)
  171. if (show_lcd) {
  172. KEEPALIVE_STATE(PAUSED_FOR_USER);
  173. wait_for_user = false;
  174. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_OPTION, mode);
  175. while (advanced_pause_menu_response == ADVANCED_PAUSE_RESPONSE_WAIT_FOR) idle(true);
  176. KEEPALIVE_STATE(IN_HANDLER);
  177. }
  178. #endif
  179. // Keep looping if "Purge More" was selected
  180. } while (
  181. #if ENABLED(ULTIPANEL)
  182. show_lcd && advanced_pause_menu_response == ADVANCED_PAUSE_RESPONSE_EXTRUDE_MORE
  183. #else
  184. 0
  185. #endif
  186. );
  187. return true;
  188. }
  189. /**
  190. * Unload filament from the hotend
  191. *
  192. * - Fail if the a safe temperature was not reached
  193. * - Show "wait for unload" placard
  194. * - Retract, pause, then unload filament
  195. * - Disable E stepper (on most machines)
  196. *
  197. * Returns 'true' if unload was completed, 'false' for abort
  198. */
  199. bool unload_filament(const float &unload_length, const bool show_lcd/*=false*/,
  200. const AdvancedPauseMode mode/*=ADVANCED_PAUSE_MODE_PAUSE_PRINT*/
  201. ) {
  202. if (!ensure_safe_temperature(mode)) {
  203. #if ENABLED(ULTIPANEL)
  204. if (show_lcd) // Show status screen
  205. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_STATUS);
  206. #endif
  207. return false;
  208. }
  209. #if DISABLED(ULTIPANEL)
  210. UNUSED(show_lcd);
  211. #else
  212. if (show_lcd)
  213. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_UNLOAD, mode);
  214. #endif
  215. // Retract filament
  216. do_pause_e_move(-FILAMENT_UNLOAD_RETRACT_LENGTH, PAUSE_PARK_RETRACT_FEEDRATE);
  217. // Wait for filament to cool
  218. safe_delay(FILAMENT_UNLOAD_DELAY);
  219. // Quickly purge
  220. do_pause_e_move(FILAMENT_UNLOAD_RETRACT_LENGTH + FILAMENT_UNLOAD_PURGE_LENGTH, planner.max_feedrate_mm_s[E_AXIS]);
  221. // Unload filament
  222. do_pause_e_move(unload_length, FILAMENT_CHANGE_UNLOAD_FEEDRATE);
  223. // Disable extruders steppers for manual filament changing (only on boards that have separate ENABLE_PINS)
  224. #if E0_ENABLE_PIN != X_ENABLE_PIN && E1_ENABLE_PIN != Y_ENABLE_PIN
  225. disable_e_stepper(active_extruder);
  226. safe_delay(100);
  227. #endif
  228. return true;
  229. }
  230. // public:
  231. /**
  232. * Pause procedure
  233. *
  234. * - Abort if already paused
  235. * - Send host action for pause, if configured
  236. * - Abort if TARGET temperature is too low
  237. * - Display "wait for start of filament change" (if a length was specified)
  238. * - Initial retract, if current temperature is hot enough
  239. * - Park the nozzle at the given position
  240. * - Call unload_filament (if a length was specified)
  241. *
  242. * Returns 'true' if pause was completed, 'false' for abort
  243. */
  244. uint8_t did_pause_print = 0;
  245. bool pause_print(const float &retract, const point_t &park_point, const float &unload_length/*=0*/, const bool show_lcd/*=false*/) {
  246. if (did_pause_print) return false; // already paused
  247. #ifdef ACTION_ON_PAUSE
  248. SERIAL_ECHOLNPGM("//action:" ACTION_ON_PAUSE);
  249. #endif
  250. #if ENABLED(ULTIPANEL)
  251. if (show_lcd) // Show initial message
  252. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_INIT);
  253. #endif
  254. if (!DEBUGGING(DRYRUN) && unload_length && thermalManager.targetTooColdToExtrude(active_extruder)) {
  255. SERIAL_ERROR_START();
  256. SERIAL_ERRORLNPGM(MSG_HOTEND_TOO_COLD);
  257. #if ENABLED(ULTIPANEL)
  258. if (show_lcd) { // Show status screen
  259. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_STATUS);
  260. LCD_MESSAGEPGM(MSG_M600_TOO_COLD);
  261. }
  262. #endif
  263. return false; // unable to reach safe temperature
  264. }
  265. // Indicate that the printer is paused
  266. ++did_pause_print;
  267. // Pause the print job and timer
  268. #if ENABLED(SDSUPPORT)
  269. if (card.sdprinting) {
  270. card.pauseSDPrint();
  271. ++did_pause_print; // Indicate SD pause also
  272. }
  273. #endif
  274. print_job_timer.pause();
  275. // Wait for synchronize steppers
  276. stepper.synchronize();
  277. // Save current position
  278. COPY(resume_position, current_position);
  279. // Initial retract before move to filament change position
  280. if (retract && thermalManager.hotEnoughToExtrude(active_extruder))
  281. do_pause_e_move(retract, PAUSE_PARK_RETRACT_FEEDRATE);
  282. #if ENABLED(NO_MOTION_BEFORE_HOMING)
  283. if (!axis_unhomed_error())
  284. #endif
  285. // Park the nozzle by moving up by z_lift and then moving to (x_pos, y_pos)
  286. Nozzle::park(2, park_point);
  287. // Unload the filament
  288. if (unload_length)
  289. unload_filament(unload_length, show_lcd);
  290. return true;
  291. }
  292. /**
  293. * - Show "Insert filament and press button to continue"
  294. * - Wait for a click before returning
  295. * - Heaters can time out, reheated before accepting a click
  296. *
  297. * Used by M125 and M600
  298. */
  299. void wait_for_filament_reload(const int8_t max_beep_count/*=0*/) {
  300. bool nozzle_timed_out = false;
  301. #if ENABLED(ULTIPANEL)
  302. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_INSERT);
  303. #endif
  304. SERIAL_ECHO_START();
  305. SERIAL_ERRORLNPGM(MSG_FILAMENT_CHANGE_INSERT);
  306. #if HAS_BUZZER
  307. filament_change_beep(max_beep_count, true);
  308. #endif
  309. // Start the heater idle timers
  310. const millis_t nozzle_timeout = (millis_t)(PAUSE_PARK_NOZZLE_TIMEOUT) * 1000UL;
  311. HOTEND_LOOP()
  312. thermalManager.start_heater_idle_timer(e, nozzle_timeout);
  313. // Wait for filament insert by user and press button
  314. KEEPALIVE_STATE(PAUSED_FOR_USER);
  315. wait_for_user = true; // LCD click or M108 will clear this
  316. while (wait_for_user) {
  317. #if HAS_BUZZER
  318. filament_change_beep(max_beep_count);
  319. #endif
  320. // If the nozzle has timed out, wait for the user to press the button to re-heat the nozzle, then
  321. // re-heat the nozzle, re-show the insert screen, restart the idle timers, and start over
  322. if (!nozzle_timed_out)
  323. HOTEND_LOOP()
  324. nozzle_timed_out |= thermalManager.is_heater_idle(e);
  325. if (nozzle_timed_out) {
  326. #if ENABLED(ULTIPANEL)
  327. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_CLICK_TO_HEAT_NOZZLE);
  328. #endif
  329. SERIAL_ECHO_START();
  330. #if ENABLED(ULTIPANEL) && ENABLED(EMERGENCY_PARSER)
  331. SERIAL_ERRORLNPGM(MSG_FILAMENT_CHANGE_HEAT);
  332. #elif ENABLED(EMERGENCY_PARSER)
  333. SERIAL_ERRORLNPGM(MSG_FILAMENT_CHANGE_HEAT_M108);
  334. #else
  335. SERIAL_ERRORLNPGM(MSG_FILAMENT_CHANGE_HEAT_LCD);
  336. #endif
  337. // Wait for LCD click or M108
  338. while (wait_for_user) idle(true);
  339. // Re-enable the heaters if they timed out
  340. HOTEND_LOOP() thermalManager.reset_heater_idle_timer(e);
  341. // Wait for the heaters to reach the target temperatures
  342. ensure_safe_temperature();
  343. #if ENABLED(ULTIPANEL)
  344. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_INSERT);
  345. #endif
  346. SERIAL_ECHO_START();
  347. #if ENABLED(ULTIPANEL) && ENABLED(EMERGENCY_PARSER)
  348. SERIAL_ERRORLNPGM(MSG_FILAMENT_CHANGE_INSERT);
  349. #elif ENABLED(EMERGENCY_PARSER)
  350. SERIAL_ERRORLNPGM(MSG_FILAMENT_CHANGE_INSERT_M108);
  351. #else
  352. SERIAL_ERRORLNPGM(MSG_FILAMENT_CHANGE_INSERT_LCD);
  353. #endif
  354. // Start the heater idle timers
  355. const millis_t nozzle_timeout = (millis_t)(PAUSE_PARK_NOZZLE_TIMEOUT) * 1000UL;
  356. HOTEND_LOOP()
  357. thermalManager.start_heater_idle_timer(e, nozzle_timeout);
  358. wait_for_user = true; // Wait for user to load filament
  359. nozzle_timed_out = false;
  360. #if HAS_BUZZER
  361. filament_change_beep(max_beep_count, true);
  362. #endif
  363. }
  364. idle(true);
  365. }
  366. KEEPALIVE_STATE(IN_HANDLER);
  367. }
  368. /**
  369. * Resume or Start print procedure
  370. *
  371. * - Abort if not paused
  372. * - Reset heater idle timers
  373. * - Load filament if specified, but only if:
  374. * - a nozzle timed out, or
  375. * - the nozzle is already heated.
  376. * - Display "wait for print to resume"
  377. * - Re-prime the nozzle...
  378. * - FWRETRACT: Recover/prime from the prior G10.
  379. * - !FWRETRACT: Retract by resume_position[E], if negative.
  380. * Not sure how this logic comes into use.
  381. * - Move the nozzle back to resume_position
  382. * - Sync the planner E to resume_position[E]
  383. * - Send host action for resume, if configured
  384. * - Resume the current SD print job, if any
  385. */
  386. void resume_print(const float &load_length/*=0*/, const float &purge_length/*=ADVANCED_PAUSE_EXTRUDE_LENGTH*/, const int8_t max_beep_count/*=0*/) {
  387. if (!did_pause_print) return;
  388. // Re-enable the heaters if they timed out
  389. bool nozzle_timed_out = false;
  390. HOTEND_LOOP() {
  391. nozzle_timed_out |= thermalManager.is_heater_idle(e);
  392. thermalManager.reset_heater_idle_timer(e);
  393. }
  394. if (nozzle_timed_out || thermalManager.hotEnoughToExtrude(active_extruder)) {
  395. // Load the new filament
  396. load_filament(load_length, purge_length, max_beep_count, true, nozzle_timed_out);
  397. }
  398. #if ENABLED(ULTIPANEL)
  399. // "Wait for print to resume"
  400. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_RESUME);
  401. #endif
  402. // Intelligent resuming
  403. #if ENABLED(FWRETRACT)
  404. // If retracted before goto pause
  405. if (fwretract.retracted[active_extruder])
  406. do_pause_e_move(-fwretract.retract_length, fwretract.retract_feedrate_mm_s);
  407. #endif
  408. // If resume_position is negative
  409. if (resume_position[E_AXIS] < 0) do_pause_e_move(resume_position[E_AXIS], PAUSE_PARK_RETRACT_FEEDRATE);
  410. // Move XY to starting position, then Z
  411. do_blocking_move_to_xy(resume_position[X_AXIS], resume_position[Y_AXIS], NOZZLE_PARK_XY_FEEDRATE);
  412. // Set Z_AXIS to saved position
  413. do_blocking_move_to_z(resume_position[Z_AXIS], NOZZLE_PARK_Z_FEEDRATE);
  414. // Now all extrusion positions are resumed and ready to be confirmed
  415. // Set extruder to saved position
  416. planner.set_e_position_mm(destination[E_AXIS] = current_position[E_AXIS] = resume_position[E_AXIS]);
  417. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  418. runout.reset();
  419. #endif
  420. #if ENABLED(ULTIPANEL)
  421. // Show status screen
  422. lcd_advanced_pause_show_message(ADVANCED_PAUSE_MESSAGE_STATUS);
  423. #endif
  424. #ifdef ACTION_ON_RESUME
  425. SERIAL_ECHOLNPGM("//action:" ACTION_ON_RESUME);
  426. #endif
  427. --did_pause_print;
  428. #if ENABLED(SDSUPPORT)
  429. if (did_pause_print) {
  430. card.startFileprint();
  431. --did_pause_print;
  432. }
  433. #endif
  434. }
  435. #endif // ADVANCED_PAUSE_FEATURE