Skip to content

Commit

Permalink
SoC eeprom, coulomb counting
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabramz committed Jan 27, 2024
1 parent 742be1d commit b96b9dc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
1 change: 1 addition & 0 deletions Core/Inc/datastructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Core/Inc/eepromdirectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <stdbool.h>

#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 */
Expand Down
36 changes: 21 additions & 15 deletions Core/Src/analyzer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "analyzer.h"
#include "eepromdirectory.h"
#include <stdint.h>
#include <stdlib.h>

acc_data_t* bmsdata;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion Core/Src/eepromdirectory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b96b9dc

Please sign in to comment.