soc.c 18 KB

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