From 6d3e4e1f8ff0ee4d8dc4e821733ac3c151fe52b7 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 30 Apr 2016 16:29:02 -0700 Subject: [PATCH] Prevent stuck M109/M190 when target is changed --- Marlin/Marlin_main.cpp | 54 ++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 18d69cadad..37c472f3ca 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -4463,15 +4463,6 @@ inline void gcode_M109() { if (code_seen('B')) autotemp_max = code_value(); #endif - bool wants_to_cool = isCoolingHotend(target_extruder); - - // Exit if S, continue if S, R, or R - if (no_wait_for_cooling && wants_to_cool) return; - - // Prevents a wait-forever situation if R is misused i.e. M109 R0 - // Try to calculate a ballpark safe margin by halving EXTRUDE_MINTEMP - if (wants_to_cool && degTargetHotend(target_extruder) < (EXTRUDE_MINTEMP)/2) return; - #if TEMP_RESIDENCY_TIME > 0 millis_t residency_start_ms = 0; // Loop until the temperature has stabilized @@ -4481,11 +4472,15 @@ inline void gcode_M109() { #define TEMP_CONDITIONS (wants_to_cool ? isCoolingHotend(target_extruder) : isHeatingHotend(target_extruder)) #endif //TEMP_RESIDENCY_TIME > 0 - KEEPALIVE_STATE(NOT_BUSY); - + float theTarget = -1; + bool wants_to_cool; cancel_heatup = false; millis_t now, next_temp_ms = 0; + + KEEPALIVE_STATE(NOT_BUSY); + do { + now = millis(); if (ELAPSED(now, next_temp_ms)) { //Print temp & remaining time every 1s while waiting next_temp_ms = now + 1000UL; @@ -4506,12 +4501,25 @@ inline void gcode_M109() { #endif } + // Target temperature might be changed during the loop + if (theTarget != degTargetHotend(target_extruder)) { + theTarget = degTargetHotend(target_extruder); + wants_to_cool = isCoolingHotend(target_extruder); + + // Exit if S, continue if S, R, or R + if (no_wait_for_cooling && wants_to_cool) break; + + // Prevent a wait-forever situation if R is misused i.e. M109 R0 + // Try to calculate a ballpark safe margin by halving EXTRUDE_MINTEMP + if (wants_to_cool && theTarget < (EXTRUDE_MINTEMP)/2) break; + } + idle(); refresh_cmd_timeout(); // to prevent stepper_inactive_time from running out #if TEMP_RESIDENCY_TIME > 0 - float temp_diff = fabs(degTargetHotend(target_extruder) - degHotend(target_extruder)); + float temp_diff = fabs(theTarget - degHotend(target_extruder)); if (!residency_start_ms) { // Start the TEMP_RESIDENCY_TIME timer when we reach target temp for the first time. @@ -4543,11 +4551,6 @@ inline void gcode_M109() { bool no_wait_for_cooling = code_seen('S'); if (no_wait_for_cooling || code_seen('R')) setTargetBed(code_value()); - bool wants_to_cool = isCoolingBed(); - - // Exit if S, continue if S, R, or R - if (no_wait_for_cooling && wants_to_cool) return; - #if TEMP_BED_RESIDENCY_TIME > 0 millis_t residency_start_ms = 0; // Loop until the temperature has stabilized @@ -4557,11 +4560,13 @@ inline void gcode_M109() { #define TEMP_BED_CONDITIONS (wants_to_cool ? isCoolingBed() : isHeatingBed()) #endif //TEMP_BED_RESIDENCY_TIME > 0 + float theTarget = -1; + bool wants_to_cool; cancel_heatup = false; millis_t now, next_temp_ms = 0; - // Wait for temperature to come close enough KEEPALIVE_STATE(NOT_BUSY); + do { now = millis(); if (ELAPSED(now, next_temp_ms)) { //Print Temp Reading every 1 second while heating up. @@ -4581,6 +4586,19 @@ inline void gcode_M109() { #endif } + // Target temperature might be changed during the loop + if (theTarget != degTargetBed()) { + theTarget = degTargetBed(); + wants_to_cool = isCoolingBed(); + + // Exit if S, continue if S, R, or R + if (no_wait_for_cooling && wants_to_cool) break; + + // Prevent a wait-forever situation if R is misused i.e. M190 R0 + // Simply don't wait for cooling below 30C + if (wants_to_cool && theTarget < (EXTRUDE_MINTEMP)/2) break; + } + idle(); refresh_cmd_timeout(); // to prevent stepper_inactive_time from running out