soc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "health.h"
  7. #include "state.h"
  8. static soc_t _soc;
  9. static uint8_t chargering = 0;
  10. static u64 time_ms = 0;
  11. static float _charger_coefficient = 1.0f;
  12. static float _discharger_coefficient = 1.0f;
  13. uint32_t charger_remain_time = 0;
  14. #define DEFALUT_MAX_COULOMB (MAX_HA * 3600.0f)
  15. static void calibrate_soc_by_ocv(void);
  16. void soc_init(void){
  17. set_log_level(MOD_SOC, L_debug);
  18. time_ms = shark_get_mseconds();
  19. if (nv_restore_soc() != 0){
  20. soc_warning("SOC: nv storage is not inited, use default value!!\n");
  21. _soc.coulomb_min = 0;
  22. _soc.coulomb_max = DEFALUT_MAX_COULOMB; //30HA,这个值最总需要soh模块给
  23. _soc.flags = 0;
  24. _soc.charger_coulomb = 0;
  25. _soc.pre_charger_coulomb = 0;
  26. _soc.dischrger_coulomb = 0;
  27. _soc.pre_discharger_coulomb = 0;
  28. _soc.total_coulomb = 0;
  29. }
  30. if ((_soc.flags & SOC_FLAG_CALIBRATED) == 0){
  31. calibrate_soc_by_ocv();
  32. nv_save_soc();
  33. }
  34. }
  35. //初始上电或者nv出问题后,通过开路电压对soc做一次初略校准
  36. static void calibrate_soc_by_ocv(void){
  37. uint16_t pack_vol = 0;
  38. for (int i = 0; i < CELLS_NUM; i++){
  39. pack_vol += measure_value()->cell_vol[i];
  40. }
  41. if (pack_vol < (2700 * CELLS_NUM)){
  42. _soc.capacity = 0;
  43. }else if (pack_vol < (2950 * CELLS_NUM)){
  44. _soc.capacity = 5;
  45. }else if (pack_vol < (3200 * CELLS_NUM)){
  46. _soc.capacity = 15;
  47. }else if (pack_vol < (3400 * CELLS_NUM)){
  48. _soc.capacity = 25;
  49. }else if (pack_vol < (3500 * CELLS_NUM)){
  50. _soc.capacity = 85;
  51. }else if (pack_vol < (3550 * CELLS_NUM)){
  52. _soc.capacity = 95;
  53. }else {
  54. _soc.capacity = 100;
  55. }
  56. _soc.coulomb_now = (_soc.coulomb_max - _soc.coulomb_min) * _soc.capacity / 100.0f + _soc.coulomb_min;
  57. soc_warning("SOC: calibrate_soc_by_ocv -> capacity = %d, pack_voltage = %d\n", _soc.capacity, pack_vol);
  58. }
  59. static __inline__ float _delta_time(void){
  60. u32 delta = shark_get_mseconds() - time_ms;
  61. time_ms = shark_get_mseconds();
  62. return (float)delta / (1000.0f); //秒
  63. }
  64. void soc_update_by_ocv(void){
  65. if (_soc.flags & SOC_FLAG_CALIBRATED){
  66. if (!chargering && bms_health()->powerdown_lower_voltage){
  67. _soc.coulomb_min = _soc.coulomb_now; //已经校准过了,而且电池进入powerdown,最小容量修正为当前容量
  68. _soc.capacity = 0;
  69. soc_warning("current coulomb %f\n", _soc.coulomb_now);
  70. return;
  71. }
  72. if (chargering){
  73. if (bms_state_get()->pack_voltage >= (54000)){
  74. _soc.capacity = 100;
  75. }
  76. }
  77. }else {
  78. if (chargering){//用ocv进行严格校准
  79. if (measure_value()->load_current <= 300.0f){
  80. //判断总电压
  81. if (bms_state_get()->pack_voltage >= 54000){
  82. _soc.capacity = 100;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. static void soc_update_charger_remain_time(void){
  89. if (!chargering) {
  90. return;
  91. }
  92. float delta_c = _soc.coulomb_max - _soc.coulomb_now;
  93. float current = measure_value()->load_current / 1000.0f; //A
  94. uint32_t remain = delta_c / current / 60; //分钟
  95. if (charger_remain_time == 0){
  96. charger_remain_time = remain;
  97. }else if (remain < charger_remain_time){
  98. charger_remain_time = remain;
  99. }
  100. if (_soc.capacity == 100) {
  101. charger_remain_time = 0;
  102. }
  103. }
  104. uint32_t soc_get_cycle(void){
  105. return _soc.total_coulomb/MAX_HA;
  106. }
  107. uint32_t soc_get_charger_remain_time(void){
  108. return charger_remain_time;
  109. }
  110. void soc_update(void){
  111. if (!chargering && bms_state_get()->charging){
  112. _soc.pre_charger_coulomb = _soc.charger_coulomb;
  113. _soc.charger_coulomb = 0;//clear charing
  114. _soc.total_coulomb += _soc.pre_charger_coulomb / 3600.0f;
  115. chargering = 1;
  116. }else if (chargering && !bms_state_get()->charging){
  117. _soc.pre_discharger_coulomb = _soc.dischrger_coulomb;
  118. _soc.dischrger_coulomb = 0; //clear discharger
  119. _soc.total_coulomb += _soc.pre_discharger_coulomb / 3600.0f;
  120. chargering = 0;
  121. }
  122. float current = measure_value()->load_current / 1000.0f; //A
  123. float delta_q = current * _delta_time();
  124. if (chargering){
  125. delta_q = delta_q * _charger_coefficient;
  126. _soc.charger_coulomb += abs(delta_q);
  127. }else {
  128. delta_q = delta_q * _discharger_coefficient;
  129. _soc.dischrger_coulomb += abs(delta_q); //转为正数
  130. }
  131. _soc.coulomb_now += delta_q; //充电加, 放电减
  132. if (_soc.coulomb_now > _soc.coulomb_max){
  133. _soc.coulomb_now = _soc.coulomb_max;
  134. }else if (_soc.coulomb_now < _soc.coulomb_min){
  135. _soc.coulomb_now = _soc.coulomb_min;
  136. }
  137. uint8_t old_cap = _soc.capacity;
  138. _soc.capacity = (_soc.coulomb_now - _soc.coulomb_min)/(_soc.coulomb_max - _soc.coulomb_min) * 100;
  139. if (chargering && (_soc.capacity == 100)){
  140. _soc.capacity = 99;//充电的时候必须通过ovc才能把电量校准到100
  141. }else if (!chargering && (_soc.capacity == 0)){
  142. _soc.capacity = 1;
  143. }
  144. //通过电压校准SOC,只能在电压范围的两端校准
  145. soc_update_by_ocv();
  146. //如果没有校准过,充电过程中,电量100%后,设置校准标志位
  147. if (chargering && (_soc.capacity == 100)){
  148. if ((_soc.flags & SOC_FLAG_CALIBRATED) == 0){
  149. _soc.coulomb_now = _soc.coulomb_max;
  150. _soc.flags |= SOC_FLAG_CALIBRATED;
  151. nv_save_soc();
  152. soc_warning("calibrate OK, charging coulomb: %f\n", _soc.charger_coulomb);
  153. }else { //如果校准过,单电芯过压,100%的容量,设置最大容量为当前容量
  154. if (bms_health()->sigle_cell_over_voltage){
  155. _soc.coulomb_max = _soc.coulomb_now;
  156. soc_warning("signal cell over vol, cap full, reset coul max to coul now: %f\n", _soc.coulomb_max);
  157. }
  158. }
  159. }
  160. _soc.energy = bms_state_get()->pack_voltage/1000.f * (_soc.coulomb_now - _soc.coulomb_min) * _soc.capacity/100.0f;
  161. if (old_cap != _soc.capacity) {
  162. nv_save_soc();
  163. }
  164. soc_update_charger_remain_time();
  165. }
  166. soc_t *get_soc(void){
  167. return &_soc;
  168. }