AppFunc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * @Author : ChenJie
  3. * @Date : 2021-12-15 10:40:06
  4. * @Version : V3.0
  5. * @LastEditors : ChenJie
  6. * @LastEditTime : 2022-05-17 19:27:00
  7. * @Description : AppFunc
  8. * @FilePath : \undefinedd:\1_WorkFiles\2_Software\10_ReplaceBatteryControl\ECTEK_ToolChain\VehicleControl\VehicleControl\src\System\Vehicle\AppFunc.c
  9. */
  10. #include "string.h"
  11. #include "stdlib.h"
  12. #include "HardwareLib.h"
  13. #include "CanVar.h"
  14. #include "math.h"
  15. #include "AppFunc.h"
  16. #include "Std_Types.h"
  17. void GetDIOState(void)
  18. {
  19. //配置高有效,底层悬空为0,触发为1,应用层输出悬空为0,触发为1
  20. // DINDrv_SetChanThres(_DIN_INDEX_X10, 1, 40950U);
  21. // DINDrv_SetChanThres(_DIN_INDEX_X17, 1, 40950U);
  22. // DINDrv_SetChanThres(_DIN_INDEX_X18, 1, 40950U);
  23. // DINDrv_SetChanThres(_DIN_INDEX_X13, 1, 40950U);
  24. // DINDrv_SetChanThres(_DIN_INDEX_X9, 1, 40950U);
  25. // DINDrv_SetChanThres(_DIN_INDEX_X5, 1, 40950U);
  26. // DINDrv_SetChanThres(_DIN_INDEX_X11, 1, 40950U);
  27. // DINDrv_SetChanThres(_DIN_INDEX_X14, 1, 40950U);
  28. DINDrv_SetChanThres(_DIN_INDEX_X10, 1, (4095*3));
  29. DINDrv_SetChanThres(_DIN_INDEX_X17, 1, (4095*3));
  30. DINDrv_SetChanThres(_DIN_INDEX_X18, 1, (4095*3));
  31. DINDrv_SetChanThres(_DIN_INDEX_X13, 1, (4095*3));
  32. DINDrv_SetChanThres(_DIN_INDEX_X9, 1, (4095*3));
  33. DINDrv_SetChanThres(_DIN_INDEX_X5, 1, (4095*3));
  34. DINDrv_SetChanThres(_DIN_INDEX_X11, 1, (4095*3));
  35. DINDrv_SetChanThres(_DIN_INDEX_X14, 1, (4095*3));
  36. chrgModuelPwrCtlFB =DINDrv_GetChanState(_DIN_INDEX_X10);
  37. paraChrgCtlFB =DINDrv_GetChanState(_DIN_INDEX_X17);
  38. outToBaseCtlFB_A =DINDrv_GetChanState(_DIN_INDEX_X18);
  39. outToChrgCtlFB_A =DINDrv_GetChanState(_DIN_INDEX_X13);
  40. outToChrgCtlFB_B =DINDrv_GetChanState(_DIN_INDEX_X9);
  41. outToBaseCtlFB_B =DINDrv_GetChanState(_DIN_INDEX_X14);
  42. fuseActionFB_A =DINDrv_GetChanState(_DIN_INDEX_X5);
  43. fuseActionFB_B =DINDrv_GetChanState(_DIN_INDEX_X11);
  44. A_volDetect =DINDrv_GetChanState(_DIN_INDEX_X7);
  45. B_volDetect =DINDrv_GetChanState(_DIN_INDEX_X6);
  46. connectorHvlock1 =DINDrv_GetChanState(_DIN_INDEX_X22);
  47. connectorHvlock2 =DINDrv_GetChanState(_DIN_INDEX_X23);
  48. }
  49. void GetAIOValue(void)
  50. {
  51. uint16 PluginTemp1_Vol = 0;
  52. uint32 PluginTemp1_R = 0;
  53. uint16 PluginTemp2_Vol = 0;
  54. uint32 PluginTemp2_R = 0;
  55. uint16 PluginTemp3_Vol = 0;
  56. uint32 PluginTemp3_R = 0;
  57. uint16 PluginTemp4_Vol = 0;
  58. uint32 PluginTemp4_R = 0;
  59. //温度采集获取
  60. PluginTemp1_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP1);
  61. PluginTemp2_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP2);
  62. PluginTemp3_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP3);
  63. PluginTemp4_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP4);
  64. PluginTemp1_R = (uint32)((PluginTemp1_Vol / (5.0 * 4095 - PluginTemp1_Vol)) * 1000.0);
  65. PluginTemp2_R = (uint32)((PluginTemp2_Vol / (5.0 * 4095 - PluginTemp2_Vol)) * 1000.0);
  66. PluginTemp3_R = (uint32)((PluginTemp3_Vol / (5.0 * 4095 - PluginTemp3_Vol)) * 1000.0);
  67. PluginTemp4_R = (uint32)((PluginTemp4_Vol / (5.0 * 4095 - PluginTemp4_Vol)) * 1000.0);
  68. chrgGunTemp1_A = (uint8)Look1_u32u8(PluginTemp1_R, R_table, T_table, 240);
  69. chrgGunTemp2_A = (uint8)Look1_u32u8(PluginTemp2_R, R_table, T_table, 240);
  70. chrgGunTemp1_B = (uint8)Look1_u32u8(PluginTemp3_R, R_table, T_table, 240);
  71. chrgGunTemp2_B = (uint8)Look1_u32u8(PluginTemp4_R, R_table, T_table, 240);
  72. }
  73. void ctrlFunc(void)
  74. {
  75. static uint8 A_volDetectTmp = 0;
  76. static uint32 xk24_outputTime = 0;
  77. if(A_volDetect==1)
  78. {
  79. A_volDetectTmp = A_volDetect;
  80. }
  81. if(A_volDetectTmp==1 && A_volDetect==0 && xk24_outputTime<3000)
  82. {
  83. PSwtDrv_Interface(_PSWT_INDEX_X24, 1);
  84. xk24_outputTime = xk24_outputTime + 10;
  85. }
  86. if(xk24_outputTime>=3000)
  87. {
  88. xk24_outputTime = 0;
  89. PSwtDrv_Interface(_PSWT_INDEX_X24, 0);
  90. A_volDetectTmp = 0;
  91. }
  92. if(chrgModulePwrCtrl==1)
  93. {
  94. PSwtDrv_Interface(_PSWT_INDEX_X36, 1);
  95. }
  96. else
  97. {
  98. PSwtDrv_Interface(_PSWT_INDEX_X36, 0);
  99. }
  100. if(chrgStartState==0&&chrgStopState==1)
  101. {
  102. if(baseSwitchCtrl==1)
  103. {
  104. PSwtDrv_Interface(_PSWT_INDEX_X32, 1);
  105. PSwtDrv_Interface(_PSWT_INDEX_X20, 1);
  106. PSwtDrv_Interface(_PSWT_INDEX_X16, 1);
  107. PSwtDrv_Interface(_PSWT_INDEX_X12, 1);
  108. PSwtDrv_Interface(_PSWT_INDEX_X8, 1);
  109. }
  110. else
  111. {
  112. PSwtDrv_Interface(_PSWT_INDEX_X32, 0);
  113. PSwtDrv_Interface(_PSWT_INDEX_X20, 0);
  114. PSwtDrv_Interface(_PSWT_INDEX_X16, 0);
  115. PSwtDrv_Interface(_PSWT_INDEX_X12, 0);
  116. PSwtDrv_Interface(_PSWT_INDEX_X8, 0);
  117. }
  118. }
  119. chrgBoxTemp1 = temp_gf1;
  120. chrgBoxTemp2 = temp_gf2;
  121. chrgBoxGasOver1 = gashighLevel_gf1;
  122. chrgBoxGasOver2 = gashighLevel_gf2;
  123. chrgBoxFumesOver1 = fumesHigh_gf1;
  124. chrgBoxFumesOver2 = fumesHigh_gf2;
  125. ctrlFXFB1 = getbit(workMode_gf1,5);
  126. ctrlFXFB2 = getbit(workMode_gf2,5);
  127. chrgBox1Warning = warningLevel_gf1;
  128. chrgBox2Warning = warningLevel_gf2;
  129. }
  130. /*
  131. void LockAndUnlockCtrl()
  132. {
  133. static uint16 LockDelay = 0;
  134. static uint16 UnlockDelay = 0;
  135. static uint16 BleConCounter = 0;
  136. static uint16 battSwpeModeCounter = 0;
  137. if (ebcd_flg_ebcManCtrlEn) //手动控制使能
  138. {
  139. if (ebcd_flg_ebcManCtrlMode == 1) //循环控制模式
  140. {
  141. if (ebcd_st_lockSensor == 1) //处于锁紧状态,传感器判定一个
  142. {
  143. LockDelay = LockDelay + 10;
  144. if (LockDelay > LockDelayTime) //锁紧状态超时就解锁
  145. {
  146. ManuControlTimes++;
  147. LockDelay = 0;
  148. ebcd_st_SwitchCtrlFlg = Unlock;
  149. }
  150. }
  151. else if (ebcd_st_unlockSensor == 1) //处于解锁状态,传感器判定一个
  152. {
  153. UnlockDelay = UnlockDelay + 10;
  154. if (UnlockDelay > UnlockDelayTime)
  155. {
  156. UnlockDelay = 0;
  157. ebcd_st_SwitchCtrlFlg = Lock;
  158. }
  159. }
  160. }
  161. else if (ebcd_flg_ebcManCtrlMode == 2) //控制锁紧
  162. {
  163. ebcd_st_SwitchCtrlFlg = Lock;
  164. }
  165. else if (ebcd_flg_ebcManCtrlMode == 3) //控制解锁
  166. {
  167. ebcd_st_SwitchCtrlFlg = Unlock;
  168. }
  169. }
  170. else //正常工作模式
  171. {
  172. switch (ebcd_st_ebcWork)
  173. {
  174. case DriveMode: //行车状态,自动进入,默认状态
  175. {
  176. ebcd_st_SwitchCtrlFlg = Lock;
  177. PSwtDrv_Interface(_PSWT_INDEX_EBCLEDCONTROL, 0); //行车状态换电指示灯关闭
  178. if (bled_flg_handShake == 1)
  179. {
  180. ebcd_st_ebcWork = CommuMode; //进入通讯状态
  181. bled_flg_handShake = 0;
  182. break;
  183. }
  184. break;
  185. }
  186. case CommuMode: //通讯状态,握手才能进入
  187. {
  188. BleConCounter = BleConCounter + 10;
  189. if ((BleConCounter % 1000) < 500) //换电指示灯闪烁
  190. {
  191. PSwtDrv_Interface(_PSWT_INDEX_EBCLEDCONTROL, 1);
  192. }
  193. else
  194. {
  195. PSwtDrv_Interface(_PSWT_INDEX_EBCLEDCONTROL, 0);
  196. }
  197. if (BleConCounter >= 60 * 1000 || bled_flg_backToDrv == 1)
  198. {
  199. ebcd_st_ebcWork = DriveMode; //切换到行车状态
  200. BleConCounter = 0;
  201. bled_flg_backToDrv = 0;
  202. break;
  203. }
  204. else if (BattCD_stWakeupU.B.bLogic == 0 && BattCD_stWakeupU.B.bWkpSig1 == 1)
  205. {
  206. ebcd_st_ebcWork = SwapMode; //切换到换电状态
  207. BleConCounter = 0;
  208. break;
  209. }
  210. if (bled_st_dataTrans == 1)
  211. {
  212. BleConCounter = 0;
  213. bled_st_dataTrans = 0;
  214. }
  215. break;
  216. }
  217. case SwapMode: //换电状态
  218. {
  219. PSwtDrv_Interface(_PSWT_INDEX_EBCLEDCONTROL, 1); //换电状态换电指示灯常亮
  220. battSwpeModeCounter = battSwpeModeCounter + 10;
  221. if (bled_flg_swapBattDone == 1)
  222. {
  223. ebcd_Nr_swapBatt++;
  224. ebcd_Nr_swapSucBatt++;
  225. bled_flg_swapBattDone = 0;
  226. ebcd_st_ebcWork = CommuMode; //切换到通讯状态
  227. break;
  228. }
  229. else if (battSwpeModeCounter >= 60 * 1000)
  230. {
  231. ebcd_Nr_swapBatt++;
  232. battSwpeModeCounter = DriveMode; //切换到行车状态
  233. ebcd_st_ebcWork = 0;
  234. break;
  235. }
  236. if (bled_st_dataTrans == 1)
  237. {
  238. battSwpeModeCounter = 0;
  239. bled_st_dataTrans = 0;
  240. }
  241. if (bled_flg_unlockCmdForce)
  242. {
  243. ebcd_st_SwitchCtrlFlg = Unlock;
  244. bled_flg_unlockCmdForce = 0;
  245. }
  246. if (bled_flg_lockCmdForce)
  247. {
  248. ebcd_st_SwitchCtrlFlg = Lock;
  249. bled_flg_lockCmdForce = 0;
  250. }
  251. break;
  252. }
  253. }
  254. }
  255. //锁紧解锁执行
  256. if (ebcd_st_SwitchCtrlFlg == Unlock)
  257. {
  258. UnlockCtrl();
  259. }
  260. else
  261. {
  262. LockCtrl();
  263. }
  264. }
  265. */
  266. /**
  267. * @brief : lookUp Table Fun
  268. * @param {uint32} u0 x
  269. * @param {uint32} bp0 x_table
  270. * @param {uint16} table y_table
  271. * @param {uint16} maxIndex
  272. * @return {*}
  273. */
  274. uint16 Look1_u32u8(uint32 u0, uint32 *bp0, uint8 *table, uint16 MaxLen)
  275. {
  276. uint32 bpIdx = 0;
  277. uint32 iLeft = 0;
  278. uint32 iRght = 0;
  279. uint16 y = 0;
  280. uint32 yL_0d0 = 0;
  281. uint32 yR_0d0 = 0;
  282. uint32 maxIndex = MaxLen - 1;
  283. if (u0 <= bp0[0U])
  284. {
  285. iLeft = 0U;
  286. iRght = 0U;
  287. }
  288. else if (u0 < bp0[maxIndex])
  289. {
  290. //对折法寻找u0的位置
  291. bpIdx = maxIndex >> 1U;
  292. iLeft = 0U;
  293. iRght = maxIndex;
  294. while ((iRght - iLeft) > 1)
  295. {
  296. if (u0 < bp0[bpIdx])
  297. {
  298. iRght = bpIdx;
  299. }
  300. else
  301. {
  302. iLeft = bpIdx;
  303. }
  304. bpIdx = (iRght + iLeft) >> 1U;
  305. }
  306. }
  307. else
  308. {
  309. iLeft = maxIndex;
  310. iRght = maxIndex;
  311. }
  312. //找到位置以后计算插值
  313. if (iLeft != iRght)
  314. {
  315. //线性插值
  316. yR_0d0 = table[iLeft + 1U];
  317. yL_0d0 = table[iLeft];
  318. if (yR_0d0 >= yL_0d0)
  319. {
  320. y = (uint16)(((uint32)(u0 - bp0[iLeft]) * (yR_0d0 - yL_0d0)) / (bp0[iLeft + 1] - bp0[iLeft]) + yL_0d0);
  321. }
  322. else
  323. {
  324. y = (uint16)(yL_0d0 - ((uint32)(u0 - bp0[iLeft]) * (yL_0d0 - yR_0d0)) / (bp0[iLeft + 1] - bp0[iLeft]));
  325. }
  326. }
  327. else
  328. {
  329. y = (uint16)table[iLeft];
  330. }
  331. return y;
  332. }
  333. /**
  334. * @brief : 故障判断函数
  335. * @param {BOOL} Enable 使能状态
  336. * @param {BOOL} Input 判断输入
  337. * @param {UINT16} *N 判断次数累计
  338. * @param {UINT16} Thr 判断次数阈值
  339. * @return {*}
  340. */
  341. BOOL JudgeTimeSystem(BOOL Enable, BOOL Input, UINT16 *N, UINT16 Thr)
  342. {
  343. BOOL Flg = FALSE;
  344. if (Input && Enable)
  345. {
  346. *N = (*N + 1) > 2000 ? 2000 : (*N + 1);
  347. }
  348. else
  349. {
  350. *N = 0;
  351. }
  352. if (*N > Thr && Enable)
  353. {
  354. Flg = TRUE;
  355. }
  356. else
  357. {
  358. Flg = FALSE;
  359. }
  360. return Flg;
  361. }
  362. BOOL DiagThrSystem1(BOOL Enable, BOOL precondition, UINT16 Input, UINT16 fltThr, UINT16 recThr, UINT16 fltNumThr, UINT16 recNumThr, UINT16 *fltNum, UINT16 *recNum, UINT8 *fitFlg)
  363. {
  364. if (Enable && precondition && Input > fltThr)
  365. {
  366. *fltNum = (*fltNum + 1) > 20000 ? 20000 : (*fltNum + 1);
  367. }
  368. else
  369. {
  370. *fltNum = 0;
  371. }
  372. if (Enable && precondition && Input < recThr)
  373. {
  374. *recNum = (*recNum + 1) > 20000 ? 20000 : (*recNum + 1);
  375. }
  376. else
  377. {
  378. *recNum = 0;
  379. }
  380. if ((*fltNum > fltNumThr || (*fitFlg && *recNum < recNumThr)) && precondition)
  381. {
  382. *fitFlg = TRUE;
  383. }
  384. else
  385. {
  386. *fitFlg = FALSE;
  387. }
  388. return *fitFlg;
  389. }
  390. /**
  391. * @brief : 获取故障码函数,从故障数组中获取故障码,并将之前的故障码向前移动
  392. * @param {UINT16} *ErrorArray
  393. * @param {UINT8} Errorlen
  394. * @return {*}
  395. */
  396. UINT16 GetErrorNum(UINT16 *ErrorArray, UINT8 Errorlen)
  397. {
  398. UINT16 OutNum;
  399. OutNum = *(ErrorArray);
  400. for (UINT8 i = 0; i < Errorlen - 1; i++)
  401. {
  402. *(ErrorArray + i) = *(ErrorArray + i + 1);
  403. if (*(ErrorArray + i + 1) == 0)
  404. break;
  405. }
  406. return OutNum;
  407. }
  408. /**
  409. * @brief : 故障码注入函数,将故障码写入到故障数组中,如果存在则不写入,如果存在则不写入
  410. * @param {UINT16} *ErrorArray
  411. * @param {UINT8} Errorlen
  412. * @param {UINT16} ErrorNum
  413. * @return {*}
  414. */
  415. UINT8 PutErrorNum(UINT16 *ErrorArray, UINT8 Errorlen, UINT16 ErrorNum)
  416. {
  417. for (UINT8 i = 0; i < Errorlen; i++)
  418. {
  419. if (*(ErrorArray + i) == 0)
  420. {
  421. *(ErrorArray + i) = ErrorNum;
  422. return 0;
  423. }
  424. else
  425. {
  426. if (*(ErrorArray + i) == ErrorNum)
  427. {
  428. return 1;
  429. }
  430. else
  431. {
  432. continue;
  433. }
  434. }
  435. }
  436. return 2;
  437. }