diff --git a/AMS_Master_Code/Core/Inc/NTC.h b/AMS_Master_Code/Core/Inc/NTC.h index a47dcac..7934f44 100644 --- a/AMS_Master_Code/Core/Inc/NTC.h +++ b/AMS_Master_Code/Core/Inc/NTC.h @@ -1,18 +1,28 @@ #include +#define USE_CALC + +#ifdef USE_CALC #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.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: // R_T = R_Pullup / (V_REF / ADC - 1) // With R_T/R_0 and R_0 = R_T@25C // 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)); - 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; -} \ No newline at end of file + 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 \ No newline at end of file diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Driver.h b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Driver.h index d3cdd65..2419260 100644 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Driver.h +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Driver.h @@ -79,6 +79,7 @@ struct ADBMS6830_Internal_Status { typedef struct { int16_t cellVoltages[MAXIMUM_CELL_VOLTAGES]; int16_t auxVoltages[MAXIMUM_AUX_VOLTAGES]; + uint16_t cellTemps[MAXIMUM_AUX_VOLTAGES]; struct ADBMS6830_Internal_Status status; uint16_t internalDieTemp; 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 d136dad..e1d6e99 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 @@ -11,6 +11,7 @@ #include "config_ADBMS6830.h" #include "swo_log.h" #include +#include "NTC.h" extern uint8_t numberofCells; @@ -128,37 +129,22 @@ HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module (*module)[N_BMS]) { // return HAL_BUSY; } - CHECK_RETURN( - readCMD(RDAUXA, rxbuf, - AUX_GROUP_A_SIZE)); // STATUS_GROUP_C_SIZE is the same as AUX_GROUP_A_SIZE, so we can reuse the buffer - for (size_t i = 0; i < N_BMS; i++) { - size_t offset = BUFFER_BMS_OFFSET(i, AUX_GROUP_A_SIZE); - (*module)[i].auxVoltages[0] = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8)); - (*module)[i].auxVoltages[1] = mV_from_ADBMS6830(rxbuf[offset + 2] | (rxbuf[offset + 3] << 8)); - (*module)[i].auxVoltages[2] = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8)); + const size_t auxGroupSizes[] = {AUX_GROUP_A_SIZE, AUX_GROUP_B_SIZE, AUX_GROUP_C_SIZE, AUX_GROUP_D_SIZE}; + const size_t auxVoltagesPerGroup[] = {3, 3, 3, 1}; // Number of voltages in each group + const uint8_t auxCommands[] = {RDAUXA, RDAUXB, RDAUXC, RDAUXD}; + + // Getting auxVoltages from the BMS and calculating cellTemps + for (size_t group = 0; group < 4; group++) { + CHECK_RETURN(readCMD(auxCommands[group], rxbuf, auxGroupSizes[group])); + 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)); for (size_t i = 0; i < N_BMS; i++) { diff --git a/AMS_Master_Code/Core/Src/main.c b/AMS_Master_Code/Core/Src/main.c index 982ef4a..9667ac5 100644 --- a/AMS_Master_Code/Core/Src/main.c +++ b/AMS_Master_Code/Core/Src/main.c @@ -25,7 +25,6 @@ #include "config_ADBMS6830.h" #include "swo_log.h" #include -#include "NTC.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -184,13 +183,11 @@ int main(void) // 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", - 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])); + modules[i].cellTemps[0], modules[i].cellTemps[1], + modules[i].cellTemps[2], modules[i].cellTemps[3], modules[i].cellTemps[4]); 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]), - ntc_mv_to_celsius(modules[i].auxVoltages[7]), ntc_mv_to_celsius(modules[i].auxVoltages[8]), - ntc_mv_to_celsius(modules[i].auxVoltages[9])); + modules[i].cellTemps[5], modules[i].cellTemps[6], + modules[i].cellTemps[7], modules[i].cellTemps[8], modules[i].cellTemps[9]); debug_log(LOG_LEVEL_INFO, " Internal temp: %d, VAnalog: %d, VDigital: %d, VRef: %d", modules[i].internalDieTemp, modules[i].analogSupplyVoltage,