From e13e5c318b72c54cf72461bc817d8e1a8dd1b73e Mon Sep 17 00:00:00 2001 From: kbracher Date: Wed, 26 Mar 2025 16:28:43 +0100 Subject: [PATCH] add: convert temperatures to degrees --- .../Core/Src/ADBMS_Abstraction.c | 2 +- AMS_Master_Code/Core/Src/main.c | 42 +++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c index 34ef50b..4db1611 100644 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c @@ -161,7 +161,7 @@ HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module (*module)[N_BMS]) { CHECK_RETURN(readCMD(RDSTATA, rxbuf, STATUS_GROUP_A_SIZE)); for (size_t i = 0; i < N_BMS; i++) { size_t offset = BUFFER_BMS_OFFSET(i, STATUS_GROUP_A_SIZE); - (*module)[i].internalDieTemp = rxbuf[offset + 2] | (rxbuf[offset + 3] << 8); + (*module)[i].internalDieTemp = (mV_from_ADBMS6830(rxbuf[offset + 2] | (rxbuf[offset + 3] << 8)) / 7.5) + 273; //datasheet page 72 } CHECK_RETURN(readCMD(RDSTATB, rxbuf, STATUS_GROUP_B_SIZE)); diff --git a/AMS_Master_Code/Core/Src/main.c b/AMS_Master_Code/Core/Src/main.c index 89c8027..b7ff39a 100644 --- a/AMS_Master_Code/Core/Src/main.c +++ b/AMS_Master_Code/Core/Src/main.c @@ -61,7 +61,32 @@ static void MX_SPI1_Init(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ -uint32_t volatile logging_mask = 0b111111; // no LOG_LEVEL_NOISY +uint32_t volatile logging_mask = 0b11111; // no LOG_LEVEL_NOISY + +#define NTC_A1 0.003354016f // see NTC RT csv from Vishay +#define NTC_B1 0.000300131f +#define NTC_C1 5.08516E-06f +#define NTC_D1 2.18765E-07f +#define NTC_R25 10000.0f // 10k ohm at 25C + +#define NTC_R_DIVIDER 10000.0f +#define NTC_V_DIVIDER 5.000f // 5V + +[[gnu::always_inline]] +static inline float ntc_ohms_to_celsius(float ohms) { + float log_ohms = logf(ohms / NTC_R25); + return 1.0f / (NTC_A1 + NTC_B1 * log_ohms + NTC_C1 * log_ohms * log_ohms + NTC_D1 * log_ohms * log_ohms * log_ohms) - 273.15f; +} + +[[gnu::always_inline]] +static inline float ohms_from_ntc_v(float v) { + return (NTC_R_DIVIDER * v) / (NTC_V_DIVIDER - v); +} + +static inline int ntc_mv_to_celsius(int mv) { + float v = (float)mv * 0.001f; + return (int)ntc_ohms_to_celsius(ohms_from_ntc_v(v)); +} /* USER CODE END 0 */ /** @@ -161,14 +186,17 @@ int main(void) modules[i].auxVoltages[5], modules[i].auxVoltages[6], modules[i].auxVoltages[7], modules[i].auxVoltages[8], modules[i].auxVoltages[9]); + + // Print temperatures + debug_log(LOG_LEVEL_INFO, " GPIO as temperatures (°C):"); debug_log(LOG_LEVEL_INFO, " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d", - modules[i].GPIO_Values[0], modules[i].GPIO_Values[1], - modules[i].GPIO_Values[2], modules[i].GPIO_Values[3], - modules[i].GPIO_Values[4]); + ntc_mv_to_celsius(modules[i].auxVoltages[0]), ntc_mv_to_celsius(modules[i].auxVoltages[1]), + ntc_mv_to_celsius(modules[i].auxVoltages[2]), ntc_mv_to_celsius(modules[i].auxVoltages[3]), + ntc_mv_to_celsius(modules[i].auxVoltages[4])); debug_log(LOG_LEVEL_INFO, " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d", - modules[i].GPIO_Values[5], modules[i].GPIO_Values[6], - modules[i].GPIO_Values[7], modules[i].GPIO_Values[8], - modules[i].GPIO_Values[9]); + ntc_mv_to_celsius(modules[i].auxVoltages[5]), ntc_mv_to_celsius(modules[i].auxVoltages[6]), + ntc_mv_to_celsius(modules[i].auxVoltages[7]), ntc_mv_to_celsius(modules[i].auxVoltages[8]), + ntc_mv_to_celsius(modules[i].auxVoltages[9])); debug_log(LOG_LEVEL_INFO, " Internal temp: %d, VAnalog: %d, VDigital: %d, VRef: %d", modules[i].internalDieTemp, modules[i].analogSupplyVoltage,