soc.c 9.8 KB

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