1. 工程概述
1.1 项目架构
1 2 3 4 5 6 7
| sd77561-AE/ ├── base_line_newton_guanyu_20250318_FCC idle_bugfix_HighTemperatureCV/ │ └── base_line_newton_guanyu/ │ ├── flash/ # 外设驱动:I2C/UART/ADC/Timer │ ├── lib/ # 核心算法库:lib_fg.c │ ├── user/ # 应用层:main.c, parameter.c │ └── chip/ # 芯片抽象层:chip_irq.c, chip_adc.c
|
1.2 代码版本演进
从 lib_fg.c 的历史记录可以看到算法的持续优化:
1 2 3 4 5 6 7
| 2019-04-13: 初始版本 2019-09-26: Fix RSOC负值问题 2020-05-21: 加入 DFCC 触发条件 2020-07-10: 加入 CTA 温度补偿 2021-01-21: 优化充电算法 2022-05-11: 移动版优化 2024-06-03: 最新版本 (v20240603)
|
2. 核心数据结构
2.1 固件配置结构
1 2 3 4 5 6 7 8 9 10
| typedef struct { int16_t l_fg_dfcc_learnratio; int16_t l_fg_dfcc_auto_maxthm; int16_t l_fg_dfcc_auto_mincurrent; int16_t l_fg_dfcc_minstep_th; int16_t l_fg_dfcc_update_maxsoc; int16_t l_fg_dfcc_update_socrange; int16_t l_fg_dfcc_update_thmrange; int16_t l_fg_dfcc_maxrange; } LIB_FG_PARAM_T;
|
2.2 算法状态结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| typedef struct { uint32_t fcc; uint32_t rc; uint32_t facc; int16_t soc; int16_t rsoc; int16_t soh; uint8_t status; int16_t cellthm; int16_t cellmv; int32_t cc; int32_t dc; int32_t dfcc; int16_t dfcc_ratio; } LIB_FG_TYPE_T;
|
2.3 状态机定义
1 2 3 4 5 6 7 8 9
| #define FG_STAT_INIT 0x00 #define FG_STAT_DSG 0x01 #define FG_STAT_CHG 0x02 #define FG_STAT_IDLE 0x04 #define FG_STAT_FC 0x08 #define FG_STAT_FD 0x10 #define FG_STAT_DFC_TCC 0x20 #define FG_STAT_EXT_HDC 0x40
|
3. 核心算法流程
3.1 主循环 fg_update()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
void fg_update(void) { cellmv = adc_read_cell_voltage(); cc = adc_read_cc(); cellthm = adc_read_temperature(); im = (cc - cc_prv) / dt; fg_cmavg_update(im); detect_battery_status(); switch (status) { case FG_STAT_DSG: fg_discharge_process(); break; case FG_STAT_CHG: fg_charge_process(); break; case FG_STAT_IDLE: fg_idle_process(); break; } if (fg_dfcc_enable) { fg_dfcc_update(); } soh_update(); fg_calculate_soc(); cc_prv = cc; }
|
3.2 放电处理 fg_discharge_process()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| void fg_discharge_process(void) { deltauah = -im * dt / 3600; dc += deltauah; rc = fcc - dc; if (detect_fast_discharge()) { fg_fast_dsg(); } if (cellmv < EODMV) { status = FG_STAT_FD; fcc = dc; } if (dfcc_condition_met()) { dfcc_update_by_lookup(); } }
|
3.3 满放检测 fg_fast_dsg()
快速放电状态下,放电末端 SOC 变化速率加快:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
void fg_fast_dsg(void) { int16_t soc_remaining = rsoc; if (soc_remaining > 100) { rsoc -= 1; } else if (soc_remaining > 0) { rsoc -= 2; } }
|
4. DFCC 自适应实现
4.1 DFCC 更新条件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
static uint8_t dfcc_condition_met(void) { int16_t dthm = abs(cellthm - dfcc_last_thm); int16_t dsoc = abs(rsoc - dfcc_last_soc); if (dthm > 10) { return 1; } if (dsoc > 200) { return 1; } if (abs(dccuh) > 5000) { return 1; } return 0; }
|
4.2 DFCC 查表更新
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
static void dfcc_update_by_lookup(void) { int soc_idx = find_soc_index(rsoc); int cur_idx = find_current_index(abs(im)); int thm_idx = find_temp_index(cellthm); int target_dfcc = DFCC_table[thm_idx][cur_idx][soc_idx]; int delta = (target_dfcc - dfcc) * dfcc_ratio / 100; if (delta > FG_DFCC_MAXRANGE) delta = FG_DFCC_MAXRANGE; if (delta < -FG_DFCC_MAXRANGE) delta = -FG_DFCC_MAXRANGE; dfcc += delta; fcc = dfcc; dfcc_last_thm = cellthm; dfcc_last_soc = rsoc; }
|
5. 温度补偿
5.1 温度对电量的影响
电池在低温下可用容量显著降低,需要温度补偿:
1 2 3
| 25°C: 100% 可用容量 0°C: 70% 可用容量 -10°C: 50% 可用容量
|
5.2 CTA (Cell Temperature Aging) 估算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
static void fg_cellthm_estimation(void) { int16_t thm_delta = cellthm - thm_base; int32_t i_sqr = im * im; int32_t dcap_thm = (i_sqr * K_thm * thm_delta) / 10000; rc += dcap_thm; thm_base = cellthm; }
|
6. 边界处理
6.1 满充检测
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #define FG_CHG_EOCTIME 30 #define FG_FC_VOLTAGE 4400 #define FG_EOC_CURRENT 100
void fg_charge_process(void) { if (cellmv > CV_THRESHOLD) { status_cv = 1; } if (status_cv && abs(im) < FG_EOC_CURRENT) { chgtime++; if (chgtime > FG_CHG_EOCTIME) { status = FG_STAT_FC; fcc = cc; } } }
|
6.2 满放检测
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #define FG_EOD_VOLTAGE 3000 #define FG_ELT_VOLTAGE 3200
void fg_discharge_process(void) { if (cellmv < FG_ELT_VOLTAGE) { status = FG_STAT_ELT; rsoc = 100; } if (cellmv < FG_EOD_VOLTAGE) { status = FG_STAT_FD; fcc = dc; rsoc = 0; } }
|
7. 调试与日志
7.1 调试打印
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #ifdef FG_DEBUG void fg_debug_print(void) { printf("[FG] FCC=%d RC=%d SOC=%d.%02d%% RSOC=%d.%02d%% SOH=%d.%02d%%\n", fcc, rc, soc/100, soc%100, rsoc/100, rsoc%100, soh/100, soh%100); printf("[FG] V=%dmV I=%dmA T=%d.%d°C Status=0x%02X\n", cellmv, im, cellthm/10, cellthm%10, status); printf("[FG] CC=%d DC=%d DFCC=%d\n", cc, dc, dfcc); } #endif
|
7.2 固件版本获取
1 2 3 4 5 6 7
|
uint32_t fg_get_version(void) { return LIB_FG_VERSION; }
|
8. 与简历要点的对应
| 简历要点 |
代码实现 |
| 电量算法核心逻辑 |
lib_fg.c 完整 SOC/DFCC 算法 |
| 满充/满放边界处理 |
fg_charge_process() / fg_discharge_process() |
| 系统阻抗导致的电量异常跳变 |
fg_cellthm_estimation() 温度补偿 |
| ADC 温度漂移校准 |
adc_read_temperature() + 滤波 |
| 电量精度误差 < 3% |
DFCC 三维权衡 + 平滑更新 |
| 动态容量估算 |
dfcc_update_by_lookup() |
| 电量报满机制 |
fg_fast_dsg() 快速放电处理 |
| 温度阻抗补偿 |
CTA 温度补偿算法 |
| 系统校准 |
OCV 查表初始化 |
9. 小结
本文介绍了库仑计电量算法的完整工程实现:
- 状态机架构:INIT → IDLE/DSG/CHG → FC/FD
- DFCC 自适应:查表 + 渐进式更新 + 触发条件
- 温度补偿:CTA 估算 + 容量补偿
- 边界处理:满充、满放、快速放电
- 调试支持:完整日志、版本查询
结合第一篇的算法原理和第二篇的仿真调参,形成了算法研究 → 仿真验证 → 固件实现的完整闭环。
系列目录
01. DFCC算法原理与电池阻抗追踪
02. DFCC参数调校与仿真验证系统
03. 库仑计SOC算法工程实现(本文)