EzAxis.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. using GSG.NET.Excel;
  2. using GSG.NET.Utils;
  3. using System;
  4. using System.Linq;
  5. using System.Threading;
  6. using VehicleControlSystem.ControlLayer.Lib.EziPlusE;
  7. namespace VehicleControlSystem.ControlLayer.Axis
  8. {
  9. public class EzAxis : AbstractAxis
  10. {
  11. static GSG.NET.Logging.Logger logger = GSG.NET.Logging.Logger.GetLogger();
  12. public AxisConfig Config { get; set; }
  13. public override AxisData ValocityData { get; set; }
  14. public override double CurrentPosition
  15. {
  16. get
  17. {
  18. int pos = 0;
  19. EziMOTIONPlusELib.FAS_GetActualPos( Config.AxisNo, ref pos );
  20. return pos * Config.Scale;
  21. }
  22. }
  23. public double CommandPosition
  24. {
  25. get
  26. {
  27. int pos = 0;
  28. EziMOTIONPlusELib.FAS_GetCommandPos( Config.AxisNo, ref pos );
  29. return pos * Config.Scale;
  30. }
  31. }
  32. public bool IsConnect { get { return EziMOTIONPlusELib.FAS_IsSlaveExist( Config.AxisNo ) == 1 ? true : false; } }
  33. E_VelocityMode e_VelocityMode = E_VelocityMode.ManualMode;
  34. public override E_VelocityMode VelocityMode
  35. {
  36. get => this.e_VelocityMode;
  37. set
  38. {
  39. if( this.e_VelocityMode == value )
  40. return;
  41. this.e_VelocityMode = value;
  42. }
  43. }
  44. public EzAxis( AxisConfig config )
  45. {
  46. this.Config = config;
  47. this.Tag = config.AxisName;
  48. this.AxisID = config.AxisNo;
  49. }
  50. /// <summary>
  51. /// 현재 위치를 0으로 함
  52. /// CommandPos, ActualPos 둘다.
  53. /// </summary>
  54. /// <returns></returns>
  55. public override int SetAbsPosition()
  56. {
  57. return EziMOTIONPlusELib.FAS_ClearPosition( Config.AxisNo );
  58. }
  59. public override int EStop()
  60. {
  61. return EziMOTIONPlusELib.FAS_EmergencyStop( Config.AxisNo );
  62. }
  63. public override int GetComVelocity()
  64. {
  65. throw new NotImplementedException();
  66. }
  67. public override int Initialize()
  68. {
  69. throw new NotImplementedException();
  70. }
  71. public override int Initialize( AxisData data )
  72. {
  73. //IO 에서 연결 시도.
  74. //EziMOTIONPlusELib.FAS_Connect( (byte)192, (byte)168, (byte)0, (byte)Config.AxisNo, Config.AxisNo );
  75. this.ValocityData = data;
  76. //초기에 설정한다.
  77. this.SetAccelTime( this.ValocityData.AutoRunAccelerate );
  78. this.SetDecelTime( this.ValocityData.AutoRunDecelerate );
  79. return 0;
  80. }
  81. int GetAllStatus( out uint axisStatus )
  82. {
  83. uint axisDIn = 0, axisDOut = 0;
  84. int cmdPos = 0, actPos = 0, posErr = 0, actVel = 0;
  85. ushort posItemNo = 0;
  86. axisStatus = 0;
  87. return EziMOTIONPlusELib.FAS_GetAllStatus( Config.AxisNo, ref axisDIn, ref axisDOut, ref axisStatus,
  88. ref cmdPos, ref actPos, ref posErr, ref actVel, ref posItemNo );
  89. }
  90. public override bool IsAxisDone()
  91. {
  92. return IsInposition() && !IsInMotion() ? true : false;
  93. }
  94. public bool IsInposition()
  95. {
  96. uint axisStatus;
  97. if( this.GetAllStatus( out axisStatus ) == EziMOTIONPlusELib.FMM_OK )
  98. {
  99. return Convert.ToBoolean( axisStatus & EziSERVO2PlusE.FFLAG_INPOSITION ) ? true : false;
  100. }
  101. return false;
  102. }
  103. public override bool IsInMotion()
  104. {
  105. uint axisStatus;
  106. if( this.GetAllStatus( out axisStatus ) == EziMOTIONPlusELib.FMM_OK )
  107. {
  108. return Convert.ToBoolean( axisStatus & EziSERVO2PlusE.FFLAG_MOTIONING ) ? true : false;
  109. }
  110. return false;
  111. }
  112. public override bool IsMotionDone()
  113. {
  114. uint axisStatus;
  115. if( this.GetAllStatus( out axisStatus ) == EziMOTIONPlusELib.FMM_OK )
  116. {
  117. return Convert.ToBoolean( axisStatus & EziSERVO2PlusE.FFLAG_MOTIONING ) ? false : true;
  118. }
  119. return false;
  120. }
  121. public override bool IsOriginReturn()
  122. {
  123. uint axisStatus;
  124. if( this.GetAllStatus( out axisStatus ) == EziMOTIONPlusELib.FMM_OK )
  125. {
  126. return Convert.ToBoolean( axisStatus & EziSERVO2PlusE.FFLAG_ORIGINRETOK ) ? true : false;
  127. }
  128. return false;
  129. }
  130. public bool IsOriginReturning()
  131. {
  132. uint axisStatus;
  133. if( this.GetAllStatus( out axisStatus ) == EziMOTIONPlusELib.FMM_OK )
  134. {
  135. return Convert.ToBoolean( axisStatus & EziSERVO2PlusE.FFLAG_ORIGINRETURNING ) ? true : false;
  136. }
  137. return false;
  138. }
  139. public override bool IsServoOn()
  140. {
  141. uint axisStatus;
  142. if( this.GetAllStatus( out axisStatus ) == EziMOTIONPlusELib.FMM_OK )
  143. {
  144. return Convert.ToBoolean( axisStatus & EziSERVO2PlusE.FFLAG_SERVOON ) ? true : false;
  145. }
  146. return false;
  147. }
  148. public override bool IsServoOff()
  149. {
  150. uint axisStatus;
  151. if( this.GetAllStatus( out axisStatus ) == EziMOTIONPlusELib.FMM_OK )
  152. {
  153. return Convert.ToBoolean( axisStatus & EziSERVO2PlusE.FFLAG_SERVOON ) ? false : true;
  154. }
  155. return false;
  156. }
  157. public override bool IsAmpFault()
  158. {
  159. uint axisStatus;
  160. if( this.GetAllStatus( out axisStatus ) == EziMOTIONPlusELib.FMM_OK )
  161. {
  162. return Convert.ToBoolean( axisStatus & EziSERVO2PlusE.FFLAG_ERRORALL ) ? true : false;
  163. }
  164. return false;
  165. }
  166. public bool IsMinusLimit()
  167. {
  168. uint axisStatus;
  169. if( this.GetAllStatus( out axisStatus ) == EziMOTIONPlusELib.FMM_OK )
  170. {
  171. return Convert.ToBoolean( axisStatus & EziSERVO2PlusE.FFLAG_HWNEGALMT ) ? true : false;
  172. }
  173. return false;
  174. }
  175. public bool IsPlusLimit()
  176. {
  177. uint axisStatus;
  178. if( this.GetAllStatus( out axisStatus ) == EziMOTIONPlusELib.FMM_OK )
  179. {
  180. return Convert.ToBoolean( axisStatus & EziSERVO2PlusE.FFLAG_HWPOSILMT ) ? true : false;
  181. }
  182. return false;
  183. }
  184. /// <summary>
  185. /// Command Pos 와 Current Pos 의 차이를 확인한다.
  186. /// </summary>
  187. /// <returns>Tolenrace 범위 내 인지 확인</returns>
  188. bool IsInTolerance()
  189. {
  190. return Math.Abs( CurrentPosition - CommandPosition ) < 0.01 ? true : false;
  191. }
  192. /// <summary>
  193. /// Jog Move
  194. /// </summary>
  195. /// <param name="velocity">입력 값이 없으면 속도 5mm/sec </param>
  196. /// <param name="dir"></param>
  197. /// <returns></returns>
  198. public override int JogMove( E_JogMoveDir dir, double dVelocity = 0.0 )
  199. {
  200. uint uiVelocity = 0;
  201. if( dVelocity == 0 )
  202. uiVelocity = (uint)Convert.ToInt32( 5 / this.Config.Scale );
  203. else
  204. uiVelocity = (uint)Convert.ToInt32( dVelocity / this.Config.Scale );
  205. if( dir == E_JogMoveDir.Positive )
  206. return EziMOTIONPlusELib.FAS_MoveVelocity( Config.AxisNo, uiVelocity, EziSERVO2PlusE.DIR_INC );
  207. else
  208. return EziMOTIONPlusELib.FAS_MoveVelocity( Config.AxisNo, uiVelocity, EziSERVO2PlusE.DIR_DEC );
  209. }
  210. public override int JogMoveFast( E_JogMoveDir dir, double dFast = 0 )
  211. {
  212. uint uiVelocity = 0;
  213. if( dFast == 0 )
  214. uiVelocity = (uint)Convert.ToInt32( this.ValocityData.JogFast / this.Config.Scale );
  215. else
  216. uiVelocity = (uint)Convert.ToInt32( dFast / this.Config.Scale );
  217. if( dir == E_JogMoveDir.Positive )
  218. return EziMOTIONPlusELib.FAS_MoveVelocity( Config.AxisNo, uiVelocity, EziSERVO2PlusE.DIR_INC );
  219. else
  220. return EziMOTIONPlusELib.FAS_MoveVelocity( Config.AxisNo, uiVelocity, EziSERVO2PlusE.DIR_DEC );
  221. }
  222. public override int JogMoveSlow( E_JogMoveDir dir, double dSlow = 0 )
  223. {
  224. uint uiVelocity = 0;
  225. if( dSlow == 0 )
  226. uiVelocity = (uint)Convert.ToInt32( this.ValocityData.JogSlow / this.Config.Scale );
  227. else
  228. uiVelocity = (uint)Convert.ToInt32( dSlow / this.Config.Scale );
  229. if( dir == E_JogMoveDir.Positive )
  230. return EziMOTIONPlusELib.FAS_MoveVelocity( Config.AxisNo, uiVelocity, EziSERVO2PlusE.DIR_INC );
  231. else
  232. return EziMOTIONPlusELib.FAS_MoveVelocity( Config.AxisNo, uiVelocity, EziSERVO2PlusE.DIR_DEC );
  233. }
  234. public override int Move( double dPosition, double dVelocity = 0, int iAccelerate = 0, int iDecelerate = 0, int iMoveType = 0 )
  235. {
  236. if( !IsOriginReturn() )
  237. return -1;
  238. if( StartMove( dPosition, dVelocity, iAccelerate, iDecelerate, iMoveType ) > 0 )
  239. {
  240. }
  241. Thread.Sleep( 300 );
  242. return Wait4Done();
  243. }
  244. public override int StartMove( double dPosition, double dVelocity, int iAccelerate = 0, int iDecelerate = 0, int iMoveType = 0 )
  245. {
  246. if( !IsConnect )
  247. return -1;
  248. if( !IsServoOn() )
  249. return -1;
  250. if( iAccelerate > 0 )
  251. SetAccelTime( iAccelerate );
  252. else
  253. SetAccelTime( this.ValocityData.AutoRunAccelerate );
  254. if( iDecelerate > 0 )
  255. SetDecelTime( iDecelerate );
  256. else
  257. SetDecelTime( this.ValocityData.AutoRunDecelerate );
  258. int iPulse = Convert.ToInt32( dPosition / this.Config.Scale );
  259. uint uiVelocity = 0;
  260. if( dVelocity == 0 )
  261. {
  262. if( this.VelocityMode == E_VelocityMode.AutoMode )
  263. uiVelocity = (uint)Convert.ToInt32( this.ValocityData.AutoRunVelocity / this.Config.Scale );
  264. else
  265. uiVelocity = (uint)Convert.ToInt32( this.ValocityData.ManualRunVelocity / this.Config.Scale );
  266. }
  267. else
  268. uiVelocity = (uint)Convert.ToInt32( dVelocity / this.Config.Scale );
  269. return EziMOTIONPlusELib.FAS_MoveSingleAxisAbsPos( Config.AxisNo, iPulse, uiVelocity );
  270. }
  271. public override int Wait4Done( bool bSkipMode = false, bool bUseInterlock = false, bool bRMove = false )
  272. {
  273. var sTime = SwUtils.CurrentTimeMillis;
  274. while( !IsAxisDone() )
  275. {
  276. Thread.Sleep( 5 );
  277. if( SwUtils.Elapsed( sTime ) > 20000 )
  278. {
  279. Stop();
  280. return -1;
  281. }
  282. if( IsAmpFault() )
  283. return -3;
  284. }
  285. if( !IsInTolerance() )
  286. return -2;
  287. return 0;
  288. }
  289. public override int OriginReturn( bool bMove )
  290. {
  291. return EziMOTIONPlusELib.FAS_MoveOriginSingleAxis( Config.AxisNo );
  292. }
  293. public override int ResetAmpFault()
  294. {
  295. return EziMOTIONPlusELib.FAS_ServoAlarmReset( Config.AxisNo );
  296. }
  297. public override int RMove( double dDistance, double dVelocity = 0, int iAccelerate = 0, int iDecelerate = 0, int iMoveType = 0, bool bClearOpt = true )
  298. {
  299. throw new NotImplementedException();
  300. }
  301. public override int ServoOff()
  302. {
  303. return EziMOTIONPlusELib.FAS_ServoEnable( Config.AxisNo, 0 );
  304. }
  305. public override int ServoOn()
  306. {
  307. int result = EziMOTIONPlusELib.FAS_IsSlaveExist(Config.AxisNo);
  308. if (result != EziMOTIONPlusELib.FMM_OK)
  309. return result;
  310. return EziMOTIONPlusELib.FAS_ServoEnable( Config.AxisNo, 1 );
  311. }
  312. public override int StartRMove( double dDistance, double dVelocity = 0, int iAccelerate = 0, int iDecelerate = 0, int iMoveType = 0, bool bClearOpt = true )
  313. {
  314. throw new NotImplementedException();
  315. }
  316. public override int Stop()
  317. {
  318. return EziMOTIONPlusELib.FAS_MoveStop( Config.AxisNo );
  319. }
  320. public int EmergencyStop()
  321. {
  322. return EziMOTIONPlusELib.FAS_EmergencyStop( Config.AxisNo );
  323. }
  324. public override int StopReturnOrigin()
  325. {
  326. throw new NotImplementedException();
  327. }
  328. public override int VMove( double dVelocity = 0, int iAccelerate = 0, bool bDir = true )
  329. {
  330. return EziMOTIONPlusELib.FAS_MoveVelocity( Config.AxisNo, (uint)dVelocity, EziSERVO2PlusE.DIR_DEC );
  331. }
  332. public override int VMove( double dVelocity = 0, int waitms = 0 )
  333. {
  334. uint uiVelocity = 0;
  335. if( dVelocity == 0 )
  336. {
  337. if( this.VelocityMode == E_VelocityMode.AutoMode )
  338. uiVelocity = (uint)Convert.ToInt32( this.ValocityData.AutoRunVelocity / this.Config.Scale );
  339. else
  340. uiVelocity = (uint)Convert.ToInt32( this.ValocityData.ManualRunVelocity / this.Config.Scale );
  341. }
  342. else
  343. uiVelocity = (uint)Convert.ToInt32( dVelocity / this.Config.Scale );
  344. int result = EziMOTIONPlusELib.FAS_MoveVelocity( Config.AxisNo, uiVelocity, EziSERVO2PlusE.DIR_DEC );
  345. if( waitms == 0 )
  346. return result;
  347. return Wait4VMoveDone( waitms ); ;
  348. }
  349. public int Wait4VMoveDone( int waitTime )
  350. {
  351. var sTime = SwUtils.CurrentTimeMillis;
  352. while( !IsAxisDone() )
  353. {
  354. Thread.Sleep( 5 );
  355. if( SwUtils.Elapsed( sTime ) > waitTime )
  356. {
  357. Stop();
  358. return -1;
  359. }
  360. }
  361. Stop();
  362. return 0;
  363. }
  364. public override int VStop()
  365. {
  366. throw new NotImplementedException();
  367. }
  368. public int SetParameter( byte paramNo, int paramValue )
  369. {
  370. return EziMOTIONPlusELib.FAS_SetParameter( Config.AxisNo, paramNo, paramValue );
  371. }
  372. public int SetAccelTime( int accelTime )
  373. {
  374. return EziMOTIONPlusELib.FAS_SetParameter( Config.AxisNo, (byte)3, accelTime );
  375. }
  376. public int SetDecelTime( int decelTime )
  377. {
  378. return EziMOTIONPlusELib.FAS_SetParameter( Config.AxisNo, (byte)4, decelTime );
  379. }
  380. public int SaveAllParameters()
  381. {
  382. return EziMOTIONPlusELib.FAS_SaveAllParameters( Config.AxisNo );
  383. }
  384. }
  385. }