MMU communication - mmu_ready

This commit is contained in:
Robert Pelnar 2018-08-20 20:53:53 +02:00
parent 1cd0c6cfea
commit 94423e6f7e
3 changed files with 99 additions and 12 deletions

View file

@ -6803,8 +6803,9 @@ if((eSoundMode==e_SOUND_MODE_LOUD)||(eSoundMode==e_SOUND_MODE_ONCE))
if (mmu_enabled) if (mmu_enabled)
{ {
printf_P(PSTR("T code: %d \n"), tmp_extruder); //printf_P(PSTR("T code: %d \n"), tmp_extruder);
mmu_printf_P(PSTR("T%d\n"), tmp_extruder); //mmu_printf_P(PSTR("T%d\n"), tmp_extruder);
mmu_command(MMU_CMD_T0 + tmp_extruder);
manage_response(true, true); manage_response(true, true);
@ -7427,6 +7428,7 @@ if((eSoundMode==e_SOUND_MODE_LOUD)||(eSoundMode==e_SOUND_MODE_ONCE))
handle_status_leds(); handle_status_leds();
#endif #endif
check_axes_activity(); check_axes_activity();
// mmu_loop();
} }
void kill(const char *full_screen_message, unsigned char id) void kill(const char *full_screen_message, unsigned char id)

View file

@ -26,8 +26,12 @@ extern char choose_extruder_menu();
bool mmu_enabled = false; bool mmu_enabled = false;
bool mmu_ready = false;
int8_t mmu_state = 0; int8_t mmu_state = 0;
uint8_t mmu_cmd = 0;
uint8_t mmu_extruder = 0; uint8_t mmu_extruder = 0;
uint8_t tmp_extruder = 0; uint8_t tmp_extruder = 0;
@ -38,6 +42,9 @@ int16_t mmu_version = -1;
int16_t mmu_buildnr = -1; int16_t mmu_buildnr = -1;
uint32_t mmu_last_request = 0;
uint32_t mmu_last_response = 0;
//clear rx buffer //clear rx buffer
void mmu_clr_rx_buf(void) void mmu_clr_rx_buf(void)
@ -49,7 +56,9 @@ void mmu_clr_rx_buf(void)
int mmu_puts_P(const char* str) int mmu_puts_P(const char* str)
{ {
mmu_clr_rx_buf(); //clear rx buffer 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 //send command - printf
@ -60,19 +69,24 @@ int mmu_printf_P(const char* format, ...)
mmu_clr_rx_buf(); //clear rx buffer mmu_clr_rx_buf(); //clear rx buffer
int r = vfprintf_P(uart2io, format, args); //send command int r = vfprintf_P(uart2io, format, args); //send command
va_end(args); va_end(args);
mmu_last_request = millis();
return r; return r;
} }
//check 'ok' response //check 'ok' response
int8_t mmu_rx_ok(void) 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 //check 'start' response
int8_t mmu_rx_start(void) 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 //initialize mmu2 unit - first part - should be done at begining of startup process
@ -138,6 +152,50 @@ void mmu_loop(void)
mmu_state = 1; mmu_state = 1;
} }
return; 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; 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 //waits for "ok" from mmu
//function returns true if "ok" was received //function returns true if "ok" was received
//if timeout is set to true function return false if there is no "ok" received before timeout //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); 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]; float y_position_bckp = current_position[Y_AXIS];
while(!response) 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 (!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 (!mmu_print_saved) { //first occurence, we are saving current position, park print head in certain position and disable nozzle heater
if (lcd_update_enabled) { if (lcd_update_enabled) {
@ -306,8 +381,10 @@ void mmu_M600_load_filament(bool automatic)
lcd_print(" "); lcd_print(" ");
lcd_print(tmp_extruder + 1); lcd_print(tmp_extruder + 1);
snmm_filaments_used |= (1 << tmp_extruder); //for stop print 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); manage_response(false, true);
mmu_extruder = tmp_extruder; //filament change is finished mmu_extruder = tmp_extruder; //filament change is finished

View file

@ -17,6 +17,13 @@ extern int16_t mmu_version;
extern int16_t mmu_buildnr; 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_puts_P(const char* str);
extern int mmu_printf_P(const char* format, ...); 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 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); extern void manage_response(bool move_axes, bool turn_off_nozzle);