working on temperature lookup table

This commit is contained in:
Moritz Ruffer 2025-03-27 23:16:21 +01:00
parent f09566ce49
commit de68618667
5 changed files with 39 additions and 20 deletions

View File

@ -1,14 +1,16 @@
#include <math.h>
// For using the mV conversion makro
#include "ADBMS_Abstraction.h"
#define USE_CALC
// #define USE_STEINHARDT
#ifdef USE_CALC
#ifdef USE_STEINHARDT
#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 VREF 3000.0f // 3V
#define VREF_mV 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
@ -18,11 +20,24 @@
// With R_T/R_0 and R_0 = R_T@25C
// R_T/R_0 = 1 / V_REF / ADC - 1
static inline uint16_t ntc_mv_to_celsius(int16_t adc) {
float log_ohms = logf(1/((VREF/adc)-1));
static inline uint16_t ntc_mv_to_celsius(int16_t adc_codes) {
// Calculate voltage from ADC
int16_t adc_mV = mV_from_ADBMS6830(adc_codes);
float log_ohms = logf(1/((VREF_mV/adc_mV)-1));
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?
#include "tempTable.h"
static inline uint16_t ntc_mv_to_celsius(int16_t adc_codes) {
// Out of bounds checking --> done by OV/UV setting of the BMS?
if (adc_codes < -6355 || adc_codes > 4640) {
// not really a good idea, still working on it
return -1;
}
return ADC2TEMP[adc_codes + 6355];
}
#endif

File diff suppressed because one or more lines are too long

View File

@ -73,13 +73,13 @@ struct ADBMS6830_Internal_Status {
};
#define MAXIMUM_CELL_VOLTAGES 16
#define MAXIMUM_AUX_VOLTAGES 10
#define MAXIMUM_GPIO 10
typedef struct {
int16_t cellVoltages[MAXIMUM_CELL_VOLTAGES];
int16_t auxVoltages[MAXIMUM_AUX_VOLTAGES];
uint16_t cellTemps[MAXIMUM_AUX_VOLTAGES];
// int16_t auxVoltages[MAXIMUM_GPIO];
uint16_t cellTemps[MAXIMUM_GPIO];
// last 32 LSB of the BMS ID --> should be unique for each BMS
uint32_t bmsID;
struct ADBMS6830_Internal_Status status;

View File

@ -140,8 +140,9 @@ HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module (*module)[N_BMS]) {
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]);
(*module)[i].cellTemps[group * 3 + j] = ntc_mv_to_celsius(rxbuf[offset + j * 2] | (rxbuf[offset + j * 2 + 1] << 8));
// (*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]);
}
}
}

View File

@ -170,15 +170,15 @@ int main(void)
modules[i].cellVoltages[14], modules[i].cellVoltages[15]);
// Print GPIO values
debug_log(LOG_LEVEL_INFO, " GPIO voltages (mV):");
debug_log(LOG_LEVEL_INFO, " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d",
modules[i].auxVoltages[0], modules[i].auxVoltages[1],
modules[i].auxVoltages[2], modules[i].auxVoltages[3],
modules[i].auxVoltages[4]);
debug_log(LOG_LEVEL_INFO, " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d",
modules[i].auxVoltages[5], modules[i].auxVoltages[6],
modules[i].auxVoltages[7], modules[i].auxVoltages[8],
modules[i].auxVoltages[9]);
// debug_log(LOG_LEVEL_INFO, " GPIO voltages (mV):");
// debug_log(LOG_LEVEL_INFO, " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d",
// modules[i].auxVoltages[0], modules[i].auxVoltages[1],
// modules[i].auxVoltages[2], modules[i].auxVoltages[3],
// modules[i].auxVoltages[4]);
// debug_log(LOG_LEVEL_INFO, " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d",
// 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):");