From c2522ce1f50dd5efcf2ab3952a239097b8e3ce59 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 8 May 2016 16:51:33 -0700 Subject: [PATCH 1/3] Fallthru in thermal runaway test when TRState changes --- Marlin/temperature.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index f8c85fbc96..a8f9c916ff 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -1056,24 +1056,21 @@ void Temperature::init() { *state = TRInactive; // Inactive state waits for a target temperature to be set case TRInactive: - if (target_temperature > 0) { - tr_target_temperature[heater_index] = target_temperature; - *state = TRFirstHeating; - } - break; + if (target_temperature <= 0) break; + tr_target_temperature[heater_index] = target_temperature; + *state = TRFirstHeating; // When first heating, wait for the temperature to be reached then go to Stable state case TRFirstHeating: - if (temperature >= tr_target_temperature[heater_index]) *state = TRStable; - break; + if (temperature < tr_target_temperature[heater_index]) break; + *state = TRStable; // While the temperature is stable watch for a bad temperature case TRStable: - // If the temperature is over the target (-hysteresis) restart the timer - if (temperature >= tr_target_temperature[heater_index] - hysteresis_degc) - *timer = millis(); - // If the timer goes too long without a reset, trigger shutdown - else if (ELAPSED(millis(), *timer + period_seconds * 1000UL)) + if (temperature < tr_target_temperature[heater_index] - hysteresis_degc && ELAPSED(millis(), *timer)) *state = TRRunaway; - break; + else { + *timer = millis() + period_seconds * 1000UL; + break; + } case TRRunaway: _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY)); } From 6b13c430ae587c87f3d94485dbb7a16f905196f2 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 8 May 2016 17:01:46 -0700 Subject: [PATCH 2/3] The TRReset state is not needed with fall-through --- Marlin/temperature.cpp | 5 +---- Marlin/temperature.h | 6 +++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index a8f9c916ff..8879b4737f 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -1048,12 +1048,9 @@ void Temperature::init() { // If the target temperature changes, restart if (tr_target_temperature[heater_index] != target_temperature) - *state = TRReset; + *state = TRInactive; switch (*state) { - case TRReset: - *timer = 0; - *state = TRInactive; // Inactive state waits for a target temperature to be set case TRInactive: if (target_temperature <= 0) break; diff --git a/Marlin/temperature.h b/Marlin/temperature.h index 41e4ac16fd..a178f1fed0 100644 --- a/Marlin/temperature.h +++ b/Marlin/temperature.h @@ -358,17 +358,17 @@ class Temperature { #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED - typedef enum TRState { TRReset, TRInactive, TRFirstHeating, TRStable, TRRunaway } TRstate; + typedef enum TRState { TRInactive, TRFirstHeating, TRStable, TRRunaway } TRstate; void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc); #if ENABLED(THERMAL_PROTECTION_HOTENDS) - TRState thermal_runaway_state_machine[EXTRUDERS] = { TRReset }; + TRState thermal_runaway_state_machine[EXTRUDERS] = { TRInactive }; millis_t thermal_runaway_timer[EXTRUDERS] = { 0 }; #endif #if HAS_THERMALLY_PROTECTED_BED - TRState thermal_runaway_bed_state_machine = TRReset; + TRState thermal_runaway_bed_state_machine = TRInactive; millis_t thermal_runaway_bed_timer; #endif From 9b92bb8f31217cf785979a7f24ea74f6b574942e Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 8 May 2016 17:07:45 -0700 Subject: [PATCH 3/3] Set the initial state based on target temperature --- Marlin/temperature.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 8879b4737f..19e66c6742 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -1047,15 +1047,14 @@ void Temperature::init() { int heater_index = heater_id >= 0 ? heater_id : EXTRUDERS; // If the target temperature changes, restart - if (tr_target_temperature[heater_index] != target_temperature) - *state = TRInactive; + if (tr_target_temperature[heater_index] != target_temperature) { + tr_target_temperature[heater_index] = target_temperature; + *state = target_temperature > 0 ? TRFirstHeating : TRInactive; + } switch (*state) { // Inactive state waits for a target temperature to be set - case TRInactive: - if (target_temperature <= 0) break; - tr_target_temperature[heater_index] = target_temperature; - *state = TRFirstHeating; + case TRInactive: break; // When first heating, wait for the temperature to be reached then go to Stable state case TRFirstHeating: if (temperature < tr_target_temperature[heater_index]) break;