From b9220a04e94dbb028b2f040aa21f65a44fdbdd87 Mon Sep 17 00:00:00 2001 From: Kilian Bracher Date: Wed, 11 Jun 2025 17:37:57 +0200 Subject: [PATCH] properly handle negative temperatures --- AMS_Master_Code/Core/Inc/NTC.h | 6 +++--- AMS_Master_Code/Core/Inc/battery.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/AMS_Master_Code/Core/Inc/NTC.h b/AMS_Master_Code/Core/Inc/NTC.h index f4eed0b..308f378 100644 --- a/AMS_Master_Code/Core/Inc/NTC.h +++ b/AMS_Master_Code/Core/Inc/NTC.h @@ -12,7 +12,7 @@ #define NTC_D1 2.18765E-07f #define VREF 3000.0f // 3V -#define TEMP_CONV 100.0f // 655.35°C max +#define TEMP_CONV 100.0f // i.e. a value of 1500 means 15.00 °C (range -327.68 to 327.67 °C) #define CELSIUS_TO_KELVIN 273.15f #define CELSIUS_TO_KELVIN_SCALED (CELSIUS_TO_KELVIN * TEMP_CONV) @@ -22,9 +22,9 @@ // R_T/R_0 = 1 / V_REF / ADC - 1 [[gnu::optimize("fast-math")]] -static inline uint16_t ntc_mv_to_celsius(int16_t adc) { +static inline int16_t ntc_mv_to_celsius(int16_t adc) { const float log_ohms = logf(1 / ((VREF / adc) - 1)); - return (uint16_t)(TEMP_CONV / (NTC_A1 + NTC_B1 * log_ohms + NTC_C1 * log_ohms * log_ohms + + return (int16_t)(TEMP_CONV / (NTC_A1 + NTC_B1 * log_ohms + NTC_C1 * log_ohms * log_ohms + NTC_D1 * log_ohms * log_ohms * log_ohms) - CELSIUS_TO_KELVIN_SCALED); } #else diff --git a/AMS_Master_Code/Core/Inc/battery.h b/AMS_Master_Code/Core/Inc/battery.h index 288b12f..a519efc 100644 --- a/AMS_Master_Code/Core/Inc/battery.h +++ b/AMS_Master_Code/Core/Inc/battery.h @@ -23,7 +23,7 @@ extern struct { } pack; struct { BMS_Chip * chip; - int16_t cellTemps[10]; + int16_t cellTemps[10]; // °C with scaled factor 100, so 15000 = 150.0 °C struct { uint8_t min_v_idx : 4; uint8_t max_v_idx : 4; // cell index: 0-15 uint8_t min_t_idx : 4; uint8_t max_t_idx : 4; // used like this: battery->module[i].chip->cellVoltages[min_v_idx]