Aligner.cs 17 KB

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