Steering.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Threading.Tasks;
  3. using GSG.NET.Logging;
  4. using GSG.NET.Utils;
  5. using OHV.Common.Shareds;
  6. using OHV.SqliteDAL;
  7. using Prism.Events;
  8. using VehicleControlSystem.ControlLayer.IO;
  9. namespace VehicleControlSystem.ControlLayer
  10. {
  11. public class Steering : ControlObjectBase
  12. {
  13. static Logger logger = Logger.GetLogger();
  14. EzIO iO = null;
  15. SqliteManager sql = null;
  16. IEventAggregator eventAggregator = null;
  17. public event EventHandler<int> OnSteeringError;
  18. private eSteeringState steeringState;
  19. public eSteeringState SteeringState
  20. {
  21. get { return steeringState; }
  22. set { SetField( ref this.steeringState , value ); }
  23. }
  24. public Steering( IIO io , SqliteManager sql , IEventAggregator ea )
  25. {
  26. this.iO = io as EzIO;
  27. this.sql = sql;
  28. this.eventAggregator = ea;
  29. this.iO.OnChangedIO += IO_OnChangedIO;
  30. }
  31. private void IO_OnChangedIO( BitBlock bit )
  32. {
  33. if ( bit.Tag.Equals( "IN_F_STEERING_DETECT_LEFT" ) || bit.Tag.Equals( "IN_F_STEERING_DETECT_RIGHT" ) )
  34. this.GetSteeringState();
  35. if ( bit.Tag.Equals( "IN_R_STEERING_DETECT_LEFT" ) || bit.Tag.Equals( "IN_R_STEERING_DETECT_RIGHT" ) )
  36. this.GetSteeringState();
  37. }
  38. void GetSteeringState( )
  39. {
  40. eSteeringState frontState = eSteeringState.None;
  41. if ( this.IsFrontLeft() && this.IsFrontRight() )
  42. frontState = eSteeringState.None;
  43. if ( !this.IsFrontLeft() && !this.IsFrontRight() )
  44. frontState = eSteeringState.None;
  45. if ( this.IsFrontLeft() && !this.IsFrontRight() )
  46. frontState = eSteeringState.Left;
  47. if ( !this.IsFrontLeft() && this.IsFrontRight() )
  48. frontState = eSteeringState.Right;
  49. eSteeringState rearState = eSteeringState.None;
  50. if ( this.IsRearLeft() && this.IsRearRight() )
  51. rearState = eSteeringState.None;
  52. if ( !this.IsRearLeft() && !this.IsRearRight() )
  53. rearState = eSteeringState.None;
  54. if ( this.IsRearLeft() && !this.IsRearRight() )
  55. rearState = eSteeringState.Left;
  56. if ( !this.IsRearLeft() && this.IsRearRight() )
  57. rearState = eSteeringState.Right;
  58. if ( frontState == eSteeringState.Left && rearState == eSteeringState.Left )
  59. this.SteeringState = eSteeringState.Left;
  60. else if ( frontState == eSteeringState.Right && rearState == eSteeringState.Right )
  61. this.SteeringState = eSteeringState.Right;
  62. else
  63. this.SteeringState = eSteeringState.None;
  64. }
  65. object lockObj = new object();
  66. bool isExecuteSteering = false;
  67. public int ControlSteering( bool isLeft = false )
  68. {
  69. if ( this.isExecuteSteering )
  70. return 0;
  71. this.isExecuteSteering = true;
  72. if ( isLeft ) //Left IO 가 직진 이라고 생각하자.
  73. {
  74. this.iO.WriteOutputIO( "OUT_F_STEERING_CWCCW" , false );
  75. this.iO.WriteOutputIO( "OUT_R_STEERING_CWCCW" , true );
  76. this.SetHightVolt();
  77. if ( this.SteeringState == eSteeringState.Left )
  78. {
  79. this.isExecuteSteering = false;
  80. return 0;
  81. }
  82. }
  83. else
  84. {
  85. this.iO.WriteOutputIO( "OUT_F_STEERING_CWCCW" , true );
  86. this.iO.WriteOutputIO( "OUT_R_STEERING_CWCCW" , false );
  87. this.SetHightVolt();
  88. if ( this.SteeringState == eSteeringState.Right )
  89. {
  90. this.isExecuteSteering = false;
  91. return 0;
  92. }
  93. }
  94. var task = Task<bool>.Run( ( ) =>
  95. {
  96. long sTime = SwUtils.CurrentTimeMillis;
  97. while ( true )
  98. {
  99. if ( SwUtils.Gt( sTime , 5 * ConstUtils.ONE_SECOND ) ) //Wait 20Sec
  100. return false;
  101. if ( isLeft )
  102. {
  103. if ( this.IsFrontLeft() && this.IsRearLeft() )
  104. break;
  105. }
  106. else
  107. {
  108. if ( this.IsFrontRight() && this.IsRearRight() )
  109. break;
  110. }
  111. }
  112. return true;
  113. } ).ContinueWith( t =>
  114. {
  115. if ( !t.Result )
  116. this.OnSteeringError?.BeginInvoke( this , 33 , null , null );
  117. else
  118. this.OnSteeringError?.BeginInvoke( this , 0 , null , null );
  119. GetSteeringState();
  120. this.isExecuteSteering = false;
  121. } );
  122. return 0;
  123. }
  124. void SetHightVolt( )
  125. {
  126. this.iO.WriteOutputIO( "OUT_F_STEERING_DA" , true );
  127. this.iO.WriteOutputIO( "OUT_R_STEERING_DA" , true );
  128. this.iO.WriteOutputIO( "OUT_F_STEERING_DA" , false , 500 );
  129. this.iO.WriteOutputIO( "OUT_R_STEERING_DA" , false , 500 );
  130. }
  131. bool IsFrontLeft( ) => this.iO.IsOn( "IN_F_STEERING_DETECT_LEFT" );
  132. bool IsFrontRight( ) => this.iO.IsOn( "IN_F_STEERING_DETECT_RIGHT" );
  133. bool IsRearLeft( ) => this.iO.IsOn( "IN_R_STEERING_DETECT_LEFT" );
  134. bool IsRearRight( ) => this.iO.IsOn( "IN_R_STEERING_DETECT_RIGHT" );
  135. public bool IsLeft( ) => this.IsFrontLeft() && this.IsRearLeft();
  136. public bool IsRight( ) => this.IsFrontRight() && this.IsRearRight();
  137. }
  138. }