SOC.c 21 KB

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