soc.c 21 KB

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