soc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. #include "soc.h"
  2. #include "app/sox/measure.h"
  3. #include "app/sox/measure_task.h"
  4. #include "app/nv_storage.h"
  5. #include "libs/logger.h"
  6. #include "Least_Square.h"
  7. #include "health.h"
  8. #include "state.h"
  9. #include "event_record.h"
  10. #define LEAST_SQUARE 0
  11. static soc_t _soc;
  12. static uint8_t chargering = 0;
  13. static u64 current_sample_ts = 0; //ms
  14. static u32 force_full_ts = 0xFFFFFFFF; //s
  15. static u32 force_empty_ts = 0xFFFFFFFF; //s
  16. static float soc_delta_time = 0;
  17. static float max_soc_delta_time = 0;
  18. static float _charger_coefficient = 1.0f;
  19. static float _discharger_coefficient = 1.0f;
  20. static uint32_t charger_remain_time = 0;
  21. static const float _discharger_gain[] = {1.0f/*>0度*/, 1.002f/*-2<t<=0*/, 1.005f/*-5<t<=-2*/, 1.008f/*-10<t<=-5*/, 1.02f/*-15<t<=-10*/, 1.04f/*-20<t<=-15*/};
  22. #define MAX_TIME_FULL_TO_EMPTY (5 * 24 * 3600) //充满到欠压5天内达到,可以校准最小电量
  23. #define MAX_TIME_EMPTY_TO_FULL (24 * 3600) //欠压到充满24小时内达到,可以校准最小电量
  24. #define DEFALUT_MAX_COULOMB (MAX_HA * 3600.0f)
  25. #define DEFALUT_MIN_COULOMB (25.0f * 3600.0f)
  26. #define FULL_MAX_VOLTAGE_CHARGING (54000)//mV
  27. #define FULL_MAX_VOLTAGE_STOP_CHARGING (53500)
  28. #define AGINT_TEST_MAX_VOLTAGE_CHARGING (53000) //mV
  29. #define FULL_MAX_VOLTAGE (54000) //mV
  30. #define FULL_MIN_CURRENT (500.0f) //mA
  31. static double start_charger_coulomb = 0.0f; //开始充电时候的容量
  32. static void calibrate_soc_by_ocv(void);
  33. static void _soc_clear(void);
  34. #if LEAST_SQUARE==1
  35. static void _least_square_timer_handler(shark_timer_t *timer);
  36. static least_square_t discharger_vol_coef;
  37. static least_square_t discharger_cell_coef;
  38. static least_square_t discharger_capacity_coef;
  39. static shark_timer_t least_square_timer = {.handler = _least_square_timer_handler};
  40. static int least_square_time = 0;
  41. static int least_square_started = 0;
  42. #define LEAST_SQUARE_STEP_TIME 1000 * 5
  43. #endif
  44. void soc_init(void){
  45. set_log_level(MOD_SOC, L_debug);
  46. current_sample_ts = shark_get_mseconds();
  47. if (nv_restore_soc() != 0){
  48. soc_warning("SOC: nv storage is not inited, use default value!!\n");
  49. _soc_clear();
  50. }
  51. //如果最大容量和默认不一致,需要重新校准
  52. if (_soc.coulomb_max != DEFALUT_MAX_COULOMB) {
  53. _soc_clear();
  54. nv_save_all_soc();
  55. }
  56. if ((_soc.flags & SOC_FLAG_CALIBRATED) == 0){
  57. calibrate_soc_by_ocv();
  58. nv_save_soc();
  59. }else {
  60. if (_soc.capacity == 100) {
  61. force_full_ts = shark_get_seconds() + 1;
  62. }else if (_soc.capacity == 0) {
  63. force_empty_ts = shark_get_seconds() + 1;
  64. }
  65. }
  66. if (soc_get_version() != SOC_CURRENT_VERSION) {
  67. //DO SOMETHING, FOR SOC VERSION CHANGED
  68. }
  69. soc_log();
  70. }
  71. u8 soc_get_version(void) {
  72. return SOC_FLAG_TO_VER(_soc.flags);
  73. }
  74. void soc_set_version(u8 version) {
  75. if (version > 7) {
  76. return;
  77. }
  78. _soc.flags &= ~SOC_FLAG_VERSON_MASK;
  79. _soc.flags |= SOC_FLAG_VERSION(version);
  80. }
  81. static void _soc_clear(void){
  82. _soc.coulomb_min = 0;
  83. _soc.coulomb_max = DEFALUT_MAX_COULOMB; //30HA,这个值最总需要soh模块给
  84. _soc.flags = 0;
  85. _soc.charger_coulomb = 0;
  86. _soc.pre_charger_coulomb = 0;
  87. _soc.dischrger_coulomb = 0;
  88. _soc.pre_discharger_coulomb = 0;
  89. _soc.total_coulomb = 0;
  90. }
  91. void soc_clear_calibrate(int keep_cycle) {
  92. float total = _soc.total_coulomb;
  93. _soc_clear();
  94. if (keep_cycle) {
  95. _soc.total_coulomb = total;
  96. }
  97. nv_save_all_soc();
  98. }
  99. void soc_restore_by_iap(uint8_t flags, uint8_t capaticy){
  100. _soc.coulomb_min = 0;
  101. _soc.coulomb_max = DEFALUT_MAX_COULOMB; //30HA,这个值最总需要soh模块给
  102. _soc.flags = 0;
  103. _soc.charger_coulomb = 0;
  104. _soc.pre_charger_coulomb = 0;
  105. _soc.dischrger_coulomb = 0;
  106. _soc.pre_discharger_coulomb = 0;
  107. _soc.total_coulomb = 0;
  108. if (flags == 1) {
  109. _soc.flags |= SOC_FLAG_CALIBRATED;
  110. }
  111. _soc.capacity = capaticy;
  112. _soc.coulomb_now = (_soc.coulomb_max - _soc.coulomb_min) * _soc.capacity / 100.0f + _soc.coulomb_min;
  113. nv_save_all_soc();
  114. }
  115. static void soc_update_discharger_coeff(void){
  116. int low_temp = 0xFFFF;
  117. for (int i = 0; i < PACK_TEMPS_NUM-1; i++) {
  118. low_temp = MIN(low_temp, measure_value()->pack_temp[i]);
  119. }
  120. if (low_temp > 0) {
  121. _discharger_coefficient = _discharger_gain[0];
  122. }else {
  123. if (low_temp > -2) {
  124. _discharger_coefficient = _discharger_gain[1];
  125. }else if (low_temp > -5) {
  126. _discharger_coefficient = _discharger_gain[2];
  127. }else if (low_temp > -10) {
  128. _discharger_coefficient = _discharger_gain[3];
  129. }else if (low_temp > -15) {
  130. _discharger_coefficient = _discharger_gain[4];
  131. }else {
  132. _discharger_coefficient = _discharger_gain[5];
  133. }
  134. force_full_ts = 0xFFFFFFFF;
  135. }
  136. if (_soc.flags & SOC_FLAG_CALIBRATED) {
  137. float coff = 1.0f;
  138. if (_soc.capacity <= 20) {
  139. if (abs(measure_value()->load_current) >= CURRENT_BIGER) {
  140. coff = 1.06f;
  141. }else if (abs(measure_value()->load_current) >= CURRENT_MID) {
  142. coff = 1.05f;
  143. }else if (abs(measure_value()->load_current) >= CURRENT_NORMAL) {
  144. coff = 1.03f;
  145. }
  146. }else if (_soc.capacity <= 40) {
  147. if (abs(measure_value()->load_current) >= CURRENT_BIGER) {
  148. coff = 1.05f;
  149. }else if (abs(measure_value()->load_current) >= CURRENT_MID) {
  150. coff = 1.03f;
  151. }else if (abs(measure_value()->load_current) >= CURRENT_NORMAL) {
  152. coff = 1.02f;
  153. }
  154. }else if (_soc.capacity <= 60) {
  155. if (abs(measure_value()->load_current) >= CURRENT_BIGER) {
  156. coff = 1.03f;
  157. }else if (abs(measure_value()->load_current) >= CURRENT_MID) {
  158. coff = 1.02f;
  159. }else if (abs(measure_value()->load_current) >= CURRENT_NORMAL) {
  160. coff = 1.01f;
  161. }
  162. }
  163. if ((abs(measure_value()->load_current) > 10.0f) && (abs(measure_value()->load_current) < 500)) {
  164. coff = 1.05f;
  165. }
  166. _discharger_coefficient = _discharger_coefficient * coff;
  167. }
  168. }
  169. #if LEAST_SQUARE==1
  170. static void start_least_square(int start){
  171. if (start && !least_square_started) {
  172. least_square_init(&discharger_vol_coef, 10);
  173. least_square_init(&discharger_cell_coef, 10);
  174. least_square_init(&discharger_capacity_coef, 10);
  175. least_square_time = 0;
  176. least_square_started = 1;
  177. shark_timer_post(&least_square_timer, LEAST_SQUARE_STEP_TIME);
  178. }else if (!start && least_square_started){
  179. least_square_time = 0;
  180. least_square_started = 0;
  181. shark_timer_cancel(&least_square_timer);
  182. }
  183. }
  184. static void _least_square_timer_handler(shark_timer_t *timer){
  185. if (least_square_put(&discharger_vol_coef, least_square_time, bms_state_get()->pack_voltage/1000.0f) == 1) {
  186. soc_error("voltage: A = %f, B = %f, v: %f\n", discharger_vol_coef.coeff.Ka, discharger_vol_coef.coeff.Cb, get_y_by_x(&discharger_vol_coef, least_square_time));
  187. int delta = get_x_by_y(&discharger_vol_coef, bms_health_pack_lower_voltage()/1000.0f) - get_x_by_y(&discharger_vol_coef, bms_state_get()->pack_voltage/1000.0f);
  188. soc_error("remain %d s to reach lower pack voltage\n", delta);
  189. }
  190. if (least_square_put(&discharger_cell_coef, least_square_time, bms_state_get()->cell_min_vol/1000.0f) == 1) {
  191. soc_error("cell: A = %f, B = %f, v: %f\n", discharger_cell_coef.coeff.Ka, discharger_cell_coef.coeff.Cb, get_y_by_x(&discharger_cell_coef, least_square_time));
  192. int delta = get_x_by_y(&discharger_cell_coef, bms_health_cell_lower_voltage()/1000.0f) - get_x_by_y(&discharger_cell_coef, bms_state_get()->cell_min_vol/1000.0f);
  193. soc_error("remain %d s to reach lower cell voltage\n", delta);
  194. }
  195. if (least_square_put(&discharger_capacity_coef, least_square_time, _soc.coulomb_now/3600.0f) == 1) {
  196. soc_error("capacity: A = %f, B = %f, c: %f\n", discharger_capacity_coef.coeff.Ka, discharger_capacity_coef.coeff.Cb, get_y_by_x(&discharger_capacity_coef, least_square_time));
  197. int delta = get_x_by_y(&discharger_capacity_coef, _soc.coulomb_min/3600.0f) - get_x_by_y(&discharger_capacity_coef, _soc.coulomb_now/3600.0f);
  198. soc_error("remain %d s to reach 0 min AH\n", delta);
  199. }
  200. least_square_time ++;
  201. shark_timer_post(&least_square_timer, LEAST_SQUARE_STEP_TIME);
  202. }
  203. #endif
  204. #define TOHA(x) (float)(x/3600.0f)
  205. void soc_log(void){
  206. soc_debug("C flags 0x%x\n", _soc.flags);
  207. soc_debug("C now: %.4f\n", TOHA(_soc.coulomb_now));
  208. soc_debug("C min: %.4f\n", TOHA(_soc.coulomb_min));
  209. soc_debug("C max: %.4f\n", TOHA(_soc.coulomb_max));
  210. soc_debug("C char: %.4f\n", TOHA(_soc.charger_coulomb));
  211. soc_debug("C dischar: %.4f\n", TOHA(_soc.dischrger_coulomb));
  212. soc_debug("C pre char: %.4f\n", TOHA(_soc.pre_discharger_coulomb));
  213. soc_debug("C pre dischar: %.4f\n", TOHA(_soc.pre_charger_coulomb));
  214. soc_debug("C tol: %.2f\n", _soc.total_coulomb);
  215. soc_debug("C energy: %f\n", _soc.energy);
  216. soc_debug("C delta time %f,%f, -- %d\n", max_soc_delta_time, soc_delta_time, force_full_ts);
  217. soc_debug("C discharger coefficient = %f\n", _discharger_coefficient);
  218. if (chargering){
  219. soc_debug("C remain %d\n", charger_remain_time);
  220. }
  221. }
  222. //初始上电或者nv出问题后,通过开路电压对soc做一次初略校准
  223. static void calibrate_soc_by_ocv(void){
  224. uint16_t pack_vol = 0;
  225. for (int i = 0; i < CELLS_NUM; i++){
  226. pack_vol += measure_value()->cell_vol[i];
  227. }
  228. if (pack_vol <= (48000)){
  229. _soc.capacity = 0;
  230. }else if (pack_vol <= 49000){
  231. _soc.capacity = 5;
  232. }else if (pack_vol <= 50000){
  233. _soc.capacity = 10;
  234. }else if (pack_vol <= 51000){
  235. _soc.capacity = 30;
  236. }else if (pack_vol <= 52000){
  237. _soc.capacity = 50;
  238. }else if (pack_vol <= 53000){
  239. _soc.capacity = 60;
  240. }else {
  241. _soc.capacity = 80;
  242. }
  243. _soc.coulomb_now = (_soc.coulomb_max - _soc.coulomb_min) * _soc.capacity / 100.0f + _soc.coulomb_min;
  244. soc_warning("SOC: calibrate_soc_by_ocv -> capacity = %d, pack_voltage = %d\n", _soc.capacity, pack_vol);
  245. }
  246. static __inline__ float _delta_time(void){
  247. u32 delta = shark_get_mseconds() - current_sample_ts;
  248. current_sample_ts = shark_get_mseconds();
  249. soc_delta_time = (float)delta / (1000.0f);
  250. if (soc_delta_time > max_soc_delta_time){
  251. max_soc_delta_time = soc_delta_time;
  252. }
  253. return soc_delta_time; //秒
  254. }
  255. static __inline__ int can_modify_min_cap(void){
  256. if (force_full_ts == 0) {
  257. return 0;
  258. }
  259. if (shark_get_seconds() > force_full_ts){
  260. if ((shark_get_seconds() - force_full_ts) >= MAX_TIME_FULL_TO_EMPTY) {
  261. return 0;
  262. }else {
  263. return 1;
  264. }
  265. }
  266. return 0;
  267. }
  268. static __inline__ int can_modify_min_when_full(void){
  269. if (force_empty_ts == 0) {
  270. return 0;
  271. }
  272. if (shark_get_seconds() > force_empty_ts){
  273. if ((shark_get_seconds() - force_empty_ts) >= MAX_TIME_EMPTY_TO_FULL) {
  274. return 0;
  275. }else {
  276. return 1;
  277. }
  278. }
  279. return 0;
  280. }
  281. static bool is_force_full = false;
  282. static u32 force_full_time = 0;
  283. bool soc_is_force_full(void) {
  284. if (is_force_full) {
  285. if (shark_get_seconds() >= 5 + force_full_time) {
  286. is_force_full = false;
  287. }
  288. }
  289. return is_force_full;
  290. }
  291. #define min_cap_lfp 0.2f
  292. static void _force_capacity_full(void){
  293. _soc.capacity = 100;
  294. is_force_full = true;
  295. force_full_time = shark_get_seconds();
  296. if (can_modify_min_when_full()) { //前面出现过电芯欠压, 当前容量没到最大容量
  297. double curr_real_cap = start_charger_coulomb + _soc.charger_coulomb;
  298. double curr_min_cap = 0.0f;
  299. if (curr_real_cap > _soc.coulomb_max) {
  300. curr_min_cap = 0.0f;
  301. }else {
  302. curr_min_cap = _soc.coulomb_max - curr_real_cap;
  303. }
  304. //don't trust if curr_min_cap big than before
  305. if (curr_min_cap < _soc.coulomb_min) {
  306. _soc.coulomb_min = _soc.coulomb_min * (1.0f - min_cap_lfp) + curr_min_cap * min_cap_lfp; //lowpass filter
  307. soc_warning("current real cap %f\n", _soc.coulomb_min);
  308. push_event(Min_Cap_For_Full, (u32)curr_real_cap);
  309. }
  310. }
  311. push_event(Charger_Full_cap, (u32)_soc.coulomb_now);
  312. _soc.coulomb_now = _soc.coulomb_max;//充满后,当前容量设置为最大容量
  313. force_full_ts = shark_get_seconds();
  314. }
  315. static int _soc_is_under_voltage(void) {
  316. return (bms_health()->powerdown_lower_voltage || bms_health()->sigle_cell_lower_voltage ||
  317. bms_health()->discharger_lower_voltage);
  318. }
  319. static int _is_normal_charging(void) {
  320. return (_soc.charger_coulomb >= (0.1f * 3600.0f));
  321. }
  322. static int _soc_update_by_ocv(uint8_t prev_charge_status){
  323. static int ocv_full_count = 0;
  324. //static int ocv_force_capaticy = 0;
  325. int changed = 0;
  326. if ((_soc.flags & SOC_FLAG_CALIBRATED) == 0){
  327. return 0;
  328. }
  329. if (!chargering){
  330. if (_soc.capacity && _soc_is_under_voltage()) {
  331. soc_warning("judge calib min col %d - %d\n", shark_get_seconds(), force_empty_ts);
  332. if (can_modify_min_cap()){
  333. if (health_is_low_current()) {
  334. _soc.coulomb_min = _soc.coulomb_now; //已经校准过了,而且电池在常温下进入powerdown,最小容量修正为当前容量
  335. }else if (health_is_mid_current()) {
  336. _soc.coulomb_min = _soc.coulomb_now * 1.0f;
  337. }else if (health_is_big_current()){
  338. _soc.coulomb_min = _soc.coulomb_now * 0.95f;
  339. }else {
  340. _soc.coulomb_min = _soc.coulomb_now * 0.9f;
  341. }
  342. _soc.coulomb_now = _soc.coulomb_min;
  343. push_event(Min_Cap_For_DisCharger, (u32)_soc.coulomb_now);
  344. soc_warning("calicablite coulomb_min %f\n", _soc.coulomb_min);
  345. }else {
  346. _soc.coulomb_now = _soc.coulomb_min;
  347. }
  348. force_empty_ts = shark_get_seconds();
  349. _soc.capacity = 0;
  350. return 1;
  351. }
  352. }
  353. if (chargering || prev_charge_status) {
  354. if (chargering && (_soc.capacity != 100)) {
  355. if (bms_health()->sigle_cell_over_voltage && _is_normal_charging()) { //单电芯过压强制充满
  356. _force_capacity_full();
  357. push_event(Charger_Full, bms_state_get()->pack_voltage);
  358. ocv_full_count = 0;
  359. changed = 1;
  360. }else if (bms_state_get()->pack_voltage >= (FULL_MAX_VOLTAGE_CHARGING) && _is_normal_charging()){
  361. if (ocv_full_count++ >= 100) { //连续100次(电流采集25(小于4A)或者5ms一次)电压和电流满足条件,强制充满
  362. _force_capacity_full();
  363. push_event(Charger_Full, 4);
  364. ocv_full_count = 0;
  365. changed = 1;
  366. }
  367. }else {
  368. ocv_full_count = 0;
  369. }
  370. } else if (!chargering && prev_charge_status && (_soc.capacity != 100)){
  371. if ((bms_state_get()->pack_voltage >= FULL_MAX_VOLTAGE_STOP_CHARGING) && _is_normal_charging()){//充电容量几乎接近最大容量
  372. _force_capacity_full();
  373. push_event(Charger_Full, 5);
  374. changed = 1;
  375. }
  376. }
  377. }
  378. return changed;
  379. }
  380. int soc_update_by_ocv(void){
  381. return _soc_update_by_ocv(0);
  382. }
  383. static void soc_calibrate(uint8_t prev_charge_status){
  384. if (!(_soc.flags & SOC_FLAG_CALIBRATED)){
  385. if (chargering){//用ocv进行严格校准
  386. if (_soc.capacity != 100){
  387. if (!bms_work_is_normal()) {
  388. if ((bms_state_get()->pack_voltage >= AGINT_TEST_MAX_VOLTAGE_CHARGING)){
  389. _force_capacity_full();
  390. push_event(Charger_Full, 12);
  391. }else if (bms_health()->sigle_cell_over_voltage) {
  392. _force_capacity_full();
  393. push_event(Charger_Full, 13);
  394. }
  395. }else {
  396. if ((bms_state_get()->pack_voltage >= FULL_MAX_VOLTAGE_CHARGING) && _is_normal_charging()){
  397. _force_capacity_full();
  398. push_event(Charger_Full, 10);
  399. }else if (bms_health()->sigle_cell_over_voltage) {
  400. _force_capacity_full();
  401. push_event(Charger_Full, 1);
  402. }
  403. }
  404. }
  405. }else if (prev_charge_status){
  406. if((_soc.capacity != 100) && ((bms_state_get()->pack_voltage >= FULL_MAX_VOLTAGE_STOP_CHARGING) || bms_health()->sigle_cell_over_voltage)){
  407. soc_debug("calibrate Capacity to 100\n");
  408. _force_capacity_full();
  409. push_event(Charger_Full, 2);
  410. }
  411. }else {
  412. if (_soc.capacity && _soc_is_under_voltage()) {
  413. _soc.coulomb_now = _soc.coulomb_min = 0;
  414. _soc.capacity = 0;
  415. }
  416. }
  417. }
  418. }
  419. static void soc_update_charger_remain_time(void){
  420. if (!chargering) {
  421. return;
  422. }
  423. float delta_c = _soc.coulomb_max - _soc.coulomb_now;
  424. float current = measure_value()->load_current / 1000.0f; //A
  425. uint32_t remain = delta_c / current / 60; //分钟
  426. if (charger_remain_time == 0){
  427. charger_remain_time = remain;
  428. }else if (remain < charger_remain_time){
  429. charger_remain_time = remain;
  430. }else { //如果充电时间变长,考虑是否快充满电流小于1A
  431. if (bms_state_get()->pack_voltage < 53000 && current > 1.5f) {
  432. charger_remain_time = remain;
  433. }
  434. }
  435. if (_soc.capacity == 100) {
  436. charger_remain_time = 0;
  437. }
  438. }
  439. uint32_t soc_get_cycle(void){
  440. return _soc.total_coulomb/MAX_HA/2;
  441. }
  442. uint8_t soc_get_soh(void){
  443. return (_soc.coulomb_max - _soc.coulomb_min)/_soc.coulomb_max * 100;
  444. }
  445. uint32_t soc_get_charger_remain_time(void){
  446. return charger_remain_time;
  447. }
  448. static void soc_update_by_current_and_time(float current_now, float delta_time, uint8_t prev_charge_status){
  449. double current = current_now / 1000.0f; //A
  450. double delta_q = current * delta_time;
  451. uint8_t est_capaticy = _soc.capacity;
  452. int update_capticy = 0;
  453. if (!chargering) {
  454. soc_update_discharger_coeff();
  455. delta_q = delta_q * _discharger_coefficient;
  456. }
  457. double est_coulomb = _soc.coulomb_now + delta_q;//计算当前容量,充电加, 放电减
  458. if (est_coulomb < 0){
  459. est_coulomb = 0;
  460. }else if (est_coulomb > _soc.coulomb_max) {
  461. est_coulomb = _soc.coulomb_max;
  462. }
  463. if (est_coulomb >= _soc.coulomb_min) {
  464. est_capaticy = ((est_coulomb - _soc.coulomb_min)/(_soc.coulomb_max - _soc.coulomb_min) + 0.005f) * 100;//四舍五入
  465. }
  466. if (chargering){
  467. delta_q = delta_q * _charger_coefficient;
  468. _soc.charger_coulomb += abs(delta_q);
  469. if ((est_capaticy < 100) && (est_capaticy >= _soc.capacity)){ //充电,容量不能等于100,需要靠电压和充电电流来矫正到100
  470. update_capticy = 1;
  471. }
  472. }else {
  473. _soc.dischrger_coulomb += abs(delta_q);
  474. if (est_coulomb < _soc.coulomb_min) {
  475. _soc.coulomb_min = est_coulomb;
  476. }
  477. if ((est_capaticy > 0) && (est_capaticy <= _soc.capacity)) { //放电,容量不能等于0,需要靠欠压或者PowerDown 矫正到0
  478. update_capticy = 1;
  479. }
  480. }
  481. if (update_capticy) {
  482. if (_soc.capacity != est_capaticy) {
  483. _soc.capacity = est_capaticy;
  484. }else {
  485. update_capticy = 0;
  486. }
  487. }
  488. _soc.coulomb_now = est_coulomb;
  489. //通过电压校准SOC,只能在电压范围的两端校准
  490. update_capticy |= _soc_update_by_ocv(prev_charge_status);
  491. soc_calibrate(prev_charge_status);
  492. //如果没有校准过,充电过程中,电量100%后,设置校准标志位
  493. if (chargering && (_soc.capacity == 100)){
  494. if ((_soc.flags & SOC_FLAG_CALIBRATED) == 0){
  495. _soc.flags |= SOC_FLAG_CALIBRATED;
  496. update_capticy = 1;
  497. soc_warning("calibrate OK, charging coulomb: %f\n", _soc.charger_coulomb);
  498. }
  499. }
  500. if (_soc.coulomb_now >= _soc.coulomb_min) {
  501. _soc.energy = bms_state_get()->pack_voltage/1000.f * (_soc.coulomb_now - _soc.coulomb_min);
  502. }
  503. if (update_capticy) {
  504. nv_save_soc();
  505. }
  506. }
  507. /*休眠bms功耗 + 电芯自放电 28天 3% (28天1AH)*/
  508. void soc_update_for_deepsleep(float sleep_time){
  509. soc_update_by_current_and_time(-(0.50f + 1000.0f/(24.f * 28.f)), sleep_time, 0); //休眠功耗310uA(300uA + 10uA固定消耗)
  510. current_sample_ts = shark_get_mseconds(); //唤醒后复位采集时间,如果不采集会重复计算
  511. }
  512. void soc_update(void){
  513. uint8_t pre_chargering = chargering;
  514. if (!chargering && bms_state_get()->charging){
  515. _soc.pre_charger_coulomb = _soc.charger_coulomb;
  516. _soc.charger_coulomb = 0;//clear charing
  517. _soc.total_coulomb += _soc.pre_charger_coulomb / 3600.0f;
  518. chargering = 1;
  519. start_charger_coulomb = _soc.coulomb_now;
  520. #if LEAST_SQUARE==1
  521. start_least_square(0);
  522. #endif
  523. soc_warning("changed to chargering, current = %d\n", measure_value()->load_current);
  524. }else if (chargering && !bms_state_get()->charging){
  525. _soc.pre_discharger_coulomb = _soc.dischrger_coulomb;
  526. _soc.dischrger_coulomb = 0; //clear discharger
  527. _soc.total_coulomb += _soc.pre_discharger_coulomb / 3600.0f;
  528. chargering = 0;
  529. charger_remain_time = 0;
  530. soc_warning("changed to dischargering, current = %d\n", measure_value()->load_current);
  531. }
  532. #if LEAST_SQUARE==1
  533. if(!chargering && abs(measure_value()->load_current) >= 5000){
  534. start_least_square(1);
  535. }
  536. #endif
  537. soc_update_by_current_and_time(measure_value()->load_current, _delta_time(), pre_chargering);
  538. soc_update_charger_remain_time();
  539. }
  540. soc_t *get_soc(void){
  541. return &_soc;
  542. }