From 2aaff47c9dc2927dc275e3e43c19b5ce4a82d07c Mon Sep 17 00:00:00 2001 From: Giuliano Zaro <3684609+GMagician@users.noreply.github.com> Date: Mon, 25 Jan 2021 15:58:52 +0100 Subject: [PATCH] Clean up MMU2 code (#20794) Co-authored-by: Scott Lahteine --- Marlin/src/feature/mmu/mmu2.cpp | 44 +++++++++++++------------------ Marlin/src/feature/mmu/mmu2.h | 1 - Marlin/src/lcd/menu/menu_mmu2.cpp | 19 ++++++------- 3 files changed, 26 insertions(+), 38 deletions(-) diff --git a/Marlin/src/feature/mmu/mmu2.cpp b/Marlin/src/feature/mmu/mmu2.cpp index a4b7f257a..e3036947d 100644 --- a/Marlin/src/feature/mmu/mmu2.cpp +++ b/Marlin/src/feature/mmu/mmu2.cpp @@ -167,6 +167,8 @@ void MMU2::mmu_loop() { case -1: if (rx_start()) { + prev_P0_request = millis(); // Initialize finda sensor timeout + DEBUG_ECHOLNPGM("MMU => 'start'"); DEBUG_ECHOLNPGM("MMU <= 'S1'"); @@ -311,7 +313,7 @@ void MMU2::mmu_loop() { // if (finda_runout_valid) DEBUG_ECHOLNPAIR_F("MMU <= 'P0'\nMMU => ", finda, 6); if (!finda && finda_runout_valid) filament_runout(); - if (cmd == 0) ready = true; + if (cmd == MMU_CMD_NONE) ready = true; state = 1; } else if (ELAPSED(millis(), prev_request + MMU_P0_TIMEOUT)) // Resend request after timeout (3s) @@ -333,18 +335,20 @@ void MMU2::mmu_loop() { #endif if (rx_ok()) { - // Response to C0 mmu command in MMU2S model - bool can_reset = true; #if HAS_PRUSA_MMU2S - if (!mmu2s_triggered && last_cmd == MMU_CMD_C0) { - can_reset = false; + // Respond to C0 MMU command in MMU2S model + const bool keep_trying = !mmu2s_triggered && last_cmd == MMU_CMD_C0; + if (keep_trying) { // MMU ok received but filament sensor not triggered, retrying... DEBUG_ECHOLNPGM("MMU => 'ok' (filament not present in gears)"); DEBUG_ECHOLNPGM("MMU <= 'C0' (keep trying)"); MMU2_COMMAND("C0"); } + #else + constexpr bool keep_trying = false; #endif - if (can_reset) { + + if (!keep_trying) { DEBUG_ECHOLNPGM("MMU => 'ok'"); ready = true; state = 1; @@ -370,11 +374,7 @@ void MMU2::mmu_loop() { */ bool MMU2::rx_start() { // check for start message - if (rx_str_P(PSTR("start\n"))) { - prev_P0_request = millis(); - return true; - } - return false; + return rx_str_P(PSTR("start\n")); } /** @@ -385,13 +385,13 @@ bool MMU2::rx_str_P(const char* str) { while (MMU2_SERIAL.available()) { rx_buffer[i++] = MMU2_SERIAL.read(); - rx_buffer[i] = '\0'; if (i == sizeof(rx_buffer) - 1) { DEBUG_ECHOLNPGM("rx buffer overrun"); break; } } + rx_buffer[i] = '\0'; uint8_t len = strlen_P(str); @@ -416,7 +416,6 @@ void MMU2::tx_str_P(const char* str) { clear_rx_buffer(); uint8_t len = strlen_P(str); LOOP_L_N(i, len) MMU2_SERIAL.write(pgm_read_byte(str++)); - rx_buffer[0] = '\0'; prev_request = millis(); } @@ -427,7 +426,6 @@ void MMU2::tx_printf_P(const char* format, int argument = -1) { clear_rx_buffer(); uint8_t len = sprintf_P(tx_buffer, format, argument); LOOP_L_N(i, len) MMU2_SERIAL.write(tx_buffer[i]); - rx_buffer[0] = '\0'; prev_request = millis(); } @@ -438,7 +436,6 @@ void MMU2::tx_printf_P(const char* format, int argument1, int argument2) { clear_rx_buffer(); uint8_t len = sprintf_P(tx_buffer, format, argument1, argument2); LOOP_L_N(i, len) MMU2_SERIAL.write(tx_buffer[i]); - rx_buffer[0] = '\0'; prev_request = millis(); } @@ -570,7 +567,7 @@ static void mmu2_not_responding() { case 'c': { while (!thermalManager.wait_for_hotend(active_extruder, false)) safe_delay(100); - execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence)); + load_to_nozzle(); } break; } @@ -791,7 +788,7 @@ bool MMU2::get_response() { } /** - * Wait for response and deal with timeout if nexcessary + * Wait for response and deal with timeout if necessary */ void MMU2::manage_response(const bool move_axes, const bool turn_off_nozzle) { @@ -917,6 +914,7 @@ void MMU2::filament_runout() { // Load filament into MMU2 void MMU2::load_filament(const uint8_t index) { if (!enabled) return; + command(MMU_CMD_L0 + index); manage_response(false, false); BUZZ(200, 404); @@ -935,6 +933,7 @@ bool MMU2::load_filament_to_nozzle(const uint8_t index) { return false; } + DISABLE_AXIS_E0(); command(MMU_CMD_T0 + index); manage_response(true, true); @@ -957,7 +956,6 @@ bool MMU2::load_filament_to_nozzle(const uint8_t index) { * filament to nozzle. */ void MMU2::load_to_nozzle() { - if (!enabled) return; execute_extruder_sequence((const E_Step *)load_to_nozzle_sequence, COUNT(load_to_nozzle_sequence)); } @@ -1020,7 +1018,8 @@ bool MMU2::unload() { return false; } - filament_ramming(); + // Unload sequence to optimize shape of the tip of the unloaded filament + execute_extruder_sequence((const E_Step *)ramming_sequence, sizeof(ramming_sequence) / sizeof(E_Step)); command(MMU_CMD_U0); manage_response(false, true); @@ -1035,13 +1034,6 @@ bool MMU2::unload() { return true; } -/** - * Unload sequence to optimize shape of the tip of the unloaded filament - */ -void MMU2::filament_ramming() { - execute_extruder_sequence((const E_Step *)ramming_sequence, sizeof(ramming_sequence) / sizeof(E_Step)); -} - void MMU2::execute_extruder_sequence(const E_Step * sequence, int steps) { planner.synchronize(); diff --git a/Marlin/src/feature/mmu/mmu2.h b/Marlin/src/feature/mmu/mmu2.h index 09ff3b668..4326989a7 100644 --- a/Marlin/src/feature/mmu/mmu2.h +++ b/Marlin/src/feature/mmu/mmu2.h @@ -71,7 +71,6 @@ private: static void manage_response(const bool move_axes, const bool turn_off_nozzle); static void load_to_nozzle(); - static void filament_ramming(); static void execute_extruder_sequence(const E_Step * sequence, int steps); static void filament_runout(); diff --git a/Marlin/src/lcd/menu/menu_mmu2.cpp b/Marlin/src/lcd/menu/menu_mmu2.cpp index 8a34e7d29..7e71f00d2 100644 --- a/Marlin/src/lcd/menu/menu_mmu2.cpp +++ b/Marlin/src/lcd/menu/menu_mmu2.cpp @@ -32,15 +32,12 @@ // Load Filament // -void _mmu2_load_filamentToNozzle(uint8_t index) { +inline void action_mmu2_load_filament_to_nozzle(const uint8_t tool) { ui.reset_status(); ui.return_to_status(); - ui.status_printf_P(0, GET_TEXT(MSG_MMU2_LOADING_FILAMENT), int(index + 1)); - if (mmu2.load_filament_to_nozzle(index)) ui.reset_status(); -} - -inline void action_mmu2_load_filament_to_nozzle(const uint8_t tool) { - _mmu2_load_filamentToNozzle(tool); + ui.status_printf_P(0, GET_TEXT(MSG_MMU2_LOADING_FILAMENT), int(tool + 1)); + if (mmu2.load_filament_to_nozzle(tool)) + ui.reset_status(); ui.return_to_status(); } @@ -59,14 +56,14 @@ void menu_mmu2_load_filament() { START_MENU(); BACK_ITEM(MSG_MMU2_MENU); ACTION_ITEM(MSG_MMU2_ALL, action_mmu2_load_all); - LOOP_L_N(i, 5) ACTION_ITEM_N(i, MSG_MMU2_FILAMENT_N, []{ _mmu2_load_filament(MenuItemBase::itemIndex); }); + LOOP_L_N(i, EXTRUDERS) ACTION_ITEM_N(i, MSG_MMU2_FILAMENT_N, []{ _mmu2_load_filament(MenuItemBase::itemIndex); }); END_MENU(); } void menu_mmu2_load_to_nozzle() { START_MENU(); BACK_ITEM(MSG_MMU2_MENU); - LOOP_L_N(i, 5) ACTION_ITEM_N(i, MSG_MMU2_FILAMENT_N, []{ action_mmu2_load_filament_to_nozzle(MenuItemBase::itemIndex); }); + LOOP_L_N(i, EXTRUDERS) ACTION_ITEM_N(i, MSG_MMU2_FILAMENT_N, []{ action_mmu2_load_filament_to_nozzle(MenuItemBase::itemIndex); }); END_MENU(); } @@ -92,7 +89,7 @@ void action_mmu2_unload_filament() { void menu_mmu2_eject_filament() { START_MENU(); BACK_ITEM(MSG_MMU2_MENU); - LOOP_L_N(i, 5) ACTION_ITEM_N(i, MSG_MMU2_FILAMENT_N, []{ _mmu2_eject_filament(MenuItemBase::itemIndex); }); + LOOP_L_N(i, EXTRUDERS) ACTION_ITEM_N(i, MSG_MMU2_FILAMENT_N, []{ _mmu2_eject_filament(MenuItemBase::itemIndex); }); END_MENU(); } @@ -133,7 +130,7 @@ void menu_mmu2_choose_filament() { #if LCD_HEIGHT > 2 STATIC_ITEM(MSG_MMU2_CHOOSE_FILAMENT_HEADER, SS_DEFAULT|SS_INVERT); #endif - LOOP_L_N(i, 5) ACTION_ITEM_N(i, MSG_MMU2_FILAMENT_N, []{ action_mmu2_chosen(MenuItemBase::itemIndex); }); + LOOP_L_N(i, EXTRUDERS) ACTION_ITEM_N(i, MSG_MMU2_FILAMENT_N, []{ action_mmu2_chosen(MenuItemBase::itemIndex); }); END_MENU(); }