Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bms/App/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ add_subdirectory(cb)
add_subdirectory(pec)
add_subdirectory(soc)
add_subdirectory(thermal)
add_subdirectory(ema)
7 changes: 7 additions & 0 deletions bms/App/math/ema/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Auto-generated CMake for Flat Module: ema
add_library(ema STATIC)

file(GLOB SRC_FILES "*.c")
target_sources(ema PRIVATE ${SRC_FILES})

target_include_directories(ema PUBLIC .)
22 changes: 22 additions & 0 deletions bms/App/math/ema/ema.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "ema.h"

void ema_init(ema_filter_t *filter, float alpha) {
filter->alpha = alpha;
filter->current_ema = 0.0f;
filter->is_initialized = 0;
}

float ema_update(ema_filter_t *filter, float new_value) {

// intially, 0 will taint the first few values
if (!filter->is_initialized) {
filter->current_ema = new_value;
filter->is_initialized = 1;
}

else {
filter->current_ema = (filter->alpha * new_value) +
((1.0f - filter->alpha) * filter->current_ema);
}
return filter->current_ema;
}
15 changes: 15 additions & 0 deletions bms/App/math/ema/ema.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef EMA_H
#define EMA_H

#include <stdint.h>

typedef struct {
float alpha;
float current_ema;
int is_initialized;
} ema_filter_t;

void ema_init(ema_filter_t *filter, float alpha);
float ema_update(ema_filter_t *filter, float new_value);

#endif
34 changes: 31 additions & 3 deletions bms/App/math/soc/coulomb.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
#ifndef COULOMB_H
#define COULOMB_H
#ifndef SOC_CURVE_H
#define SOC_CURVE_H

#include <stdint.h>
#include <stdbool.h>

#endif
#define DT_SECONDS 0.05f // 50 ms time step
#define DEADZONE_THR 0.01f // Current deadzone threshold (Amps)

#define SINGLE_18650_CHARGE_CAPACITY 3000
#define NUM_PARALLEL_CELLS 3
#define TOTAL_PACK_CAPACITY (SINGLE_18650_CHARGE_CAPACITY * NUM_PARALLEL_CELLS)

static batterymodel_t battery_model;
static emafilter_t soc_filter;
static const float q_total_pack = TOTAL_PACK_CAPACITY;

typedef struct {
float pack_current;
float min_cell_voltage;
float max_cell_voltage;
float accumulated_coulombs;
float delta_charge_since_reset;
float system_soc;
uint32_t rest_duration_ms;
} batterymodel_t;

typedef struct {
float alpha;
float current_ema;
bool is_initialized;
} emafilter_t;

#endif
Empty file added bms/App/math/soc/soc_curve.c
Empty file.
19 changes: 19 additions & 0 deletions bms/App/math/soc/soc_curve.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef SOC_CURVE_H
#define SOC_CURVE_H

#define SOC_LUT_SIZE 21

// example idk

static const float soc_curve_cell_voltages[SOC_LUT_SIZE] = {
3.00F, 3.06f, 3.12f, 3.18f, 3.24f, 3.30F, 3.36f, 3.42f, 3.48f, 3.54f,
3.60F, 3.66f, 3.72f, 3.78f, 3.84f, 3.90F, 3.96f, 4.02f, 4.08f, 4.14f, 4.20F
};

static const float soc_curve_percentages[SOC_LUT_SIZE] = {
0.0F, 5.0F, 10.0F, 15.0F, 20.0F, 25.0F, 30.0F, 40.0F, 50.0F, 58.0F,
65.0F, 72.0F, 78.0F, 83.0F, 88.0F, 92.0F, 95.0F, 97.0F, 99.0F, 99.5f, 100.0F
};


#endif
1 change: 1 addition & 0 deletions bms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ target_link_libraries(${PROJECT_NAME}
daemons
threads
gui
ema
)

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
Expand Down
Loading