From 81d7a6b81d2171915bf5cfa4f14255e683743959 Mon Sep 17 00:00:00 2001 From: ellensp Date: Tue, 27 Apr 2021 16:36:39 +1200 Subject: [PATCH] Use temp_info_t for temp_redundant (#21715) Fixes #21712 Co-authored-by: Scott Lahteine --- Marlin/src/module/temperature.cpp | 49 +++++++++++++++---------------- Marlin/src/module/temperature.h | 30 +++++++++++-------- 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp index 8e2b2d6b91..462af7f61a 100644 --- a/Marlin/src/module/temperature.cpp +++ b/Marlin/src/module/temperature.cpp @@ -256,7 +256,11 @@ const char str_t_thermal_runaway[] PROGMEM = STR_T_THERMAL_RUNAWAY, #endif #if HAS_HOTEND - hotend_info_t Temperature::temp_hotend[HOTEND_TEMPS]; // = { 0 } + hotend_info_t Temperature::temp_hotend[HOTENDS]; + #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT) + temp_info_t Temperature::temp_redundant; + #endif + #define _HMT(N) HEATER_##N##_MAXTEMP, const celsius_t Temperature::hotend_maxtemp[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP, HEATER_5_MAXTEMP, HEATER_6_MAXTEMP, HEATER_7_MAXTEMP); #endif @@ -420,11 +424,6 @@ const char str_t_thermal_runaway[] PROGMEM = STR_T_THERMAL_RUNAWAY, bool Temperature::inited = false; #endif -#if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT) - int16_t Temperature::redundant_temperature_raw = 0; - celsius_float_t Temperature::redundant_temperature = 0.0; -#endif - volatile bool Temperature::raw_temps_ready = false; #if ENABLED(PID_EXTRUSION_SCALING) @@ -1225,12 +1224,12 @@ void Temperature::manage_heater() { #if DISABLED(IGNORE_THERMOCOUPLE_ERRORS) #if TEMP_SENSOR_0_IS_MAX_TC - if (temp_hotend[0].celsius > _MIN(HEATER_0_MAXTEMP, TEMP_SENSOR_0_MAX_TC_TMAX - 1.0)) max_temp_error(H_E0); - if (temp_hotend[0].celsius < _MAX(HEATER_0_MINTEMP, TEMP_SENSOR_0_MAX_TC_TMIN + .01)) min_temp_error(H_E0); + if (degHotend(0) > _MIN(HEATER_0_MAXTEMP, TEMP_SENSOR_0_MAX_TC_TMAX - 1.0)) max_temp_error(H_E0); + if (degHotend(0) < _MAX(HEATER_0_MINTEMP, TEMP_SENSOR_0_MAX_TC_TMIN + .01)) min_temp_error(H_E0); #endif #if TEMP_SENSOR_1_IS_MAX_TC - if (temp_hotend[1].celsius > _MIN(HEATER_1_MAXTEMP, TEMP_SENSOR_1_MAX_TC_TMAX - 1.0)) max_temp_error(H_E1); - if (temp_hotend[1].celsius < _MAX(HEATER_1_MINTEMP, TEMP_SENSOR_1_MAX_TC_TMIN + .01)) min_temp_error(H_E1); + if (TERN(TEMP_SENSOR_1_AS_REDUNDANT, degHotendRedundant(), degHotend(1)) > _MIN(HEATER_1_MAXTEMP, TEMP_SENSOR_1_MAX_TC_TMAX - 1.0)) max_temp_error(H_E1); + if (TERN(TEMP_SENSOR_1_AS_REDUNDANT, degHotendRedundant(), degHotend(1)) < _MAX(HEATER_1_MINTEMP, TEMP_SENSOR_1_MAX_TC_TMIN + .01)) min_temp_error(H_E1); #endif #endif @@ -1266,7 +1265,7 @@ void Temperature::manage_heater() { #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT) // Make sure measured temperatures are close together - if (ABS(temp_hotend[0].celsius - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) + if (ABS(degHotend(0) - degHotendRedundant()) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) _temp_error(H_E0, PSTR(STR_REDUNDANCY), GET_TEXT(MSG_ERR_REDUNDANT_TEMP)); #endif @@ -1668,7 +1667,7 @@ void Temperature::manage_heater() { SERIAL_EOL(); } - celsius_float_t Temperature::user_thermistor_to_deg_c(const uint8_t t_index, const int raw) { + celsius_float_t Temperature::user_thermistor_to_deg_c(const uint8_t t_index, const int16_t raw) { //#if (MOTHERBOARD == BOARD_RAMPS_14_EFB) // static uint32_t clocks_total = 0; // static uint32_t calls = 0; @@ -1717,8 +1716,8 @@ void Temperature::manage_heater() { #if HAS_HOTEND // Derived from RepRap FiveD extruder::getTemperature() // For hot end temperature measurement. - celsius_float_t Temperature::analog_to_celsius_hotend(const int raw, const uint8_t e) { - if (e > HOTENDS - DISABLED(TEMP_SENSOR_1_AS_REDUNDANT)) { + celsius_float_t Temperature::analog_to_celsius_hotend(const int16_t raw, const uint8_t e) { + if (e >= HOTENDS + ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)) { SERIAL_ERROR_START(); SERIAL_ECHO(e); SERIAL_ECHOLNPGM(STR_INVALID_EXTRUDER_NUM); @@ -1826,7 +1825,7 @@ void Temperature::manage_heater() { #if HAS_HEATED_BED // For bed temperature measurement. - celsius_float_t Temperature::analog_to_celsius_bed(const int raw) { + celsius_float_t Temperature::analog_to_celsius_bed(const int16_t raw) { #if TEMP_SENSOR_BED_IS_CUSTOM return user_thermistor_to_deg_c(CTI_BED, raw); #elif TEMP_SENSOR_BED_IS_THERMISTOR @@ -1844,7 +1843,7 @@ void Temperature::manage_heater() { #if HAS_TEMP_CHAMBER // For chamber temperature measurement. - celsius_float_t Temperature::analog_to_celsius_chamber(const int raw) { + celsius_float_t Temperature::analog_to_celsius_chamber(const int16_t raw) { #if TEMP_SENSOR_CHAMBER_IS_CUSTOM return user_thermistor_to_deg_c(CTI_CHAMBER, raw); #elif TEMP_SENSOR_CHAMBER_IS_THERMISTOR @@ -1862,7 +1861,7 @@ void Temperature::manage_heater() { #if HAS_TEMP_COOLER // For cooler temperature measurement. - celsius_float_t Temperature::analog_to_celsius_cooler(const int raw) { + celsius_float_t Temperature::analog_to_celsius_cooler(const int16_t raw) { #if TEMP_SENSOR_COOLER_IS_CUSTOM return user_thermistor_to_deg_c(CTI_COOLER, raw); #elif TEMP_SENSOR_COOLER_IS_THERMISTOR @@ -1880,7 +1879,7 @@ void Temperature::manage_heater() { #if HAS_TEMP_PROBE // For probe temperature measurement. - celsius_float_t Temperature::analog_to_celsius_probe(const int raw) { + celsius_float_t Temperature::analog_to_celsius_probe(const int16_t raw) { #if TEMP_SENSOR_PROBE_IS_CUSTOM return user_thermistor_to_deg_c(CTI_PROBE, raw); #elif TEMP_SENSOR_PROBE_IS_THERMISTOR @@ -1904,15 +1903,15 @@ void Temperature::manage_heater() { */ void Temperature::updateTemperaturesFromRawValues() { TERN_(TEMP_SENSOR_0_IS_MAX_TC, temp_hotend[0].raw = READ_MAX_TC(0)); - TERN_(TEMP_SENSOR_1_IS_MAX_TC, temp_hotend[1].raw = READ_MAX_TC(1)); + TERN_(TEMP_SENSOR_1_IS_MAX_TC, TERN(TEMP_SENSOR_1_AS_REDUNDANT, temp_redundant, temp_hotend[1]).raw = READ_MAX_TC(1)); #if HAS_HOTEND HOTEND_LOOP() temp_hotend[e].celsius = analog_to_celsius_hotend(temp_hotend[e].raw, e); #endif + TERN_(TEMP_SENSOR_1_AS_REDUNDANT, temp_redundant.celsius = analog_to_celsius_hotend(temp_redundant.raw, 1)); TERN_(HAS_HEATED_BED, temp_bed.celsius = analog_to_celsius_bed(temp_bed.raw)); TERN_(HAS_TEMP_CHAMBER, temp_chamber.celsius = analog_to_celsius_chamber(temp_chamber.raw)); TERN_(HAS_TEMP_COOLER, temp_cooler.celsius = analog_to_celsius_cooler(temp_cooler.raw)); TERN_(HAS_TEMP_PROBE, temp_probe.celsius = analog_to_celsius_probe(temp_probe.raw)); - TERN_(TEMP_SENSOR_1_AS_REDUNDANT, redundant_temperature = analog_to_celsius_hotend(redundant_temperature_raw, 1)); TERN_(FILAMENT_WIDTH_SENSOR, filwidth.update_measured_mm()); TERN_(HAS_POWER_MONITOR, power_monitor.capture_values()); @@ -2707,7 +2706,7 @@ void Temperature::update_raw_temperatures() { #if HAS_TEMP_ADC_1 #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT) - redundant_temperature_raw = temp_hotend[1].acc; + temp_redundant.update(); #elif !TEMP_SENSOR_1_IS_MAX_TC temp_hotend[1].update(); #endif @@ -2741,7 +2740,7 @@ void Temperature::readings_ready() { #if HAS_HOTEND HOTEND_LOOP() temp_hotend[e].reset(); - TERN_(TEMP_SENSOR_1_AS_REDUNDANT, temp_hotend[1].reset()); + TERN_(TEMP_SENSOR_1_AS_REDUNDANT, temp_redundant.reset()); #endif TERN_(HAS_HEATED_BED, temp_bed.reset()); @@ -3245,7 +3244,7 @@ void Temperature::isr() { #if HAS_TEMP_ADC_1 case PrepareTemp_1: HAL_START_ADC(TEMP_1_PIN); break; - case MeasureTemp_1: ACCUMULATE_ADC(temp_hotend[1]); break; + case MeasureTemp_1: ACCUMULATE_ADC(TERN(TEMP_SENSOR_1_AS_REDUNDANT, temp_redundant, temp_hotend[1])); break; #endif #if HAS_TEMP_ADC_2 @@ -3443,9 +3442,9 @@ void Temperature::isr() { #endif ); #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT) - if (include_r) print_heater_state(redundant_temperature, degTargetHotend(target_extruder) + if (include_r) print_heater_state(degHotendRedundant(), degTargetHotend(0) #if ENABLED(SHOW_TEMP_ADC_VALUES) - , redundant_temperature_raw + , rawHotendTempRedundant() #endif , H_REDUNDANT ); diff --git a/Marlin/src/module/temperature.h b/Marlin/src/module/temperature.h index b7d52e8a08..2bb773f805 100644 --- a/Marlin/src/module/temperature.h +++ b/Marlin/src/module/temperature.h @@ -321,8 +321,10 @@ class Temperature { public: #if HAS_HOTEND - #define HOTEND_TEMPS (HOTENDS + ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)) - static hotend_info_t temp_hotend[HOTEND_TEMPS]; + #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT) + static temp_info_t temp_redundant; + #endif + static hotend_info_t temp_hotend[HOTENDS]; static const celsius_t hotend_maxtemp[HOTENDS]; static inline celsius_t hotend_max_target(const uint8_t e) { return hotend_maxtemp[e] - (HOTEND_OVERSHOOT); } #endif @@ -423,11 +425,6 @@ class Temperature { static hotend_watch_t watch_hotend[HOTENDS]; #endif - #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT) - static uint16_t redundant_temperature_raw; - static celsius_t redundant_temperature; - #endif - #if ENABLED(PID_EXTRUSION_SCALING) static int32_t last_e_position, lpq[LPQ_MAX_LEN]; static lpq_ptr_t lpq_ptr; @@ -501,7 +498,7 @@ class Temperature { static user_thermistor_t user_thermistor[USER_THERMISTORS]; static void log_user_thermistor(const uint8_t t_index, const bool eprom=false); static void reset_user_thermistors(); - static celsius_float_t user_thermistor_to_deg_c(const uint8_t t_index, const int raw); + static celsius_float_t user_thermistor_to_deg_c(const uint8_t t_index, const int16_t raw); static inline bool set_pull_up_res(int8_t t_index, float value) { //if (!WITHIN(t_index, 0, USER_THERMISTORS - 1)) return false; if (!WITHIN(value, 1, 1000000)) return false; @@ -529,19 +526,19 @@ class Temperature { #endif #if HAS_HOTEND - static celsius_float_t analog_to_celsius_hotend(const int raw, const uint8_t e); + static celsius_float_t analog_to_celsius_hotend(const int16_t raw, const uint8_t e); #endif #if HAS_HEATED_BED - static celsius_float_t analog_to_celsius_bed(const int raw); + static celsius_float_t analog_to_celsius_bed(const int16_t raw); #endif #if HAS_TEMP_PROBE - static celsius_float_t analog_to_celsius_probe(const int raw); + static celsius_float_t analog_to_celsius_probe(const int16_t raw); #endif #if HAS_TEMP_CHAMBER - static celsius_float_t analog_to_celsius_chamber(const int raw); + static celsius_float_t analog_to_celsius_chamber(const int16_t raw); #endif #if HAS_TEMP_COOLER - static celsius_float_t analog_to_celsius_cooler(const int raw); + static celsius_float_t analog_to_celsius_cooler(const int16_t raw); #endif #if HAS_FAN @@ -631,6 +628,10 @@ class Temperature { return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].celsius); } + #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT) + static inline celsius_float_t degHotendRedundant() { return temp_redundant.celsius; } + #endif + static inline celsius_t wholeDegHotend(const uint8_t E_NAME) { return TERN0(HAS_HOTEND, static_cast(temp_hotend[HOTEND_INDEX].celsius + 0.5f)); } @@ -639,6 +640,9 @@ class Temperature { static inline int16_t rawHotendTemp(const uint8_t E_NAME) { return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].raw); } + #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT) + static inline int16_t rawHotendTempRedundant() { return temp_redundant.raw; } + #endif #endif static inline celsius_t degTargetHotend(const uint8_t E_NAME) {