Aligner.cs 16 KB

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