soc.c 13 KB

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