Steering.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using GSG.NET.Concurrent;
  6. using GSG.NET.Logging;
  7. using GSG.NET.Utils;
  8. using OHV.Common.Shareds;
  9. using OHV.SqliteDAL;
  10. using Prism.Events;
  11. using VehicleControlSystem.ControlLayer.IO;
  12. using VehicleControlSystem.ControlLayer.MQ;
  13. namespace VehicleControlSystem.ControlLayer
  14. {
  15. public class Steering : ControlObjectBase, IDisposable
  16. {
  17. static Logger logger = Logger.GetLogger();
  18. static Logger SteerLogger = Logger.GetLogger("SteerLogger");
  19. ThreadCancel threadCancel = new ThreadCancel();
  20. TsQueue<eSteerCommand> qReqSteer = new TsQueue<eSteerCommand>();
  21. EzIO iO = null;
  22. SqliteManager sql = null;
  23. IEventAggregator eventAggregator = null;
  24. ZmqManager zmqManager = null;
  25. public event EventHandler<int> OnSteeringError;
  26. private eSteeringState steeringState;
  27. public eSteeringState SteeringState
  28. {
  29. get { return steeringState; }
  30. set { if (SetField(ref this.steeringState, value)) { /*this.zmqManager.SetCurrentSteeringState(value);*/ } }
  31. }
  32. public Steering() { }
  33. public void Dispose()
  34. {
  35. this.threadCancel.Cancel();
  36. }
  37. public void AssignRefObject(IIO io, SqliteManager sql, IEventAggregator ea, ZmqManager zm)
  38. {
  39. this.iO = io as EzIO;
  40. this.iO.OnChangedIO += IO_OnChangedIO;
  41. this.sql = sql;
  42. this.eventAggregator = ea;
  43. this.zmqManager = zm;
  44. this.threadCancel.AddGo(_DoControlSteer);
  45. }
  46. #region Thread Method
  47. void _DoControlSteer()
  48. {
  49. while (!this.threadCancel.Canceled)
  50. {
  51. var q = this.qReqSteer.Dequeue();
  52. //this.ControlSteerNWaitDone( q );
  53. this.ExecuteCommandSteer(q);
  54. }
  55. }
  56. void ControlSteerNWaitDone(eSteeringState requestState)
  57. {
  58. logger.D($"Steer - Start {requestState} -------------------------------------------------------");
  59. if (requestState == eSteeringState.Left)
  60. {
  61. this.iO.WriteOutputIO("OUT_F_STEERING_CWCCW", false);
  62. this.iO.WriteOutputIO("OUT_R_STEERING_CWCCW", true);
  63. }
  64. else
  65. {
  66. this.iO.WriteOutputIO("OUT_F_STEERING_CWCCW", true);
  67. this.iO.WriteOutputIO("OUT_R_STEERING_CWCCW", false);
  68. }
  69. //Hight Voltage On
  70. this.iO.WriteOutputIO("OUT_F_STEERING_DA", true);
  71. this.iO.WriteOutputIO("OUT_R_STEERING_DA", true);
  72. logger.D($"Steer - OutPut On");
  73. var sT = SwUtils.CurrentTimeMillis;
  74. bool isCheckedOutput = false;
  75. var currentDirection = eSteeringState.None;
  76. long eT = 0;
  77. while (true)
  78. {
  79. LockUtils.Wait(10);
  80. currentDirection = GetSteerDirection();
  81. if (requestState == currentDirection)
  82. break;
  83. eT = SwUtils.Elapsed(sT);
  84. if (eT > 700 && !isCheckedOutput)
  85. {
  86. this.iO.WriteOutputIO("OUT_F_STEERING_DA", false);
  87. this.iO.WriteOutputIO("OUT_R_STEERING_DA", false);
  88. isCheckedOutput = true;
  89. logger.D($"Steer - Output Off {eT}ms");
  90. }
  91. if (eT > ConstUtils.ONE_SECOND * 2)
  92. {
  93. logger.D($"Steer - Wait Time Out {eT}ms");
  94. this.SteeringState = eSteeringState.None;
  95. if (!isCheckedOutput)
  96. {
  97. this.iO.WriteOutputIO("OUT_F_STEERING_DA", false);
  98. this.iO.WriteOutputIO("OUT_R_STEERING_DA", false);
  99. logger.D($"Steer - Output Off {SwUtils.Elapsed(sT)}ms");
  100. }
  101. return;
  102. }
  103. }
  104. if (!isCheckedOutput) //위에서 Off 를 안했을 경우가 생길 수 있음. (700ms 이내에 조향 바뀌었을때)
  105. {
  106. this.iO.WriteOutputIO("OUT_F_STEERING_DA", false);
  107. this.iO.WriteOutputIO("OUT_R_STEERING_DA", false);
  108. logger.D($"Steer - Output Off {SwUtils.Elapsed(sT)}ms");
  109. }
  110. logger.D($"Steer - Drive Request Start");
  111. this.SteeringState = currentDirection;
  112. logger.D($"Steer - Contorl End {SwUtils.Elapsed(sT)}ms -------------------------------------------------------");
  113. SteerLogger.I(SwUtils.Elapsed(sT));
  114. }
  115. void ExecuteCommandSteer(eSteerCommand command)
  116. {
  117. logger.D($"Execute Steer Command - {command}");
  118. var cmdSt = SwUtils.CurrentTimeMillis;
  119. Action<bool> actionSteerFront = (isLeft) =>
  120. {
  121. if (isLeft)
  122. this.iO.WriteOutputIO("OUT_F_STEERING_CWCCW", false);
  123. else
  124. this.iO.WriteOutputIO("OUT_F_STEERING_CWCCW", true);
  125. };
  126. Action<bool> actionSteerRear = (isLeft) =>
  127. {
  128. if (isLeft)
  129. this.iO.WriteOutputIO("OUT_R_STEERING_CWCCW", true);
  130. else
  131. this.iO.WriteOutputIO("OUT_R_STEERING_CWCCW", false);
  132. };
  133. Action<bool> actionOnFrontHightVolteOn = (isOn) =>
  134. {
  135. if (isOn)
  136. this.iO.WriteOutputIO("OUT_F_STEERING_DA", true);
  137. else
  138. this.iO.WriteOutputIO("OUT_F_STEERING_DA", false);
  139. };
  140. Action<bool> actionOnRearHightVolteOn = (isOn) =>
  141. {
  142. if (isOn)
  143. this.iO.WriteOutputIO("OUT_R_STEERING_DA", true);
  144. else
  145. this.iO.WriteOutputIO("OUT_R_STEERING_DA", false);
  146. };
  147. Action<eSteerCommand> actionSteer = (cmd) =>
  148. {
  149. List<Func<bool>> inputList = new List<Func<bool>>();
  150. List<Action<bool>> outputList = new List<Action<bool>>();
  151. switch (cmd)
  152. {
  153. case eSteerCommand.LL:
  154. case eSteerCommand.ManualLL:
  155. inputList.Add(IsFrontLeft);
  156. inputList.Add(IsRearLeft);
  157. this.iO.WriteOutputIO("OUT_F_STEERING_OFF", true);
  158. this.iO.WriteOutputIO("OUT_R_STEERING_OFF", true);
  159. actionSteerFront(true);
  160. actionSteerRear(true);
  161. outputList.Add(actionOnRearHightVolteOn);
  162. outputList.Add(actionOnFrontHightVolteOn);
  163. break;
  164. case eSteerCommand.LX:
  165. inputList.Add(IsFrontLeft);
  166. this.iO.WriteOutputIO("OUT_F_STEERING_OFF", true);
  167. actionSteerFront(true);
  168. outputList.Add(actionOnFrontHightVolteOn);
  169. break;
  170. case eSteerCommand.XL:
  171. inputList.Add(IsRearLeft);
  172. this.iO.WriteOutputIO("OUT_R_STEERING_OFF", true);
  173. actionSteerRear(true);
  174. outputList.Add(actionOnRearHightVolteOn);
  175. break;
  176. case eSteerCommand.RR:
  177. case eSteerCommand.ManualRR:
  178. inputList.Add(IsFrontRight);
  179. inputList.Add(IsRearRight);
  180. this.iO.WriteOutputIO("OUT_F_STEERING_OFF", true);
  181. this.iO.WriteOutputIO("OUT_R_STEERING_OFF", true);
  182. actionSteerFront(false);
  183. actionSteerRear(false);
  184. outputList.Add(actionOnRearHightVolteOn);
  185. outputList.Add(actionOnFrontHightVolteOn);
  186. break;
  187. case eSteerCommand.RX:
  188. inputList.Add(IsFrontRight);
  189. this.iO.WriteOutputIO("OUT_F_STEERING_OFF", true);
  190. actionSteerFront(false);
  191. outputList.Add(actionOnFrontHightVolteOn);
  192. break;
  193. case eSteerCommand.XR:
  194. inputList.Add(IsRearRight);
  195. this.iO.WriteOutputIO("OUT_R_STEERING_OFF", true);
  196. actionSteerRear(false);
  197. outputList.Add(actionOnRearHightVolteOn);
  198. break;
  199. default:
  200. break;
  201. }
  202. outputList.ForEach(x => x.Invoke(true));
  203. var sT = SwUtils.CurrentTimeMillis;
  204. while (true)
  205. {
  206. LockUtils.Wait(10);
  207. if (inputList.All(x => x.Invoke()))
  208. {
  209. logger.D($"Execute Steer - {cmd} - Sensor On {SwUtils.Elapsed(sT)}mm");
  210. break;
  211. }
  212. if (SwUtils.Elapsed(sT) > 700)
  213. {
  214. logger.D($"Execute Steer Time out - {cmd} - {SwUtils.Elapsed(sT)}mm");
  215. break;
  216. }
  217. }
  218. outputList.ForEach(x => x.Invoke(false));
  219. };
  220. switch (command)
  221. {
  222. case eSteerCommand.LL:
  223. case eSteerCommand.ManualLL:
  224. case eSteerCommand.LX:
  225. case eSteerCommand.XL:
  226. case eSteerCommand.RR:
  227. case eSteerCommand.ManualRR:
  228. case eSteerCommand.RX:
  229. case eSteerCommand.XR:
  230. actionSteer(command);
  231. break;
  232. case eSteerCommand.OffOff:
  233. //Todo: 전후방 전원 Off
  234. this.iO.WriteOutputIO("OUT_F_STEERING_OFF", false);
  235. this.iO.WriteOutputIO("OUT_R_STEERING_OFF", false);
  236. break;
  237. case eSteerCommand.OnOn:
  238. this.iO.WriteOutputIO("OUT_F_STEERING_OFF", true);
  239. this.iO.WriteOutputIO("OUT_R_STEERING_OFF", true);
  240. break;
  241. case eSteerCommand.FrontOn:
  242. this.iO.WriteOutputIO("OUT_F_STEERING_OFF", true);
  243. break;
  244. case eSteerCommand.RearOn:
  245. this.iO.WriteOutputIO("OUT_R_STEERING_OFF", true);
  246. break;
  247. case eSteerCommand.FrontOff:
  248. this.iO.WriteOutputIO("OUT_F_STEERING_OFF", false);
  249. break;
  250. case eSteerCommand.RearOff:
  251. this.iO.WriteOutputIO("OUT_R_STEERING_OFF", false);
  252. break;
  253. default:
  254. break;
  255. }
  256. if ( command == eSteerCommand.ManualLL || command == eSteerCommand.ManualRR)
  257. {
  258. this.SteeringState = GetSteerDirection();
  259. }
  260. logger.D($"Complete Steer Command - {command} / {SwUtils.Elapsed(cmdSt)}ms");
  261. }
  262. eSteeringState GetSteerDirection()
  263. {
  264. if (this.IsLeft())
  265. return eSteeringState.Left;
  266. if (this.IsRight())
  267. return eSteeringState.Right;
  268. return eSteeringState.None;
  269. }
  270. public void RequestControl(eSteeringState state)
  271. {
  272. //this.qReqSteer.Enqueue( state );
  273. switch (state)
  274. {
  275. case eSteeringState.None:
  276. break;
  277. case eSteeringState.Left:
  278. this.RequestCommand(eSteerCommand.ManualLL);
  279. break;
  280. case eSteeringState.Right:
  281. this.RequestCommand(eSteerCommand.ManualRR);
  282. break;
  283. default:
  284. break;
  285. }
  286. }
  287. public void RequestCommand(eSteerCommand command) => this.qReqSteer.Enqueue(command);
  288. #endregion
  289. private void IO_OnChangedIO(BitBlock bit)
  290. {
  291. //if ( bit.Tag.Equals( "IN_F_STEERING_DETECT_LEFT" ) || bit.Tag.Equals( "IN_F_STEERING_DETECT_RIGHT" ) )
  292. // this.GetSteeringState();
  293. //if ( bit.Tag.Equals( "IN_R_STEERING_DETECT_LEFT" ) || bit.Tag.Equals( "IN_R_STEERING_DETECT_RIGHT" ) )
  294. // this.GetSteeringState();
  295. }
  296. public eSteeringState GetSteeringState()
  297. {
  298. eSteeringState frontState = eSteeringState.None;
  299. if (this.IsFrontLeft() && this.IsFrontRight())
  300. frontState = eSteeringState.None;
  301. if (!this.IsFrontLeft() && !this.IsFrontRight())
  302. frontState = eSteeringState.None;
  303. if (this.IsFrontRight())
  304. frontState = eSteeringState.Right;
  305. else if (this.IsFrontLeft())
  306. frontState = eSteeringState.Left;
  307. else
  308. frontState = eSteeringState.None;
  309. eSteeringState rearState = eSteeringState.None;
  310. if (this.IsRearLeft() && this.IsRearRight())
  311. rearState = eSteeringState.None;
  312. if (!this.IsRearLeft() && !this.IsRearRight())
  313. rearState = eSteeringState.None;
  314. if (this.IsRearRight())
  315. rearState = eSteeringState.Right;
  316. else if (this.IsRearLeft())
  317. rearState = eSteeringState.Left;
  318. else
  319. rearState = eSteeringState.None;
  320. eSteeringState totalState = eSteeringState.None;
  321. if (frontState == eSteeringState.Left && rearState == eSteeringState.Left)
  322. totalState = eSteeringState.Left;
  323. else if (frontState == eSteeringState.Right && rearState == eSteeringState.Right)
  324. totalState = eSteeringState.Right;
  325. else
  326. totalState = eSteeringState.None;
  327. return totalState;
  328. }
  329. readonly object lockObject = new object();
  330. bool isExecuteSteering = false;
  331. public int ControlSteering(bool isLeft = false)
  332. {
  333. if (this.isExecuteSteering)
  334. return 0;
  335. this.isExecuteSteering = true;
  336. var sT = SwUtils.CurrentTimeMillis;
  337. lock (lockObject)
  338. {
  339. if (isLeft) //Left IO 가 직진 이라고 생각하자.
  340. {
  341. this.iO.WriteOutputIO("OUT_F_STEERING_CWCCW", false);
  342. this.iO.WriteOutputIO("OUT_R_STEERING_CWCCW", true);
  343. this.SetHightVolt();
  344. if (this.SteeringState == eSteeringState.Left)
  345. {
  346. this.isExecuteSteering = false;
  347. return 0;
  348. }
  349. }
  350. else
  351. {
  352. this.iO.WriteOutputIO("OUT_F_STEERING_CWCCW", true);
  353. this.iO.WriteOutputIO("OUT_R_STEERING_CWCCW", false);
  354. this.SetHightVolt();
  355. if (this.SteeringState == eSteeringState.Right)
  356. {
  357. this.isExecuteSteering = false;
  358. return 0;
  359. }
  360. }
  361. var task = Task<bool>.Run(() =>
  362. {
  363. long sTime = SwUtils.CurrentTimeMillis;
  364. while (true)
  365. {
  366. LockUtils.Wait(5);
  367. if (SwUtils.Gt(sTime, 2 * ConstUtils.ONE_SECOND)) //Wait 3Sec
  368. return false;
  369. if (isLeft)
  370. {
  371. if (this.IsLeft())
  372. break;
  373. }
  374. else
  375. {
  376. if (this.IsRight())
  377. break;
  378. }
  379. }
  380. return true;
  381. }).ContinueWith(t =>
  382. {
  383. if (!t.Result)
  384. this.OnSteeringError?.BeginInvoke(this, 33, null, null);
  385. else
  386. this.OnSteeringError?.BeginInvoke(this, 0, null, null);
  387. logger.D($"[Steer Release Lock] - {SwUtils.Elapsed(sT)} ms");
  388. LockUtils.Wait(200);
  389. this.SteeringState = GetSteeringState();
  390. this.isExecuteSteering = false;
  391. });
  392. }
  393. return 0;
  394. }
  395. /// <summary>
  396. /// Error 를 만들지 않고 동작 만 한다.
  397. /// </summary>
  398. /// <param name="isLeft"></param>
  399. public void ControlSteeringNotResult(bool isLeft = false)
  400. {
  401. if (this.isExecuteSteering) return;
  402. this.isExecuteSteering = true;
  403. var sT = SwUtils.CurrentTimeMillis;
  404. if (isLeft) //Left IO 가 직진 이라고 생각하자.
  405. {
  406. this.iO.WriteOutputIO("OUT_F_STEERING_CWCCW", false);
  407. this.iO.WriteOutputIO("OUT_R_STEERING_CWCCW", true);
  408. this.SetHightVolt();
  409. if (this.SteeringState == eSteeringState.Left)
  410. {
  411. this.isExecuteSteering = false;
  412. return;
  413. }
  414. }
  415. else
  416. {
  417. this.iO.WriteOutputIO("OUT_F_STEERING_CWCCW", true);
  418. this.iO.WriteOutputIO("OUT_R_STEERING_CWCCW", false);
  419. this.SetHightVolt();
  420. if (this.SteeringState == eSteeringState.Right)
  421. {
  422. this.isExecuteSteering = false;
  423. return;
  424. }
  425. }
  426. var task = Task<bool>.Run(() =>
  427. {
  428. long sTime = SwUtils.CurrentTimeMillis;
  429. while (true)
  430. {
  431. LockUtils.Wait(5);
  432. if (SwUtils.Gt(sTime, 2 * ConstUtils.ONE_SECOND)) //Wait 3Sec
  433. return false;
  434. if (isLeft)
  435. {
  436. if (this.IsLeft())
  437. break;
  438. }
  439. else
  440. {
  441. if (this.IsRight())
  442. break;
  443. }
  444. }
  445. return true;
  446. }).ContinueWith(t =>
  447. {
  448. var eT = SwUtils.Elapsed(sT);
  449. logger.D($"[Steer Release Lock] - {eT} ms");
  450. if (!t.Result)
  451. this.SteeringState = eSteeringState.None;
  452. else
  453. {
  454. LockUtils.Wait(200);
  455. this.SteeringState = GetSteeringState();
  456. }
  457. this.isExecuteSteering = false;
  458. });
  459. }
  460. /// <summary>
  461. /// Test Control
  462. /// </summary>
  463. /// <param name="isLeft"></param>
  464. public void TestSteerControl(bool isLeft = false)
  465. {
  466. if (this.isExecuteSteering) return;
  467. this.isExecuteSteering = true;
  468. logger.D($"[Steer Lock] ----------------------------------------------------------------");
  469. var sT = SwUtils.CurrentTimeMillis;
  470. Task.Run(() =>
  471. {
  472. if (isLeft) //Left IO 가 직진 이라고 생각하자.
  473. {
  474. this.iO.WriteOutputIO("OUT_F_STEERING_CWCCW", false);
  475. this.iO.WriteOutputIO("OUT_R_STEERING_CWCCW", true);
  476. this.iO.WriteOutputIO("OUT_F_STEERING_DA", true);
  477. this.iO.WriteOutputIO("OUT_R_STEERING_DA", true);
  478. LockUtils.Wait(700);
  479. this.iO.WriteOutputIO("OUT_F_STEERING_DA", false);
  480. this.iO.WriteOutputIO("OUT_R_STEERING_DA", false);
  481. }
  482. else
  483. {
  484. this.iO.WriteOutputIO("OUT_F_STEERING_CWCCW", true);
  485. this.iO.WriteOutputIO("OUT_R_STEERING_CWCCW", false);
  486. this.iO.WriteOutputIO("OUT_F_STEERING_DA", true);
  487. this.iO.WriteOutputIO("OUT_R_STEERING_DA", true);
  488. LockUtils.Wait(700);
  489. this.iO.WriteOutputIO("OUT_F_STEERING_DA", false);
  490. this.iO.WriteOutputIO("OUT_R_STEERING_DA", false);
  491. }
  492. });
  493. Task.Run(() =>
  494. {
  495. long sTime = SwUtils.CurrentTimeMillis;
  496. while (true)
  497. {
  498. LockUtils.Wait(5);
  499. if (SwUtils.Gt(sTime, 2 * ConstUtils.ONE_SECOND))
  500. {
  501. this.SteeringState = eSteeringState.None;
  502. this.isExecuteSteering = false;
  503. logger.D($"Steer Control Fail {SwUtils.Elapsed(sT)}");
  504. return;
  505. }
  506. if (isLeft)
  507. {
  508. if (this.IsLeft())
  509. break;
  510. }
  511. else
  512. {
  513. if (this.IsRight())
  514. break;
  515. }
  516. }
  517. LockUtils.Wait(200); //안정화 시간.
  518. logger.D($"Request Steer State Start");
  519. this.SteeringState = GetSteeringState();
  520. logger.D($"Request Steer State End");
  521. var eT = SwUtils.Elapsed(sT);
  522. logger.D($"[Steer Release Lock] - {eT} ms ----------------------------------------------------------");
  523. SteerLogger.I(eT);
  524. });
  525. this.isExecuteSteering = false;
  526. }
  527. void SetHightVolt()
  528. {
  529. this.iO.WriteOutputIO("OUT_F_STEERING_DA", true);
  530. this.iO.WriteOutputIO("OUT_R_STEERING_DA", true);
  531. this.iO.WriteOutputIO("OUT_F_STEERING_DA", false, 400);
  532. this.iO.WriteOutputIO("OUT_R_STEERING_DA", false, 700);
  533. }
  534. bool IsFrontLeft() => this.iO.IsOn("IN_F_STEERING_DETECT_LEFT");
  535. bool IsFrontRight() => this.iO.IsOn("IN_F_STEERING_DETECT_RIGHT");
  536. bool IsRearLeft() => this.iO.IsOn("IN_R_STEERING_DETECT_LEFT");
  537. bool IsRearRight() => this.iO.IsOn("IN_R_STEERING_DETECT_RIGHT");
  538. public bool IsLeft() => this.IsFrontLeft() && this.IsRearLeft();
  539. public bool IsRight() => this.IsFrontRight() && this.IsRearRight();
  540. }
  541. }