soc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 time_ms = 0;
  13. static float soc_delta_time = 0;
  14. static float max_soc_delta_time = 0;
  15. static float _charger_coefficient = 1.0f;
  16. static float _discharger_coefficient = 1.0f;
  17. static uint8_t is_force_full = 0;
  18. static uint8_t is_force_empty = 0;
  19. uint32_t charger_remain_time = 0;
  20. #define DEFALUT_MAX_COULOMB (MAX_HA * 3600.0f)
  21. #define DEFALUT_MIN_COULOMB (25.0f * 3600.0f)
  22. static void calibrate_soc_by_ocv(void);
  23. #if LEAST_SQUARE==1
  24. static void _least_square_timer_handler(shark_timer_t *timer);
  25. static least_square_t discharger_vol_coef;
  26. static least_square_t discharger_cell_coef;
  27. static least_square_t discharger_capacity_coef;
  28. static shark_timer_t least_square_timer = {.handler = _least_square_timer_handler};
  29. static int least_square_time = 0;
  30. static int least_square_started = 0;
  31. #define LEAST_SQUARE_STEP_TIME 1000 * 5
  32. #endif
  33. void soc_init(void){
  34. set_log_level(MOD_SOC, L_debug);
  35. time_ms = shark_get_mseconds();
  36. if (nv_restore_soc() != 0){
  37. soc_warning("SOC: nv storage is not inited, use default value!!\n");
  38. _soc.coulomb_min = 0;
  39. _soc.coulomb_max = DEFALUT_MAX_COULOMB; //30HA,这个值最总需要soh模块给
  40. _soc.flags = 0;
  41. _soc.charger_coulomb = 0;
  42. _soc.pre_charger_coulomb = 0;
  43. _soc.dischrger_coulomb = 0;
  44. _soc.pre_discharger_coulomb = 0;
  45. _soc.total_coulomb = 0;
  46. }
  47. if ((_soc.flags & SOC_FLAG_CALIBRATED) == 0){
  48. calibrate_soc_by_ocv();
  49. nv_save_soc();
  50. }
  51. soc_log();
  52. }
  53. #if LEAST_SQUARE==1
  54. static void start_least_square(int start){
  55. if (start && !least_square_started) {
  56. least_square_init(&discharger_vol_coef, 10);
  57. least_square_init(&discharger_cell_coef, 10);
  58. least_square_init(&discharger_capacity_coef, 10);
  59. least_square_time = 0;
  60. least_square_started = 1;
  61. shark_timer_post(&least_square_timer, LEAST_SQUARE_STEP_TIME);
  62. }else if (!start && least_square_started){
  63. least_square_time = 0;
  64. least_square_started = 0;
  65. shark_timer_cancel(&least_square_timer);
  66. }
  67. }
  68. static void _least_square_timer_handler(shark_timer_t *timer){
  69. if (least_square_put(&discharger_vol_coef, least_square_time, bms_state_get()->pack_voltage/1000.0f) == 1) {
  70. 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));
  71. 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);
  72. soc_error("remain %d s to reach lower pack voltage\n", delta);
  73. }
  74. if (least_square_put(&discharger_cell_coef, least_square_time, bms_state_get()->cell_min_vol/1000.0f) == 1) {
  75. 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));
  76. 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);
  77. soc_error("remain %d s to reach lower cell voltage\n", delta);
  78. }
  79. if (least_square_put(&discharger_capacity_coef, least_square_time, _soc.coulomb_now/3600.0f) == 1) {
  80. 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));
  81. 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);
  82. soc_error("remain %d s to reach 0 min AH\n", delta);
  83. }
  84. least_square_time ++;
  85. shark_timer_post(&least_square_timer, LEAST_SQUARE_STEP_TIME);
  86. }
  87. #endif
  88. #define TOHA(x) (float)(x/3600.0f)
  89. void soc_log(void){
  90. soc_debug("C flags 0x%x\n", _soc.flags);
  91. soc_debug("C now: %.4f\n", TOHA(_soc.coulomb_now));
  92. soc_debug("C min: %.4f\n", TOHA(_soc.coulomb_min));
  93. soc_debug("C max: %.4f\n", TOHA(_soc.coulomb_max));
  94. soc_debug("C char: %.4f\n", TOHA(_soc.charger_coulomb));
  95. soc_debug("C dischar: %.4f\n", TOHA(_soc.dischrger_coulomb));
  96. soc_debug("C pre char: %.4f\n", TOHA(_soc.pre_discharger_coulomb));
  97. soc_debug("C pre dischar: %.4f\n", TOHA(_soc.pre_charger_coulomb));
  98. soc_debug("C tol: %.2f\n", _soc.total_coulomb);
  99. soc_debug("C energy: %f\n", _soc.energy);
  100. soc_debug("C delta time %f,%f\n", max_soc_delta_time, soc_delta_time);
  101. if (chargering){
  102. soc_debug("C remain %d\n", charger_remain_time);
  103. }
  104. }
  105. //初始上电或者nv出问题后,通过开路电压对soc做一次初略校准
  106. static void calibrate_soc_by_ocv(void){
  107. uint16_t pack_vol = 0;
  108. for (int i = 0; i < CELLS_NUM; i++){
  109. pack_vol += measure_value()->cell_vol[i];
  110. }
  111. if (pack_vol < (2700 * CELLS_NUM)){
  112. _soc.capacity = 0;
  113. }else if (pack_vol < (2950 * CELLS_NUM)){
  114. _soc.capacity = 5;
  115. }else if (pack_vol < (3200 * CELLS_NUM)){
  116. _soc.capacity = 15;
  117. }else if (pack_vol < (3400 * CELLS_NUM)){
  118. _soc.capacity = 25;
  119. }else if (pack_vol < (3500 * CELLS_NUM)){
  120. _soc.capacity = 85;
  121. }else if (pack_vol < (3550 * CELLS_NUM)){
  122. _soc.capacity = 95;
  123. }else {
  124. _soc.capacity = 100;
  125. }
  126. _soc.coulomb_now = (_soc.coulomb_max - _soc.coulomb_min) * _soc.capacity / 100.0f + _soc.coulomb_min;
  127. soc_warning("SOC: calibrate_soc_by_ocv -> capacity = %d, pack_voltage = %d\n", _soc.capacity, pack_vol);
  128. }
  129. static __inline__ float _delta_time(void){
  130. u32 delta = shark_get_mseconds() - time_ms;
  131. time_ms = shark_get_mseconds();
  132. soc_delta_time = (float)delta / (1000.0f);
  133. if (soc_delta_time > max_soc_delta_time){
  134. max_soc_delta_time = soc_delta_time;
  135. }
  136. return soc_delta_time; //秒
  137. }
  138. int soc_update_by_ocv(void){
  139. int changed = 0;
  140. if (_soc.flags & SOC_FLAG_CALIBRATED){
  141. if (!chargering){
  142. if (bms_health()->is_work_temp_normal) {
  143. if (!is_force_empty && (bms_health()->powerdown_lower_voltage || bms_health()->sigle_cell_lower_voltage || bms_health()->discharger_lower_voltage)) {
  144. _soc.coulomb_min = _soc.coulomb_now; //已经校准过了,而且电池在常温下进入powerdown,最小容量修正为当前容量
  145. _soc.capacity = 0;
  146. is_force_empty = 1;
  147. changed = 1;
  148. soc_warning("current coulomb %f\n", _soc.coulomb_now);
  149. }
  150. }
  151. }
  152. if (chargering && !is_force_full){
  153. if (bms_state_get()->pack_voltage >= (53500) && (measure_value()->load_current <= 500.0f)){
  154. _soc.capacity = 100;
  155. is_force_full = 1;
  156. changed = 1;
  157. }
  158. }
  159. }
  160. return changed;
  161. }
  162. static void soc_calibrate(uint8_t prev_charge_status){
  163. static int cali_full_count = 0;
  164. if (!(_soc.flags & SOC_FLAG_CALIBRATED)){
  165. if (chargering){//用ocv进行严格校准
  166. if (!is_force_full && (measure_value()->load_current <= 500.0f) && (bms_state_get()->pack_voltage >= 53500)){
  167. cali_full_count ++;
  168. if (cali_full_count == 10) {
  169. soc_debug("calibrate Capacity to 100, measure_value()->load_current %d\n", measure_value()->load_current);
  170. _soc.capacity = 100;
  171. is_force_full = 1;
  172. }
  173. }
  174. }else if (prev_charge_status){
  175. if(!is_force_full && (bms_state_get()->pack_voltage >= 53500)){
  176. soc_debug("calibrate Capacity to 100\n");
  177. _soc.capacity = 100;
  178. is_force_full = 1;
  179. }
  180. }
  181. }
  182. }
  183. static void soc_update_charger_remain_time(void){
  184. if (!chargering) {
  185. return;
  186. }
  187. float delta_c = _soc.coulomb_max - _soc.coulomb_now;
  188. float current = measure_value()->load_current / 1000.0f; //A
  189. uint32_t remain = delta_c / current / 60; //分钟
  190. if (charger_remain_time == 0){
  191. charger_remain_time = remain;
  192. }else if (remain < charger_remain_time){
  193. charger_remain_time = remain;
  194. }
  195. if (_soc.capacity == 100) {
  196. charger_remain_time = 0;
  197. }
  198. }
  199. uint32_t soc_get_cycle(void){
  200. return _soc.total_coulomb/MAX_HA;
  201. }
  202. uint32_t soc_get_charger_remain_time(void){
  203. return charger_remain_time;
  204. }
  205. static void soc_update_by_current_and_time(float current_now, float delta_time, uint8_t prev_charge_status){
  206. double current = current_now / 1000.0f; //A
  207. double delta_q = current * delta_time;
  208. if (chargering){
  209. delta_q = delta_q * _charger_coefficient;
  210. _soc.charger_coulomb += abs(delta_q);
  211. }else {
  212. delta_q = delta_q * _discharger_coefficient;
  213. _soc.dischrger_coulomb += abs(delta_q); //转为正数
  214. }
  215. _soc.coulomb_now = _soc.coulomb_now + delta_q; //充电加, 放电减
  216. if (_soc.coulomb_now < 0){
  217. _soc.coulomb_now = 0;
  218. }
  219. uint8_t old_cap = _soc.capacity;
  220. if ((_soc.coulomb_now - _soc.coulomb_min) >= 0){
  221. _soc.capacity = ((_soc.coulomb_now - _soc.coulomb_min)/(_soc.coulomb_max - _soc.coulomb_min) + 0.005f) * 100;//四舍五入
  222. }else {
  223. _soc.capacity = 0;
  224. }
  225. if (_soc.capacity > 100){
  226. _soc.capacity = 100;
  227. }
  228. if (chargering && (_soc.capacity == 100) && (!is_force_full)){
  229. _soc.capacity = 99;//充电的时候必须通过ocv才能把电量校准到100
  230. }else if (!chargering && (_soc.capacity == 0) && !is_force_empty){
  231. _soc.capacity = 1;
  232. }
  233. //通过电压校准SOC,只能在电压范围的两端校准
  234. soc_update_by_ocv();
  235. soc_calibrate(prev_charge_status);
  236. //如果没有校准过,充电过程中,电量100%后,设置校准标志位
  237. if (chargering && (_soc.capacity == 100)){
  238. if ((_soc.flags & SOC_FLAG_CALIBRATED) == 0){
  239. _soc.coulomb_now = _soc.coulomb_max;
  240. _soc.flags |= SOC_FLAG_CALIBRATED;
  241. nv_save_soc();
  242. soc_warning("calibrate OK, charging coulomb: %f\n", _soc.charger_coulomb);
  243. }else { //如果校准过,单电芯过压,100%的容量,设置最大容量为当前容量
  244. if (bms_health()->sigle_cell_over_voltage){
  245. if ((_soc.coulomb_now >= DEFALUT_MIN_COULOMB) && (_soc.coulomb_now <= DEFALUT_MAX_COULOMB)) {
  246. _soc.coulomb_max = _soc.coulomb_now;
  247. soc_warning("signal cell over vol, cap full, reset coul max to coul now: %f\n", _soc.coulomb_max);
  248. }
  249. }
  250. }
  251. }
  252. _soc.energy = bms_state_get()->pack_voltage/1000.f * (_soc.coulomb_now - _soc.coulomb_min);
  253. if (old_cap != _soc.capacity) {
  254. nv_save_soc();
  255. }
  256. }
  257. /*休眠bms功耗 + 电芯自放电 28天 3% (28天1AH)*/
  258. void soc_update_for_deepsleep(float sleep_time){
  259. soc_update_by_current_and_time(-(0.32f + 1000.0f/(24.f * 28.f)), sleep_time, 0); //休眠功耗310uA(300uA + 10uA固定消耗)
  260. }
  261. void soc_update(void){
  262. uint8_t pre_chargering = chargering;
  263. if (!chargering && bms_state_get()->charging){
  264. _soc.pre_charger_coulomb = _soc.charger_coulomb;
  265. _soc.charger_coulomb = 0;//clear charing
  266. _soc.total_coulomb += _soc.pre_charger_coulomb / 3600.0f;
  267. chargering = 1;
  268. is_force_empty = 0;
  269. if (_soc.capacity < 100) {
  270. is_force_full = 0;
  271. }
  272. #if LEAST_SQUARE==1
  273. start_least_square(0);
  274. #endif
  275. soc_warning("changed to chargering, current = %d\n", measure_value()->load_current);
  276. }else if (chargering && !bms_state_get()->charging){
  277. _soc.pre_discharger_coulomb = _soc.dischrger_coulomb;
  278. _soc.dischrger_coulomb = 0; //clear discharger
  279. _soc.total_coulomb += _soc.pre_discharger_coulomb / 3600.0f;
  280. chargering = 0;
  281. if (_soc.capacity < 100) {
  282. is_force_full = 0;
  283. }
  284. soc_warning("changed to dischargering, current = %d\n", measure_value()->load_current);
  285. }
  286. #if LEAST_SQUARE==1
  287. if(!chargering && abs(measure_value()->load_current) >= 5000){
  288. start_least_square(1);
  289. }
  290. #endif
  291. soc_update_by_current_and_time(measure_value()->load_current, _delta_time(), pre_chargering);
  292. soc_update_charger_remain_time();
  293. }
  294. soc_t *get_soc(void){
  295. return &_soc;
  296. }