soc.c 7.6 KB

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