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 15KB

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