AppFunc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. void LockAndUnlockCtrl()
  18. {
  19. static uint16 TimerCounter = 0;
  20. TimerCounter = TimerCounter + 10;
  21. if (!ebcd_flg_ebcEolDone) //下线检测模式
  22. {
  23. static uint16 Delay = 0;
  24. Delay = Delay + 10;
  25. if (Delay > 10000)
  26. {
  27. PSwtDrv_Interface(_PSWT_INDEX_HBAK1_CHAN, 1);
  28. }
  29. else
  30. {
  31. PSwtDrv_Interface(_PSWT_INDEX_HBAK1_CHAN, 0);
  32. }
  33. if (Delay > 20000)
  34. {
  35. Delay = 0;
  36. }
  37. }
  38. else //正常工作模式
  39. {
  40. }
  41. }
  42. /**
  43. * @brief : 互锁检测函数
  44. * @param {*}
  45. * @return {*}
  46. */
  47. void GetHVLockState(void)
  48. {
  49. UINT8 m_flg_HVlock1 = 0, m_flg_HVlock2 = 0;
  50. // PWM输出,互锁1检测,频率100HZ,占空比30%
  51. uint16 VehCo_fTestUI = 1000;
  52. uint16 VehCo_rTestUW = 3000;
  53. uint32 PwmFreAcq = 0;
  54. uint16 PwmDutyAcq = 0;
  55. PPWMDrv_Interface(_PPWM_INDEX_HVLOCK2, VehCo_fTestUI, VehCo_rTestUW);
  56. PulseAcqDrv_GetChanFreq(_PULSEACQ_INDEX_HVLOCK2, &PwmFreAcq, &PwmDutyAcq);
  57. if (abs(PwmFreAcq - VehCo_fTestUI) < 100 && abs(PwmDutyAcq - VehCo_rTestUW) < 500)
  58. {
  59. m_flg_HVlock1 = 1;
  60. }
  61. else
  62. {
  63. m_flg_HVlock1 = 0;
  64. }
  65. //互锁2检测,配置高有效,悬空为0则未接入,高电平为1则接入,
  66. // DINDrv_SetChanThres(_DIN_INDEX_PLUGHVLOCK, 0, 4095 * 3);
  67. m_flg_HVlock2 = DINDrv_GetChanState(_DIN_INDEX_PLUGHVLOCK);
  68. }
  69. /**
  70. * @brief : 数字量传感器信号检测函数
  71. * @param {*}
  72. * @return {*}
  73. */
  74. void GetDIOState(void)
  75. {
  76. uint8 temp[4];
  77. //松开传感器检测,配置低有效,底层悬空为1,触发为0,应用层输出悬空为0,触发为1
  78. memset(temp, 0x00, 4);
  79. ebcd_st_unlockSensor = 0;
  80. DINDrv_SetChanThres(_DIN_INDEX_UNLOCKSENSOR1, 1, 4095U);
  81. DINDrv_SetChanThres(_DIN_INDEX_UNLOCKSENSOR2, 1, 4095U);
  82. DINDrv_SetChanThres(_DIN_INDEX_UNLOCKSENSOR3, 1, 4095U);
  83. DINDrv_SetChanThres(_DIN_INDEX_UNLOCKSENSOR4, 1, 4095U);
  84. temp[0] = !DINDrv_GetChanState(_DIN_INDEX_UNLOCKSENSOR1);
  85. temp[1] = !DINDrv_GetChanState(_DIN_INDEX_UNLOCKSENSOR2);
  86. temp[2] = !DINDrv_GetChanState(_DIN_INDEX_UNLOCKSENSOR3);
  87. temp[3] = !DINDrv_GetChanState(_DIN_INDEX_UNLOCKSENSOR4);
  88. ebcd_st_unlockSensor = (getbit(temp[3], 0) << 3) | (getbit(temp[2], 0) << 2) | (getbit(temp[1], 0) << 1) | (getbit(temp[0], 0) << 0);
  89. //夹紧传感器检测,配置低有效,底层悬空为1,触发为0,应用层输出悬空为0,触发为1
  90. memset(temp, 0x00, 4);
  91. ebcd_st_lockSensor = 0;
  92. DINDrv_SetChanThres(_DIN_INDEX_LOCKSENSOR1, 1, 4095U);
  93. DINDrv_SetChanThres(_DIN_INDEX_LOCKSENSOR2, 1, 4095U);
  94. DINDrv_SetChanThres(_DIN_INDEX_LOCKSENSOR3, 1, 4095U);
  95. DINDrv_SetChanThres(_DIN_INDEX_LOCKSENSOR4, 1, 4095U);
  96. temp[0] = !DINDrv_GetChanState(_DIN_INDEX_LOCKSENSOR1);
  97. temp[1] = !DINDrv_GetChanState(_DIN_INDEX_LOCKSENSOR2);
  98. temp[2] = !DINDrv_GetChanState(_DIN_INDEX_LOCKSENSOR3);
  99. temp[3] = !DINDrv_GetChanState(_DIN_INDEX_LOCKSENSOR4);
  100. ebcd_st_lockSensor = (getbit(temp[3], 0) << 3) | (getbit(temp[2], 0) << 2) | (getbit(temp[1], 0) << 1) | (getbit(temp[0], 0) << 0);
  101. //落座传感器检测,高有效,悬空为0则未接入,高电平为1则接入
  102. memset(temp, 0x00, 4);
  103. temp[0] = !DINDrv_GetChanState(_DIN_INDEX_READYSENSOR1);
  104. temp[1] = !DINDrv_GetChanState(_DIN_INDEX_READYSENSOR2);
  105. ebcd_st_pedstSensor = (getbit(temp[1], 0) << 1) | (getbit(temp[0], 0) << 0);
  106. //根据上述量得到,运动状态值
  107. if (ebcd_st_lockSensor == 0x0F)
  108. {
  109. ebcd_st_lockSucJug = 1;
  110. }
  111. else if (ebcd_st_lockSensor == 0x00)
  112. {
  113. ebcd_st_lockSucJug = 0;
  114. }
  115. else
  116. {
  117. ebcd_st_lockSucJug = 2;
  118. }
  119. if (ebcd_st_unlockSensor == 0x0F)
  120. {
  121. ebcd_st_unlockSucJug = 1;
  122. }
  123. else if (ebcd_st_unlockSensor == 0x00)
  124. {
  125. ebcd_st_unlockSucJug = 2;
  126. }
  127. else
  128. {
  129. ebcd_st_unlockSucJug = 0;
  130. }
  131. if (ebcd_st_pedstSucJug == 0x03)
  132. {
  133. ebcd_st_pedstSucJug = 1;
  134. }
  135. else if (ebcd_st_pedstSucJug == 0x00)
  136. {
  137. ebcd_st_pedstSucJug = 0;
  138. }
  139. else
  140. {
  141. ebcd_st_pedstSucJug = 2;
  142. }
  143. }
  144. /**
  145. * @brief : 获取模拟量输入值,并进行转换
  146. * @param {*}
  147. * @return {*}
  148. */
  149. void GetAIOValue(void)
  150. {
  151. uint16 AirPressureTemp_Vol = 0;
  152. uint16 PluginTemp1_Vol = 0;
  153. uint32 PluginTemp1_R = 0;
  154. uint16 PluginTemp2_Vol = 0;
  155. uint32 PluginTemp2_R = 0;
  156. uint16 PluginTemp3_Vol = 0;
  157. uint32 PluginTemp3_R = 0;
  158. uint16 PluginTemp4_Vol = 0;
  159. uint32 PluginTemp4_R = 0;
  160. AirPressureTemp_Vol = ATDDrv_GetChanResult(_ATD_INDEX_AIRPRESSURE);
  161. AirPressureTemp_Vol = (uint16)(AirPressureTemp_Vol * 1000 / 4095.0);
  162. /*气压数据转换*/
  163. AirPressureTemp_Vol = MAX(AirPressureTemp_Vol, 500);
  164. AirPressureTemp_Vol = MIN(AirPressureTemp_Vol, 4500);
  165. ebcd_P_airSensor = (uint8)((AirPressureTemp_Vol - 500) / 40);
  166. /*温度采集获取*/
  167. PluginTemp1_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP1);
  168. PluginTemp2_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP2);
  169. PluginTemp3_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP3);
  170. PluginTemp4_Vol = ATDDrv_GetChanResult(_ATD_INDEX_PLUGINTEMP4);
  171. PluginTemp1_R = (uint32)((PluginTemp1_Vol / (5.0 * 4095 - PluginTemp1_Vol)) * 1000.0);
  172. PluginTemp2_R = (uint32)((PluginTemp2_Vol / (5.0 * 4095 - PluginTemp2_Vol)) * 1000.0);
  173. PluginTemp3_R = (uint32)((PluginTemp3_Vol / (5.0 * 4095 - PluginTemp3_Vol)) * 1000.0);
  174. PluginTemp4_R = (uint32)((PluginTemp4_Vol / (5.0 * 4095 - PluginTemp4_Vol)) * 1000.0);
  175. ebcd_T_plugin[0] = (uint8)Look1_u32u8(PluginTemp1_R, R_table, T_table, 240);
  176. ebcd_T_plugin[1] = (uint8)Look1_u32u8(PluginTemp2_R, R_table, T_table, 240);
  177. ebcd_T_plugin[2] = (uint8)Look1_u32u8(PluginTemp3_R, R_table, T_table, 240);
  178. ebcd_T_plugin[3] = (uint8)Look1_u32u8(PluginTemp4_R, R_table, T_table, 240);
  179. }
  180. /**
  181. * @brief : lookUp Table Fun
  182. * @param {uint32} u0 x
  183. * @param {uint32} bp0 x_table
  184. * @param {uint16} table y_table
  185. * @param {uint16} maxIndex
  186. * @return {*}
  187. */
  188. uint16 Look1_u32u8(uint32 u0, uint32 *bp0, uint8 *table, uint16 MaxLen)
  189. {
  190. uint32 bpIdx = 0;
  191. uint32 iLeft = 0;
  192. uint32 iRght = 0;
  193. uint16 y = 0;
  194. uint32 yL_0d0 = 0;
  195. uint32 yR_0d0 = 0;
  196. uint32 maxIndex = MaxLen - 1;
  197. if (u0 <= bp0[0U])
  198. {
  199. iLeft = 0U;
  200. iRght = 0U;
  201. }
  202. else if (u0 < bp0[maxIndex])
  203. {
  204. //对折法寻找u0的位置
  205. bpIdx = maxIndex >> 1U;
  206. iLeft = 0U;
  207. iRght = maxIndex;
  208. while ((iRght - iLeft) > 1)
  209. {
  210. if (u0 < bp0[bpIdx])
  211. {
  212. iRght = bpIdx;
  213. }
  214. else
  215. {
  216. iLeft = bpIdx;
  217. }
  218. bpIdx = (iRght + iLeft) >> 1U;
  219. }
  220. }
  221. else
  222. {
  223. iLeft = maxIndex;
  224. iRght = maxIndex;
  225. }
  226. //找到位置以后计算插值
  227. if (iLeft != iRght)
  228. {
  229. //线性插值
  230. yR_0d0 = table[iLeft + 1U];
  231. yL_0d0 = table[iLeft];
  232. if (yR_0d0 >= yL_0d0)
  233. {
  234. y = (uint16)(((uint32)(u0 - bp0[iLeft]) * (yR_0d0 - yL_0d0)) / (bp0[iLeft + 1] - bp0[iLeft]) + yL_0d0);
  235. }
  236. else
  237. {
  238. y = (uint16)(yL_0d0 - ((uint32)(u0 - bp0[iLeft]) * (yL_0d0 - yR_0d0)) / (bp0[iLeft + 1] - bp0[iLeft]));
  239. }
  240. }
  241. else
  242. {
  243. y = (uint16)table[iLeft];
  244. }
  245. return y;
  246. }
  247. #ifdef _APP_TEST_CODE
  248. void TestDeviceFun(void)
  249. {
  250. uint8 ManuEnable = 0;
  251. //控制算法
  252. AccPedCD_Update();
  253. AccPedCD_Monitor();
  254. PulseAcqDrv_GetChanFreq(_PULSEACQ_INDEX_CP_CHAN, &VehCo_fInputUIA[0], &VehCo_rInputUWA[0]);
  255. // DODrv_SetChanState(_DO_INDEX_CPCTL_CHAN,(uint8)VehCo_bTestCPCtrlUW_C);
  256. uint16 DistenceBufferV[2] = {0, 0}, DistenceBufferR[2] = {0, 0};
  257. //开关1采集
  258. DINDrv_SetChanThres(_DIN_INDEX_BAK1_CHAN, 1, 4095U);
  259. Switch1 = !DINDrv_GetChanState(_DIN_INDEX_BAK1_CHAN);
  260. //手动控制采集
  261. DINDrv_SetChanThres(_DIN_INDEX_BAK2_CHAN, 1, 4095U);
  262. ManuEnable = !DINDrv_GetChanState(_DIN_INDEX_BAK2_CHAN);
  263. //位移量采集
  264. DistenceBufferV[0] = ATDDrv_GetChanResult(_ATD_INDEX_BAK3_CHAN);
  265. DistenceBufferR[0] = (uint16)((DistenceBufferV[0] / (5.0 * 4095 - DistenceBufferV[0])) * 1000.0);
  266. Distence1 = (uint16)((2132 - DistenceBufferR[0]) * (0.1219));
  267. //压力采集
  268. PressureValueBuffer = ATDDrv_GetChanResult(_ATD_INDEX_ACCPED1_CHAN);
  269. PressureValue = (uint32)(PressureValueBuffer * 1000 / 4095.0);
  270. //控制输出
  271. VehCo_ctEEPTestUI += 10;
  272. VehCo_ctEEPTestUB += 10;
  273. if (VehCo_ctEEPTestUI >= 4 * 1000)
  274. {
  275. PSwtDrv_Interface(_PSWT_INDEX_HBAK1_CHAN, 1); //输出 解锁持续9-4s
  276. ControlState = 1; //解锁
  277. }
  278. else
  279. {
  280. PSwtDrv_Interface(_PSWT_INDEX_HBAK1_CHAN, 0); //不输出
  281. ControlState = 2; //锁定 4 秒
  282. }
  283. if (VehCo_ctEEPTestUI >= 9 * 1000)
  284. {
  285. Control_Times++;
  286. VehCo_ctEEPTestUI = 0;
  287. }
  288. if (ManuEnable == 0)
  289. {
  290. Control_Times = 0;
  291. VehCo_ctEEPTestUI = 0;
  292. }
  293. }
  294. #endif
  295. #ifdef _APP_TEST_MOTOR_CODE
  296. void MotorTestFun(void)
  297. {
  298. static uint32 MotorRunTimer = 0;
  299. static uint16 MotorLockReadyTimer = 0;
  300. static sint8 LockMotorNum = -1;
  301. static sint8 unLockMotorNum = -1;
  302. static uint32 testTimer = 0;
  303. GetDIOState();
  304. GetAIOValue();
  305. testTimer = testTimer + 10;
  306. MotorControlLockNum = LockMotorNum;
  307. MotorControlunLockNum = unLockMotorNum;
  308. if (MotorControlCmd == 0) //可进入调试模式
  309. {
  310. if (MotorDebugCmd == 1)
  311. {
  312. if (getbit(MotorControlEnable, 0) == 1)
  313. {
  314. MotorControlFunc(0, 1);
  315. return;
  316. }
  317. else if (getbit(MotorControlEnable, 1) == 1)
  318. {
  319. MotorControlFunc(1, 1);
  320. return;
  321. }
  322. else if (getbit(MotorControlEnable, 2) == 1)
  323. {
  324. MotorControlFunc(2, 1);
  325. return;
  326. }
  327. else if (getbit(MotorControlEnable, 3) == 1)
  328. {
  329. MotorControlFunc(3, 1);
  330. return;
  331. }
  332. else
  333. {
  334. return;
  335. }
  336. }
  337. else if (MotorDebugCmd == 2)
  338. {
  339. if (getbit(MotorControlEnable, 0) == 1)
  340. {
  341. MotorControlFunc(0, 2);
  342. return;
  343. }
  344. else if (getbit(MotorControlEnable, 1) == 1)
  345. {
  346. MotorControlFunc(1, 2);
  347. return;
  348. }
  349. else if (getbit(MotorControlEnable, 2) == 1)
  350. {
  351. MotorControlFunc(2, 2);
  352. return;
  353. }
  354. else if (getbit(MotorControlEnable, 3) == 1)
  355. {
  356. MotorControlFunc(3, 2);
  357. return;
  358. }
  359. else
  360. {
  361. return;
  362. }
  363. }
  364. else
  365. {
  366. if (MotorLifeTestCmd == 1)
  367. {
  368. if (LockMotorNum < 4 && testTimer > 5000)
  369. {
  370. MotorControlCmd = 1;
  371. }
  372. else if (unLockMotorNum < 5 && LockMotorNum == 4 && testTimer > 5000)
  373. {
  374. MotorControlCmd = 2;
  375. }
  376. }
  377. else
  378. {
  379. MotorControlFunc(0, 0xF1);
  380. LockMotorNum = -1;
  381. unLockMotorNum = -1;
  382. return;
  383. }
  384. }
  385. }
  386. else if (MotorControlCmd == 1) //控制锁紧的自动流程
  387. {
  388. switch (LockMotorNum)
  389. {
  390. case -1:
  391. {
  392. MotorControlFunc(0, 0xF1);
  393. LockMotorNum = 0;
  394. break;
  395. }
  396. case 0:
  397. {
  398. if ((MotorRunTimer >= RunFailedDelay) || (MotorLockReadyTimer >= LockDelay) || getbit(MotorControlEnable, LockMotorNum) != 1)
  399. {
  400. MotorControlFunc(LockMotorNum, 0xF1);
  401. LockMotorNum = 1;
  402. MotorRunTimer = 0;
  403. MotorLockReadyTimer = 0;
  404. }
  405. else
  406. {
  407. if (getbit(ebcd_st_lockSensor, LockMotorNum) == 1)
  408. {
  409. MotorLockReadyTimer = MotorLockReadyTimer + 10;
  410. }
  411. MotorControlFunc(LockMotorNum, 1);
  412. MotorRunTimer = MotorRunTimer + 10;
  413. }
  414. break;
  415. }
  416. case 1:
  417. {
  418. if ((MotorRunTimer >= RunFailedDelay) || (MotorLockReadyTimer >= LockDelay) || getbit(MotorControlEnable, LockMotorNum) != 1)
  419. {
  420. MotorControlFunc(LockMotorNum, 0xF1);
  421. LockMotorNum = 2;
  422. MotorRunTimer = 0;
  423. MotorLockReadyTimer = 0;
  424. }
  425. else
  426. {
  427. if (getbit(ebcd_st_lockSensor, LockMotorNum) == 1)
  428. {
  429. MotorLockReadyTimer = MotorLockReadyTimer + 10;
  430. }
  431. MotorControlFunc(LockMotorNum, 1);
  432. MotorRunTimer = MotorRunTimer + 10;
  433. }
  434. break;
  435. }
  436. case 2:
  437. {
  438. if ((MotorRunTimer >= RunFailedDelay) || (MotorLockReadyTimer >= LockDelay) || getbit(MotorControlEnable, LockMotorNum) != 1)
  439. {
  440. MotorControlFunc(LockMotorNum, 0xF1);
  441. LockMotorNum = 3;
  442. MotorRunTimer = 0;
  443. MotorLockReadyTimer = 0;
  444. }
  445. else
  446. {
  447. if (getbit(ebcd_st_lockSensor, LockMotorNum) == 1)
  448. {
  449. MotorLockReadyTimer = MotorLockReadyTimer + 10;
  450. }
  451. MotorControlFunc(LockMotorNum, 1);
  452. MotorRunTimer = MotorRunTimer + 10;
  453. }
  454. break;
  455. }
  456. case 3:
  457. {
  458. if ((MotorRunTimer >= RunFailedDelay) || (MotorLockReadyTimer >= LockDelay) || getbit(MotorControlEnable, LockMotorNum) != 1)
  459. {
  460. MotorControlFunc(LockMotorNum, 0xF1);
  461. LockMotorNum = 4;
  462. unLockMotorNum = -1;
  463. MotorRunTimer = 0;
  464. MotorLockReadyTimer = 0;
  465. testTimer = 0;
  466. }
  467. else
  468. {
  469. if (getbit(ebcd_st_lockSensor, LockMotorNum) == 1)
  470. {
  471. MotorLockReadyTimer = MotorLockReadyTimer + 10;
  472. }
  473. MotorControlFunc(LockMotorNum, 1);
  474. MotorRunTimer = MotorRunTimer + 10;
  475. }
  476. break;
  477. }
  478. default:
  479. break;
  480. }
  481. }
  482. else if (MotorControlCmd == 2) //控制解锁的自动流程
  483. {
  484. switch (unLockMotorNum)
  485. {
  486. case -1:
  487. {
  488. MotorControlFunc(0, 0xF2);
  489. unLockMotorNum = 0;
  490. break;
  491. }
  492. case 0:
  493. {
  494. if ((MotorRunTimer >= RunFailedDelay) || (MotorLockReadyTimer >= 1) || getbit(MotorControlEnable, unLockMotorNum) != 1)
  495. {
  496. MotorControlFunc(unLockMotorNum, 0xF2);
  497. unLockMotorNum = 1;
  498. MotorRunTimer = 0;
  499. MotorLockReadyTimer = 0;
  500. }
  501. else
  502. {
  503. if (getbit(ebcd_st_unlockSensor, unLockMotorNum) == 1)
  504. {
  505. MotorLockReadyTimer = MotorLockReadyTimer + 10;
  506. }
  507. MotorControlFunc(unLockMotorNum, 2);
  508. MotorRunTimer = MotorRunTimer + 10;
  509. }
  510. break;
  511. }
  512. case 1:
  513. {
  514. if ((MotorRunTimer >= RunFailedDelay) || (MotorLockReadyTimer >= 1) || getbit(MotorControlEnable, unLockMotorNum) != 1)
  515. {
  516. MotorControlFunc(unLockMotorNum, 0xF2);
  517. unLockMotorNum = 2;
  518. MotorRunTimer = 0;
  519. MotorLockReadyTimer = 0;
  520. }
  521. else
  522. {
  523. if (getbit(ebcd_st_unlockSensor, unLockMotorNum) == 1)
  524. {
  525. MotorLockReadyTimer = MotorLockReadyTimer + 10;
  526. }
  527. MotorControlFunc(unLockMotorNum, 2);
  528. MotorRunTimer = MotorRunTimer + 10;
  529. }
  530. break;
  531. }
  532. case 2:
  533. {
  534. if ((MotorRunTimer >= RunFailedDelay) || (MotorLockReadyTimer >= 1) || getbit(MotorControlEnable, unLockMotorNum) != 1)
  535. {
  536. MotorControlFunc(unLockMotorNum, 0xF2);
  537. unLockMotorNum = 3;
  538. MotorRunTimer = 0;
  539. MotorLockReadyTimer = 0;
  540. }
  541. else
  542. {
  543. if (getbit(ebcd_st_unlockSensor, unLockMotorNum) == 1)
  544. {
  545. MotorLockReadyTimer = MotorLockReadyTimer + 10;
  546. }
  547. MotorControlFunc(unLockMotorNum, 2);
  548. MotorRunTimer = MotorRunTimer + 10;
  549. }
  550. break;
  551. }
  552. case 3:
  553. {
  554. if ((MotorRunTimer >= RunFailedDelay) || (MotorLockReadyTimer >= 1) || getbit(MotorControlEnable, unLockMotorNum) != 1)
  555. {
  556. MotorControlFunc(unLockMotorNum, 0xF2);
  557. unLockMotorNum = 4;
  558. MotorRunTimer = 0;
  559. MotorLockReadyTimer = 0;
  560. }
  561. else
  562. {
  563. if (getbit(ebcd_st_unlockSensor, unLockMotorNum) == 1)
  564. {
  565. MotorLockReadyTimer = MotorLockReadyTimer + 10;
  566. }
  567. MotorControlFunc(unLockMotorNum, 2);
  568. MotorRunTimer = MotorRunTimer + 10;
  569. }
  570. break;
  571. }
  572. case 4:
  573. {
  574. MotorControlFunc(0, 0xF1);
  575. unLockMotorNum = 5;
  576. LockMotorNum = -1;
  577. if (MotorLifeTestCmd == 1)
  578. {
  579. LifeTestCounter++;
  580. testTimer = 0;
  581. EEPROMDrv_bSaveInstantUB = 1;
  582. }
  583. break;
  584. }
  585. default:
  586. break;
  587. }
  588. }
  589. }
  590. void MotorControlFunc(UINT8 MotorIdx, UINT8 RotateDirec) // MotorIdx 0-3 表示四个电机,RotateDirec F1停止正转,F2表示反转停止,1表示正转,2表示反转
  591. {
  592. switch (MotorIdx)
  593. {
  594. case 0:
  595. {
  596. switch (RotateDirec)
  597. {
  598. case 0xF1:
  599. {
  600. PSwtDrv_Interface(_M1_C, 0);
  601. PSwtDrv_Interface(_M2_C, 0);
  602. PSwtDrv_Interface(_M3_C, 0);
  603. PSwtDrv_Interface(_M4_C, 0);
  604. PSwtDrv_Interface(_M_D_C, 0);
  605. break;
  606. }
  607. case 0xF2:
  608. {
  609. PSwtDrv_Interface(_M1_C, 1);
  610. PSwtDrv_Interface(_M2_C, 1);
  611. PSwtDrv_Interface(_M3_C, 1);
  612. PSwtDrv_Interface(_M4_C, 1);
  613. PSwtDrv_Interface(_M_D_C, 1);
  614. break;
  615. }
  616. case 1:
  617. {
  618. PSwtDrv_Interface(_M1_C, 0);
  619. PSwtDrv_Interface(_M_D_C, 0);
  620. PSwtDrv_Interface(_M1_C, 1);
  621. break;
  622. }
  623. case 2:
  624. {
  625. PSwtDrv_Interface(_M1_C, 1);
  626. PSwtDrv_Interface(_M_D_C, 1);
  627. PSwtDrv_Interface(_M1_C, 0);
  628. break;
  629. }
  630. default:
  631. break;
  632. }
  633. break;
  634. }
  635. case 1:
  636. {
  637. switch (RotateDirec)
  638. {
  639. case 0xF1:
  640. {
  641. PSwtDrv_Interface(_M1_C, 0);
  642. PSwtDrv_Interface(_M2_C, 0);
  643. PSwtDrv_Interface(_M3_C, 0);
  644. PSwtDrv_Interface(_M4_C, 0);
  645. PSwtDrv_Interface(_M_D_C, 0);
  646. break;
  647. }
  648. case 0xF2:
  649. {
  650. PSwtDrv_Interface(_M1_C, 1);
  651. PSwtDrv_Interface(_M2_C, 1);
  652. PSwtDrv_Interface(_M3_C, 1);
  653. PSwtDrv_Interface(_M4_C, 1);
  654. PSwtDrv_Interface(_M_D_C, 1);
  655. break;
  656. }
  657. case 0:
  658. {
  659. PSwtDrv_Interface(_M2_C, 0);
  660. PSwtDrv_Interface(_M_D_C, 0);
  661. break;
  662. }
  663. case 1:
  664. {
  665. PSwtDrv_Interface(_M2_C, 0);
  666. PSwtDrv_Interface(_M_D_C, 0);
  667. PSwtDrv_Interface(_M2_C, 1);
  668. break;
  669. }
  670. case 2:
  671. {
  672. PSwtDrv_Interface(_M2_C, 1);
  673. PSwtDrv_Interface(_M_D_C, 1);
  674. PSwtDrv_Interface(_M2_C, 0);
  675. break;
  676. }
  677. default:
  678. break;
  679. }
  680. break;
  681. }
  682. case 2:
  683. {
  684. switch (RotateDirec)
  685. {
  686. case 0xF1:
  687. {
  688. PSwtDrv_Interface(_M1_C, 0);
  689. PSwtDrv_Interface(_M2_C, 0);
  690. PSwtDrv_Interface(_M3_C, 0);
  691. PSwtDrv_Interface(_M4_C, 0);
  692. PSwtDrv_Interface(_M_D_C, 0);
  693. break;
  694. }
  695. case 0xF2:
  696. {
  697. PSwtDrv_Interface(_M1_C, 1);
  698. PSwtDrv_Interface(_M2_C, 1);
  699. PSwtDrv_Interface(_M3_C, 1);
  700. PSwtDrv_Interface(_M4_C, 1);
  701. PSwtDrv_Interface(_M_D_C, 1);
  702. break;
  703. }
  704. case 0:
  705. {
  706. PSwtDrv_Interface(_M3_C, 0);
  707. PSwtDrv_Interface(_M_D_C, 0);
  708. break;
  709. }
  710. case 1:
  711. {
  712. PSwtDrv_Interface(_M3_C, 0);
  713. PSwtDrv_Interface(_M_D_C, 0);
  714. PSwtDrv_Interface(_M3_C, 1);
  715. break;
  716. }
  717. case 2:
  718. {
  719. PSwtDrv_Interface(_M3_C, 1);
  720. PSwtDrv_Interface(_M_D_C, 1);
  721. PSwtDrv_Interface(_M3_C, 0);
  722. break;
  723. }
  724. default:
  725. break;
  726. }
  727. break;
  728. }
  729. case 3:
  730. {
  731. switch (RotateDirec)
  732. {
  733. case 0xF1:
  734. {
  735. PSwtDrv_Interface(_M1_C, 0);
  736. PSwtDrv_Interface(_M2_C, 0);
  737. PSwtDrv_Interface(_M3_C, 0);
  738. PSwtDrv_Interface(_M4_C, 0);
  739. PSwtDrv_Interface(_M_D_C, 0);
  740. break;
  741. }
  742. case 0xF2:
  743. {
  744. PSwtDrv_Interface(_M1_C, 1);
  745. PSwtDrv_Interface(_M2_C, 1);
  746. PSwtDrv_Interface(_M3_C, 1);
  747. PSwtDrv_Interface(_M4_C, 1);
  748. PSwtDrv_Interface(_M_D_C, 1);
  749. break;
  750. }
  751. case 0:
  752. {
  753. PSwtDrv_Interface(_M4_C, 0);
  754. PSwtDrv_Interface(_M_D_C, 0);
  755. break;
  756. }
  757. case 1:
  758. {
  759. PSwtDrv_Interface(_M4_C, 0);
  760. PSwtDrv_Interface(_M_D_C, 0);
  761. PSwtDrv_Interface(_M4_C, 1);
  762. break;
  763. }
  764. case 2:
  765. {
  766. PSwtDrv_Interface(_M4_C, 1);
  767. PSwtDrv_Interface(_M_D_C, 1);
  768. PSwtDrv_Interface(_M4_C, 0);
  769. break;
  770. }
  771. default:
  772. break;
  773. }
  774. break;
  775. }
  776. default:
  777. break;
  778. }
  779. }
  780. #endif