AppFunc.c 9.8 KB

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