temp rework

This commit is contained in:
Moritz Ruffer 2025-03-26 22:52:00 +01:00
parent 5198da828d
commit d3a79d0fde
4 changed files with 34 additions and 40 deletions

View File

@ -1,18 +1,28 @@
#include <math.h> #include <math.h>
#define USE_CALC
#ifdef USE_CALC
#define NTC_A1 0.003354016f // see NTC RT csv from Vishay #define NTC_A1 0.003354016f // see NTC RT csv from Vishay
#define NTC_B1 0.000300131f #define NTC_B1 0.000300131f
#define NTC_C1 5.08516E-06f #define NTC_C1 5.08516E-06f
#define NTC_D1 2.18765E-07f #define NTC_D1 2.18765E-07f
#define VREF 3000.0 #define VREF 3000.0f // 3V
#define TEMP_CONV 1000.0f // 65.535°C max; maybe 100 for better safety against overflow
#define CELSIUS_TO_KELVIN 273.15f
#define CELSIUS_TO_KELVIN_SCALED CELSIUS_TO_KELVIN * TEMP_CONV
// More efficient (?) calc using: // More efficient (?) calc using:
// R_T = R_Pullup / (V_REF / ADC - 1) // R_T = R_Pullup / (V_REF / ADC - 1)
// With R_T/R_0 and R_0 = R_T@25C // With R_T/R_0 and R_0 = R_T@25C
// R_T/R_0 = 1 / V_REF / ADC - 1 // R_T/R_0 = 1 / V_REF / ADC - 1
static inline int ntc_mv_to_celsius(int16_t adc) { static inline uint16_t ntc_mv_to_celsius(int16_t adc) {
float log_ohms = logf(1/((VREF/adc)-1)); float log_ohms = logf(1/((VREF/adc)-1));
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; return (uint16_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
// Lookup Table coming soon; not really needed but fun?
#endif

View File

@ -79,6 +79,7 @@ struct ADBMS6830_Internal_Status {
typedef struct { typedef struct {
int16_t cellVoltages[MAXIMUM_CELL_VOLTAGES]; int16_t cellVoltages[MAXIMUM_CELL_VOLTAGES];
int16_t auxVoltages[MAXIMUM_AUX_VOLTAGES]; int16_t auxVoltages[MAXIMUM_AUX_VOLTAGES];
uint16_t cellTemps[MAXIMUM_AUX_VOLTAGES];
struct ADBMS6830_Internal_Status status; struct ADBMS6830_Internal_Status status;
uint16_t internalDieTemp; uint16_t internalDieTemp;

View File

@ -11,6 +11,7 @@
#include "config_ADBMS6830.h" #include "config_ADBMS6830.h"
#include "swo_log.h" #include "swo_log.h"
#include <stddef.h> #include <stddef.h>
#include "NTC.h"
extern uint8_t numberofCells; extern uint8_t numberofCells;
@ -128,37 +129,22 @@ HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module (*module)[N_BMS]) {
// return HAL_BUSY; // return HAL_BUSY;
} }
CHECK_RETURN( const size_t auxGroupSizes[] = {AUX_GROUP_A_SIZE, AUX_GROUP_B_SIZE, AUX_GROUP_C_SIZE, AUX_GROUP_D_SIZE};
readCMD(RDAUXA, rxbuf, const size_t auxVoltagesPerGroup[] = {3, 3, 3, 1}; // Number of voltages in each group
AUX_GROUP_A_SIZE)); // STATUS_GROUP_C_SIZE is the same as AUX_GROUP_A_SIZE, so we can reuse the buffer const uint8_t auxCommands[] = {RDAUXA, RDAUXB, RDAUXC, RDAUXD};
for (size_t i = 0; i < N_BMS; i++) {
size_t offset = BUFFER_BMS_OFFSET(i, AUX_GROUP_A_SIZE); // Getting auxVoltages from the BMS and calculating cellTemps
(*module)[i].auxVoltages[0] = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8)); for (size_t group = 0; group < 4; group++) {
(*module)[i].auxVoltages[1] = mV_from_ADBMS6830(rxbuf[offset + 2] | (rxbuf[offset + 3] << 8)); CHECK_RETURN(readCMD(auxCommands[group], rxbuf, auxGroupSizes[group]));
(*module)[i].auxVoltages[2] = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8)); for (size_t i = 0; i < N_BMS; i++) {
size_t offset = BUFFER_BMS_OFFSET(i, auxGroupSizes[group]);
for (size_t j = 0; j < auxVoltagesPerGroup[group]; j++) {
(*module)[i].auxVoltages[group * 3 + j] = mV_from_ADBMS6830(rxbuf[offset + j * 2] | (rxbuf[offset + j * 2 + 1] << 8));
(*module)[i].cellTemps[group * 3 + j] = ntc_mv_to_celsius((*module)[i].auxVoltages[group * 3 + j]);
}
}
} }
CHECK_RETURN(readCMD(RDAUXB, rxbuf, AUX_GROUP_B_SIZE));
for (size_t i = 0; i < N_BMS; i++) {
size_t offset = BUFFER_BMS_OFFSET(i, AUX_GROUP_B_SIZE);
(*module)[i].auxVoltages[3] = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8));
(*module)[i].auxVoltages[4] = mV_from_ADBMS6830(rxbuf[offset + 2] | (rxbuf[offset + 3] << 8));
(*module)[i].auxVoltages[5] = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8));
}
CHECK_RETURN(readCMD(RDAUXC, rxbuf, AUX_GROUP_C_SIZE));
for (size_t i = 0; i < N_BMS; i++) {
size_t offset = BUFFER_BMS_OFFSET(i, AUX_GROUP_C_SIZE);
(*module)[i].auxVoltages[6] = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8));
(*module)[i].auxVoltages[7] = mV_from_ADBMS6830(rxbuf[offset + 2] | (rxbuf[offset + 3] << 8));
(*module)[i].auxVoltages[8] = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8));
}
CHECK_RETURN(readCMD(RDAUXD, rxbuf, AUX_GROUP_D_SIZE));
for (size_t i = 0; i < N_BMS; i++) {
size_t offset = BUFFER_BMS_OFFSET(i, AUX_GROUP_D_SIZE);
(*module)[i].auxVoltages[9] = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8));
}
CHECK_RETURN(readCMD(RDSTATA, rxbuf, STATUS_GROUP_A_SIZE)); CHECK_RETURN(readCMD(RDSTATA, rxbuf, STATUS_GROUP_A_SIZE));
for (size_t i = 0; i < N_BMS; i++) { for (size_t i = 0; i < N_BMS; i++) {

View File

@ -25,7 +25,6 @@
#include "config_ADBMS6830.h" #include "config_ADBMS6830.h"
#include "swo_log.h" #include "swo_log.h"
#include <string.h> #include <string.h>
#include "NTC.h"
/* USER CODE END Includes */ /* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/
@ -184,13 +183,11 @@ int main(void)
// Print temperatures // Print temperatures
debug_log(LOG_LEVEL_INFO, " GPIO as temperatures (°C):"); debug_log(LOG_LEVEL_INFO, " GPIO as temperatures (°C):");
debug_log(LOG_LEVEL_INFO, " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d", debug_log(LOG_LEVEL_INFO, " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d",
ntc_mv_to_celsius(modules[i].auxVoltages[0]), ntc_mv_to_celsius(modules[i].auxVoltages[1]), modules[i].cellTemps[0], modules[i].cellTemps[1],
ntc_mv_to_celsius(modules[i].auxVoltages[2]), ntc_mv_to_celsius(modules[i].auxVoltages[3]), modules[i].cellTemps[2], modules[i].cellTemps[3], modules[i].cellTemps[4]);
ntc_mv_to_celsius(modules[i].auxVoltages[4]));
debug_log(LOG_LEVEL_INFO, " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d", debug_log(LOG_LEVEL_INFO, " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d",
ntc_mv_to_celsius(modules[i].auxVoltages[5]), ntc_mv_to_celsius(modules[i].auxVoltages[6]), modules[i].cellTemps[5], modules[i].cellTemps[6],
ntc_mv_to_celsius(modules[i].auxVoltages[7]), ntc_mv_to_celsius(modules[i].auxVoltages[8]), modules[i].cellTemps[7], modules[i].cellTemps[8], modules[i].cellTemps[9]);
ntc_mv_to_celsius(modules[i].auxVoltages[9]));
debug_log(LOG_LEVEL_INFO, " Internal temp: %d, VAnalog: %d, VDigital: %d, VRef: %d", debug_log(LOG_LEVEL_INFO, " Internal temp: %d, VAnalog: %d, VDigital: %d, VRef: %d",
modules[i].internalDieTemp, modules[i].analogSupplyVoltage, modules[i].internalDieTemp, modules[i].analogSupplyVoltage,