From e878572b935352b50139306717c761cdb7d13380 Mon Sep 17 00:00:00 2001 From: Bjarke Istrup Pedersen Date: Mon, 7 Aug 2017 16:39:00 +0200 Subject: [PATCH 1/2] Ported MarlinFirmware/#3109 - Provide feedback to hosts when busy --- Firmware/Marlin_main.cpp | 62 +++++++++++++++++++ .../variants/1_75mm_MK1-RAMBo10a-E3Dv6full.h | 3 + .../variants/1_75mm_MK1-RAMBo13a-E3Dv6full.h | 3 + ...5mm_MK2-MultiMaterial-RAMBo10a-E3Dv6full.h | 3 + ...5mm_MK2-MultiMaterial-RAMBo13a-E3Dv6full.h | 3 + .../variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h | 3 + .../variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h | 3 + 7 files changed, 80 insertions(+) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index a783af48a..492defe70 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -368,6 +368,26 @@ int fanSpeed=0; bool cancel_heatup = false ; +#ifdef HOST_KEEPALIVE_FEATURE + // States for managing Marlin and host communication + // Marlin sends messages if blocked or busy + enum MarlinBusyState { + NOT_BUSY, // Not in a handler + IN_HANDLER, // Processing a GCode + IN_PROCESS, // Known to be blocking command input (as in G29) + PAUSED_FOR_USER, // Blocking pending any input + PAUSED_FOR_INPUT // Blocking pending text input (concept) + }; + + static MarlinBusyState busy_state = NOT_BUSY; + static long next_busy_signal_ms = -1; + + #define KEEPALIVE_STATE(n) do { busy_state = n; } while (0) +#else + #define host_keepalive(); + #define KEEPALIVE_STATE(n); +#endif + #ifdef FILAMENT_SENSOR //Variables for Filament Sensor input float filament_width_nominal=DEFAULT_NOMINAL_FILAMENT_DIA; //Set nominal filament width, can be changed with M404 @@ -1300,6 +1320,33 @@ int serial_read_stream() { } } +#ifdef HOST_KEEPALIVE_FEATURE +void host_keepalive() { + long ms = millis(); + if (busy_state != NOT_BUSY) { + if (ms < next_busy_signal_ms) return; + switch (busy_state) { + case NOT_BUSY: + break; + case IN_HANDLER: + case IN_PROCESS: + SERIAL_ECHO_START; + SERIAL_ECHOLNPGM("busy: processing"); + break; + case PAUSED_FOR_USER: + SERIAL_ECHO_START; + SERIAL_ECHOLNPGM("busy: paused for user"); + break; + case PAUSED_FOR_INPUT: + SERIAL_ECHO_START; + SERIAL_ECHOLNPGM("busy: paused for input"); + break; + } + } + next_busy_signal_ms = ms + 2000UL; +} +#endif + // The loop() function is called in an endless loop by the Arduino framework from the default main() routine. // Before loop(), the setup() function is called by the main() routine. void loop() @@ -1361,6 +1408,7 @@ void loop() manage_heater(); isPrintPaused ? manage_inactivity(true) : manage_inactivity(false); checkHitEndstops(); + host_keepalive(); lcd_update(); } @@ -2058,6 +2106,8 @@ void process_commands() float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD; int8_t SilentMode; #endif + KEEPALIVE_STATE(IN_HANDLER); + if (code_seen("M117")) { //moved to highest priority place to be able to to print strings which includes "G", "PRUSA" and "^" starpos = (strchr(strchr_pointer + 5, '*')); if (starpos != NULL) @@ -2388,6 +2438,8 @@ void process_commands() case 28: //G28 Home all Axis one at a time homing_flag = true; + KEEPALIVE_STATE(IN_HANDLER); + #ifdef ENABLE_AUTO_BED_LEVELING plan_bed_level_matrix.set_to_identity(); //Reset the plane ("erase" all leveling data) #endif //ENABLE_AUTO_BED_LEVELING @@ -3441,20 +3493,24 @@ void process_commands() previous_millis_cmd = millis(); if (codenum > 0){ codenum += millis(); // keep track of when we started waiting + KEEPALIVE_STATE(PAUSED_FOR_USER); while(millis() < codenum && !lcd_clicked()){ manage_heater(); manage_inactivity(true); lcd_update(); } + KEEPALIVE_STATE(IN_HANDLER); lcd_ignore_click(false); }else{ if (!lcd_detected()) break; + KEEPALIVE_STATE(PAUSED_FOR_USER); while(!lcd_clicked()){ manage_heater(); manage_inactivity(true); lcd_update(); } + KEEPALIVE_STATE(IN_HANDLER); } if (IS_SD_PRINTING) LCD_MESSAGERPGM(MSG_RESUMING); @@ -4940,6 +4996,8 @@ Sigma_Exit: temp=70; if (code_seen('S')) temp=code_value(); if (code_seen('C')) c=code_value(); + + KEEPALIVE_STATE(NOT_BUSY); PID_autotune(temp, e, c); } break; @@ -5217,6 +5275,7 @@ case 404: //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or disp int counterBeep = 0; lcd_wait_interact(); load_filament_time = millis(); + KEEPALIVE_STATE(PAUSED_FOR_USER); while(!lcd_clicked()){ cnt++; @@ -5253,6 +5312,7 @@ case 404: //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or disp } } + KEEPALIVE_STATE(IN_HANDLER); WRITE(BEEPER, LOW); #ifdef SNMM display_loading(); @@ -5717,6 +5777,8 @@ case 404: //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or disp SERIAL_ECHOLNPGM("\""); } + KEEPALIVE_STATE(NOT_BUSY); + ClearToSend(); } diff --git a/Firmware/variants/1_75mm_MK1-RAMBo10a-E3Dv6full.h b/Firmware/variants/1_75mm_MK1-RAMBo10a-E3Dv6full.h index ba9f55289..0dc72e79f 100644 --- a/Firmware/variants/1_75mm_MK1-RAMBo10a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK1-RAMBo10a-E3Dv6full.h @@ -186,6 +186,9 @@ ADDITIONAL FEATURES SETTINGS #define TEMP_RUNAWAY_EXTRUDER_HYSTERESIS 15 #define TEMP_RUNAWAY_EXTRUDER_TIMEOUT 45 +// USB host keep alive +#define HOST_KEEPALIVE_FEATURE + /*------------------------------------ MOTOR CURRENT SETTINGS *------------------------------------*/ diff --git a/Firmware/variants/1_75mm_MK1-RAMBo13a-E3Dv6full.h b/Firmware/variants/1_75mm_MK1-RAMBo13a-E3Dv6full.h index 1822a9d73..634c578a3 100644 --- a/Firmware/variants/1_75mm_MK1-RAMBo13a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK1-RAMBo13a-E3Dv6full.h @@ -186,6 +186,9 @@ ADDITIONAL FEATURES SETTINGS #define TEMP_RUNAWAY_EXTRUDER_HYSTERESIS 15 #define TEMP_RUNAWAY_EXTRUDER_TIMEOUT 45 +// USB host keep alive +#define HOST_KEEPALIVE_FEATURE + /*------------------------------------ MOTOR CURRENT SETTINGS *------------------------------------*/ diff --git a/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo10a-E3Dv6full.h b/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo10a-E3Dv6full.h index 06a7410c3..eab7541a1 100644 --- a/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo10a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo10a-E3Dv6full.h @@ -181,6 +181,9 @@ ADDITIONAL FEATURES SETTINGS #define TEMP_RUNAWAY_EXTRUDER_HYSTERESIS 15 #define TEMP_RUNAWAY_EXTRUDER_TIMEOUT 45 +// USB host keep alive +#define HOST_KEEPALIVE_FEATURE + /*------------------------------------ MOTOR CURRENT SETTINGS *------------------------------------*/ diff --git a/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo13a-E3Dv6full.h b/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo13a-E3Dv6full.h index a0afa5f7f..a39519bd2 100644 --- a/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo13a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo13a-E3Dv6full.h @@ -183,6 +183,9 @@ ADDITIONAL FEATURES SETTINGS #define TEMP_RUNAWAY_EXTRUDER_HYSTERESIS 15 #define TEMP_RUNAWAY_EXTRUDER_TIMEOUT 45 +// USB host keep alive +#define HOST_KEEPALIVE_FEATURE + /*------------------------------------ MOTOR CURRENT SETTINGS *------------------------------------*/ diff --git a/Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h b/Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h index d7019845f..a182f9fec 100644 --- a/Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h @@ -181,6 +181,9 @@ ADDITIONAL FEATURES SETTINGS #define TEMP_RUNAWAY_EXTRUDER_HYSTERESIS 15 #define TEMP_RUNAWAY_EXTRUDER_TIMEOUT 45 +// USB host keep alive +#define HOST_KEEPALIVE_FEATURE + /*------------------------------------ MOTOR CURRENT SETTINGS *------------------------------------*/ diff --git a/Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h b/Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h index 720e74f5e..ff37a14e8 100644 --- a/Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h @@ -183,6 +183,9 @@ ADDITIONAL FEATURES SETTINGS #define TEMP_RUNAWAY_EXTRUDER_HYSTERESIS 15 #define TEMP_RUNAWAY_EXTRUDER_TIMEOUT 45 +// USB host keep alive +#define HOST_KEEPALIVE_FEATURE + /*------------------------------------ MOTOR CURRENT SETTINGS *------------------------------------*/ From 9c7de9292af1b854bdc561eee92482f3baa5b60a Mon Sep 17 00:00:00 2001 From: Bjarke Istrup Pedersen Date: Wed, 9 Aug 2017 10:24:19 +0200 Subject: [PATCH 2/2] Ported the remaining HOST_KEEPALIVE commits from Marlin --- Firmware/Configuration.h | 8 ++++- Firmware/Marlin.h | 6 ++++ Firmware/Marlin_main.cpp | 34 +++++++++++++++---- .../variants/1_75mm_MK1-RAMBo10a-E3Dv6full.h | 3 -- .../variants/1_75mm_MK1-RAMBo13a-E3Dv6full.h | 3 -- ...5mm_MK2-MultiMaterial-RAMBo10a-E3Dv6full.h | 3 -- ...5mm_MK2-MultiMaterial-RAMBo13a-E3Dv6full.h | 3 -- .../variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h | 3 -- .../variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h | 3 -- 9 files changed, 40 insertions(+), 26 deletions(-) diff --git a/Firmware/Configuration.h b/Firmware/Configuration.h index 5a3abcd4c..ceb8ae89c 100644 --- a/Firmware/Configuration.h +++ b/Firmware/Configuration.h @@ -461,7 +461,13 @@ const bool Z_MAX_ENDSTOP_INVERTING = true; // set to true to invert the logic of // please keep turned on if you can. //#define EEPROM_CHITCHAT - +// Host Keepalive +// +// When enabled Marlin will send a busy status message to the host +// every couple of seconds when it can't accept commands. +// +#define HOST_KEEPALIVE_FEATURE // Disable this if your host doesn't like keepalive messages +#define HOST_KEEPALIVE_INTERVAL 2 // Number of seconds between "busy" messages. Set with M113. //LCD and SD support #define ULTRA_LCD //general LCD support, also 16x2 diff --git a/Firmware/Marlin.h b/Firmware/Marlin.h index a5c7ffa2a..1215bd468 100644 --- a/Firmware/Marlin.h +++ b/Firmware/Marlin.h @@ -108,6 +108,8 @@ FORCE_INLINE void serialprintPGM(const char *str) } } +#define NOMORE(v,n) do{ if (v > n) v = n; }while(0) + bool is_buffer_empty(); void get_command(); void process_commands(); @@ -280,6 +282,10 @@ extern float retract_length, retract_length_swap, retract_feedrate, retract_zlif extern float retract_recover_length, retract_recover_length_swap, retract_recover_feedrate; #endif +#ifdef HOST_KEEPALIVE_FEATURE +extern uint8_t host_keepalive_interval; +#endif + extern unsigned long starttime; extern unsigned long stoptime; extern int bowden_length[4]; diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 492defe70..a0ce4e307 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -151,6 +151,7 @@ // Rxxx Wait for extruder current temp to reach target temp. Waits when heating and cooling // IF AUTOTEMP is enabled, S B F. Exit autotemp by any M109 without F // M112 - Emergency stop +// M113 - Get or set the timeout interval for Host Keepalive "busy" messages // M114 - Output current position to serial port // M115 - Capabilities string // M117 - display message @@ -380,8 +381,8 @@ bool cancel_heatup = false ; }; static MarlinBusyState busy_state = NOT_BUSY; - static long next_busy_signal_ms = -1; - + static long prev_busy_signal_ms = -1; + uint8_t host_keepalive_interval = HOST_KEEPALIVE_INTERVAL; #define KEEPALIVE_STATE(n) do { busy_state = n; } while (0) #else #define host_keepalive(); @@ -1321,13 +1322,15 @@ int serial_read_stream() { } #ifdef HOST_KEEPALIVE_FEATURE +/** +* Output a "busy" message at regular intervals +* while the machine is not accepting commands. +*/ void host_keepalive() { long ms = millis(); - if (busy_state != NOT_BUSY) { - if (ms < next_busy_signal_ms) return; + if (host_keepalive_interval && busy_state != NOT_BUSY) { + if (ms - prev_busy_signal_ms < 1000UL * host_keepalive_interval) return; switch (busy_state) { - case NOT_BUSY: - break; case IN_HANDLER: case IN_PROCESS: SERIAL_ECHO_START; @@ -1343,7 +1346,7 @@ void host_keepalive() { break; } } - next_busy_signal_ms = ms + 2000UL; + prev_busy_signal_ms = ms; } #endif @@ -4215,6 +4218,7 @@ Sigma_Exit: }} #endif SERIAL_PROTOCOLLN(""); + KEEPALIVE_STATE(NOT_BUSY); return; break; case 109: @@ -4252,11 +4256,14 @@ Sigma_Exit: /* See if we are heating up or cooling down */ target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling + KEEPALIVE_STATE(NOT_BUSY); + cancel_heatup = false; wait_for_heater(codenum); //loops until target temperature is reached LCD_MESSAGERPGM(MSG_HEATING_COMPLETE); + KEEPALIVE_STATE(IN_HANDLER); heating_status = 2; if (farm_mode) { prusa_statistics(2); }; @@ -4284,6 +4291,7 @@ Sigma_Exit: cancel_heatup = false; target_direction = isHeatingBed(); // true if heating, false if cooling + KEEPALIVE_STATE(NOT_BUSY); while ( (target_direction)&&(!cancel_heatup) ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false)) ) { if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up. @@ -4306,6 +4314,7 @@ Sigma_Exit: lcd_update(); } LCD_MESSAGERPGM(MSG_BED_DONE); + KEEPALIVE_STATE(IN_HANDLER); heating_status = 4; previous_millis_cmd = millis(); @@ -4449,6 +4458,17 @@ Sigma_Exit: else gcode_LastN = 0; break; +#ifdef HOST_KEEPALIVE_FEATURE + case 113: // M113 - Get or set Host Keepalive interval + if (code_seen('S')) { + host_keepalive_interval = (uint8_t)code_value_short(); + NOMORE(host_keepalive_interval, 60); + } else { + SERIAL_ECHO_START; + SERIAL_ECHOPAIR("M113 S", (unsigned long)host_keepalive_interval); + SERIAL_PROTOCOLLN(""); + } +#endif case 115: // M115 if (code_seen('V')) { // Report the Prusa version number. diff --git a/Firmware/variants/1_75mm_MK1-RAMBo10a-E3Dv6full.h b/Firmware/variants/1_75mm_MK1-RAMBo10a-E3Dv6full.h index 0dc72e79f..ba9f55289 100644 --- a/Firmware/variants/1_75mm_MK1-RAMBo10a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK1-RAMBo10a-E3Dv6full.h @@ -186,9 +186,6 @@ ADDITIONAL FEATURES SETTINGS #define TEMP_RUNAWAY_EXTRUDER_HYSTERESIS 15 #define TEMP_RUNAWAY_EXTRUDER_TIMEOUT 45 -// USB host keep alive -#define HOST_KEEPALIVE_FEATURE - /*------------------------------------ MOTOR CURRENT SETTINGS *------------------------------------*/ diff --git a/Firmware/variants/1_75mm_MK1-RAMBo13a-E3Dv6full.h b/Firmware/variants/1_75mm_MK1-RAMBo13a-E3Dv6full.h index 634c578a3..1822a9d73 100644 --- a/Firmware/variants/1_75mm_MK1-RAMBo13a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK1-RAMBo13a-E3Dv6full.h @@ -186,9 +186,6 @@ ADDITIONAL FEATURES SETTINGS #define TEMP_RUNAWAY_EXTRUDER_HYSTERESIS 15 #define TEMP_RUNAWAY_EXTRUDER_TIMEOUT 45 -// USB host keep alive -#define HOST_KEEPALIVE_FEATURE - /*------------------------------------ MOTOR CURRENT SETTINGS *------------------------------------*/ diff --git a/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo10a-E3Dv6full.h b/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo10a-E3Dv6full.h index eab7541a1..06a7410c3 100644 --- a/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo10a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo10a-E3Dv6full.h @@ -181,9 +181,6 @@ ADDITIONAL FEATURES SETTINGS #define TEMP_RUNAWAY_EXTRUDER_HYSTERESIS 15 #define TEMP_RUNAWAY_EXTRUDER_TIMEOUT 45 -// USB host keep alive -#define HOST_KEEPALIVE_FEATURE - /*------------------------------------ MOTOR CURRENT SETTINGS *------------------------------------*/ diff --git a/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo13a-E3Dv6full.h b/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo13a-E3Dv6full.h index a39519bd2..a0afa5f7f 100644 --- a/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo13a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo13a-E3Dv6full.h @@ -183,9 +183,6 @@ ADDITIONAL FEATURES SETTINGS #define TEMP_RUNAWAY_EXTRUDER_HYSTERESIS 15 #define TEMP_RUNAWAY_EXTRUDER_TIMEOUT 45 -// USB host keep alive -#define HOST_KEEPALIVE_FEATURE - /*------------------------------------ MOTOR CURRENT SETTINGS *------------------------------------*/ diff --git a/Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h b/Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h index a182f9fec..d7019845f 100644 --- a/Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h @@ -181,9 +181,6 @@ ADDITIONAL FEATURES SETTINGS #define TEMP_RUNAWAY_EXTRUDER_HYSTERESIS 15 #define TEMP_RUNAWAY_EXTRUDER_TIMEOUT 45 -// USB host keep alive -#define HOST_KEEPALIVE_FEATURE - /*------------------------------------ MOTOR CURRENT SETTINGS *------------------------------------*/ diff --git a/Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h b/Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h index ff37a14e8..720e74f5e 100644 --- a/Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h +++ b/Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h @@ -183,9 +183,6 @@ ADDITIONAL FEATURES SETTINGS #define TEMP_RUNAWAY_EXTRUDER_HYSTERESIS 15 #define TEMP_RUNAWAY_EXTRUDER_TIMEOUT 45 -// USB host keep alive -#define HOST_KEEPALIVE_FEATURE - /*------------------------------------ MOTOR CURRENT SETTINGS *------------------------------------*/