From 94423e6f7e07e48f7970b27f64b15bf7b04d7ab3 Mon Sep 17 00:00:00 2001 From: Robert Pelnar Date: Mon, 20 Aug 2018 20:53:53 +0200 Subject: [PATCH] MMU communication - mmu_ready --- Firmware/Marlin_main.cpp | 6 ++- Firmware/mmu.cpp | 95 ++++++++++++++++++++++++++++++++++++---- Firmware/mmu.h | 10 ++++- 3 files changed, 99 insertions(+), 12 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 33302d41..1e606bf0 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -6803,8 +6803,9 @@ if((eSoundMode==e_SOUND_MODE_LOUD)||(eSoundMode==e_SOUND_MODE_ONCE)) if (mmu_enabled) { - printf_P(PSTR("T code: %d \n"), tmp_extruder); - mmu_printf_P(PSTR("T%d\n"), tmp_extruder); + //printf_P(PSTR("T code: %d \n"), tmp_extruder); + //mmu_printf_P(PSTR("T%d\n"), tmp_extruder); + mmu_command(MMU_CMD_T0 + tmp_extruder); manage_response(true, true); @@ -7427,6 +7428,7 @@ if((eSoundMode==e_SOUND_MODE_LOUD)||(eSoundMode==e_SOUND_MODE_ONCE)) handle_status_leds(); #endif check_axes_activity(); +// mmu_loop(); } void kill(const char *full_screen_message, unsigned char id) diff --git a/Firmware/mmu.cpp b/Firmware/mmu.cpp index 75e436a1..4361d5b1 100644 --- a/Firmware/mmu.cpp +++ b/Firmware/mmu.cpp @@ -26,8 +26,12 @@ extern char choose_extruder_menu(); bool mmu_enabled = false; +bool mmu_ready = false; + int8_t mmu_state = 0; +uint8_t mmu_cmd = 0; + uint8_t mmu_extruder = 0; uint8_t tmp_extruder = 0; @@ -38,6 +42,9 @@ int16_t mmu_version = -1; int16_t mmu_buildnr = -1; +uint32_t mmu_last_request = 0; +uint32_t mmu_last_response = 0; + //clear rx buffer void mmu_clr_rx_buf(void) @@ -49,7 +56,9 @@ void mmu_clr_rx_buf(void) int mmu_puts_P(const char* str) { mmu_clr_rx_buf(); //clear rx buffer - return fputs_P(str, uart2io); //send command + int r = fputs_P(str, uart2io); //send command + mmu_last_request = millis(); + return r; } //send command - printf @@ -60,19 +69,24 @@ int mmu_printf_P(const char* format, ...) mmu_clr_rx_buf(); //clear rx buffer int r = vfprintf_P(uart2io, format, args); //send command va_end(args); + mmu_last_request = millis(); return r; } //check 'ok' response int8_t mmu_rx_ok(void) { - return uart2_rx_str_P(PSTR("ok\n")); + int8_t res = uart2_rx_str_P(PSTR("ok\n")); + if (res == 1) mmu_last_response = millis(); + return res; } //check 'start' response int8_t mmu_rx_start(void) { - return uart2_rx_str_P(PSTR("start\n")); + int8_t res = uart2_rx_str_P(PSTR("start\n")); + if (res == 1) mmu_last_response = millis(); + return res; } //initialize mmu2 unit - first part - should be done at begining of startup process @@ -138,6 +152,50 @@ void mmu_loop(void) mmu_state = 1; } return; + case 1: + if (mmu_cmd) //command request ? + { + if ((mmu_cmd >= MMU_CMD_T0) && (mmu_cmd <= MMU_CMD_T4)) + { + int extruder = mmu_cmd - MMU_CMD_T0; + printf_P(PSTR("MMU <= 'T%d'\n"), extruder); + mmu_printf_P(PSTR("T%d\n"), extruder); + mmu_state = 3; // wait for response + } + mmu_cmd = 0; + } + else if ((mmu_last_response + 1000) < millis()) //request every 1s + { + puts_P(PSTR("MMU <= 'P0'")); + mmu_puts_P(PSTR("P0\n")); //send 'read finda' request + mmu_state = 2; + } + return; + case 2: //response to command P0 + if (mmu_rx_ok() > 0) + { + fscanf_P(uart2io, PSTR("%hhu"), &mmu_finda); //scan finda from buffer + printf_P(PSTR("MMU => '%dok'\n"), mmu_finda); + mmu_state = 1; + mmu_ready = true; + } + else if ((mmu_last_request + 30000) < millis()) + { //resend request after timeout (30s) + mmu_state = 1; + } + return; + case 3: //response to commands T0-T4 + if (mmu_rx_ok() > 0) + { + printf_P(PSTR("MMU => 'ok'\n"), mmu_finda); + mmu_ready = true; + mmu_state = 1; + } + else if ((mmu_last_request + 30000) < millis()) + { //resend request after timeout (30s) + mmu_state = 1; + } + return; } } @@ -162,9 +220,26 @@ int8_t mmu_set_filament_type(uint8_t extruder, uint8_t filament) return timeout?1:0; } -bool mmu_get_response(bool timeout) +void mmu_command(uint8_t cmd) { - printf_P(PSTR("mmu_get_response - begin\n")); + mmu_cmd = cmd; + mmu_ready = false; +} + +bool mmu_get_response(void) +{ + KEEPALIVE_STATE(IN_PROCESS); + while (!mmu_ready) + { + mmu_loop(); + if (mmu_state != 3) + break; + } + bool ret = mmu_ready; + mmu_ready = false; + return ret; + +/* printf_P(PSTR("mmu_get_response - begin\n")); //waits for "ok" from mmu //function returns true if "ok" was received //if timeout is set to true function return false if there is no "ok" received before timeout @@ -182,7 +257,7 @@ bool mmu_get_response(bool timeout) } } printf_P(PSTR("mmu_get_response - end %d\n"), response?1:0); - return response; + return response;*/ } @@ -197,7 +272,7 @@ void manage_response(bool move_axes, bool turn_off_nozzle) float y_position_bckp = current_position[Y_AXIS]; while(!response) { - response = mmu_get_response(true); //wait for "ok" from mmu + response = mmu_get_response(); //wait for "ok" from mmu if (!response) { //no "ok" was received in reserved time frame, user will fix the issue on mmu unit if (!mmu_print_saved) { //first occurence, we are saving current position, park print head in certain position and disable nozzle heater if (lcd_update_enabled) { @@ -306,8 +381,10 @@ void mmu_M600_load_filament(bool automatic) lcd_print(" "); lcd_print(tmp_extruder + 1); snmm_filaments_used |= (1 << tmp_extruder); //for stop print - printf_P(PSTR("T code: %d \n"), tmp_extruder); - mmu_printf_P(PSTR("T%d\n"), tmp_extruder); + +// printf_P(PSTR("T code: %d \n"), tmp_extruder); +// mmu_printf_P(PSTR("T%d\n"), tmp_extruder); + mmu_command(MMU_CMD_T0 + tmp_extruder); manage_response(false, true); mmu_extruder = tmp_extruder; //filament change is finished diff --git a/Firmware/mmu.h b/Firmware/mmu.h index 9411ec37..763c15ea 100644 --- a/Firmware/mmu.h +++ b/Firmware/mmu.h @@ -17,6 +17,13 @@ extern int16_t mmu_version; extern int16_t mmu_buildnr; +#define MMU_CMD_NONE 0 +#define MMU_CMD_T0 0x10 +#define MMU_CMD_T1 0x11 +#define MMU_CMD_T2 0x12 +#define MMU_CMD_T3 0x13 +#define MMU_CMD_T4 0x14 + extern int mmu_puts_P(const char* str); extern int mmu_printf_P(const char* format, ...); @@ -33,8 +40,9 @@ extern void mmu_reset(void); extern int8_t mmu_set_filament_type(uint8_t extruder, uint8_t filament); +extern void mmu_command(uint8_t cmd); -extern bool mmu_get_response(bool timeout); +extern bool mmu_get_response(void); extern void manage_response(bool move_axes, bool turn_off_nozzle);