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"
28 #include "../../lcd/menu/menu_mmu2.h"
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"
45 #if ENABLED(EXTENSIBLE_UI)
46 #include "../../lcd/extui/ui_api.h"
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;
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
;
96 bool MMU2::mmu2s_triggered
;
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
];
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
}
115 , can_load_sequence
[] PROGMEM
= { MMU2_CAN_LOAD_SEQUENCE
}
116 , can_load_increment_sequence
[] PROGMEM
= { MMU2_CAN_LOAD_INCREMENT_SEQUENCE
}
126 set_runout_valid(false);
128 #if PIN_EXISTS(MMU2_RST)
129 WRITE(MMU2_RST_PIN
, HIGH
);
130 SET_OUTPUT(MMU2_RST_PIN
);
133 MMU2_SERIAL
.begin(MMU_BAUD
);
134 extruder
= MMU2_NO_TOOL
;
143 DEBUG_ECHOLNPGM("MMU <= reset");
145 #if PIN_EXISTS(MMU2_RST)
146 WRITE(MMU2_RST_PIN
, LOW
);
148 WRITE(MMU2_RST_PIN
, HIGH
);
150 MMU2_COMMAND("X0"); // Send soft reset
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)
162 void MMU2::mmu_loop() {
170 DEBUG_ECHOLNPGM("MMU => 'start'");
171 DEBUG_ECHOLNPGM("MMU <= 'S1'");
173 MMU2_COMMAND("S1"); // Read Version
176 else if (millis() > 3000000) {
177 SERIAL_ECHOLNPGM("MMU not responding - DISABLED");
184 sscanf(rx_buffer
, "%huok\n", &version
);
186 DEBUG_ECHOLNPAIR("MMU => ", version
, "\nMMU <= 'S2'");
188 MMU2_COMMAND("S2"); // Read Build Number
195 sscanf(rx_buffer
, "%huok\n", &buildnr
);
197 DEBUG_ECHOLNPAIR("MMU => ", buildnr
);
201 #if ENABLED(MMU2_MODE_12V)
202 DEBUG_ECHOLNPGM("MMU <= 'M1'");
204 MMU2_COMMAND("M1"); // Stealth Mode
208 DEBUG_ECHOLNPGM("MMU <= 'P0'");
210 MMU2_COMMAND("P0"); // Read FINDA
216 #if ENABLED(MMU2_MODE_12V)
220 DEBUG_ECHOLNPGM("MMU => ok");
222 DEBUG_ECHOLNPGM("MMU <= 'P0'");
224 MMU2_COMMAND("P0"); // Read FINDA
232 sscanf(rx_buffer
, "%hhuok\n", &finda
);
234 DEBUG_ECHOLNPAIR("MMU => ", finda
, "\nMMU - ENABLED");
238 TERN_(HAS_PRUSA_MMU2S
, mmu2s_triggered
= false);
244 if (WITHIN(cmd
, MMU_CMD_T0
, MMU_CMD_T4
)) {
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
)) {
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
) {
261 DEBUG_ECHOLNPGM("MMU <= 'C0'");
263 state
= 3; // wait for response
265 else if (cmd
== MMU_CMD_U0
) {
267 DEBUG_ECHOLNPGM("MMU <= 'U0'");
270 state
= 3; // wait for response
272 else if (WITHIN(cmd
, MMU_CMD_E0
, MMU_CMD_E4
)) {
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'");
283 state
= 3; // wait for response
285 else if (WITHIN(cmd
, MMU_CMD_F0
, MMU_CMD_F4
)) {
287 int filament
= cmd
- MMU_CMD_F0
;
288 DEBUG_ECHOPAIR("MMU <= F", filament
, " ");
289 DEBUG_ECHO_F(cmd_arg
, DEC
);
291 tx_printf_P(PSTR("F%d %d\n"), filament
, cmd_arg
);
292 state
= 3; // wait for response
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());
306 case 2: // response to command P0
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;
317 else if (ELAPSED(millis(), prev_request
+ MMU_P0_TIMEOUT
)) // Resend request after timeout (3s)
320 TERN_(HAS_PRUSA_MMU2S
, check_filament());
323 case 3: // response to mmu commands
324 #if ENABLED(MMU_EXTRUDER_SENSOR)
326 if (FILAMENT_PRESENT() && mmu_loading_flag
) {
327 DEBUG_ECHOLNPGM("MMU <= 'A'");
328 MMU2_COMMAND("A"); // send 'abort' request
330 DEBUG_ECHOLNPGM("MMU IDLER_SENSOR = 0 - ABORT");
336 // Response to C0 mmu command in MMU2S model
337 bool can_reset
= true;
339 if (!mmu2s_triggered
&& last_cmd
== MMU_CMD_C0
) {
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)");
348 DEBUG_ECHOLNPGM("MMU => 'ok'");
351 last_cmd
= MMU_CMD_NONE
;
354 else if (ELAPSED(millis(), prev_request
+ MMU_CMD_TIMEOUT
)) {
355 // resend request after timeout
357 DEBUG_ECHOLNPGM("MMU retry");
359 last_cmd
= MMU_CMD_NONE
;
363 TERN_(HAS_PRUSA_MMU2S
, check_filament());
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();
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();
390 if (i
== sizeof(rx_buffer
) - 1) {
391 DEBUG_ECHOLNPGM("rx buffer overrun");
396 uint8_t len
= strlen_P(str
);
398 if (i
< len
) return false;
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
413 * Transfer data to MMU, no argument
415 void MMU2::tx_str_P(const char* str
) {
417 uint8_t len
= strlen_P(str
);
418 LOOP_L_N(i
, len
) MMU2_SERIAL
.write(pgm_read_byte(str
++));
420 prev_request
= millis();
424 * Transfer data to MMU, single argument
426 void MMU2::tx_printf_P(const char* format
, int argument
= -1) {
428 uint8_t len
= sprintf_P(tx_buffer
, format
, argument
);
429 LOOP_L_N(i
, len
) MMU2_SERIAL
.write(tx_buffer
[i
]);
431 prev_request
= millis();
435 * Transfer data to MMU, two arguments
437 void MMU2::tx_printf_P(const char* format
, int argument1
, int argument2
) {
439 uint8_t len
= sprintf_P(tx_buffer
, format
, argument1
, argument2
);
440 LOOP_L_N(i
, len
) MMU2_SERIAL
.write(tx_buffer
[i
]);
442 prev_request
= millis();
446 * Empty the rx buffer
448 void MMU2::clear_rx_buffer() {
449 while (MMU2_SERIAL
.available()) MMU2_SERIAL
.read();
454 * Check if we received 'ok' from MMU
457 if (rx_str_P(PSTR("ok\n"))) {
458 prev_P0_request
= millis();
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
);
485 bool MMU2::load_to_gears() {
487 manage_response(true, true);
488 LOOP_L_N(i
, MMU2_C0_RETRY
) { // Keep loading until filament reaches gears
489 if (mmu2s_triggered
) break;
491 manage_response(true, true);
494 const bool success
= mmu2s_triggered
&& can_load();
495 if (!success
) mmu2_not_responding();
502 void MMU2::tool_change(const uint8_t index
) {
504 if (!enabled
) return;
506 set_runout_valid(false);
508 if (index
!= extruder
) {
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
521 SERIAL_ECHOLNPAIR(STR_ACTIVE_EXTRUDER
, int(extruder
));
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);
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
);
553 #if ENABLED(MMU2_MENUS)
554 planner
.synchronize();
555 const uint8_t index
= mmu2_choose_filament();
557 command(MMU_CMD_T0
+ index
);
558 manage_response(true, true);
560 if (load_to_gears()) {
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
));
577 set_runout_valid(true);
580 #elif ENABLED(MMU_EXTRUDER_SENSOR)
585 void MMU2::tool_change(const uint8_t index
) {
586 if (!enabled
) return;
588 set_runout_valid(false);
590 if (index
!= extruder
) {
592 if (FILAMENT_PRESENT()) {
593 DEBUG_ECHOLNPGM("Unloading\n");
594 mmu_loading_flag
= false;
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();
609 SERIAL_ECHOLNPAIR(STR_ACTIVE_EXTRUDER
, int(extruder
));
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);
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
);
642 DEBUG_ECHOLNPGM("case x\n");
643 #if ENABLED(MMU2_MENUS)
644 planner
.synchronize();
645 uint8_t index
= mmu2_choose_filament();
647 command(MMU_CMD_T0
+ index
);
648 manage_response(true, true);
649 mmu_continue_loading();
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
));
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;
676 manage_response(true, true);
678 if (!FILAMENT_PRESENT()) {
679 DEBUG_ECHOLNPGM("Filament never reached sensor, runout");
685 #else // !HAS_PRUSA_MMU2S && !MMU_EXTRUDER_SENSOR
690 void MMU2::tool_change(const uint8_t index
) {
691 if (!enabled
) return;
693 set_runout_valid(false);
695 if (index
!= extruder
) {
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);
701 extruder
= index
; //filament change is finished
705 SERIAL_ECHOLNPAIR(STR_ACTIVE_EXTRUDER
, int(extruder
));
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);
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
);
737 DEBUG_ECHOLNPGM("case x\n");
738 #if ENABLED(MMU2_MENUS)
739 planner
.synchronize();
740 uint8_t index
= mmu2_choose_filament();
742 command(MMU_CMD_T0
+ index
);
743 manage_response(true, true);
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
));
762 set_runout_valid(true);
765 #endif // HAS_PRUSA_MMU2S
770 void MMU2::command(const uint8_t mmu_cmd
) {
771 if (!enabled
) return;
777 * Wait for response from MMU
779 bool MMU2::get_response() {
780 while (cmd
!= MMU_CMD_NONE
) idle();
784 if (state
!= 3) break;
787 const bool ret
= ready
;
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
);
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
);
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
));
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();
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
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:");
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.");
911 DEBUG_ECHOLNPGM(" succeeded.");
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);
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
)) {
934 LCD_ALERTMESSAGEPGM(MSG_HOTEND_TOO_COLD
);
938 command(MMU_CMD_T0
+ index
);
939 manage_response(true, true);
941 const bool success
= load_to_gears();
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
)) {
970 LCD_ALERTMESSAGEPGM(MSG_HOTEND_TOO_COLD
);
974 LCD_MESSAGEPGM(MSG_MMU2_EJECTING_FILAMENT
);
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);
984 LCD_MESSAGEPGM(MSG_MMU2_EJECT_RECOVER
);
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();
993 manage_response(false, false);
999 extruder
= MMU2_NO_TOOL
;
1001 set_runout_valid(false);
1011 * Unload from hotend and retract to MMU
1013 bool MMU2::unload() {
1015 if (!enabled
) return false;
1017 if (thermalManager
.tooColdToExtrude(active_extruder
)) {
1019 LCD_ALERTMESSAGEPGM(MSG_HOTEND_TOO_COLD
);
1025 command(MMU_CMD_U0
);
1026 manage_response(false, true);
1031 extruder
= MMU2_NO_TOOL
;
1033 set_runout_valid(false);
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();
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
));
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();
1069 #endif // HAS_PRUSA_MMU2