AppFunc.c 18 KB

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