diff --git a/Core/Inc/datastructs.h b/Core/Inc/datastructs.h index 5fb35d3..2d784fa 100644 --- a/Core/Inc/datastructs.h +++ b/Core/Inc/datastructs.h @@ -53,6 +53,7 @@ typedef enum { BATTERY_THERMISTOR = 0x8000, CHARGER_CAN_FAULT = 0x10000, CHARGE_LIMIT_ENFORCEMENT_FAULT = 0x20000, + EEPROM_FAULT = 0x40000, MAX_FAULTS = 0x80000000 /* Maximum allowable fault code */ } bms_fault_t; diff --git a/Core/Inc/eepromdirectory.h b/Core/Inc/eepromdirectory.h index 23c3665..7810c87 100644 --- a/Core/Inc/eepromdirectory.h +++ b/Core/Inc/eepromdirectory.h @@ -6,7 +6,7 @@ #include #define NUM_EEPROM_FAULTS 5 -#define NUM_EEPROM_ITEMS 2 +#define NUM_EEPROM_ITEMS 3 #define EEPROM_ROOT_ADDR 0 /* index 0 = newest, index 4 = oldest */ diff --git a/Core/Src/analyzer.c b/Core/Src/analyzer.c index 183803b..5c32968 100644 --- a/Core/Src/analyzer.c +++ b/Core/Src/analyzer.c @@ -1,4 +1,6 @@ #include "analyzer.h" +#include "eepromdirectory.h" +#include #include acc_data_t* bmsdata; @@ -53,7 +55,7 @@ const uint8_t TEMP_TO_CCL[14] = 20, 15, 10, 5, 1, 1 }; -/** +/** * @brief Lookup table for State of Charge * * @note each index covers 0.1V increase (voltage range is 2.9V - 4.2V, deltaV = 1.3V, @@ -499,28 +501,32 @@ void disable_therms() } } -void calc_state_of_charge() +bms_fault_t calc_state_of_charge() { - /* Spltting the delta voltage into 18 increments */ - const uint16_t increments - = ((uint16_t)(MAX_VOLT * 10000 - MIN_VOLT * 10000) / ((MAX_VOLT - MIN_VOLT) * 10)); - - /* Retrieving a index of 0-18 */ - uint8_t index = ((bmsdata->min_ocv.val) - MIN_VOLT * 10000) / increments; - bmsdata->soc = STATE_OF_CHARGE_CURVE[index]; + int32_t prev_voltage; - if (bmsdata->soc != 100) { - float interpolation - = (float)(STATE_OF_CHARGE_CURVE[index + 1] - STATE_OF_CHARGE_CURVE[index]) / increments; - bmsdata->soc - += (uint8_t)(interpolation - * (((bmsdata->min_ocv.val) - (int32_t)(MIN_VOLT * 10000)) % increments)); + //The last SoC is stored in EEPROM to be loaded on startup + if (is_first_reading_){ + if(!eeprom_read_data_key("CHARGE", *prev_voltage, 4)) { + return EEPROM_FAULT; + } + } else { + prev_voltage = bmsdata->min_ocv.val; } + int32_t new_voltage = prev_voltage + delta_time * (bmsdata->pack_current * 1000); + + //State of charge as a percentage of max charge + uint8_t soc = (uint8_t) ((new_voltage - MIN_VOLT * 10000) / MAX_VOLT * 10000); + + bmsdata->soc = soc; + if (bmsdata->soc < 0) { bmsdata->soc = 0; } + + } void high_curr_therm_check() diff --git a/Core/Src/eepromdirectory.c b/Core/Src/eepromdirectory.c index db4ab99..0d9df90 100644 --- a/Core/Src/eepromdirectory.c +++ b/Core/Src/eepromdirectory.c @@ -7,7 +7,10 @@ void eepromInit() eeprom_data[0].size = 1; eeprom_data[1].id = (char*)("FAULTS"); - eeprom_data[1].size = 21; + eeprom_data[1].size = 21; + + eeprom_data[2].id = (char*)("CHARGE"); + eeprom_data[2].size = 4; // Initialize EEPROM addresses given data and length