AppFunc.c 18 KB

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