GSIDrive.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using GSG.NET.Concurrent;
  2. using GSG.NET.Logging;
  3. using OHV.Common.Shareds;
  4. using OHV.SqliteDAL;
  5. using System;
  6. using VehicleControlSystem.ControlLayer.DB;
  7. using VehicleControlSystem.Managers;
  8. namespace VehicleControlSystem.ControlLayer.Drive
  9. {
  10. public class GSIDrive : ControlObjectBase
  11. {
  12. public enum eDriveServoState
  13. {
  14. ServoFault = -1,
  15. WrongServoNum = 0,
  16. ServoOn = 1,
  17. ServoOff = 2,
  18. ServoStop = 3,
  19. None,
  20. }
  21. Logger logger = Logger.GetLogger();
  22. public bool IsStop { get; set; }
  23. double currentPos = 0;
  24. public double CurrentPos { get { return this.currentPos; } set { SetField( ref this.currentPos, value ); } }
  25. private string currentTag;
  26. public string CurrentTag { get { return currentTag; } set { SetField( ref this.currentTag, value ); } }
  27. private double currentSpeed = 0;
  28. public double CurrentSpeed { get { return currentSpeed; } set { SetField( ref this.currentSpeed, value ); } }
  29. private double currentTorque = 0;
  30. public double CurrentTorque { get { return currentTorque; } set { SetField( ref this.currentTorque, value ); } }
  31. public bool IsErrorOn { get; set; }
  32. eSteeringState reqSteeringState = eSteeringState.None;
  33. public eSteeringState ReqSteeringState { get { return this.reqSteeringState; } set { SetField( ref this.reqSteeringState, value ); } }
  34. private eDriveServoState driveServoState;
  35. public eDriveServoState DriveServoState
  36. {
  37. get { return driveServoState; }
  38. set { SetField( ref driveServoState, value ); }
  39. }
  40. TaskCancel taskCancel = new TaskCancel();
  41. ThreadCancel threadCancel = new ThreadCancel();
  42. SqliteManager sql = null;
  43. public GSIDrive( SqliteManager sql )
  44. {
  45. this.sql = sql;
  46. }
  47. public void Init()
  48. {
  49. Redis.Instance.Init();
  50. this.threadCancel.AddGo( Thread_DriveStateChcker );
  51. this.threadCancel.AddGo( Thread_Logger );
  52. PhysicalCheckupLogger.Instance.Connecte();
  53. }
  54. public void Dispose()
  55. {
  56. Redis.Instance.Dispose();
  57. this.threadCancel.Cancel();
  58. LockUtils.Wait( 50 );
  59. this.threadCancel.StopWaitAll();
  60. PhysicalCheckupLogger.Instance.Dispose();
  61. }
  62. void Thread_DriveStateChcker()
  63. {
  64. logger.D( "[Drive] - Thread Drive State Checker Start" );
  65. while ( !this.threadCancel.Canceled )
  66. {
  67. try
  68. {
  69. LockUtils.Wait( 10 );
  70. this.GetReqSteeringState();
  71. //this.GetServoState();
  72. //if (Redis.Instance.GetDriveMove() )
  73. //{
  74. // this.LoggingState();
  75. //}
  76. }
  77. catch ( Exception e)
  78. {
  79. logger.E( $"[Drive] - Error : [{e}]" );
  80. }
  81. }
  82. logger.D( "[Drive] - Thread Drive State Checker Disposed" );
  83. }
  84. bool isLoggingStart = false;
  85. public bool IsloggingStart
  86. {
  87. get => this.isLoggingStart;
  88. set
  89. {
  90. if ( SetField( ref this.isLoggingStart, value ) )
  91. {
  92. if ( value )
  93. PhysicalCheckupLogger.Instance.SetPLCStartDrive();
  94. else
  95. PhysicalCheckupLogger.Instance.ResetPLCStartDrive();
  96. }
  97. }
  98. }
  99. void Thread_Logger()
  100. {
  101. while ( !this.threadCancel.Canceled )
  102. {
  103. try
  104. {
  105. LockUtils.Wait( 10 );
  106. if ( Redis.Instance.GetDriveMove() )
  107. {
  108. this.IsloggingStart = true;
  109. this.LoggingState();
  110. }
  111. else
  112. this.IsloggingStart = false;
  113. }
  114. catch ( Exception e )
  115. {
  116. logger.E( $"[Drive] - Error : [{e}]" );
  117. }
  118. }
  119. }
  120. void LoggingState()
  121. {
  122. var currentBCR = Redis.Instance.CurrentBCRValue();
  123. var speed = Redis.Instance.ActualVelocityToSpeed();
  124. var fTorque = Redis.Instance.TorqueFront();
  125. var fRPM = Redis.Instance.ActualVelocityToFrontRPM();
  126. var fLoadFacter = Redis.Instance.LoadFacterFront();
  127. PhysicalCheckupLogger.Instance.FrontWheelLogging( speed.ToString(), fTorque.ToString(), fRPM.ToString(), fLoadFacter.ToString(), currentBCR.ToString() );
  128. var rTorque = Redis.Instance.TorqueRear();
  129. var rRPM = Redis.Instance.ActualVelocityToRearRPM();
  130. var rLoadFacter = Redis.Instance.LoadFacterRear();
  131. PhysicalCheckupLogger.Instance.RearWheelLoggging( speed.ToString(), rTorque.ToString(), rRPM.ToString(), rLoadFacter.ToString(), currentBCR.ToString() );
  132. }
  133. void GetReqSteeringState()
  134. {
  135. var ret = Redis.Instance.GetSteering();
  136. if ( ret == 1 )// Left
  137. this.ReqSteeringState = eSteeringState.Left;
  138. else if ( ret == -1 ) //Right
  139. this.ReqSteeringState = eSteeringState.Right;
  140. else if ( ret == 0 ) // Do Nothing
  141. this.ReqSteeringState = eSteeringState.None;
  142. else
  143. { }
  144. }
  145. bool IsDriveFault()
  146. {
  147. var ret = Redis.Instance.GetSystemState();
  148. return ret == 2 ? true : false;
  149. }
  150. public void SetDriveOperationMode( eOperatationMode mode )
  151. {
  152. if ( mode == eOperatationMode.ManualMode )
  153. Redis.Instance.SetSystemOperation( 3 );
  154. else if ( mode == eOperatationMode.AutoMode )
  155. Redis.Instance.SetSystemOperation( 2 );
  156. else { }
  157. }
  158. public eOperatationMode GetDriveOperationMode()
  159. {
  160. var ret = Redis.Instance.GetSystemState();
  161. if ( ret == 3 )
  162. return eOperatationMode.ManualMode;
  163. else if ( ret == 4 )
  164. return eOperatationMode.AutoMode;
  165. else
  166. return eOperatationMode.InitialMode;
  167. }
  168. public int MoveToPoint( string point, double velocity )
  169. {
  170. //if ( this.DriveServoState != eDriveServoState.ServoStop )
  171. // return 9999;
  172. //Drive Fault 상태 확인
  173. if ( IsDriveFault() )
  174. return 34;
  175. //ToDo: 이동 명령 실행.
  176. int result = 0;
  177. if (! Redis.Instance.RouteMapMoveTo( point ) )
  178. {
  179. //Todo: Error 처리
  180. }
  181. return 0;
  182. }
  183. /// <summary>
  184. /// 급정시 사용.
  185. /// </summary>
  186. //public void EStop() => OHVdriveSetServoCommand(3);
  187. /// <summary>
  188. /// Manual 상태에서 가감속 Stop
  189. /// </summary>
  190. public void Stop()
  191. {
  192. }
  193. //public void JogForWard() => OHVdriveSetManualOperationCommand( 1 );
  194. //public void JogBackward() => OHVdriveSetManualOperationCommand( -1 );
  195. //public void ServoOn() => OHVdriveSetServoCommand( 1 );
  196. //public void ServoOff() => OHVdriveSetServoCommand( 2 );
  197. //public void AmpFaultReset() => OHVdriveSetServoCommand( -1 );
  198. public void SetObstacleState( eObstacleState state )
  199. {
  200. int field = 0;
  201. switch ( state )
  202. {
  203. case eObstacleState.Normal:
  204. field = 0;
  205. break;
  206. case eObstacleState.Abnormal:
  207. field = 1;
  208. break;
  209. case eObstacleState.Blocked:
  210. field = 2;
  211. break;
  212. case eObstacleState.Decelerate:
  213. field = 3;
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219. public bool VelocityChainge( double velocity )
  220. {
  221. return true;
  222. }
  223. #region Test Method
  224. #endregion
  225. }
  226. }