SOC.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. #include "SOC.h"
  2. #include "look1_is16lu16n16tu16_binlcase.h"
  3. #include "look1_iu16lu16n16tu16_binlcase.h"
  4. boolean_T FirstRun_SOC;
  5. //---------------SOC初始化-----------------------------------------------
  6. void SOC_Init(void)
  7. {
  8. FirstRun_SOC = true;
  9. }
  10. //-------------------------------------------------------------------------
  11. //---------------------------SOC-------------------------------------------
  12. //-------------------------------------------------------------------------
  13. void SOC(void)
  14. {
  15. static uint16_T socn_pct_battSocEE;
  16. static uint16_T socn_pct_bcuSocEE;
  17. static uint16_T socn_Q_cap;
  18. //static int16_T socn_ohm_cellRAvrg;
  19. //
  20. real_T Q;
  21. real_T battcurr;
  22. boolean_T socn_flg_ekfInvalidMin;
  23. boolean_T socn_flg_ekfInvalidMax;
  24. boolean_T socn_flg_ekfInvalidAvrg;
  25. boolean_T socn_flg_ekfInvalid;
  26. real_T ocv;
  27. real_T Ro;
  28. real_T Rp;
  29. real_T C;
  30. real_T deltU;
  31. real_T UL;
  32. real_T A[4];
  33. real_T B[2];
  34. real_T H[2];
  35. real_T K[2];
  36. real_T P1[4];
  37. static real_T P_Min_Delay[4];
  38. static real_T P_Max_Delay[4];
  39. static real_T P_avrg_Delay[4];
  40. real_T soc1;
  41. static real_T soc_Min_Delay;
  42. static real_T soc_Avrg_Delay;
  43. static real_T soc_Max_Delay;
  44. real_T Up1;
  45. static real_T Up_Min_Delay;
  46. static real_T Up_Max_Delay;
  47. static real_T Up_Avrg_Delay;
  48. uint16_T EKFSOCMin;
  49. uint16_T EKFSOCMax;
  50. uint16_T EKFSOCAvrg;
  51. uint16_T cellSocMax;
  52. uint16_T cellSocMin;
  53. uint16_T factor;
  54. //
  55. static real_T ahDelay;
  56. int16_T ahSoc;
  57. //
  58. static uint16_T ekfInvalidCntl;
  59. static boolean_T onceFlg_est;
  60. static int16_T ahSoc0_est;
  61. static uint16_T ekfSoc0_est;
  62. //
  63. uint16_T socd_V_chrgCCV;
  64. uint16_T socd_V_disChrgCCV;
  65. static boolean_T overFlg;
  66. static boolean_T fulFlg;
  67. static uint8_T overCntl;
  68. static uint8_T fulCntl;
  69. static boolean_T onceFlg_utrckOver;
  70. static int16_T ahSoc0_utrckOver;
  71. static uint16_T estSoc0_utrckOver;
  72. static uint16_T Soc_Delay;
  73. static boolean_T lowFlg;
  74. static uint8_T lowCntl;
  75. static boolean_T onceFlg_utrckLow;
  76. static int16_T ahSoc0_utrckLow;
  77. static uint16_T estSoc0_utrckLow;
  78. uint16_T socn_pct_utrackSoc;
  79. uint16_T socTemp;
  80. static uint8_T chrgCntl;
  81. static uint8_T disChrgCntl;
  82. //
  83. //
  84. static uint8_T statCntl;
  85. boolean_T statFlg;
  86. static uint8_T ihd_st_chrgSta_Delay;
  87. static uint16_T socd_pct_battSoc_Delay;
  88. static uint16_T socd_pct_battSoc0;
  89. static uint16_T socd_pct_bcuSoc0;
  90. uint16_T delSOC;
  91. uint16_T bcuSoc;
  92. int16_T coinSoc;
  93. uint16_T x[3];
  94. uint16_T y[3];
  95. boolean_T Flg;
  96. static boolean_T onceFlg_chrg;
  97. static boolean_T onceFlg_dischrg;
  98. static uint16_T socd_pct_battSoc_save;
  99. static uint16_T socd_pct_bcuSoc_save;
  100. uint16_T i;
  101. //
  102. if (FirstRun_SOC)
  103. {
  104. onceFlg_est = true;
  105. ekfInvalidCntl = 0;
  106. overCntl = 0;
  107. fulCntl = 0;
  108. lowCntl = 0;
  109. overFlg = false;
  110. fulFlg = false;
  111. lowFlg = false;
  112. Soc_Delay = 0;
  113. onceFlg_utrckOver = true;
  114. onceFlg_utrckLow = true;
  115. ihd_st_chrgSta_Delay = 0;
  116. socd_pct_battSoc_Delay = 0;
  117. onceFlg_chrg = true;
  118. onceFlg_dischrg = true;
  119. socd_pct_battSoc_save = 0;
  120. socd_pct_bcuSoc_save = 0;
  121. }
  122. //=====================================================================
  123. ////////////////////////初始值获取//////////////////////////////////////
  124. //=====================================================================
  125. if (FirstRun_SOC)
  126. { //
  127. if (socd_pct_battSocEi > 1000 || socd_pct_bcuSocEi > 1000 || ihd_st_EOLState == 0 ||
  128. (((int16_T)(socd_pct_battSocEi - socd_pct_bcuSocEi) > 300 || (int16_T)(socd_pct_battSocEi - socd_pct_bcuSocEi) < -300) && ihd_tm_parkTime >= cmnc_tm_parkTime && sfmd_I_curr < 10 && sfmd_I_curr > -10))
  129. {
  130. socn_pct_battSocEE = look1_iu16lu16n16tu16_binlcase(sfmd_V_cellUAvrg, (&(cmnm_V_ocv[0])), (&(cmnm_pct_soc[0])), 12U);
  131. socn_pct_bcuSocEE = look1_iu16lu16n16tu16_binlcase(sfmd_V_cellUAvrg, (&(cmnm_V_ocv[0])), (&(cmnm_pct_soc[0])), 12U);
  132. }
  133. else
  134. {
  135. socn_pct_battSocEE = socd_pct_battSocEi;
  136. socn_pct_bcuSocEE = socd_pct_bcuSocEi;
  137. }
  138. //
  139. if (ihd_tm_parkTime >= cmnc_tm_parkTime && sfmd_I_curr < 10 && sfmd_I_curr > -10)
  140. {
  141. socn_pct_battSocEE = look1_iu16lu16n16tu16_binlcase(sfmd_V_cellUAvrg, (&(cmnm_V_ocv[0])), (&(cmnm_pct_soc[0])), 12U);
  142. }
  143. socn_Q_cap = (uint16_T)((uint16_T)((uint32_T)sohd_pct_bcuSoh * cmnc_Q_ratedCp / 2000U) << 1);
  144. //socn_ohm_cellRAvrg = ArrMean(&sorv_ohm_cellREi[0], cmnc_num_cellUNum);
  145. }
  146. //printf("1---- battSocEi:%d,bcuSocEi:%d,battSocEE:%d,bcuSocEE:%d\n",socd_pct_battSocEi,socd_pct_bcuSocEi,socn_pct_battSocEE,socn_pct_bcuSocEE);
  147. //======================================================================
  148. ////////////////////////EKFSOC//////////////////////////////////////////
  149. //======================================================================
  150. battcurr = (real_T)sfmd_I_curr * 0.1;
  151. Q = (real_T)socn_Q_cap * 0.1;
  152. //-------------------------EKFmin---------------------------------------
  153. if (FirstRun_SOC)
  154. {
  155. soc_Min_Delay = (real_T)socn_pct_battSocEE * 0.1;
  156. Up_Min_Delay = 0;
  157. P_Min_Delay[0] = 10;
  158. P_Min_Delay[1] = 0;
  159. P_Min_Delay[2] = 0;
  160. P_Min_Delay[3] = 10;
  161. }
  162. //参数查表
  163. ocv = (real_T)look1_iu16lu16n16tu16_binlcase((uint16_T)(soc_Min_Delay * 10), (&(cmnm_pct_soc[0])), (&(cmnm_V_ocv[0])), 12U) * 0.001;
  164. Ro = (real_T)look1_iu16lu16n16tu16_binlcase((uint16_T)(soc_Min_Delay * 10), (&(cmnm_pct_soc[0])), (&(cmnm_R_ohm[0])), 12U) * 0.001 * 0.001;
  165. Rp = (real_T)look1_iu16lu16n16tu16_binlcase((uint16_T)(soc_Min_Delay * 10), (&(cmnm_pct_soc[0])), (&(cmnm_R_polar[0])), 12U) * 0.001 * 0.001;
  166. C = (real_T)look1_iu16lu16n16tu16_binlcase((uint16_T)(soc_Min_Delay * 10), (&(cmnm_pct_soc[0])), (&(cmnm_F_polar[0])), 12U) * 0.001 * 1000;
  167. A[0] = 1;
  168. A[1] = 0;
  169. A[2] = 0;
  170. A[3] = exp(-1 / (Rp * C));
  171. B[0] = 1 / Q / 3600 * 100;
  172. B[1] = Rp * (1 - exp(-1 / (Rp * C)));
  173. H[0] = docvmath(soc_Min_Delay);
  174. H[1] = 1;
  175. //先验
  176. soc1 = soc_Min_Delay * A[0] + B[0] * battcurr;
  177. Up1 = Up_Min_Delay * A[3] + B[1] * battcurr;
  178. UL = ocv + battcurr * Ro + Up1;
  179. P1[0] = P_Min_Delay[0] + 0.001;
  180. P1[1] = P_Min_Delay[1] * A[3] + 0.001;
  181. P1[2] = P_Min_Delay[2] * A[3] + 0.001;
  182. P1[3] = P_Min_Delay[3] * A[3] * A[3] + 0.001;
  183. //增益
  184. K[0] = (P1[0] * H[0] + P1[2]) / (H[0] * P1[0] * H[0] + P1[1] * H[0] + H[0] * P1[2] + P1[3] + 0.5);
  185. K[1] = (P1[1] * H[0] + P1[3]) / (H[0] * P1[0] * H[0] + P1[1] * H[0] + H[0] * P1[2] + P1[3] + 0.5);
  186. //后验
  187. deltU = (real_T)sfmd_V_cellUMin * 0.001 - UL;
  188. soc_Min_Delay = soc1 + K[0] * deltU;
  189. if (soc_Min_Delay < (real_T)socc_pct_battSocLow * 0.1)
  190. {
  191. soc_Min_Delay = (real_T)socc_pct_battSocLow * 0.1;
  192. }
  193. if (soc_Min_Delay > (real_T)socc_pct_battSocUp * 0.1)
  194. {
  195. soc_Min_Delay = (real_T)socc_pct_battSocUp * 0.1;
  196. }
  197. Up_Min_Delay = Up1 + K[1] * deltU;
  198. //P更新
  199. P_Min_Delay[0] = (1 - K[0] * H[0]) * P1[0] - K[0] * P1[1];
  200. P_Min_Delay[1] = -K[1] * H[0] * P1[0] + P1[1] * (1 - K[1]);
  201. P_Min_Delay[2] = (1 - K[0] * H[0]) * P1[2] - K[0] * P1[3];
  202. P_Min_Delay[3] = -K[1] * H[0] * P1[2] + P1[3] * (1 - K[1]);
  203. //输出
  204. EKFSOCMin = (uint16_T)(soc_Min_Delay * 10);
  205. socn_flg_ekfInvalidMin = (deltU > 0.01) || (deltU < -0.01);
  206. //printf("2----socmin:%f,U:%d,R:%f\n",soc_Min_Delay,sfmd_V_cellUMin,Ro);
  207. //------------------------EKFSOCmax-----------------------------------
  208. if (FirstRun_SOC)
  209. {
  210. soc_Max_Delay = (real_T)socn_pct_battSocEE * 0.1;
  211. Up_Max_Delay = 0;
  212. P_Max_Delay[0] = 10;
  213. P_Max_Delay[1] = 0;
  214. P_Max_Delay[2] = 0;
  215. P_Max_Delay[3] = 10;
  216. }
  217. // 参数查表
  218. ocv = (real_T)look1_iu16lu16n16tu16_binlcase((uint16_T)(soc_Max_Delay * 10), (&(cmnm_pct_soc[0])), (&(cmnm_V_ocv[0])), 12U) * 0.001;
  219. Ro = (real_T)look1_iu16lu16n16tu16_binlcase((uint16_T)(soc_Max_Delay * 10), (&(cmnm_pct_soc[0])), (&(cmnm_R_ohm[0])), 12U) * 0.001 * 0.001 ;
  220. Rp = (real_T)look1_iu16lu16n16tu16_binlcase((uint16_T)(soc_Max_Delay * 10), (&(cmnm_pct_soc[0])), (&(cmnm_R_polar[0])), 12U) * 0.001 * 0.001;
  221. C = (real_T)look1_iu16lu16n16tu16_binlcase((uint16_T)(soc_Max_Delay * 10), (&(cmnm_pct_soc[0])), (&(cmnm_F_polar[0])), 12U) * 0.001 * 1000;
  222. A[0] = 1;
  223. A[1] = 0;
  224. A[2] = 0;
  225. A[3] = exp(-1 / (Rp * C));
  226. B[0] = 1 / Q / 3600 * 100;
  227. B[1] = Rp * (1 - exp(-1 / (Rp * C)));
  228. H[0] = docvmath(soc_Max_Delay);
  229. H[1] = 1;
  230. //先验
  231. soc1 = soc_Max_Delay * A[0] + B[0] * battcurr;
  232. Up1 = Up_Max_Delay * A[3] + B[1] * battcurr;
  233. UL = ocv + battcurr * Ro + Up1;
  234. P1[0] = P_Max_Delay[0] + 0.001;
  235. P1[1] = P_Max_Delay[1] * A[3] + 0.001;
  236. P1[2] = P_Max_Delay[2] * A[3] + 0.002;
  237. P1[3] = P_Max_Delay[3] * A[3] * A[3] + 0.001;
  238. //增益
  239. K[0] = (P1[0] * H[0] + P1[2]) / (H[0] * P1[0] * H[0] + P1[1] * H[0] + H[0] * P1[2] + P1[3] + 0.5);
  240. K[1] = (P1[1] * H[0] + P1[3]) / (H[0] * P1[0] * H[0] + P1[1] * H[0] + H[0] * P1[2] + P1[3] + 0.5);
  241. //后验
  242. deltU = (real_T)sfmd_V_cellUMax * 0.001 - UL;
  243. soc_Max_Delay = soc1 + K[0] * deltU;
  244. if (soc_Max_Delay < (real_T)socc_pct_battSocLow * 0.1)
  245. {
  246. soc_Max_Delay = (real_T)socc_pct_battSocLow * 0.1;
  247. }
  248. if (soc_Max_Delay > (real_T)socc_pct_battSocUp * 0.1)
  249. {
  250. soc_Max_Delay = (real_T)socc_pct_battSocUp * 0.1;
  251. }
  252. Up_Max_Delay = Up1 + K[1] * deltU;
  253. //P更新
  254. P_Max_Delay[0] = (1 - K[0] * H[0]) * P1[0] - K[0] * P1[1];
  255. P_Max_Delay[1] = -K[1] * H[0] * P1[0] + P1[1] * (1 - K[1]);
  256. P_Max_Delay[2] = (1 - K[0] * H[0]) * P1[2] - K[0] * P1[3];
  257. P_Max_Delay[3] = -K[1] * H[0] * P1[2] + P1[3] * (1 - K[1]);
  258. //输出
  259. EKFSOCMax = (uint16_T)(soc_Max_Delay * 10);
  260. socn_flg_ekfInvalidMax = (deltU > 0.01) || (deltU < -0.01);
  261. //printf("4----socmax:%f,U:%d,R:%f\n\n",soc_Max_Delay,sfmd_V_cellUMax,Ro);
  262. //-----------------------EKFSOC----------------------------------------
  263. socn_flg_ekfInvalid = socn_flg_ekfInvalidMax || socn_flg_ekfInvalidMin;
  264. if (EKFSOCMax > 800)
  265. {
  266. factor = 100;
  267. }
  268. else if (EKFSOCMin < 200)
  269. {
  270. factor = 0;
  271. }
  272. else
  273. {
  274. factor = (uint16_T)(((uint16_T)(((uint32_T)(EKFSOCMin - 200) << 6) / (800 - (EKFSOCMax - EKFSOCMin) - 200)) * 25U) >> 4);
  275. }
  276. socd_pct_ekfSoc = (uint16_T)(((1 - (real_T)(factor * 0.01)) * (real_T)(EKFSOCMin * 0.1) + (real_T)(factor * 0.01) * (real_T)(EKFSOCMax * 0.1)) * 10);
  277. // printf("4----socd_pct_ekfSoc:%d,EKFSOCMax:%d,EKFSOCMin:%d,\n",socd_pct_ekfSoc,EKFSOCMax,EKFSOCMin);
  278. //-------------------------EKFavrg---------------------------------------
  279. if (FirstRun_SOC)
  280. {
  281. soc_Avrg_Delay = (real_T)socn_pct_battSocEE * 0.1;
  282. Up_Avrg_Delay = 0;
  283. P_avrg_Delay[0] = 10;
  284. P_avrg_Delay[1] = 0;
  285. P_avrg_Delay[2] = 0;
  286. P_avrg_Delay[3] = 10;
  287. }
  288. //参数查表
  289. ocv = (real_T)look1_iu16lu16n16tu16_binlcase((uint16_T)(soc_Avrg_Delay * 10), (&(cmnm_pct_soc[0])), (&(cmnm_V_ocv[0])), 12U) * 0.001;
  290. Ro = (real_T)look1_iu16lu16n16tu16_binlcase((uint16_T)(soc_Avrg_Delay * 10), (&(cmnm_pct_soc[0])), (&(cmnm_R_ohm[0])), 12U) * 0.001 * 0.001;
  291. Rp = (real_T)look1_iu16lu16n16tu16_binlcase((uint16_T)(soc_Avrg_Delay * 10), (&(cmnm_pct_soc[0])), (&(cmnm_R_polar[0])), 12U) * 0.001 * 0.001;
  292. C = (real_T)look1_iu16lu16n16tu16_binlcase((uint16_T)(soc_Avrg_Delay * 10), (&(cmnm_pct_soc[0])), (&(cmnm_F_polar[0])), 12U) * 0.001 * 1000;
  293. A[0] = 1;
  294. A[1] = 0;
  295. A[2] = 0;
  296. A[3] = exp(-1 / (Rp * C));
  297. B[0] = 1 / Q / 3600 * 100;
  298. B[1] = Rp * (1 - exp(-1 / (Rp * C)));
  299. H[0] = docvmath(soc_Avrg_Delay);
  300. H[1] = 1;
  301. //先验
  302. soc1 = soc_Avrg_Delay * A[0] + B[0] * battcurr;
  303. Up1 = Up_Avrg_Delay * A[3] + B[1] * battcurr;
  304. UL = ocv + battcurr * Ro + Up1;
  305. P1[0] = P_avrg_Delay[0] + 0.001;
  306. P1[1] = P_avrg_Delay[1] * A[3] + 0.001;
  307. P1[2] = P_avrg_Delay[2] * A[3] + 0.001;
  308. P1[3] = P_avrg_Delay[3] * A[3] * A[3] + 0.001;
  309. //增益
  310. K[0] = (P1[0] * H[0] + P1[2]) / (H[0] * P1[0] * H[0] + P1[1] * H[0] + H[0] * P1[2] + P1[3] + 0.5);
  311. K[1] = (P1[1] * H[0] + P1[3]) / (H[0] * P1[0] * H[0] + P1[1] * H[0] + H[0] * P1[2] + P1[3] + 0.5);
  312. //后验
  313. deltU = (real_T)sfmd_V_cellUAvrg * 0.001 - UL;
  314. soc_Avrg_Delay = soc1 + K[0] * deltU;
  315. if (soc_Avrg_Delay < (real_T)socc_pct_battSocLow * 0.1)
  316. {
  317. soc_Avrg_Delay = (real_T)socc_pct_battSocLow * 0.1;
  318. }
  319. if (soc_Avrg_Delay > (real_T)socc_pct_battSocUp * 0.1)
  320. {
  321. soc_Avrg_Delay = (real_T)socc_pct_battSocUp * 0.1;
  322. }
  323. Up_Avrg_Delay = Up1 + K[1] * deltU;
  324. //P更新
  325. P_avrg_Delay[0] = (1 - K[0] * H[0]) * P1[0] - K[0] * P1[1];
  326. P_avrg_Delay[1] = -K[1] * H[0] * P1[0] + P1[1] * (1 - K[1]);
  327. P_avrg_Delay[2] = (1 - K[0] * H[0]) * P1[2] - K[0] * P1[3];
  328. P_avrg_Delay[3] = -K[1] * H[0] * P1[2] + P1[3] * (1 - K[1]);
  329. //输出
  330. EKFSOCAvrg = (uint16_T)(soc_Avrg_Delay * 10);
  331. socn_flg_ekfInvalidAvrg = (deltU > 0.01) || (deltU < -0.01);
  332. //printf("4----soc:%f,Up:%f,U:%d,deltU:%f,K[0]:%f,K[1]:%f\n\n",soc_Avrg_Delay,Up_Avrg_Delay,sfmd_V_cellUAvrg,deltU,K[0],K[1]);
  333. //
  334. for(i = 0;i < cmnc_num_cellUNum;i++)
  335. {
  336. socv_pct_cellSoc[i] = look1_iu16lu16n16tu16_binlcase((look1_iu16lu16n16tu16_binlcase(EKFSOCAvrg, (&(cmnm_pct_soc[0])), (&(cmnm_V_ocv[0])), 12U) + cdmv_V_deltOCV[i]), (&(cmnm_V_ocv[0])),(&(cmnm_pct_soc[0])),12U);
  337. }
  338. socd_flg_cellSocDisable = socn_flg_ekfInvalidAvrg || cdmd_flg_deltOCVDisable;
  339. cellSocMax = ArrMax(&socv_pct_cellSoc[0],cmnc_num_cellUNum);
  340. cellSocMin = ArrMin(&socv_pct_cellSoc[0],cmnc_num_cellUNum);
  341. if (cellSocMax > 800)
  342. {
  343. factor = 100;
  344. }
  345. else if (cellSocMin < 200)
  346. {
  347. factor = 0;
  348. }
  349. else
  350. {
  351. factor = (uint16_T)(((uint16_T)(((uint32_T)(cellSocMin - 200) << 6) / (800 - (cellSocMax - cellSocMin) - 200)) * 25U) >> 4);
  352. }
  353. socd_pct_cellBattSoc = (uint16_T)(((1 - (real_T)(factor * 0.01)) * (real_T)(cellSocMin * 0.1) + (real_T)(factor * 0.01) * (real_T)(cellSocMax * 0.1)) * 10);
  354. //======================================================================
  355. ////////////////////////AhSOC//////////////////////////////////////////
  356. //======================================================================
  357. if (FirstRun_SOC)
  358. {
  359. ahDelay = (real_T)(socn_pct_battSocEE * 0.1);
  360. }
  361. else
  362. {
  363. ahDelay = ahDelay + battcurr / (real_T)(cmnc_Q_ratedCp * 0.1) / 36.0;//cmnc_Q_ratedCp
  364. }
  365. ahSoc = (int16_T)(ahDelay * 10);
  366. if (ahSoc > socc_pct_battSocUp)
  367. {
  368. socd_pct_ahSoc = socc_pct_battSocUp;
  369. }
  370. else if (ahSoc < socc_pct_battSocLow)
  371. {
  372. socd_pct_ahSoc = socc_pct_battSocLow;
  373. }
  374. else
  375. {
  376. socd_pct_ahSoc = (uint16_T)ahSoc;
  377. }
  378. //printf("5----ahDelay:%f,ahSoc:%d,battcurr:%f,sfmd_I_curr:%d\n",ahDelay,ahSoc,battcurr,sfmd_I_curr);
  379. //======================================================================
  380. ///////////////////////estSOC//////////////////////////////////////////
  381. //======================================================================
  382. if (!socn_flg_ekfInvalid)
  383. {
  384. ekfInvalidCntl = (ekfInvalidCntl + 1) > 250 ? 250 : (ekfInvalidCntl + 1);
  385. }
  386. else
  387. {
  388. ekfInvalidCntl = 0;
  389. }
  390. if (ekfInvalidCntl < 20)
  391. {
  392. if (onceFlg_est)
  393. {
  394. ahSoc0_est = ahSoc;
  395. ekfSoc0_est = socd_pct_ekfSoc;
  396. onceFlg_est = false;
  397. }
  398. socd_pct_estSoc = (int16_T)(ahSoc - ahSoc0_est + ekfSoc0_est) > 0 ? (uint16_T)(ahSoc - ahSoc0_est + ekfSoc0_est) : 0;
  399. }
  400. else
  401. {
  402. onceFlg_est = true;
  403. socd_pct_estSoc = socd_pct_ekfSoc;
  404. }
  405. //
  406. if (socd_pct_estSoc > socc_pct_battSocUp)
  407. {
  408. socd_pct_estSoc = socc_pct_battSocUp;
  409. }
  410. if (socd_pct_estSoc < socc_pct_battSocLow)
  411. {
  412. socd_pct_estSoc = socc_pct_battSocLow;
  413. }
  414. //printf("6----ahSoc0_est:%d,ekfSoc0_est:%d,socd_pct_estSoc:%d\n",ahSoc0_est,ekfSoc0_est,socd_pct_estSoc);
  415. //======================================================================
  416. ////////////////////////UtrackSOC//////////////////////////////////////////
  417. //======================================================================
  418. if (ihd_st_workStat == 2)
  419. {
  420. disChrgCntl = 0;
  421. chrgCntl = (chrgCntl + 1) > 250 ? 250 : (chrgCntl + 1);
  422. lowCntl = 0;
  423. lowFlg = false;
  424. socd_V_chrgCCV = look1_is16lu16n16tu16_binlcase(sfmd_I_curr,&socm_I_chrgCor[0],&socm_V_chrgCor[0],2U);
  425. if (sfmd_V_cellUMax >= socd_V_chrgCCV)
  426. {
  427. overCntl = (overCntl + 1) > 250 ? 250 : (overCntl + 1);
  428. }
  429. else
  430. {
  431. overCntl = 0;
  432. }
  433. if (overCntl > 2 || overFlg)
  434. {
  435. overFlg = 1;
  436. }
  437. //
  438. if (sfmd_V_cellUMax >= cmnc_V_chrgFul)
  439. {
  440. fulCntl = (fulCntl + 1) > 250 ? 250 : (fulCntl + 1);
  441. }
  442. else
  443. {
  444. fulCntl = 0;
  445. }
  446. if ((fulCntl > 2) || fulFlg)
  447. {
  448. fulFlg = 1;
  449. }
  450. //
  451. if (overFlg)
  452. {
  453. if (onceFlg_utrckOver)
  454. {
  455. onceFlg_utrckOver = false;
  456. ahSoc0_utrckOver = ahSoc;
  457. estSoc0_utrckOver = socd_pct_estSoc > socc_pct_chrgCor ? socd_pct_estSoc : socc_pct_chrgCor;
  458. }
  459. socTemp = (uint16_T)(ahSoc - ahSoc0_utrckOver + estSoc0_utrckOver);
  460. }
  461. else if (chrgCntl > 2)
  462. {
  463. onceFlg_utrckOver = true;
  464. socTemp = socd_pct_estSoc > socc_pct_chrgCor ? socc_pct_chrgCor : socd_pct_estSoc;
  465. }
  466. else
  467. {
  468. socTemp = socd_pct_estSoc;
  469. }
  470. //
  471. socn_pct_utrackSoc = Soc_Delay + (socTemp > Soc_Delay ? (socTemp - Soc_Delay) : 0);
  472. Soc_Delay = socn_pct_utrackSoc;
  473. if (fulFlg)
  474. {
  475. socn_pct_utrackSoc = socc_pct_battSocUp;
  476. }
  477. else
  478. {
  479. socn_pct_utrackSoc = socn_pct_utrackSoc > (socc_pct_battSocUp - 1) ? (socc_pct_battSocUp - 1) : socn_pct_utrackSoc;
  480. }
  481. // printf("7----overCntl:%d,overFlg:%d,fulCntl:%d,fulFlg:%d,ahSoc0_utrckOver:%d,estSoc0_utrckOver:%d,socn_pct_utrackSoc:%d,socTemp:%d\n",overCntl,overFlg,fulCntl,fulFlg,ahSoc0_utrckOver,estSoc0_utrckOver,socn_pct_utrackSoc,socTemp);
  482. }
  483. else
  484. {
  485. chrgCntl = 0;
  486. disChrgCntl = (disChrgCntl + 1) > 250 ? 250 : (disChrgCntl + 1);
  487. Soc_Delay = 0;
  488. overCntl = 0;
  489. overFlg = false;
  490. fulFlg = false;
  491. fulCntl = 0;
  492. if (!cdmd_flg_deltOCVDisable)
  493. {
  494. socd_V_disChrgCCV = look1_is16lu16n16tu16_binlcase(sfmd_I_curr,&socm_I_disChrgCor[0],&socm_V_disChrgCor[0],2U) + (uint16_T)(sfmd_I_curr * (cdmv_ohm_deltR[sfmd_idx_cellUMin]) * 0.001 * 0.1);
  495. }
  496. else
  497. {
  498. socd_V_disChrgCCV = look1_is16lu16n16tu16_binlcase(sfmd_I_curr,&socm_I_disChrgCor[0],&socm_V_disChrgCor[0],2U);
  499. }
  500. if (sfmd_V_cellUMin <= socd_V_disChrgCCV)
  501. {
  502. lowCntl = (lowCntl + 1) > 250 ? 250 : (lowCntl + 1);
  503. }
  504. else
  505. {
  506. lowCntl = 0;
  507. }
  508. if (lowCntl > 2 || lowFlg)
  509. {
  510. lowFlg = true;
  511. }
  512. //
  513. if (lowFlg)
  514. {
  515. if (onceFlg_utrckLow)
  516. {
  517. onceFlg_utrckLow = false;
  518. ahSoc0_utrckLow = ahSoc;
  519. estSoc0_utrckLow = socc_pct_disChrgCor;
  520. }
  521. socn_pct_utrackSoc = (int16_T)(ahSoc - ahSoc0_utrckLow + estSoc0_utrckLow) > 0 ? (uint16_T)(ahSoc - ahSoc0_utrckLow + estSoc0_utrckLow) : 0;
  522. }
  523. else if (disChrgCntl > 2)
  524. {
  525. onceFlg_utrckLow = true;
  526. socn_pct_utrackSoc = socd_pct_estSoc < socc_pct_disChrgCor ? socc_pct_disChrgCor : socd_pct_estSoc;
  527. }
  528. else
  529. {
  530. socn_pct_utrackSoc = socd_pct_estSoc;
  531. }
  532. // printf("8----lowCntl:%d,lowFlg:%d,ahSoc0_utrckLow:%d,estSoc0_utrckLow:%d,socn_pct_utrackSoc:%d\n",lowCntl,lowFlg,ahSoc0_utrckLow,estSoc0_utrckLow,socn_pct_utrackSoc);
  533. }
  534. socd_pct_battSoc = socn_pct_utrackSoc;
  535. socd_pct_battSocEo = socn_pct_utrackSoc;
  536. //===============================================================================================================================================================
  537. ////////////////////////////////////////////////BCUSOC///////////////////////////////////////////////////////////////////////////////////////////////////////////
  538. //===============================================================================================================================================================
  539. if (sfmd_I_curr < 10 && sfmd_I_curr > -10)
  540. {
  541. statCntl = (statCntl + 1) > 250 ? 250 : (statCntl + 1);
  542. }
  543. else
  544. {
  545. statCntl = 0;
  546. }
  547. statFlg = statCntl > 2;
  548. Flg = (FirstRun_SOC || (ihd_st_chrgSta_Delay == 2 && ihd_st_workStat != 2) || (ihd_st_chrgSta_Delay != 2 && ihd_st_workStat == 2) || ((int16_T)(socd_pct_battSoc - socd_pct_battSoc_Delay) > 5 || (int16_T)(socd_pct_battSoc - socd_pct_battSoc_Delay) < -5) || statFlg);
  549. ihd_st_chrgSta_Delay = ihd_st_workStat;
  550. socd_pct_battSoc_Delay = socd_pct_battSoc;
  551. //
  552. if (Flg)
  553. {
  554. socd_pct_battSoc0 = socd_pct_battSoc;
  555. if (FirstRun_SOC)
  556. {
  557. socd_pct_bcuSoc0 = socn_pct_bcuSocEE;
  558. }
  559. else
  560. {
  561. socd_pct_bcuSoc0 = socd_pct_bcuSoc;
  562. }
  563. }
  564. //printf("9----statCntl:%d,statFlg:%d,Flg:%d,socd_pct_bcuSoc0:%d,socd_pct_bcuSoc0:%d,Flg:%d\n",statCntl,statFlg,Flg,socd_pct_bcuSoc0,socd_pct_bcuSoc0,Flg);
  565. //
  566. if (ihd_st_workStat == 2)
  567. {
  568. delSOC = socd_pct_battSoc0 > socd_pct_bcuSoc0 ? (socd_pct_battSoc0 - socd_pct_bcuSoc0) : (socd_pct_bcuSoc0 - socd_pct_battSoc0);
  569. coinSoc = (socd_pct_battSoc0 > socd_pct_bcuSoc0 ? socd_pct_battSoc0 : socd_pct_bcuSoc0) + (delSOC > 50 ? 50 : delSOC);
  570. x[0] = socd_pct_battSoc0;
  571. x[1] = coinSoc > socc_pct_battSocUp ? socc_pct_battSocUp : (uint16_T)coinSoc;
  572. x[2] = socc_pct_battSocUp;
  573. y[0] = socd_pct_bcuSoc0;
  574. y[1] = coinSoc > socc_pct_battSocUp ? socc_pct_battSocUp : (uint16_T)coinSoc;
  575. y[2] = socc_pct_battSocUp;
  576. bcuSoc = SOC_LookUp(socd_pct_battSoc, &x[0], &y[0]);
  577. //
  578. if (onceFlg_chrg)
  579. {
  580. onceFlg_chrg = false;
  581. onceFlg_dischrg = true;
  582. socd_pct_bcuSoc = 2000;
  583. }
  584. socd_pct_bcuSoc = SOCfitSystem(bcuSoc, &socd_pct_bcuSoc, 1);
  585. //
  586. if (fulFlg)
  587. {
  588. socd_pct_bcuSoc = socc_pct_battSocUp;
  589. }
  590. else
  591. {
  592. socd_pct_bcuSoc = socd_pct_bcuSoc > (socc_pct_battSocUp - 1) ? (socc_pct_battSocUp - 1) : socd_pct_bcuSoc;
  593. }
  594. //printf("10-----x:[%d-%d-%d],y:[%d-%d-%d],bcusoc:%d,socd_pct_bcuSoc:%d\n",x[0],x[1],x[2],y[0],y[1],y[2],bcuSoc,socd_pct_bcuSoc);
  595. }
  596. else
  597. {
  598. //
  599. delSOC = socd_pct_battSoc0 > socd_pct_bcuSoc0 ? (socd_pct_battSoc0 - socd_pct_bcuSoc0) : (socd_pct_bcuSoc0 - socd_pct_battSoc0);
  600. coinSoc = (int16_T)((socd_pct_battSoc0 < socd_pct_bcuSoc0 ? socd_pct_battSoc0 : socd_pct_bcuSoc0) - (delSOC > 50 ? 50 : delSOC));
  601. x[0] = socc_pct_battSocLow;
  602. x[1] = coinSoc > socc_pct_battSocLow ? (uint16_T)coinSoc : socc_pct_battSocLow;
  603. x[2] = socd_pct_battSoc0;
  604. y[0] = socc_pct_battSocLow;
  605. y[1] = coinSoc > socc_pct_battSocLow ? (uint16_T)coinSoc : socc_pct_battSocLow;
  606. y[2] = socd_pct_bcuSoc0;
  607. bcuSoc = SOC_LookUp(socd_pct_battSoc, &x[0], &y[0]);
  608. //
  609. if (onceFlg_dischrg)
  610. {
  611. onceFlg_chrg = true;
  612. onceFlg_dischrg = false;
  613. socd_pct_bcuSoc = 2000;
  614. }
  615. socd_pct_bcuSoc = SOCfitSystem(bcuSoc, &socd_pct_bcuSoc, 1);
  616. //printf("11-----x:[%d-%d-%d],y:[%d-%d-%d],bcusoc:%d,socd_pct_bcuSoc:%d\n",x[0],x[1],x[2],y[0],y[1],y[2],bcuSoc,socd_pct_bcuSoc);
  617. }
  618. socd_pct_bcuSocEo = socd_pct_bcuSoc;
  619. //=======================================VCUSOC========================================================================================
  620. socd_pct_vcuSoc = (uint16_T)((uint32_T)((socd_pct_bcuSoc - socc_pct_battSocLow) * 1000) / (socc_pct_battSocUp - socc_pct_battSocLow));
  621. //printf("BattSOC:%d,BCUSOC:%d,VcuSoc:%d\n",socd_pct_battSoc,socd_pct_bcuSoc,socd_pct_vcuSoc);
  622. //================================================================================================
  623. //------------------EEsave-------------------------------------------=============================
  624. //================================================================================================
  625. if ((int16_T)(socd_pct_battSoc - socd_pct_battSoc_save) > 10 || (int16_T)(socd_pct_battSoc - socd_pct_battSoc_save) < -10 || (int16_T)(socd_pct_bcuSoc - socd_pct_bcuSoc_save) > 10 || (int16_T)(socd_pct_bcuSoc - socd_pct_bcuSoc_save) < -10 )
  626. {
  627. socd_flg_EEsave = 1;
  628. socd_pct_battSoc_save = socd_pct_battSoc;
  629. socd_pct_bcuSoc_save = socd_pct_bcuSoc;
  630. }
  631. else
  632. {
  633. socd_flg_EEsave = 0;
  634. }
  635. FirstRun_SOC = false;
  636. }
  637. //===================================================================================================================
  638. //----------------------------function-------------------------------------------------------------------------------
  639. //===================================================================================================================
  640. real_T docvmath(real_T soc)
  641. {
  642. real_T docv;
  643. docv = ((((((-1.0936E-13 * pow(soc, 7.0) +
  644. 3.9249E-11 * pow(soc, 6.0)) +
  645. -5.5776E-9 * pow(soc, 5.0)) +
  646. 3.996E-7 * pow(soc, 4.0)) +
  647. -1.5332E-5 * pow(soc, 3.0)) +
  648. soc * soc * 0.0003192) +
  649. -0.00371 * soc) +
  650. 0.02732;
  651. return docv;
  652. }
  653. ////
  654. uint16_T SOC_LookUp(uint16_T battsoc, uint16_T x[3], uint16_T y[3])
  655. {
  656. uint16_T bcusoc;
  657. if (battsoc <= x[0])
  658. {
  659. bcusoc = y[0];
  660. }
  661. //
  662. if (battsoc >= x[2])
  663. {
  664. bcusoc = y[2];
  665. }
  666. //
  667. if (battsoc > x[0] && battsoc < x[1]) //(x-x0)*(y1-y0)/(x1-x0)+y0
  668. {
  669. bcusoc = (uint16_T)((real_T)((battsoc - x[0]) * 0.1) * (real_T)((y[1] - y[0]) * 0.1) / (real_T)((x[1] - x[0]) * 0.1) * 10) + y[0];
  670. }
  671. //
  672. if (battsoc >= x[1] && battsoc < x[2]) //(x-x1)*(y2-y1)/(x2-x1)+y1
  673. {
  674. bcusoc = (uint16_T)((real_T)((battsoc - x[1]) * 0.1) * (real_T)((y[2] - y[1]) * 0.1) / (real_T)((x[2] - x[1]) * 0.1) * 10) + y[1];
  675. }
  676. return bcusoc;
  677. }
  678. //-------------------------------------------------------------------------
  679. uint16_T SOCfitSystem(uint16_T SOC, uint16_T *SOCfit, uint16_T m)
  680. {
  681. int16_T socdelt;
  682. socdelt = (int16_T)(SOC - *SOCfit);
  683. if (socdelt > m)
  684. {
  685. *SOCfit = *SOCfit + (socdelt > m ? m : socdelt);
  686. }
  687. if (socdelt < -m && socdelt > -1000)
  688. {
  689. *SOCfit = *SOCfit + (socdelt < -m ? -m : socdelt);
  690. }
  691. if ((socdelt <= m && socdelt >= -m) || socdelt <= -1000)
  692. {
  693. *SOCfit = SOC;
  694. }
  695. return *SOCfit;
  696. }