85 lines
2.5 KiB
C
85 lines
2.5 KiB
C
#include "battery.h"
|
|
#include "ADBMS_Driver.h"
|
|
#include "NTC.h"
|
|
#include "config_ADBMS6830.h"
|
|
#include <string.h>
|
|
|
|
#define SWO_LOG_PREFIX "[BATTERY] "
|
|
#include "swo_log.h"
|
|
|
|
uint16_t min_voltage = 0xFFFF;
|
|
uint16_t max_voltage = 0;
|
|
typeof(module_voltages) module_voltages = {[0 ... N_BMS - 1] = {0xFFFF, 0}};
|
|
int16_t min_temp = INT16_MAX;
|
|
int16_t max_temp = INT16_MIN;
|
|
typeof(module_temps) module_temps = {[0 ... N_BMS - 1] = {INT16_MAX, INT16_MIN}};
|
|
|
|
int16_t cellTemps[N_BMS][N_CELLS];
|
|
|
|
HAL_StatusTypeDef battery_init(SPI_HandleTypeDef *hspi) {
|
|
auto ret = AMS_Init(hspi);
|
|
if (ret.status != ADBMS_NO_ERROR) {
|
|
debug_log(LOG_LEVEL_ERROR, "Failed to initialize BMS: %s",
|
|
ADBMS_Status_ToString(ret.status));
|
|
if (ret.bms_id != -1) {
|
|
debug_log_cont(LOG_LEVEL_ERROR, " (on BMS ID: %hd)", ret.bms_id);
|
|
}
|
|
return HAL_ERROR;
|
|
}
|
|
debug_log(LOG_LEVEL_INFO, "Battery initialized successfully");
|
|
return HAL_OK;
|
|
}
|
|
|
|
HAL_StatusTypeDef battery_update() {
|
|
auto ret = AMS_Idle_Loop();
|
|
if (ret.status != ADBMS_NO_ERROR) {
|
|
debug_log(LOG_LEVEL_ERROR, "Failed to update battery data: %s",
|
|
ADBMS_Status_ToString(ret.status));
|
|
if (ret.bms_id != -1) {
|
|
debug_log_cont(LOG_LEVEL_ERROR, " (on BMS ID: %hd)", ret.bms_id);
|
|
}
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
min_voltage = 0xFFFF;
|
|
max_voltage = 0;
|
|
min_temp = INT16_MAX;
|
|
max_temp = INT16_MIN;
|
|
|
|
for (size_t i = 0; i < N_BMS; i++) {
|
|
for (size_t j = 0; j < N_CELLS; j++) {
|
|
if (modules[i].cellVoltages[j] > min_voltage) {
|
|
min_voltage = modules[i].cellVoltages[j];
|
|
}
|
|
if (modules[i].cellVoltages[j] < max_voltage) {
|
|
max_voltage = modules[i].cellVoltages[j];
|
|
}
|
|
if (modules[i].cellVoltages[j] > module_voltages[i].max) {
|
|
module_voltages[i].max = modules[i].cellVoltages[j];
|
|
}
|
|
if (modules[i].cellVoltages[j] < module_voltages[i].min) {
|
|
module_voltages[i].min = modules[i].cellVoltages[j];
|
|
}
|
|
}
|
|
|
|
for (size_t j = 0; j < 10; j++) { //10 GPIOs
|
|
cellTemps[i][j] = ntc_mv_to_celsius(modules[i].auxVoltages[j]);
|
|
|
|
if (cellTemps[i][j] > max_temp) {
|
|
max_temp = cellTemps[i][j];
|
|
}
|
|
if (cellTemps[i][j] < min_temp) {
|
|
min_temp = cellTemps[i][j];
|
|
}
|
|
|
|
if (cellTemps[i][j] > module_temps[i].max) {
|
|
module_temps[i].max = cellTemps[i][j];
|
|
}
|
|
if (cellTemps[i][j] < module_temps[i].min) {
|
|
module_temps[i].min = cellTemps[i][j];
|
|
}
|
|
}
|
|
}
|
|
|
|
return HAL_OK;
|
|
} |