soc.c 18 KB

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