AppFunc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * @Author : ChenJie
  3. * @Date : 2021-12-15 10:40:06
  4. * @Version : V3.0
  5. * @LastEditors : ChenJie
  6. * @LastEditTime : 2022-01-11 18:14:41
  7. * @Description : AppFunc
  8. * @FilePath : \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. #define Lock 0
  18. #define Unlock 1
  19. #define LockCtrl() PSwtDrv_Interface(_PSWT_INDEX_HBAK1_CHAN, Lock)
  20. #define UnlockCtrl() PSwtDrv_Interface(_PSWT_INDEX_HBAK1_CHAN, Unlock)
  21. void LockAndUnlockCtrl()
  22. {
  23. static uint16 LockDelay = 0;
  24. static uint16 UnlockDelay = 0;
  25. static uint8 CtrlFlg = 0;
  26. static uint16 BleConCounter = 0;
  27. static uint16 battSwpeModeCounter = 0;
  28. if (ebcd_flg_ebcManCtrlEn) //手动控制使能
  29. {
  30. if (ebcd_flg_ebcManCtrlMode==1)//循环控制模式
  31. {
  32. if(ebcd_st_lockSensor==1)//处于锁紧状态,传感器判定一个
  33. {
  34. LockDelay = LockDelay + 10;
  35. if(LockDelay>LockDelayTime)//锁紧状态超时就解锁
  36. {
  37. ManuControlTimes++;
  38. LockDelay = 0;
  39. CtrlFlg = Unlock;
  40. }
  41. }
  42. else if(ebcd_st_unlockSensor==1)//处于解锁状态,传感器判定一个
  43. {
  44. UnlockDelay = UnlockDelay + 10;
  45. if(UnlockDelay>UnlockDelayTime)
  46. {
  47. UnlockDelay = 0;
  48. CtrlFlg = Lock;
  49. }
  50. }
  51. }
  52. else if(ebcd_flg_ebcManCtrlMode==2)//控制锁紧
  53. {
  54. CtrlFlg = Lock;
  55. }
  56. else if(ebcd_flg_ebcManCtrlMode==3)//控制解锁
  57. {
  58. CtrlFlg = Unlock;
  59. }
  60. }
  61. else //正常工作模式
  62. {
  63. switch(ebcd_st_ebcWork)
  64. {
  65. case DriveMode://行车状态,自动进入,默认状态
  66. {
  67. PSwtDrv_Interface(_PSWT_INDEX_EBCLEDCONTROL,0);//行车状态换电指示灯关闭
  68. if(bled_flg_handShake==1)
  69. {
  70. ebcd_st_ebcWork = CommuMode;//进入通讯状态
  71. bled_flg_handShake = 0;
  72. break;
  73. }
  74. break;
  75. }
  76. case CommuMode://通讯状态,握手才能进入
  77. {
  78. BleConCounter = BleConCounter + 10;
  79. if((BleConCounter%1000)<500)//换电指示灯闪烁
  80. {
  81. PSwtDrv_Interface(_PSWT_INDEX_EBCLEDCONTROL,1);
  82. }
  83. else
  84. {
  85. PSwtDrv_Interface(_PSWT_INDEX_EBCLEDCONTROL,0);
  86. }
  87. if(BleConCounter>=60*1000||bled_flg_backToDrv == 1)
  88. {
  89. ebcd_st_ebcWork = DriveMode;//切换到行车状态
  90. BleConCounter = 0;
  91. bled_flg_backToDrv = 0;
  92. break;
  93. }
  94. else if(BattCD_stWakeupU.B.bLogic==0 && BattCD_stWakeupU.B.bWkpSig1==1)
  95. {
  96. ebcd_st_ebcWork = SwapMode;//切换到换电状态
  97. BleConCounter = 0;
  98. break;
  99. }
  100. if(bled_st_dataTrans==1)
  101. {
  102. BleConCounter = 0;
  103. bled_st_dataTrans = 0;
  104. }
  105. break;
  106. }
  107. case SwapMode://换电状态
  108. {
  109. PSwtDrv_Interface(_PSWT_INDEX_EBCLEDCONTROL,1);//换电状态换电指示灯常亮
  110. battSwpeModeCounter = battSwpeModeCounter + 10;
  111. if(bled_flg_swapBattDone==1)
  112. {
  113. bled_flg_swapBattDone = 0;
  114. ebcd_st_ebcWork = CommuMode; //切换到通讯状态
  115. break;
  116. }
  117. else if(battSwpeModeCounter>=60*1000)
  118. {
  119. battSwpeModeCounter = DriveMode;//切换到行车状态
  120. ebcd_st_ebcWork = 0;
  121. break;
  122. }
  123. if(bled_st_dataTrans==1)
  124. {
  125. battSwpeModeCounter = 0;
  126. bled_st_dataTrans = 0;
  127. }
  128. if(bled_flg_unlockCmdForce)
  129. {
  130. CtrlFlg = Unlock;
  131. bled_flg_unlockCmdForce = 0;
  132. }
  133. if(bled_flg_lockCmdForce)
  134. {
  135. CtrlFlg = Lock;
  136. bled_flg_lockCmdForce = 0;
  137. }
  138. break;
  139. }
  140. }
  141. }
  142. //锁紧解锁执行
  143. if(CtrlFlg)
  144. {
  145. UnlockCtrl();
  146. }
  147. else
  148. {
  149. LockCtrl();
  150. }
  151. }
  152. /**
  153. * @brief : 互锁检测函数
  154. * @param {*}
  155. * @return {*}
  156. */
  157. void GetHVLockState(void)
  158. {
  159. // PWM输出,互锁1检测,频率100HZ,占空比30%
  160. uint16 VehCo_fTestUI = 1000;
  161. uint16 VehCo_rTestUW = 3000;
  162. uint32 PwmFreAcq = 0;
  163. uint16 PwmDutyAcq = 0;
  164. PPWMDrv_Interface(_PPWM_INDEX_HVLOCK2, VehCo_fTestUI, VehCo_rTestUW);
  165. PulseAcqDrv_GetChanFreq(_PULSEACQ_INDEX_HVLOCK2, &PwmFreAcq, &PwmDutyAcq);
  166. if (abs(PwmFreAcq - VehCo_fTestUI) < 100 && abs(PwmDutyAcq - VehCo_rTestUW) < 500)
  167. {
  168. m_flg_HVlock1 = 1;
  169. }
  170. else
  171. {
  172. m_flg_HVlock1 = 0;
  173. }
  174. //互锁2检测,配置高有效,悬空为0则未接入,高电平为1则接入,
  175. // DINDrv_SetChanThres(_DIN_INDEX_PLUGHVLOCK, 0, 4095 * 3);
  176. m_flg_HVlock2 = DINDrv_GetChanState(_DIN_INDEX_PLUGHVLOCK);
  177. }
  178. /**
  179. * @brief : 数字量传感器信号检测函数
  180. * @param {*}
  181. * @return {*}
  182. */
  183. void GetDIOState(void)
  184. {
  185. uint8 temp[4];
  186. //松开传感器检测,配置低有效,底层悬空为1,触发为0,应用层输出悬空为0,触发为1
  187. memset(temp, 0x00, 4);
  188. ebcd_st_unlockSensor = 0;
  189. DINDrv_SetChanThres(_DIN_INDEX_UNLOCKSENSOR1, 1, 4095U);
  190. DINDrv_SetChanThres(_DIN_INDEX_UNLOCKSENSOR2, 1, 4095U);
  191. DINDrv_SetChanThres(_DIN_INDEX_UNLOCKSENSOR3, 1, 4095U);
  192. DINDrv_SetChanThres(_DIN_INDEX_UNLOCKSENSOR4, 1, 4095U);
  193. temp[0] = !DINDrv_GetChanState(_DIN_INDEX_UNLOCKSENSOR1);
  194. temp[1] = !DINDrv_GetChanState(_DIN_INDEX_UNLOCKSENSOR2);
  195. temp[2] = !DINDrv_GetChanState(_DIN_INDEX_UNLOCKSENSOR3);
  196. temp[3] = !DINDrv_GetChanState(_DIN_INDEX_UNLOCKSENSOR4);
  197. ebcd_st_unlockSensor = (getbit(temp[3], 0) << 3) | (getbit(temp[2], 0) << 2) | (getbit(temp[1], 0) << 1) | (getbit(temp[0], 0) << 0);
  198. //夹紧传感器检测,配置低有效,底层悬空为1,触发为0,应用层输出悬空为0,触发为1
  199. memset(temp, 0x00, 4);
  200. ebcd_st_lockSensor = 0;
  201. DINDrv_SetChanThres(_DIN_INDEX_LOCKSENSOR1, 1, 4095U);
  202. DINDrv_SetChanThres(_DIN_INDEX_LOCKSENSOR2, 1, 4095U);
  203. DINDrv_SetChanThres(_DIN_INDEX_LOCKSENSOR3, 1, 4095U);
  204. DINDrv_SetChanThres(_DIN_INDEX_LOCKSENSOR4, 1, 4095U);
  205. temp[0] = !DINDrv_GetChanState(_DIN_INDEX_LOCKSENSOR1);
  206. temp[1] = !DINDrv_GetChanState(_DIN_INDEX_LOCKSENSOR2);
  207. temp[2] = !DINDrv_GetChanState(_DIN_INDEX_LOCKSENSOR3);
  208. temp[3] = !DINDrv_GetChanState(_DIN_INDEX_LOCKSENSOR4);
  209. ebcd_st_lockSensor = (getbit(temp[3], 0) << 3) | (getbit(temp[2], 0) << 2) | (getbit(temp[1], 0) << 1) | (getbit(temp[0], 0) << 0);
  210. //落座传感器检测,高有效,悬空为0则未接入,高电平为1则接入
  211. memset(temp, 0x00, 4);
  212. temp[0] = !DINDrv_GetChanState(_DIN_INDEX_READYSENSOR1);
  213. temp[1] = !DINDrv_GetChanState(_DIN_INDEX_READYSENSOR2);
  214. ebcd_st_pedstSensor = (getbit(temp[1], 0) << 1) | (getbit(temp[0], 0) << 0);
  215. //根据上述量得到,运动状态值
  216. if (ebcd_st_lockSensor == 0x0F)
  217. {
  218. ebcd_st_lockSucJug = 1;
  219. }
  220. else if (ebcd_st_lockSensor == 0x00)
  221. {
  222. ebcd_st_lockSucJug = 0;
  223. }
  224. else
  225. {
  226. ebcd_st_lockSucJug = 2;
  227. }
  228. if (ebcd_st_unlockSensor == 0x0F)
  229. {
  230. ebcd_st_unlockSucJug = 1;
  231. }
  232. else if (ebcd_st_unlockSensor == 0x00)
  233. {
  234. ebcd_st_unlockSucJug = 2;
  235. }
  236. else
  237. {
  238. ebcd_st_unlockSucJug = 0;
  239. }
  240. if (ebcd_st_pedstSucJug == 0x03)
  241. {
  242. ebcd_st_pedstSucJug = 1;
  243. }
  244. else if (ebcd_st_pedstSucJug == 0x00)
  245. {
  246. ebcd_st_pedstSucJug = 0;
  247. }
  248. else
  249. {
  250. ebcd_st_pedstSucJug = 2;
  251. }
  252. }
  253. /**
  254. * @brief : 获取模拟量输入值,并进行转换
  255. * @param {*}
  256. * @return {*}
  257. */
  258. void GetAIOValue(void)
  259. {
  260. uint16 AirPressureTemp_Vol = 0;
  261. uint16 PluginTemp1_Vol = 0;
  262. uint32 PluginTemp1_R = 0;
  263. uint16 PluginTemp2_Vol = 0;
  264. uint32 PluginTemp2_R = 0;
  265. uint16 PluginTemp3_Vol = 0;
  266. uint32 PluginTemp3_R = 0;
  267. uint16 PluginTemp4_Vol = 0;
  268. uint32 PluginTemp4_R = 0;
  269. AirPressureTemp_Vol = ATDDrv_GetChanResult(_ATD_INDEX_AIRPRESSURE);
  270. AirPressureTemp_Vol = (uint16)(AirPressureTemp_Vol * 1000 / 4095.0);
  271. Test_LockPressure = AirPressureTemp_Vol;//锁紧力测试变量
  272. /*气压数据转换*/
  273. AirPressureTemp_Vol = MAX(AirPressureTemp_Vol, 500);
  274. AirPressureTemp_Vol = MIN(AirPressureTemp_Vol, 4500);
  275. ebcd_P_airSensor = (uint8)((AirPressureTemp_Vol - 500) / 40);
  276. /*温度采集获取*/
  277. PluginTemp1_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP1);
  278. PluginTemp2_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP2);
  279. PluginTemp3_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP3);
  280. PluginTemp4_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP4);
  281. PluginTemp1_R = (uint32)((PluginTemp1_Vol / (5.0 * 4095 - PluginTemp1_Vol)) * 1000.0);
  282. PluginTemp2_R = (uint32)((PluginTemp2_Vol / (5.0 * 4095 - PluginTemp2_Vol)) * 1000.0);
  283. PluginTemp3_R = (uint32)((PluginTemp3_Vol / (5.0 * 4095 - PluginTemp3_Vol)) * 1000.0);
  284. PluginTemp4_R = (uint32)((PluginTemp4_Vol / (5.0 * 4095 - PluginTemp4_Vol)) * 1000.0);
  285. ebcd_T_plugin[0] = (uint8)Look1_u32u8(PluginTemp1_R, R_table, T_table, 240);
  286. ebcd_T_plugin[1] = (uint8)Look1_u32u8(PluginTemp2_R, R_table, T_table, 240);
  287. ebcd_T_plugin[2] = (uint8)Look1_u32u8(PluginTemp3_R, R_table, T_table, 240);
  288. ebcd_T_plugin[3] = (uint8)Look1_u32u8(PluginTemp4_R, R_table, T_table, 240);
  289. }
  290. /**
  291. * @brief : lookUp Table Fun
  292. * @param {uint32} u0 x
  293. * @param {uint32} bp0 x_table
  294. * @param {uint16} table y_table
  295. * @param {uint16} maxIndex
  296. * @return {*}
  297. */
  298. uint16 Look1_u32u8(uint32 u0, uint32 *bp0, uint8 *table, uint16 MaxLen)
  299. {
  300. uint32 bpIdx = 0;
  301. uint32 iLeft = 0;
  302. uint32 iRght = 0;
  303. uint16 y = 0;
  304. uint32 yL_0d0 = 0;
  305. uint32 yR_0d0 = 0;
  306. uint32 maxIndex = MaxLen - 1;
  307. if (u0 <= bp0[0U])
  308. {
  309. iLeft = 0U;
  310. iRght = 0U;
  311. }
  312. else if (u0 < bp0[maxIndex])
  313. {
  314. //对折法寻找u0的位置
  315. bpIdx = maxIndex >> 1U;
  316. iLeft = 0U;
  317. iRght = maxIndex;
  318. while ((iRght - iLeft) > 1)
  319. {
  320. if (u0 < bp0[bpIdx])
  321. {
  322. iRght = bpIdx;
  323. }
  324. else
  325. {
  326. iLeft = bpIdx;
  327. }
  328. bpIdx = (iRght + iLeft) >> 1U;
  329. }
  330. }
  331. else
  332. {
  333. iLeft = maxIndex;
  334. iRght = maxIndex;
  335. }
  336. //找到位置以后计算插值
  337. if (iLeft != iRght)
  338. {
  339. //线性插值
  340. yR_0d0 = table[iLeft + 1U];
  341. yL_0d0 = table[iLeft];
  342. if (yR_0d0 >= yL_0d0)
  343. {
  344. y = (uint16)(((uint32)(u0 - bp0[iLeft]) * (yR_0d0 - yL_0d0)) / (bp0[iLeft + 1] - bp0[iLeft]) + yL_0d0);
  345. }
  346. else
  347. {
  348. y = (uint16)(yL_0d0 - ((uint32)(u0 - bp0[iLeft]) * (yL_0d0 - yR_0d0)) / (bp0[iLeft + 1] - bp0[iLeft]));
  349. }
  350. }
  351. else
  352. {
  353. y = (uint16)table[iLeft];
  354. }
  355. return y;
  356. }