MMU2 as standard serial device (#20771)
[Marlin.git] / Marlin / src / feature / mmu / mmu2.cpp
bloba4b7f257a9b86cefe50918541251b410a95f50b0
1 /**
2 * Marlin 3D Printer Firmware
3 * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
5 * Based on Sprinter and grbl.
6 * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
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.
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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 #include "../../inc/MarlinConfig.h"
25 #if HAS_PRUSA_MMU2
27 #include "mmu2.h"
28 #include "../../lcd/menu/menu_mmu2.h"
30 MMU2 mmu2;
32 #include "../../gcode/gcode.h"
33 #include "../../lcd/marlinui.h"
34 #include "../../libs/buzzer.h"
35 #include "../../libs/nozzle.h"
36 #include "../../module/temperature.h"
37 #include "../../module/planner.h"
38 #include "../../module/stepper/indirection.h"
39 #include "../../MarlinCore.h"
41 #if ENABLED(HOST_PROMPT_SUPPORT)
42 #include "../../feature/host_actions.h"
43 #endif
45 #if ENABLED(EXTENSIBLE_UI)
46 #include "../../lcd/extui/ui_api.h"
47 #endif
49 #define DEBUG_OUT ENABLED(MMU2_DEBUG)
50 #include "../../core/debug_out.h"
52 #define MMU_TODELAY 100
53 #define MMU_TIMEOUT 10
54 #define MMU_CMD_TIMEOUT 45000UL // 45s timeout for mmu commands (except P0)
55 #define MMU_P0_TIMEOUT 3000UL // Timeout for P0 command: 3seconds
57 #define MMU2_COMMAND(S) tx_str_P(PSTR(S "\n"))
59 #if ENABLED(MMU_EXTRUDER_SENSOR)
60 uint8_t mmu_idl_sens = 0;
61 static bool mmu_loading_flag = false;
62 #endif
64 #define MMU_CMD_NONE 0
65 #define MMU_CMD_T0 0x10
66 #define MMU_CMD_T1 0x11
67 #define MMU_CMD_T2 0x12
68 #define MMU_CMD_T3 0x13
69 #define MMU_CMD_T4 0x14
70 #define MMU_CMD_L0 0x20
71 #define MMU_CMD_L1 0x21
72 #define MMU_CMD_L2 0x22
73 #define MMU_CMD_L3 0x23
74 #define MMU_CMD_L4 0x24
75 #define MMU_CMD_C0 0x30
76 #define MMU_CMD_U0 0x40
77 #define MMU_CMD_E0 0x50
78 #define MMU_CMD_E1 0x51
79 #define MMU_CMD_E2 0x52
80 #define MMU_CMD_E3 0x53
81 #define MMU_CMD_E4 0x54
82 #define MMU_CMD_R0 0x60
83 #define MMU_CMD_F0 0x70
84 #define MMU_CMD_F1 0x71
85 #define MMU_CMD_F2 0x72
86 #define MMU_CMD_F3 0x73
87 #define MMU_CMD_F4 0x74
89 #define MMU_REQUIRED_FW_BUILDNR TERN(MMU2_MODE_12V, 132, 126)
91 #define MMU2_NO_TOOL 99
92 #define MMU_BAUD 115200
94 bool MMU2::enabled, MMU2::ready, MMU2::mmu_print_saved;
95 #if HAS_PRUSA_MMU2S
96 bool MMU2::mmu2s_triggered;
97 #endif
98 uint8_t MMU2::cmd, MMU2::cmd_arg, MMU2::last_cmd, MMU2::extruder;
99 int8_t MMU2::state = 0;
100 volatile int8_t MMU2::finda = 1;
101 volatile bool MMU2::finda_runout_valid;
102 int16_t MMU2::version = -1, MMU2::buildnr = -1;
103 millis_t MMU2::prev_request, MMU2::prev_P0_request;
104 char MMU2::rx_buffer[MMU_RX_SIZE], MMU2::tx_buffer[MMU_TX_SIZE];
106 struct E_Step {
107 float extrude; //!< extrude distance in mm
108 feedRate_t feedRate; //!< feed rate in mm/s
111 static constexpr E_Step
112 ramming_sequence[] PROGMEM = { MMU2_RAMMING_SEQUENCE }
113 , load_to_nozzle_sequence[] PROGMEM = { MMU2_LOAD_TO_NOZZLE_SEQUENCE }
114 #if HAS_PRUSA_MMU2S
115 , can_load_sequence[] PROGMEM = { MMU2_CAN_LOAD_SEQUENCE }
116 , can_load_increment_sequence[] PROGMEM = { MMU2_CAN_LOAD_INCREMENT_SEQUENCE }
117 #endif
120 MMU2::MMU2() {
121 rx_buffer[0] = '\0';
124 void MMU2::init() {
126 set_runout_valid(false);
128 #if PIN_EXISTS(MMU2_RST)
129 WRITE(MMU2_RST_PIN, HIGH);
130 SET_OUTPUT(MMU2_RST_PIN);
131 #endif
133 MMU2_SERIAL.begin(MMU_BAUD);
134 extruder = MMU2_NO_TOOL;
136 safe_delay(10);
137 reset();
138 rx_buffer[0] = '\0';
139 state = -1;
142 void MMU2::reset() {
143 DEBUG_ECHOLNPGM("MMU <= reset");
145 #if PIN_EXISTS(MMU2_RST)
146 WRITE(MMU2_RST_PIN, LOW);
147 safe_delay(20);
148 WRITE(MMU2_RST_PIN, HIGH);
149 #else
150 MMU2_COMMAND("X0"); // Send soft reset
151 #endif
154 uint8_t MMU2::get_current_tool() {
155 return extruder == MMU2_NO_TOOL ? -1 : extruder;
158 #if EITHER(HAS_PRUSA_MMU2S, MMU_EXTRUDER_SENSOR)
159 #define FILAMENT_PRESENT() (READ(FIL_RUNOUT1_PIN) != FIL_RUNOUT1_STATE)
160 #endif
162 void MMU2::mmu_loop() {
164 switch (state) {
166 case 0: break;
168 case -1:
169 if (rx_start()) {
170 DEBUG_ECHOLNPGM("MMU => 'start'");
171 DEBUG_ECHOLNPGM("MMU <= 'S1'");
173 MMU2_COMMAND("S1"); // Read Version
174 state = -2;
176 else if (millis() > 3000000) {
177 SERIAL_ECHOLNPGM("MMU not responding - DISABLED");
178 state = 0;
180 break;
182 case -2:
183 if (rx_ok()) {
184 sscanf(rx_buffer, "%huok\n", &version);
186 DEBUG_ECHOLNPAIR("MMU => ", version, "\nMMU <= 'S2'");
188 MMU2_COMMAND("S2"); // Read Build Number
189 state = -3;
191 break;
193 case -3:
194 if (rx_ok()) {
195 sscanf(rx_buffer, "%huok\n", &buildnr);
197 DEBUG_ECHOLNPAIR("MMU => ", buildnr);
199 check_version();
201 #if ENABLED(MMU2_MODE_12V)
202 DEBUG_ECHOLNPGM("MMU <= 'M1'");
204 MMU2_COMMAND("M1"); // Stealth Mode
205 state = -5;
207 #else
208 DEBUG_ECHOLNPGM("MMU <= 'P0'");
210 MMU2_COMMAND("P0"); // Read FINDA
211 state = -4;
212 #endif
214 break;
216 #if ENABLED(MMU2_MODE_12V)
217 case -5:
218 // response to M1
219 if (rx_ok()) {
220 DEBUG_ECHOLNPGM("MMU => ok");
222 DEBUG_ECHOLNPGM("MMU <= 'P0'");
224 MMU2_COMMAND("P0"); // Read FINDA
225 state = -4;
227 break;
228 #endif
230 case -4:
231 if (rx_ok()) {
232 sscanf(rx_buffer, "%hhuok\n", &finda);
234 DEBUG_ECHOLNPAIR("MMU => ", finda, "\nMMU - ENABLED");
236 enabled = true;
237 state = 1;
238 TERN_(HAS_PRUSA_MMU2S, mmu2s_triggered = false);
240 break;
242 case 1:
243 if (cmd) {
244 if (WITHIN(cmd, MMU_CMD_T0, MMU_CMD_T4)) {
245 // tool change
246 int filament = cmd - MMU_CMD_T0;
247 DEBUG_ECHOLNPAIR("MMU <= T", filament);
248 tx_printf_P(PSTR("T%d\n"), filament);
249 TERN_(MMU_EXTRUDER_SENSOR, mmu_idl_sens = 1); // enable idler sensor, if any
250 state = 3; // wait for response
252 else if (WITHIN(cmd, MMU_CMD_L0, MMU_CMD_L4)) {
253 // load
254 int filament = cmd - MMU_CMD_L0;
255 DEBUG_ECHOLNPAIR("MMU <= L", filament);
256 tx_printf_P(PSTR("L%d\n"), filament);
257 state = 3; // wait for response
259 else if (cmd == MMU_CMD_C0) {
260 // continue loading
261 DEBUG_ECHOLNPGM("MMU <= 'C0'");
262 MMU2_COMMAND("C0");
263 state = 3; // wait for response
265 else if (cmd == MMU_CMD_U0) {
266 // unload current
267 DEBUG_ECHOLNPGM("MMU <= 'U0'");
269 MMU2_COMMAND("U0");
270 state = 3; // wait for response
272 else if (WITHIN(cmd, MMU_CMD_E0, MMU_CMD_E4)) {
273 // eject filament
274 int filament = cmd - MMU_CMD_E0;
275 DEBUG_ECHOLNPAIR("MMU <= E", filament);
276 tx_printf_P(PSTR("E%d\n"), filament);
277 state = 3; // wait for response
279 else if (cmd == MMU_CMD_R0) {
280 // recover after eject
281 DEBUG_ECHOLNPGM("MMU <= 'R0'");
282 MMU2_COMMAND("R0");
283 state = 3; // wait for response
285 else if (WITHIN(cmd, MMU_CMD_F0, MMU_CMD_F4)) {
286 // filament type
287 int filament = cmd - MMU_CMD_F0;
288 DEBUG_ECHOPAIR("MMU <= F", filament, " ");
289 DEBUG_ECHO_F(cmd_arg, DEC);
290 DEBUG_EOL();
291 tx_printf_P(PSTR("F%d %d\n"), filament, cmd_arg);
292 state = 3; // wait for response
295 last_cmd = cmd;
296 cmd = MMU_CMD_NONE;
298 else if (ELAPSED(millis(), prev_P0_request + 300)) {
299 MMU2_COMMAND("P0"); // Read FINDA
300 state = 2; // wait for response
303 TERN_(HAS_PRUSA_MMU2S, check_filament());
304 break;
306 case 2: // response to command P0
307 if (rx_ok()) {
308 sscanf(rx_buffer, "%hhuok\n", &finda);
310 // This is super annoying. Only activate if necessary
311 // if (finda_runout_valid) DEBUG_ECHOLNPAIR_F("MMU <= 'P0'\nMMU => ", finda, 6);
313 if (!finda && finda_runout_valid) filament_runout();
314 if (cmd == 0) ready = true;
315 state = 1;
317 else if (ELAPSED(millis(), prev_request + MMU_P0_TIMEOUT)) // Resend request after timeout (3s)
318 state = 1;
320 TERN_(HAS_PRUSA_MMU2S, check_filament());
321 break;
323 case 3: // response to mmu commands
324 #if ENABLED(MMU_EXTRUDER_SENSOR)
325 if (mmu_idl_sens) {
326 if (FILAMENT_PRESENT() && mmu_loading_flag) {
327 DEBUG_ECHOLNPGM("MMU <= 'A'");
328 MMU2_COMMAND("A"); // send 'abort' request
329 mmu_idl_sens = 0;
330 DEBUG_ECHOLNPGM("MMU IDLER_SENSOR = 0 - ABORT");
333 #endif
335 if (rx_ok()) {
336 // Response to C0 mmu command in MMU2S model
337 bool can_reset = true;
338 #if HAS_PRUSA_MMU2S
339 if (!mmu2s_triggered && last_cmd == MMU_CMD_C0) {
340 can_reset = false;
341 // MMU ok received but filament sensor not triggered, retrying...
342 DEBUG_ECHOLNPGM("MMU => 'ok' (filament not present in gears)");
343 DEBUG_ECHOLNPGM("MMU <= 'C0' (keep trying)");
344 MMU2_COMMAND("C0");
346 #endif
347 if (can_reset) {
348 DEBUG_ECHOLNPGM("MMU => 'ok'");
349 ready = true;
350 state = 1;
351 last_cmd = MMU_CMD_NONE;
354 else if (ELAPSED(millis(), prev_request + MMU_CMD_TIMEOUT)) {
355 // resend request after timeout
356 if (last_cmd) {
357 DEBUG_ECHOLNPGM("MMU retry");
358 cmd = last_cmd;
359 last_cmd = MMU_CMD_NONE;
361 state = 1;
363 TERN_(HAS_PRUSA_MMU2S, check_filament());
364 break;
369 * Check if MMU was started
371 bool MMU2::rx_start() {
372 // check for start message
373 if (rx_str_P(PSTR("start\n"))) {
374 prev_P0_request = millis();
375 return true;
377 return false;
381 * Check if the data received ends with the given string.
383 bool MMU2::rx_str_P(const char* str) {
384 uint8_t i = strlen(rx_buffer);
386 while (MMU2_SERIAL.available()) {
387 rx_buffer[i++] = MMU2_SERIAL.read();
388 rx_buffer[i] = '\0';
390 if (i == sizeof(rx_buffer) - 1) {
391 DEBUG_ECHOLNPGM("rx buffer overrun");
392 break;
396 uint8_t len = strlen_P(str);
398 if (i < len) return false;
400 str += len;
402 while (len--) {
403 char c0 = pgm_read_byte(str--), c1 = rx_buffer[i--];
404 if (c0 == c1) continue;
405 if (c0 == '\r' && c1 == '\n') continue; // match cr as lf
406 if (c0 == '\n' && c1 == '\r') continue; // match lf as cr
407 return false;
409 return true;
413 * Transfer data to MMU, no argument
415 void MMU2::tx_str_P(const char* str) {
416 clear_rx_buffer();
417 uint8_t len = strlen_P(str);
418 LOOP_L_N(i, len) MMU2_SERIAL.write(pgm_read_byte(str++));
419 rx_buffer[0] = '\0';
420 prev_request = millis();
424 * Transfer data to MMU, single argument
426 void MMU2::tx_printf_P(const char* format, int argument = -1) {
427 clear_rx_buffer();
428 uint8_t len = sprintf_P(tx_buffer, format, argument);
429 LOOP_L_N(i, len) MMU2_SERIAL.write(tx_buffer[i]);
430 rx_buffer[0] = '\0';
431 prev_request = millis();
435 * Transfer data to MMU, two arguments
437 void MMU2::tx_printf_P(const char* format, int argument1, int argument2) {
438 clear_rx_buffer();
439 uint8_t len = sprintf_P(tx_buffer, format, argument1, argument2);
440 LOOP_L_N(i, len) MMU2_SERIAL.write(tx_buffer[i]);
441 rx_buffer[0] = '\0';
442 prev_request = millis();
446 * Empty the rx buffer
448 void MMU2::clear_rx_buffer() {
449 while (MMU2_SERIAL.available()) MMU2_SERIAL.read();
450 rx_buffer[0] = '\0';
454 * Check if we received 'ok' from MMU
456 bool MMU2::rx_ok() {
457 if (rx_str_P(PSTR("ok\n"))) {
458 prev_P0_request = millis();
459 return true;
461 return false;
465 * Check if MMU has compatible firmware
467 void MMU2::check_version() {
468 if (buildnr < MMU_REQUIRED_FW_BUILDNR) {
469 SERIAL_ERROR_MSG("Invalid MMU2 firmware. Version >= " STRINGIFY(MMU_REQUIRED_FW_BUILDNR) " required.");
470 kill(GET_TEXT(MSG_KILL_MMU2_FIRMWARE));
474 static void mmu2_not_responding() {
475 LCD_MESSAGEPGM(MSG_MMU2_NOT_RESPONDING);
476 BUZZ(100, 659);
477 BUZZ(200, 698);
478 BUZZ(100, 659);
479 BUZZ(300, 440);
480 BUZZ(100, 659);
483 #if HAS_PRUSA_MMU2S
485 bool MMU2::load_to_gears() {
486 command(MMU_CMD_C0);
487 manage_response(true, true);
488 LOOP_L_N(i, MMU2_C0_RETRY) { // Keep loading until filament reaches gears
489 if (mmu2s_triggered) break;
490 command(MMU_CMD_C0);
491 manage_response(true, true);
492 check_filament();
494 const bool success = mmu2s_triggered && can_load();
495 if (!success) mmu2_not_responding();
496 return success;
500 * Handle tool change
502 void MMU2::tool_change(const uint8_t index) {
504 if (!enabled) return;
506 set_runout_valid(false);
508 if (index != extruder) {
510 DISABLE_AXIS_E0();
511 ui.status_printf_P(0, GET_TEXT(MSG_MMU2_LOADING_FILAMENT), int(index + 1));
513 command(MMU_CMD_T0 + index);
514 manage_response(true, true);
516 if (load_to_gears()) {
517 extruder = index; // filament change is finished
518 active_extruder = 0;
519 ENABLE_AXIS_E0();
520 SERIAL_ECHO_START();
521 SERIAL_ECHOLNPAIR(STR_ACTIVE_EXTRUDER, int(extruder));
523 ui.reset_status();
526 set_runout_valid(true);
530 * Handle special T?/Tx/Tc commands
532 * T? Gcode to extrude shouldn't have to follow, load to extruder wheels is done automatically
533 * Tx Same as T?, except nozzle doesn't have to be preheated. Tc must be placed after extruder nozzle is preheated to finish filament load.
534 * Tc Load to nozzle after filament was prepared by Tx and extruder nozzle is already heated.
536 void MMU2::tool_change(const char* special) {
537 if (!enabled) return;
539 set_runout_valid(false);
541 switch (*special) {
542 case '?': {
543 #if ENABLED(MMU2_MENUS)
544 const uint8_t index = mmu2_choose_filament();
545 while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
546 load_filament_to_nozzle(index);
547 #else
548 BUZZ(400, 40);
549 #endif
550 } break;
552 case 'x': {
553 #if ENABLED(MMU2_MENUS)
554 planner.synchronize();
555 const uint8_t index = mmu2_choose_filament();
556 DISABLE_AXIS_E0();
557 command(MMU_CMD_T0 + index);
558 manage_response(true, true);
560 if (load_to_gears()) {
561 mmu_loop();
562 ENABLE_AXIS_E0();
563 extruder = index;
564 active_extruder = 0;
566 #else
567 BUZZ(400, 40);
568 #endif
569 } break;
571 case 'c': {
572 while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
573 execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence));
574 } break;
577 set_runout_valid(true);
580 #elif ENABLED(MMU_EXTRUDER_SENSOR)
583 * Handle tool change
585 void MMU2::tool_change(const uint8_t index) {
586 if (!enabled) return;
588 set_runout_valid(false);
590 if (index != extruder) {
591 DISABLE_AXIS_E0();
592 if (FILAMENT_PRESENT()) {
593 DEBUG_ECHOLNPGM("Unloading\n");
594 mmu_loading_flag = false;
595 command(MMU_CMD_U0);
596 manage_response(true, true);
598 ui.status_printf_P(0, GET_TEXT(MSG_MMU2_LOADING_FILAMENT), int(index + 1));
599 mmu_loading_flag = true;
600 command(MMU_CMD_T0 + index);
601 manage_response(true, true);
602 mmu_continue_loading();
603 command(MMU_CMD_C0);
604 extruder = index;
605 active_extruder = 0;
607 ENABLE_AXIS_E0();
608 SERIAL_ECHO_START();
609 SERIAL_ECHOLNPAIR(STR_ACTIVE_EXTRUDER, int(extruder));
611 ui.reset_status();
614 set_runout_valid(true);
618 * Handle special T?/Tx/Tc commands
620 * T? Gcode to extrude shouldn't have to follow, load to extruder wheels is done automatically
621 * Tx Same as T?, except nozzle doesn't have to be preheated. Tc must be placed after extruder nozzle is preheated to finish filament load.
622 * Tc Load to nozzle after filament was prepared by Tx and extruder nozzle is already heated.
624 void MMU2::tool_change(const char* special) {
625 if (!enabled) return;
627 set_runout_valid(false);
629 switch (*special) {
630 case '?': {
631 DEBUG_ECHOLNPGM("case ?\n");
632 #if ENABLED(MMU2_MENUS)
633 uint8_t index = mmu2_choose_filament();
634 while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
635 load_filament_to_nozzle(index);
636 #else
637 BUZZ(400, 40);
638 #endif
639 } break;
641 case 'x': {
642 DEBUG_ECHOLNPGM("case x\n");
643 #if ENABLED(MMU2_MENUS)
644 planner.synchronize();
645 uint8_t index = mmu2_choose_filament();
646 DISABLE_AXIS_E0();
647 command(MMU_CMD_T0 + index);
648 manage_response(true, true);
649 mmu_continue_loading();
650 command(MMU_CMD_C0);
651 mmu_loop();
653 ENABLE_AXIS_E0();
654 extruder = index;
655 active_extruder = 0;
656 #else
657 BUZZ(400, 40);
658 #endif
659 } break;
661 case 'c': {
662 DEBUG_ECHOLNPGM("case c\n");
663 while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
664 execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence));
665 } break;
668 set_runout_valid(true);
671 void MMU2::mmu_continue_loading() {
672 for (uint8_t i = 0; i < MMU_LOADING_ATTEMPTS_NR; i++) {
673 DEBUG_ECHOLNPAIR("Additional load attempt #", i);
674 if (FILAMENT_PRESENT()) break;
675 command(MMU_CMD_C0);
676 manage_response(true, true);
678 if (!FILAMENT_PRESENT()) {
679 DEBUG_ECHOLNPGM("Filament never reached sensor, runout");
680 filament_runout();
682 mmu_idl_sens = 0;
685 #else // !HAS_PRUSA_MMU2S && !MMU_EXTRUDER_SENSOR
688 * Handle tool change
690 void MMU2::tool_change(const uint8_t index) {
691 if (!enabled) return;
693 set_runout_valid(false);
695 if (index != extruder) {
696 DISABLE_AXIS_E0();
697 ui.status_printf_P(0, GET_TEXT(MSG_MMU2_LOADING_FILAMENT), int(index + 1));
698 command(MMU_CMD_T0 + index);
699 manage_response(true, true);
700 command(MMU_CMD_C0);
701 extruder = index; //filament change is finished
702 active_extruder = 0;
703 ENABLE_AXIS_E0();
704 SERIAL_ECHO_START();
705 SERIAL_ECHOLNPAIR(STR_ACTIVE_EXTRUDER, int(extruder));
706 ui.reset_status();
709 set_runout_valid(true);
713 * Handle special T?/Tx/Tc commands
715 * T? Gcode to extrude shouldn't have to follow, load to extruder wheels is done automatically
716 * Tx Same as T?, except nozzle doesn't have to be preheated. Tc must be placed after extruder nozzle is preheated to finish filament load.
717 * Tc Load to nozzle after filament was prepared by Tx and extruder nozzle is already heated.
719 void MMU2::tool_change(const char* special) {
720 if (!enabled) return;
722 set_runout_valid(false);
724 switch (*special) {
725 case '?': {
726 DEBUG_ECHOLNPGM("case ?\n");
727 #if ENABLED(MMU2_MENUS)
728 uint8_t index = mmu2_choose_filament();
729 while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
730 load_filament_to_nozzle(index);
731 #else
732 BUZZ(400, 40);
733 #endif
734 } break;
736 case 'x': {
737 DEBUG_ECHOLNPGM("case x\n");
738 #if ENABLED(MMU2_MENUS)
739 planner.synchronize();
740 uint8_t index = mmu2_choose_filament();
741 DISABLE_AXIS_E0();
742 command(MMU_CMD_T0 + index);
743 manage_response(true, true);
744 command(MMU_CMD_C0);
745 mmu_loop();
747 ENABLE_AXIS_E0();
748 extruder = index;
749 active_extruder = 0;
750 #else
751 BUZZ(400, 40);
752 #endif
753 } break;
755 case 'c': {
756 DEBUG_ECHOLNPGM("case c\n");
757 while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100);
758 execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence));
759 } break;
762 set_runout_valid(true);
765 #endif // HAS_PRUSA_MMU2S
768 * Set next command
770 void MMU2::command(const uint8_t mmu_cmd) {
771 if (!enabled) return;
772 cmd = mmu_cmd;
773 ready = false;
777 * Wait for response from MMU
779 bool MMU2::get_response() {
780 while (cmd != MMU_CMD_NONE) idle();
782 while (!ready) {
783 idle();
784 if (state != 3) break;
787 const bool ret = ready;
788 ready = false;
790 return ret;
794 * Wait for response and deal with timeout if nexcessary
796 void MMU2::manage_response(const bool move_axes, const bool turn_off_nozzle) {
798 constexpr xyz_pos_t park_point = NOZZLE_PARK_POINT;
799 bool response = false;
800 mmu_print_saved = false;
801 xyz_pos_t resume_position;
802 int16_t resume_hotend_temp = thermalManager.degTargetHotend(active_extruder);
804 KEEPALIVE_STATE(PAUSED_FOR_USER);
806 while (!response) {
808 response = get_response(); // wait for "ok" from mmu
810 if (!response) { // No "ok" was received in reserved time frame, user will fix the issue on mmu unit
811 if (!mmu_print_saved) { // First occurrence. Save current position, park print head, disable nozzle heater.
813 planner.synchronize();
815 mmu_print_saved = true;
817 SERIAL_ECHOLNPGM("MMU not responding");
819 resume_hotend_temp = thermalManager.degTargetHotend(active_extruder);
820 resume_position = current_position;
822 if (move_axes && all_axes_homed())
823 nozzle.park(0, park_point /*= NOZZLE_PARK_POINT*/);
825 if (turn_off_nozzle) thermalManager.setTargetHotend(0, active_extruder);
827 mmu2_not_responding();
830 else if (mmu_print_saved) {
831 SERIAL_ECHOLNPGM("MMU starts responding\n");
833 if (turn_off_nozzle && resume_hotend_temp) {
834 thermalManager.setTargetHotend(resume_hotend_temp, active_extruder);
835 LCD_MESSAGEPGM(MSG_HEATING);
836 BUZZ(200, 40);
838 while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(1000);
841 if (move_axes && all_axes_homed()) {
842 LCD_MESSAGEPGM(MSG_MMU2_RESUMING);
843 BUZZ(198, 404); BUZZ(4, 0); BUZZ(198, 404);
845 // Move XY to starting position, then Z
846 do_blocking_move_to_xy(resume_position, feedRate_t(NOZZLE_PARK_XY_FEEDRATE));
848 // Move Z_AXIS to saved position
849 do_blocking_move_to_z(resume_position.z, feedRate_t(NOZZLE_PARK_Z_FEEDRATE));
851 else {
852 BUZZ(198, 404); BUZZ(4, 0); BUZZ(198, 404);
853 LCD_MESSAGEPGM(MSG_MMU2_RESUMING);
859 void MMU2::set_filament_type(const uint8_t index, const uint8_t filamentType) {
860 if (!enabled) return;
862 cmd_arg = filamentType;
863 command(MMU_CMD_F0 + index);
865 manage_response(true, true);
868 void MMU2::filament_runout() {
869 queue.inject_P(PSTR(MMU2_FILAMENT_RUNOUT_SCRIPT));
870 planner.synchronize();
873 #if HAS_PRUSA_MMU2S
875 void MMU2::check_filament() {
876 const bool present = FILAMENT_PRESENT();
877 if (cmd == MMU_CMD_NONE && last_cmd == MMU_CMD_C0) {
878 if (present && !mmu2s_triggered) {
879 DEBUG_ECHOLNPGM("MMU <= 'A'");
880 tx_str_P(PSTR("A\n"));
882 // Slowly spin the extruder during C0
883 else {
884 while (planner.movesplanned() < 3) {
885 current_position.e += 0.25;
886 line_to_current_position(MMM_TO_MMS(120));
890 mmu2s_triggered = present;
893 bool MMU2::can_load() {
894 execute_extruder_sequence((const E_Step *)can_load_sequence, COUNT(can_load_sequence));
896 int filament_detected_count = 0;
897 const int steps = (MMU2_CAN_LOAD_RETRACT) / (MMU2_CAN_LOAD_INCREMENT);
898 DEBUG_ECHOLNPGM("MMU can_load:");
899 LOOP_L_N(i, steps) {
900 execute_extruder_sequence((const E_Step *)can_load_increment_sequence, COUNT(can_load_increment_sequence));
901 check_filament(); // Don't trust the idle function
902 DEBUG_CHAR(mmu2s_triggered ? 'O' : 'o');
903 if (mmu2s_triggered) ++filament_detected_count;
906 if (filament_detected_count <= steps - (MMU2_CAN_LOAD_DEVIATION) / (MMU2_CAN_LOAD_INCREMENT)) {
907 DEBUG_ECHOLNPGM(" failed.");
908 return false;
911 DEBUG_ECHOLNPGM(" succeeded.");
912 return true;
915 #endif
917 // Load filament into MMU2
918 void MMU2::load_filament(const uint8_t index) {
919 if (!enabled) return;
920 command(MMU_CMD_L0 + index);
921 manage_response(false, false);
922 BUZZ(200, 404);
926 * Switch material and load to nozzle
928 bool MMU2::load_filament_to_nozzle(const uint8_t index) {
930 if (!enabled) return false;
932 if (thermalManager.tooColdToExtrude(active_extruder)) {
933 BUZZ(200, 404);
934 LCD_ALERTMESSAGEPGM(MSG_HOTEND_TOO_COLD);
935 return false;
938 command(MMU_CMD_T0 + index);
939 manage_response(true, true);
941 const bool success = load_to_gears();
942 if (success) {
943 mmu_loop();
944 extruder = index;
945 active_extruder = 0;
946 load_to_nozzle();
947 BUZZ(200, 404);
949 return success;
953 * Load filament to nozzle of multimaterial printer
955 * This function is used only after T? (user select filament) and M600 (change filament).
956 * It is not used after T0 .. T4 command (select filament), in such case, gcode is responsible for loading
957 * filament to nozzle.
959 void MMU2::load_to_nozzle() {
960 if (!enabled) return;
961 execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence));
964 bool MMU2::eject_filament(const uint8_t index, const bool recover) {
966 if (!enabled) return false;
968 if (thermalManager.tooColdToExtrude(active_extruder)) {
969 BUZZ(200, 404);
970 LCD_ALERTMESSAGEPGM(MSG_HOTEND_TOO_COLD);
971 return false;
974 LCD_MESSAGEPGM(MSG_MMU2_EJECTING_FILAMENT);
976 ENABLE_AXIS_E0();
977 current_position.e -= MMU2_FILAMENTCHANGE_EJECT_FEED;
978 line_to_current_position(MMM_TO_MMS(2500));
979 planner.synchronize();
980 command(MMU_CMD_E0 + index);
981 manage_response(false, false);
983 if (recover) {
984 LCD_MESSAGEPGM(MSG_MMU2_EJECT_RECOVER);
985 BUZZ(200, 404);
986 TERN_(HOST_PROMPT_SUPPORT, host_prompt_do(PROMPT_USER_CONTINUE, PSTR("MMU2 Eject Recover"), CONTINUE_STR));
987 TERN_(EXTENSIBLE_UI, ExtUI::onUserConfirmRequired_P(PSTR("MMU2 Eject Recover")));
988 wait_for_user_response();
989 BUZZ(200, 404);
990 BUZZ(200, 404);
992 command(MMU_CMD_R0);
993 manage_response(false, false);
996 ui.reset_status();
998 // no active tool
999 extruder = MMU2_NO_TOOL;
1001 set_runout_valid(false);
1003 BUZZ(200, 404);
1005 DISABLE_AXIS_E0();
1007 return true;
1011 * Unload from hotend and retract to MMU
1013 bool MMU2::unload() {
1015 if (!enabled) return false;
1017 if (thermalManager.tooColdToExtrude(active_extruder)) {
1018 BUZZ(200, 404);
1019 LCD_ALERTMESSAGEPGM(MSG_HOTEND_TOO_COLD);
1020 return false;
1023 filament_ramming();
1025 command(MMU_CMD_U0);
1026 manage_response(false, true);
1028 BUZZ(200, 404);
1030 // no active tool
1031 extruder = MMU2_NO_TOOL;
1033 set_runout_valid(false);
1035 return true;
1039 * Unload sequence to optimize shape of the tip of the unloaded filament
1041 void MMU2::filament_ramming() {
1042 execute_extruder_sequence((const E_Step *)ramming_sequence, sizeof(ramming_sequence) / sizeof(E_Step));
1045 void MMU2::execute_extruder_sequence(const E_Step * sequence, int steps) {
1047 planner.synchronize();
1048 ENABLE_AXIS_E0();
1050 const E_Step* step = sequence;
1052 LOOP_L_N(i, steps) {
1053 const float es = pgm_read_float(&(step->extrude));
1054 const feedRate_t fr_mm_m = pgm_read_float(&(step->feedRate));
1056 DEBUG_ECHO_START();
1057 DEBUG_ECHOLNPAIR("E step ", es, "/", fr_mm_m);
1059 current_position.e += es;
1060 line_to_current_position(MMM_TO_MMS(fr_mm_m));
1061 planner.synchronize();
1063 step++;
1066 DISABLE_AXIS_E0();
1069 #endif // HAS_PRUSA_MMU2