soc.c 13 KB

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