Integrate current

The current counter on the shunt can't be activated for some reason.
This commit is contained in:
Jasper Blanckenburg 2023-06-25 16:41:29 +02:00
parent 5dba504e10
commit 208d84e2a5
3 changed files with 24 additions and 13 deletions

View File

@ -10,16 +10,17 @@
#define SHUNT_THRESH_OVERTEMP 1000 // 1/10 °C #define SHUNT_THRESH_OVERTEMP 1000 // 1/10 °C
typedef struct { typedef struct {
int32_t current; int32_t current; // mA
int32_t voltage_bat; int32_t voltage_bat; // mV
int32_t voltage_veh; int32_t voltage_veh; // mV
int32_t voltage3; int32_t voltage3; // mV
int32_t busbartemp; int32_t busbartemp;
int32_t power; int32_t power;
int32_t energy; int32_t energy;
int32_t current_counter; float current_counter; // mAs
uint32_t last_message; uint32_t last_message;
uint32_t last_current_message;
} ShuntData; } ShuntData;
extern ShuntData shunt_data; extern ShuntData shunt_data;

View File

@ -20,6 +20,7 @@ void shunt_init() {
shunt_data.energy = 0; shunt_data.energy = 0;
shunt_data.current_counter = 0; shunt_data.current_counter = 0;
shunt_data.last_message = 0; shunt_data.last_message = 0;
shunt_data.last_current_message = 0;
} }
void shunt_check() { void shunt_check() {
@ -46,6 +47,12 @@ void shunt_handle_can_msg(uint16_t id, const uint8_t *data) {
switch (id) { switch (id) {
case CAN_ID_SHUNT_CURRENT: case CAN_ID_SHUNT_CURRENT:
shunt_data.current = result; shunt_data.current = result;
if (shunt_data.last_current_message > 0) {
uint32_t now = HAL_GetTick();
float dt = (now - shunt_data.last_current_message) * 0.001f;
shunt_data.current_counter += shunt_data.current * dt;
}
shunt_data.last_current_message = HAL_GetTick();
break; break;
case CAN_ID_SHUNT_VOLTAGE1: case CAN_ID_SHUNT_VOLTAGE1:
shunt_data.voltage_bat = result; shunt_data.voltage_bat = result;
@ -63,7 +70,10 @@ void shunt_handle_can_msg(uint16_t id, const uint8_t *data) {
shunt_data.power = result; shunt_data.power = result;
break; break;
case CAN_ID_SHUNT_CURRENT_COUNTER: case CAN_ID_SHUNT_CURRENT_COUNTER:
shunt_data.current_counter = result; // TODO: Use this when we get the shunt to emit current counter data (the
// shunt apparently emits As, not mAs)
// shunt_data.current_counter = result * 1000;
break; break;
case CAN_ID_SHUNT_ENERGY_COUNTER: case CAN_ID_SHUNT_ENERGY_COUNTER:
shunt_data.energy = result; shunt_data.energy = result;

View File

@ -10,7 +10,7 @@
#define SOC_ESTIMATION_NO_CURRENT_THRESH 200 // mA #define SOC_ESTIMATION_NO_CURRENT_THRESH 200 // mA
#define SOC_ESTIMATION_NO_CURRENT_TIME 100000 // ms #define SOC_ESTIMATION_NO_CURRENT_TIME 100000 // ms
#define SOC_ESTIMATION_BATTERY_CAPACITY 19528 // mAh #define SOC_ESTIMATION_BATTERY_CAPACITY 70300800 // mAs
ocv_soc_pair_t OCV_SOC_PAIRS[] = { ocv_soc_pair_t OCV_SOC_PAIRS[] = {
{25000, 0.00f}, {29900, 3.97f}, {32300, 9.36f}, {33200, 12.60f}, {25000, 0.00f}, {29900, 3.97f}, {32300, 9.36f}, {33200, 12.60f},
{33500, 13.68f}, {34100, 20.15f}, {35300, 32.01f}, {38400, 66.53f}, {33500, 13.68f}, {34100, 20.15f}, {35300, 32.01f}, {38400, 66.53f},
@ -23,7 +23,7 @@ int current_was_flowing;
uint32_t last_current_time; uint32_t last_current_time;
uint32_t first_current_time; uint32_t first_current_time;
float soc_before_current; float soc_before_current;
int32_t ah_before_current; float mAs_before_current;
void soc_init() { void soc_init() {
current_soc = 0; current_soc = 0;
@ -38,7 +38,7 @@ void soc_update() {
if (!current_was_flowing) { if (!current_was_flowing) {
first_current_time = now; first_current_time = now;
soc_before_current = current_soc; soc_before_current = current_soc;
ah_before_current = shunt_data.current_counter; mAs_before_current = shunt_data.current_counter;
} }
current_was_flowing = 1; current_was_flowing = 1;
} else { } else {
@ -52,9 +52,9 @@ void soc_update() {
current_soc = soc_for_ocv(min_voltage); current_soc = soc_for_ocv(min_voltage);
} else { } else {
// Otherwise, use the current counter to update SoC // Otherwise, use the current counter to update SoC
int32_t ah_delta = shunt_data.current_counter - ah_before_current; float as_delta = shunt_data.current_counter - mAs_before_current;
current_soc = current_soc =
soc_before_current + (float)ah_delta / SOC_ESTIMATION_BATTERY_CAPACITY; soc_before_current + as_delta / SOC_ESTIMATION_BATTERY_CAPACITY;
} }
} }