AppFunc.c 8.9 KB

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