SOC.c 25 KB

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