Aligner.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.RT.Event;
  5. using Aitex.Core.RT.DataCenter;
  6. using Aitex.Core.Util;
  7. using Aitex.Sorter.Common;
  8. using TSC = Aitex.Sorter.Common;
  9. using Aitex.Core.RT.Device;
  10. using MECF.Framework.Common.Equipment;
  11. using MECF.Framework.Common.SubstrateTrackings;
  12. namespace Aitex.Sorter.RT.Device.Robot
  13. {
  14. public class Aligner : BaseDevice, IDevice
  15. {
  16. public const string delimiter = "\r";
  17. public int LastErrorCode { get; set; }
  18. public int Status { get; set; }
  19. public int ErrorCode { get; set; }
  20. public int ElapseTime { get; set; }
  21. public int Notch { get; set; }
  22. public bool Initalized { get; set; }
  23. public bool Communication
  24. {
  25. get
  26. {
  27. return !_commErr;
  28. }
  29. }
  30. public bool Busy
  31. {
  32. get { return _backgroundHandler != null || _foregroundHandler != null; }
  33. }
  34. public bool Moving
  35. {
  36. get { return _backgroundHandler != null; }
  37. }
  38. public bool Error
  39. {
  40. get
  41. {
  42. return ErrorCode > 0 || _commErr;
  43. }
  44. }
  45. //public string ErrorMessage
  46. //{
  47. // get
  48. // {
  49. // return _factory.GetError(LastErrorCode);
  50. // }
  51. //}
  52. public DeviceState State
  53. {
  54. get
  55. {
  56. if (!Initalized)
  57. {
  58. return DeviceState.Unknown;
  59. }
  60. if (Error)
  61. {
  62. return DeviceState.Error;
  63. }
  64. if (Busy)
  65. return DeviceState.Busy;
  66. return DeviceState.Idle;
  67. }
  68. }
  69. public bool WaferOnAligner
  70. {
  71. get
  72. {
  73. return (Status & ((int)StateBit.WaferOnGrip | (int)StateBit.WaferOnCCD)) > 0;
  74. }
  75. }
  76. public bool StateVacuumGripStatus { get; internal set; }
  77. public bool StateWaferPresent { get; internal set; }
  78. public bool StateWaferHold { get; internal set; }
  79. private static Object _locker = new Object();
  80. private AsyncSocket _socket;
  81. private IHandler _eventHandler = null;
  82. private IHandler _backgroundHandler = null; //moving
  83. private IHandler _foregroundHandler = null; //current handler
  84. private IAlignerHandlerFactory _factory = null;
  85. private bool _commErr = false;
  86. //private bool _exceuteErr = false;
  87. private string _addr;
  88. private DeviceTimer _timerQuery = new DeviceTimer();
  89. private int _queryPeriod = 5000; //ms
  90. private bool _queryState = true;
  91. private string AlignerMotionError = "AlignerMotionError";
  92. public Aligner(string module, string name, string display, string deviceId, string address, RobotType type = RobotType.SR100)
  93. : base(module, name, display, deviceId)
  94. {
  95. _addr = address;
  96. _socket = new AsyncSocket(address);
  97. _socket.OnDataChanged += new AsyncSocket.MessageHandler(OnDataChanged);
  98. _socket.OnErrorHappened += new AsyncSocket.ErrorHandler(OnErrorHandler);
  99. switch (type)
  100. {
  101. case RobotType.NX100:
  102. _factory = new NX100.AL.HandlerFactory();
  103. break;
  104. default:
  105. _factory = new SR100.AL.HandlerFactory();
  106. break;
  107. }
  108. _eventHandler = _factory.Event();
  109. Initalized = false;
  110. _commErr = false;
  111. WaferManager.Instance.SubscribeLocation(name, 1);
  112. }
  113. public bool Initialize()
  114. {
  115. _socket.Connect(this._addr);
  116. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.AlignerInit), (out string reason, int time, object[] param) =>
  117. {
  118. bool ret = Init(out reason);
  119. if (ret)
  120. {
  121. reason = string.Format("{0} {1}", Name, "Init");
  122. return true;
  123. }
  124. return false;
  125. });
  126. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.AlignerHome), (out string reason, int time, object[] param) =>
  127. {
  128. bool ret = Home(out reason);
  129. if (ret)
  130. {
  131. reason = string.Format("{0} {1}", Name, "Home");
  132. return true;
  133. }
  134. return false;
  135. });
  136. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.AlignerReset), (out string reason, int time, object[] param) =>
  137. {
  138. bool ret = Clear(out reason);
  139. if (ret)
  140. {
  141. reason = string.Format("{0} {1}", Name, "Reset Error");
  142. return true;
  143. }
  144. return false;
  145. });
  146. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.AlignerGrip), (out string reason, int time, object[] param) =>
  147. {
  148. bool ret = Grip(0,out reason);
  149. if (ret)
  150. {
  151. reason = string.Format("{0} {1}", Name, "Hold Wafer");
  152. return true;
  153. }
  154. return false;
  155. });
  156. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.AlignerRelease), (out string reason, int time, object[] param) =>
  157. {
  158. bool ret = Release(0, out reason);
  159. if (ret)
  160. {
  161. reason = string.Format("{0} {1}", Name, "Release Wafer");
  162. return true;
  163. }
  164. return false;
  165. });
  166. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.AlignerLiftUp), (out string reason, int time, object[] param) =>
  167. {
  168. bool ret = LiftUp(out reason);
  169. if (ret)
  170. {
  171. reason = string.Format("{0} {1}", Name, "Lifter Up");
  172. return true;
  173. }
  174. return false;
  175. });
  176. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.AlignerLiftDown), (out string reason, int time, object[] param) =>
  177. {
  178. bool ret = LiftDown(out reason);
  179. if (ret)
  180. {
  181. reason = string.Format("{0} {1}", Name, "Lifter Down");
  182. return true;
  183. }
  184. return false;
  185. });
  186. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.AlignerStop), (out string reason, int time, object[] param) =>
  187. {
  188. bool ret = Stop(out reason);
  189. if (ret)
  190. {
  191. reason = string.Format("{0} {1}", Name, "Stop Align");
  192. return true;
  193. }
  194. return false;
  195. });
  196. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.AlignerAlign), (out string reason, int time, object[] param) =>
  197. {
  198. double angle = double.Parse((string)param[0]);
  199. bool ret = Align(angle, out reason);
  200. if (ret)
  201. {
  202. reason = string.Format("{0} {1}", Name, "PreAlign");
  203. return true;
  204. }
  205. return false;
  206. });
  207. DATA.Subscribe(ModuleName.Aligner.ToString(), ParamName.AlignerState, () => State);
  208. DATA.Subscribe(ModuleName.Aligner.ToString(), ParamName.AlignerBusy, () => Busy);
  209. DATA.Subscribe(ModuleName.Aligner.ToString(), ParamName.AlignerError, () => ErrorCode);
  210. DATA.Subscribe(ModuleName.Aligner.ToString(), ParamName.AlignerElapseTime, () => ElapseTime);
  211. DATA.Subscribe(ModuleName.Aligner.ToString(), ParamName.AlignerNotch, () => Notch);
  212. DATA.Subscribe(ModuleName.Aligner.ToString(), ParamName.WaferOnAligner, () => WaferOnAligner);
  213. EV.Subscribe(new EventItem("Event", AlignerMotionError, "Aligner error", EventLevel.Alarm, Aitex.Core.RT.Event.EventType.HostNotification));
  214. return true;
  215. }
  216. public void Terminate()
  217. {
  218. _socket.Dispose();
  219. }
  220. public void Monitor()
  221. {
  222. object maintenance = DATA.Poll(ModuleName.System.ToString(), ParamName.IsMaintenanceMode);
  223. bool bTrigger = maintenance==null || (bool)maintenance;
  224. if (bTrigger && _timerQuery.IsTimeout())
  225. {
  226. if (!Busy)
  227. {
  228. string reason = string.Empty;
  229. if (_queryState)
  230. QueryState(out reason);
  231. _queryState = !_queryState;
  232. _timerQuery.Start(_queryPeriod);
  233. }
  234. }
  235. }
  236. public void Reset()
  237. {
  238. lock (_locker)
  239. {
  240. _foregroundHandler = null;
  241. _backgroundHandler = null;
  242. }
  243. //_exceuteErr = false;
  244. if (_commErr)
  245. {
  246. _commErr = false;
  247. _socket.Connect(this._addr);
  248. }
  249. }
  250. #region Command
  251. public bool Init(out string reason)
  252. {
  253. reason = string.Empty;
  254. return execute(_factory.Init(), out reason);
  255. }
  256. public bool Home(out string reason)
  257. {
  258. reason = string.Empty;
  259. return execute(_factory.Home(), out reason);
  260. }
  261. public bool Clear(out string reason)
  262. {
  263. reason = string.Empty;
  264. return execute(_factory.Clear(), out reason);
  265. }
  266. public bool Grip(Hand hand, out string reason)
  267. {
  268. reason = string.Empty;
  269. return execute(_factory.Grip(hand), out reason);
  270. }
  271. public bool Release(Hand hand, out string reason)
  272. {
  273. reason = string.Empty;
  274. return execute(_factory.Release(hand), out reason);
  275. }
  276. public bool LiftUp(out string reason)
  277. {
  278. reason = string.Empty;
  279. return execute(_factory.LiftUp(), out reason);
  280. }
  281. public bool LiftDown(out string reason)
  282. {
  283. reason = string.Empty;
  284. return execute(_factory.LiftDown(), out reason);
  285. }
  286. public bool Stop(out string reason)
  287. {
  288. reason = string.Empty;
  289. lock (_locker)
  290. {
  291. _foregroundHandler = null;
  292. _backgroundHandler = null;
  293. }
  294. return execute(_factory.Stop(), out reason);
  295. }
  296. public bool Align(double angle, out string reason)
  297. {
  298. reason = string.Empty;
  299. return execute(_factory.Align(angle), out reason);
  300. }
  301. public bool QueryState(out string reason)
  302. {
  303. reason = string.Empty;
  304. return execute(_factory.QueryState(), out reason);
  305. }
  306. #endregion
  307. private bool execute(IHandler handler, out string reason)
  308. {
  309. reason = string.Empty;
  310. lock (_locker)
  311. {
  312. if (_foregroundHandler != null)
  313. {
  314. reason = "System busy, please wait or reset system.";
  315. EV.PostMessage(Name, EventEnum.DefaultWarning, string.Format("{0} {1} {2}", this.DeviceID, handler.ToString(), reason));
  316. return false;
  317. }
  318. if (_backgroundHandler != null && handler.IsBackground)
  319. {
  320. reason = "System busy,one background command is running, please wait or reset system.";
  321. EV.PostMessage(Name, EventEnum.DefaultWarning, string.Format("{0} {1} {2}", this.DeviceID, handler.ToString(), reason));
  322. return false;
  323. }
  324. handler.Unit = (int)Unit.Aligner;
  325. if(!handler.Execute(ref _socket))
  326. {
  327. reason = "Communication error,please check it.";
  328. return false;
  329. }
  330. if (handler.IsBackground)
  331. _backgroundHandler = handler;
  332. else
  333. _foregroundHandler = handler;
  334. }
  335. return true;
  336. }
  337. private void OnDataChanged(string package)
  338. {
  339. try
  340. {
  341. package = package.ToUpper();
  342. string[] msgs = Regex.Split(package, delimiter);
  343. foreach (string msg in msgs)
  344. {
  345. if (msg.Length > 0)
  346. {
  347. bool completed = false;
  348. string resp = msg;
  349. lock (_locker)
  350. {
  351. if (_foregroundHandler != null && _foregroundHandler.OnMessage(ref _socket, resp, out completed))
  352. {
  353. _foregroundHandler = null;
  354. }
  355. else if (_backgroundHandler != null && _backgroundHandler.OnMessage(ref _socket, resp, out completed))
  356. {
  357. if (completed)
  358. {
  359. string reason = string.Empty;
  360. QueryState(out reason);
  361. _backgroundHandler = null;
  362. }
  363. }
  364. else
  365. {
  366. if (_eventHandler != null)
  367. {
  368. if (_eventHandler.OnMessage(ref _socket, resp, out completed))
  369. {
  370. if (completed)
  371. {
  372. EV.PostMessage("Aligner", EventEnum.DefaultWarning, string.Format(" has error. {0:X}", ErrorCode));
  373. OnError($"Aligner occurred error code {ErrorCode}.");
  374. }
  375. }
  376. }
  377. }
  378. }
  379. }
  380. }
  381. }
  382. catch (ExcuteFailedException e)
  383. {
  384. EV.PostMessage("Aligner", EventEnum.DefaultWarning, string.Format("executed failed. {0}", e.Message));
  385. OnError("Aligner Execution failure.");
  386. }
  387. catch (InvalidPackageException e)
  388. {
  389. EV.PostMessage("Aligner", EventEnum.DefaultWarning, string.Format("receive invalid package. {0}", e.Message));
  390. OnError("Received invilid message from Aligner.");
  391. }
  392. catch (System.Exception ex)
  393. {
  394. _commErr = true;
  395. LOG.Write("Aligner failed:" + ex.ToString());
  396. }
  397. }
  398. private void OnErrorHandler(ErrorEventArgs args)
  399. {
  400. Initalized = false;
  401. _commErr = true;
  402. EV.PostMessage(Module, EventEnum.CommunicationError, Display, args.Reason);
  403. OnError("Aligner communication error.");
  404. //LOG.Error(string.Format("{0} Communication failed, {1}", Name, args.Reason));
  405. }
  406. //private bool checkslot(int min, int max, int slot)
  407. //{
  408. // return slot >= min && slot < max;
  409. //}
  410. private void OnError(string error)
  411. {
  412. EV.Notify(AlignerMotionError,new SerializableDictionary<string, object>
  413. {
  414. {"AlarmText",error }
  415. });
  416. }
  417. }
  418. }