From 69aaf5d096e96639a8951854bf6e00ef7f7d9fdd Mon Sep 17 00:00:00 2001 From: revilor Date: Wed, 6 Feb 2019 03:56:22 +0100 Subject: [PATCH] MMU2 fixes (#13082) --- Marlin/src/feature/prusa_MMU2/mmu2.cpp | 39 +++++++++++++------------- Marlin/src/feature/prusa_MMU2/mmu2.h | 14 ++++++++- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/Marlin/src/feature/prusa_MMU2/mmu2.cpp b/Marlin/src/feature/prusa_MMU2/mmu2.cpp index 5bdd4dde4..aebd0ad9d 100644 --- a/Marlin/src/feature/prusa_MMU2/mmu2.cpp +++ b/Marlin/src/feature/prusa_MMU2/mmu2.cpp @@ -38,10 +38,6 @@ MMU2 mmu2; #include "../../module/stepper_indirection.h" #include "../../Marlin.h" -#if ENABLED(FILAMENT_RUNOUT_SENSOR) - #include "../runout.h" -#endif - #define MMU_TODELAY 100 #define MMU_TIMEOUT 10 #define MMU_CMD_TIMEOUT 60000ul //5min timeout for mmu commands (except P0) @@ -89,7 +85,7 @@ int8_t MMU2::state = 0; volatile int8_t MMU2::finda = 1; volatile bool MMU2::findaRunoutValid; int16_t MMU2::version = -1, MMU2::buildnr = -1; -millis_t MMU2::next_request, MMU2::next_response; +millis_t MMU2::last_request, MMU2::next_P0_request; char MMU2::rx_buffer[16], MMU2::tx_buffer[16]; #if HAS_LCD_MENU && ENABLED(MMU2_MENUS) @@ -109,7 +105,8 @@ MMU2::MMU2() { } void MMU2::init() { - findaRunoutValid = false; + + set_runout_valid(false); #if PIN_EXISTS(MMU2_RST) // TODO use macros for this @@ -321,7 +318,7 @@ void MMU2::mmuLoop() { last_cmd = cmd; cmd = MMU_CMD_NONE; } - else if (ELAPSED(millis(), next_response)) { + else if (ELAPSED(millis(), next_P0_request)) { // read FINDA tx_str_P(PSTR("P0\n")); state = 2; // wait for response @@ -350,7 +347,7 @@ void MMU2::mmuLoop() { if (!finda && findaRunoutValid) filamentRunout(); } - else if (ELAPSED(millis(), next_request)) // Resend request after timeout (30s) + else if (ELAPSED(millis(), last_request + MMU_P0_TIMEOUT)) // Resend request after timeout (30s) state = 1; break; @@ -365,7 +362,7 @@ void MMU2::mmuLoop() { state = 1; last_cmd = MMU_CMD_NONE; } - else if (ELAPSED(millis(), next_request)) { + else if (ELAPSED(millis(), last_request + MMU_CMD_TIMEOUT)) { // resend request after timeout if (last_cmd) { #if ENABLED(MMU2_DEBUG) @@ -388,7 +385,7 @@ void MMU2::mmuLoop() { bool MMU2::rx_start() { // check for start message if (rx_str_P(PSTR("start\n"))) { - next_response = millis() + 300; + next_P0_request = millis() + 300; return true; } return false; @@ -439,7 +436,7 @@ void MMU2::tx_str_P(const char* str) { uint8_t len = strlen_P(str); for (uint8_t i = 0; i < len; i++) mmuSerial.write(pgm_read_byte(str++)); rx_buffer[0] = '\0'; - next_request = millis() + MMU_P0_TIMEOUT; + last_request = millis(); } @@ -451,7 +448,7 @@ void MMU2::tx_printf_P(const char* format, int argument = -1) { uint8_t len = sprintf_P(tx_buffer, format, argument); for (uint8_t i = 0; i < len; i++) mmuSerial.write(tx_buffer[i]); rx_buffer[0] = '\0'; - next_request = millis() + MMU_P0_TIMEOUT; + last_request = millis(); } @@ -463,7 +460,7 @@ void MMU2::tx_printf_P(const char* format, int argument1, int argument2) { uint8_t len = sprintf_P(tx_buffer, format, argument1, argument2); for (uint8_t i = 0; i < len; i++) mmuSerial.write(tx_buffer[i]); rx_buffer[0] = '\0'; - next_request = millis() + MMU_P0_TIMEOUT; + last_request = millis(); } @@ -481,7 +478,7 @@ void MMU2::clear_rx_buffer() { */ bool MMU2::rx_ok() { if (rx_str_P(PSTR("ok\n"))) { - next_response = millis() + 300; + next_P0_request = millis() + 300; return true; } return false; @@ -508,7 +505,7 @@ void MMU2::toolChange(uint8_t index) { if (!enabled) return; - findaRunoutValid = false; + set_runout_valid(false); if (index != extruder) { @@ -534,7 +531,7 @@ void MMU2::toolChange(uint8_t index) { KEEPALIVE_STATE(NOT_BUSY); } - findaRunoutValid = true; + set_runout_valid(true); } @@ -553,7 +550,7 @@ void MMU2::toolChange(const char* special) { #if ENABLED(MMU2_MENUS) - findaRunoutValid = false; + set_runout_valid(false); KEEPALIVE_STATE(IN_HANDLER); switch(*special) { @@ -585,7 +582,7 @@ void MMU2::toolChange(const char* special) { KEEPALIVE_STATE(NOT_BUSY); - findaRunoutValid = true; + set_runout_valid(true); #endif } @@ -806,7 +803,8 @@ void MMU2::filamentRunout() { // no active tool extruder = MMU2_NO_TOOL; - findaRunoutValid = false; + + set_runout_valid(false); BUZZ(200, 404); @@ -845,7 +843,8 @@ void MMU2::filamentRunout() { // no active tool extruder = MMU2_NO_TOOL; - findaRunoutValid = false; + + set_runout_valid(false); KEEPALIVE_STATE(NOT_BUSY); diff --git a/Marlin/src/feature/prusa_MMU2/mmu2.h b/Marlin/src/feature/prusa_MMU2/mmu2.h index 7d7a4facf..88f0ac047 100644 --- a/Marlin/src/feature/prusa_MMU2/mmu2.h +++ b/Marlin/src/feature/prusa_MMU2/mmu2.h @@ -24,6 +24,10 @@ #include "../../inc/MarlinConfig.h" +#if ENABLED(FILAMENT_RUNOUT_SENSOR) + #include "../runout.h" +#endif + struct E_Step; class MMU2 { @@ -75,8 +79,16 @@ private: static volatile int8_t finda; static volatile bool findaRunoutValid; static int16_t version, buildnr; - static millis_t next_request, next_response; + static millis_t last_request, next_P0_request; static char rx_buffer[16], tx_buffer[16]; + + static inline void set_runout_valid(const bool valid) { + findaRunoutValid = valid; + #if ENABLED(FILAMENT_RUNOUT_SENSOR) + if (valid) runout.reset(); + #endif + } + }; extern MMU2 mmu2;