soc.c 17 KB

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