soc.c 6.0 KB

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