properly handle negative temperatures

This commit is contained in:
Kilian Bracher 2025-06-11 17:37:57 +02:00
parent bd078764e4
commit b9220a04e9
Signed by: k.bracher
SSH Key Fingerprint: SHA256:mXpyZkK7RDiJ7qeHCKJX108woM0cl5TrCvNBJASu6lM
2 changed files with 4 additions and 4 deletions

View File

@ -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

View File

@ -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]