Robot.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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 Aitex.Core.RT.SCCore;
  11. using MECF.Framework.Common.Communications;
  12. using MECF.Framework.Common.Equipment;
  13. using MECF.Framework.Common.SubstrateTrackings;
  14. namespace Aitex.Sorter.RT.Device.Robot
  15. {
  16. public enum RobotType
  17. {
  18. SR100,
  19. NX100,
  20. }
  21. public class Robot : BaseDevice, IDevice
  22. {
  23. public event Action<ModuleName, string> OnSlotMapRead;
  24. public const string delimiter = "\r";
  25. public int LastErrorCode { get; set; }
  26. public int Status { get; set; }
  27. public int ErrorCode { get; set; }
  28. public int ElapseTime { get; set; }
  29. public int Speed { get; set; }
  30. public int Rotation { get; set; }
  31. public int Extension { get; set; }
  32. public int Wrist1 { get; set; }
  33. public int Wrist2 { get; set; }
  34. public int Evevation { get; set; }
  35. public bool StateWaferOnBlade1 { get; set; }
  36. public bool StateWaferOnBlade2 { get; set; }
  37. public bool StateBlade1Gripped { get; set; }
  38. public bool StateBlade2Gripped { get; set; }
  39. public bool StateInterlock1 { get; set; }
  40. public bool StateInterlock2 { get; set; }
  41. public bool StateInterlock3 { get; set; }
  42. public bool StateInterlock4 { get; set; }
  43. public bool StateInterlock5 { get; set; }
  44. public bool StateInterlock6 { get; set; }
  45. public bool StateInterlock7 { get; set; }
  46. public bool StateInterlock8 { get; set; }
  47. public bool Swap { get; set; }
  48. public Hand PlaceBalde { get; set; }
  49. public ModuleName Blade1Target
  50. {
  51. get;
  52. set;
  53. }
  54. public ModuleName Blade2Target { get; set; }
  55. public bool Blade1Enable {
  56. get
  57. {
  58. return SC.GetValue<bool>("Robot.Blade1Enable");
  59. }
  60. set
  61. {
  62. SC.SetItemValue("Robot.Blade1Enable" ,value);
  63. }
  64. }
  65. public bool Blade2Enable
  66. {
  67. get
  68. {
  69. return SC.GetValue<bool>("Robot.Blade2Enable");
  70. }
  71. set
  72. {
  73. SC.SetItemValue("Robot.Blade2Enable", value);
  74. }
  75. }
  76. public int Blade2Slots
  77. {
  78. get
  79. {
  80. int value = SC.GetValue<int>("Robot.Blade2Slots");
  81. if (value == 0)
  82. value = 1;
  83. return value;
  84. }
  85. }
  86. public bool Initalized { get; set; }
  87. public bool Communication
  88. {
  89. get
  90. {
  91. return !_commErr;
  92. }
  93. }
  94. public bool Busy
  95. {
  96. get { return _backgroundHandler != null || _foregroundHandler != null; }
  97. }
  98. public bool Moving
  99. {
  100. get { return _backgroundHandler != null; }
  101. }
  102. public bool Error
  103. {
  104. get
  105. {
  106. return ErrorCode > 0 || _commErr || _exceuteErr;
  107. }
  108. }
  109. public DeviceState State
  110. {
  111. get
  112. {
  113. if (!Initalized)
  114. {
  115. return DeviceState.Unknown;
  116. }
  117. if (Error)
  118. {
  119. return DeviceState.Error;
  120. }
  121. if (Busy)
  122. return DeviceState.Busy;
  123. return DeviceState.Idle;
  124. }
  125. }
  126. //public string ErrorMessage
  127. //{
  128. // get
  129. // {
  130. // return _factory.GetError(LastErrorCode);
  131. // }
  132. //}
  133. public bool WaferOnBlade1 { get { return (Status & (int)StateBit.WaferOnBlade1) > 0; } }
  134. public bool WaferOnBlade2 { get { return (Status & (int)StateBit.WaferOnBlade2) > 0; } }
  135. public Hand TransferBlade {
  136. get
  137. {
  138. if (!Blade1Enable) return Hand.Blade2;
  139. if (!Blade2Enable) return Hand.Blade1;
  140. return Hand.Both;
  141. }
  142. }
  143. private static Object _locker = new Object();
  144. private AsyncSocket _socket;
  145. private IHandler _eventHandler = null;
  146. private IHandler _backgroundHandler = null; //moving
  147. private IHandler _foregroundHandler = null; //current handler
  148. private IHandlerFactory _factory = null;
  149. private DeviceTimer _timerQuery = new DeviceTimer();
  150. private int _queryPeriod = 5000; //ms
  151. private bool _queryState = true;
  152. private bool _exceuteErr = false;
  153. private bool _commErr = false;
  154. private string _addr;
  155. private string AlarmRobotError = "RobotError";
  156. public Robot(string module, string name, string display, string deviceId, string address, RobotType type = RobotType.SR100)
  157. : base(module, name, display, deviceId)
  158. {
  159. _addr = address;
  160. _socket = new AsyncSocket(address);
  161. _socket.OnDataChanged += new AsyncSocket.MessageHandler(OnDataChanged);
  162. _socket.OnErrorHappened += new AsyncSocket.ErrorHandler(OnErrorHandler);
  163. switch (type)
  164. {
  165. case RobotType.NX100:
  166. _factory = new NX100.HandlerFactory();
  167. break;
  168. default:
  169. _factory = new SR100.HandlerFactory();
  170. break;
  171. }
  172. Initalized = false;
  173. WaferManager.Instance.SubscribeLocation(name, 1+ Blade2Slots);
  174. }
  175. public bool Initialize()
  176. {
  177. _commErr = false;
  178. _socket.Connect(this._addr);
  179. DEVICE.Register(String.Format("{0}.{1}", ModuleName.Robot.ToString(), TSC.DeviceOperationName.RobotInit), (out string reason, int time, object[] param) =>
  180. {
  181. bool ret = Init(out reason);
  182. if (ret)
  183. {
  184. reason = string.Format("{0} {1}", Name, "Initializing");
  185. return true;
  186. }
  187. return false;
  188. });
  189. DEVICE.Register(String.Format("{0}.{1}", ModuleName.Robot.ToString(), TSC.DeviceOperationName.RobotHome), (out string reason, int time, object[] param) =>
  190. {
  191. bool ret = Home(out reason);
  192. if (ret)
  193. {
  194. reason = string.Format("{0} {1}", Name, "Homing");
  195. return true;
  196. }
  197. return false;
  198. });
  199. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.RobotReset), (out string reason, int time, object[] param) =>
  200. {
  201. bool ret = Clear(out reason);
  202. if (ret)
  203. {
  204. reason = string.Format("{0} {1}", Name, "Reset");
  205. return true;
  206. }
  207. return false;
  208. });
  209. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.RobotGrip), (out string reason, int time, object[] param) =>
  210. {
  211. Hand hand = (Hand)Enum.Parse(typeof(Hand), (string)param[0], true);
  212. bool ret = Grip(hand,out reason);
  213. //bool ret = WaferMapping(ModuleName.LP1, out reason);
  214. if (ret)
  215. {
  216. reason = string.Format("{0} {1}", Name, "Grip");
  217. return true;
  218. }
  219. return false;
  220. });
  221. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.RobotRelease), (out string reason, int time, object[] param) =>
  222. {
  223. Hand hand = (Hand)Enum.Parse(typeof(Hand), (string)param[0], true);
  224. bool ret = Release(hand, out reason);
  225. //bool ret = QueryWaferMap(ModuleName.LP1, out reason);
  226. if (ret)
  227. {
  228. reason = string.Format("{0} {1}", Name, "Release");
  229. return true;
  230. }
  231. return false;
  232. });
  233. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.RobotStop), (out string reason, int time, object[] param) =>
  234. {
  235. bool ret = Stop(false, out reason);
  236. if (ret)
  237. {
  238. reason = string.Format("{0} {1}", Name, "Stopping");
  239. return true;
  240. }
  241. return false;
  242. });
  243. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.RobotPick), (out string reason, int time, object[] param) =>
  244. {
  245. ModuleName chamber = (ModuleName)Enum.Parse(typeof(ModuleName), (string)param[0], true);
  246. int slot = int.Parse((string)param[1]);
  247. Hand hand = (Hand)Enum.Parse(typeof(Hand), (string)param[2], true);
  248. bool ret = Pick(chamber, slot, hand, out reason);
  249. if (ret)
  250. {
  251. reason = string.Format("{0} {1}", Name, "Pick wafer");
  252. return true;
  253. }
  254. return false;
  255. });
  256. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.RobotPlace), (out string reason, int time, object[] param) =>
  257. {
  258. ModuleName chamber = (ModuleName)Enum.Parse(typeof(ModuleName), (string)param[0], true);
  259. int slot = int.Parse((string)param[1]);
  260. Hand hand = (Hand)Enum.Parse(typeof(Hand), (string)param[2], true);
  261. bool ret = Place(chamber, slot, hand, out reason);
  262. if (ret)
  263. {
  264. reason = string.Format("{0} {1}", Name, "Place wafer");
  265. return true;
  266. }
  267. return false;
  268. });
  269. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.RobotExchane), (out string reason, int time, object[] param) =>
  270. {
  271. ModuleName chamber = (ModuleName)Enum.Parse(typeof(ModuleName), (string)param[0], true);
  272. int slot = int.Parse((string)param[1]);
  273. Hand hand = (Hand)Enum.Parse(typeof(Hand), (string)param[2], true);
  274. bool ret = Exchange(chamber, slot, hand, out reason);
  275. if (ret)
  276. {
  277. reason = string.Format("{0} {1}", Name, "Swap wafer");
  278. return true;
  279. }
  280. return false;
  281. });
  282. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.RobotGoto), (out string reason, int time, object[] param) =>
  283. {
  284. ModuleName chamber = (ModuleName)Enum.Parse(typeof(ModuleName), (string)param[0], true);
  285. int slot = int.Parse((string)param[1]);
  286. Hand hand = (Hand)Enum.Parse(typeof(Hand), (string)param[2], true);
  287. Motion next = (Motion)Enum.Parse(typeof(Motion), (string)param[3], true);
  288. int x = int.Parse((string)param[4]);
  289. int y = int.Parse((string)param[5]);
  290. int z = int.Parse((string)param[6]);
  291. bool ret = Goto(chamber, slot, next, hand, x,y,z, out reason);
  292. if (ret)
  293. {
  294. reason = string.Format("{0}{1}", Name, "Goto");
  295. return true;
  296. }
  297. return false;
  298. });
  299. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.RobotBladeEnable), (out string reason, int time, object[] param) =>
  300. {
  301. Hand hand = (Hand)Enum.Parse(typeof(Hand), (string)param[0], true);
  302. bool bEnable = bool.Parse((string)param[1]);
  303. bool ret = Enable(hand, bEnable, out reason);
  304. if (ret)
  305. {
  306. reason = string.Format("{0}{1}", Name, "Disable robot arm");
  307. return true;
  308. }
  309. return false;
  310. });
  311. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotState, () => State);
  312. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotBusy, () => Busy);
  313. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotError, () => ErrorCode);
  314. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotBlade1Traget, () => Blade1Target);
  315. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotBlade2Traget, () => Blade2Target);
  316. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotBlade1Enabled, () => Blade1Enable);
  317. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotBlade2Enabled, () => Blade2Enable);
  318. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotSwap, () => Swap);
  319. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotSwapPlaceBalde, () => PlaceBalde);
  320. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotSpeed, () => Speed);
  321. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotPosRotationAxis, () => Rotation);
  322. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotPosExtensionAxis, () => Extension);
  323. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotPosWrist1Axis, () => Wrist1);
  324. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotPosWrist2Axis, () => Wrist2);
  325. DATA.Subscribe(ModuleName.Robot.ToString(), ParamName.RobotPosEvevationAxis, () => Evevation);
  326. EV.Subscribe(new EventItem("Event", AlarmRobotError, "Robot error", EventLevel.Alarm, Aitex.Core.RT.Event.EventType.HostNotification));
  327. Reset();
  328. string str = string.Empty;
  329. _eventHandler = _factory.Event();
  330. return true;
  331. }
  332. public void Terminate()
  333. {
  334. }
  335. public void Monitor()
  336. {
  337. var isMaintenance = DATA.Poll("System.IsMaintenanceMode");
  338. bool bTrigger = isMaintenance!=null && (bool)isMaintenance;
  339. if (bTrigger && _timerQuery.IsTimeout())
  340. {
  341. if (!Busy)
  342. {
  343. string reason = string.Empty;
  344. if (_queryState)
  345. QueryState(out reason);
  346. _queryState = !_queryState;
  347. _timerQuery.Start(_queryPeriod);
  348. }
  349. }
  350. }
  351. public void Reset()
  352. {
  353. lock (_locker)
  354. {
  355. _foregroundHandler = null;
  356. _backgroundHandler = null;
  357. }
  358. _exceuteErr = false;
  359. if (_commErr)
  360. {
  361. _commErr = false;
  362. _socket.Connect(this._addr);
  363. }
  364. Swap = false;
  365. }
  366. public bool Enable(Hand hand, bool bEnable, out string reason)
  367. {
  368. reason = string.Empty;
  369. if (hand == Hand.Blade1)
  370. {
  371. Blade2Enable = bEnable;
  372. }
  373. else if (hand == Hand.Blade2)
  374. {
  375. Blade2Enable = bEnable;
  376. }
  377. else
  378. {
  379. reason = "Can't disable all blade";
  380. return false;
  381. }
  382. return true;
  383. }
  384. #region Command
  385. public bool Init(out string reason)
  386. {
  387. lock (_locker)
  388. {
  389. _foregroundHandler = null;
  390. _backgroundHandler = null;
  391. }
  392. reason = string.Empty;
  393. return execute(_factory.Init(), out reason);
  394. }
  395. public bool Home(out string reason)
  396. {
  397. lock (_locker)
  398. {
  399. _foregroundHandler = null;
  400. _backgroundHandler = null;
  401. }
  402. reason = string.Empty;
  403. return execute(_factory.Home(), out reason);
  404. }
  405. public bool Grip(Hand hand,out string reason)
  406. {
  407. reason = string.Empty;
  408. return execute(_factory.Grip(hand), out reason);
  409. }
  410. public bool Release(Hand hand, out string reason)
  411. {
  412. reason = string.Empty;
  413. return execute(_factory.Release(hand), out reason);
  414. }
  415. public bool WaferMapping(ModuleName loadport, out string reason)
  416. {
  417. reason = string.Empty;
  418. return execute(_factory.WaferMapping(loadport), out reason);
  419. }
  420. public bool QueryWaferMap(ModuleName loadport, out string reason)
  421. {
  422. reason = string.Empty;
  423. return execute(_factory.QueryWaferMap(loadport), out reason);
  424. }
  425. public bool QueryState(out string reason)
  426. {
  427. reason = string.Empty;
  428. return execute(_factory.QueryState(), out reason);
  429. }
  430. public bool QueryPosition(out string reason)
  431. {
  432. reason = string.Empty;
  433. return execute(_factory.QueryPosition(), out reason);
  434. }
  435. public bool Clear(out string reason)
  436. {
  437. reason = string.Empty;
  438. return execute(_factory.Clear(), out reason);
  439. }
  440. public bool Stop(bool isEmergency, out string reason)
  441. {
  442. reason = string.Empty;
  443. lock (_locker)
  444. {
  445. _foregroundHandler = null;
  446. _backgroundHandler = null;
  447. }
  448. return execute(_factory.Stop(isEmergency), out reason);
  449. }
  450. public bool Resume( out string reason)
  451. {
  452. reason = string.Empty;
  453. return execute(_factory.Resume(), out reason);
  454. }
  455. public bool SetSpeed(int speed, out string reason)
  456. {
  457. reason = string.Empty;
  458. return execute(_factory.SetSpeed(speed), out reason);
  459. }
  460. public bool Goto(ModuleName chamber, int slot, Motion motion,Hand hand, int x, int y, int z,out string reason)
  461. {
  462. reason = string.Empty;
  463. if (chamber == ModuleName.Robot)
  464. {
  465. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  466. return false;
  467. }
  468. if (ModuleHelper.IsLoadPort(chamber))
  469. {
  470. if (hand == Hand.Both)
  471. {
  472. if (!checkslot(0, 25 - Blade2Slots, slot)) //0-20 | 20,21,22,23,24
  473. {
  474. reason = string.Format("Pick invalid parameter,double hand, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  475. return false;
  476. }
  477. }
  478. else if(hand == Hand.Blade2)
  479. {
  480. if (!checkslot(0, 25 - Blade2Slots + 1, slot)) //0 - 21| 21,22,23,24
  481. {
  482. reason = string.Format("Pick invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  483. return false;
  484. }
  485. }
  486. else
  487. {
  488. if (!checkslot(0, 25, slot))
  489. {
  490. reason = string.Format("Pick invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  491. return false;
  492. }
  493. }
  494. }
  495. else
  496. {
  497. if (hand == Hand.Both)
  498. {
  499. reason = string.Format("Pick invalid parameter, double pick,chamber is {0}, slot is {1}", chamber.ToString(), slot);
  500. return false;
  501. }
  502. }
  503. return execute(_factory.Goto( chamber, slot, motion, hand, x,y,z), out reason);
  504. }
  505. public bool MoveTo(ModuleName chamber, int slot, Hand hand, bool isPick, int x, int y, int z,out string reason)
  506. {
  507. reason = string.Empty;
  508. if (chamber == ModuleName.Robot)
  509. {
  510. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  511. return false;
  512. }
  513. if (ModuleHelper.IsLoadPort(chamber))
  514. {
  515. if (hand == Hand.Both)
  516. {
  517. if (!checkslot(0, 25 - Blade2Slots, slot)) //0-20 | 20,21,22,23,24
  518. {
  519. reason = string.Format("Pick invalid parameter,double hand, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  520. return false;
  521. }
  522. }
  523. else if(hand == Hand.Blade2)
  524. {
  525. if (!checkslot(0, 25 - Blade2Slots + 1, slot)) //0 - 21| 21,22,23,24
  526. {
  527. reason = string.Format("Pick invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  528. return false;
  529. }
  530. }
  531. else
  532. {
  533. if (!checkslot(0, 25, slot))
  534. {
  535. reason = string.Format("Pick invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  536. return false;
  537. }
  538. }
  539. }
  540. else
  541. {
  542. if (hand == Hand.Both)
  543. {
  544. reason = string.Format("Pick invalid parameter, double pick,chamber is {0}, slot is {1}", chamber.ToString(), slot);
  545. return false;
  546. }
  547. }
  548. return execute(_factory.MoveTo( chamber, slot, hand, isPick, x,y,z), out reason);
  549. }
  550. //slot:0 based
  551. public bool Pick(ModuleName chamber, int slot, Hand hand, out string reason)
  552. {
  553. reason = string.Empty;
  554. if (chamber == ModuleName.Robot)
  555. {
  556. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  557. return false;
  558. }
  559. if (ModuleHelper.IsLoadPort(chamber))
  560. {
  561. if (hand == Hand.Both)
  562. {
  563. if (!checkslot(0, 25 - Blade2Slots, slot)) //0-20 | 20,21,22,23,24
  564. {
  565. reason = string.Format("Pick invalid parameter,double hand, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  566. return false;
  567. }
  568. }
  569. else if (hand == Hand.Blade2)
  570. {
  571. if (!checkslot(0, 25 - Blade2Slots + 1, slot)) //0 - 21| 21,22,23,24
  572. {
  573. reason = string.Format("Pick invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  574. return false;
  575. }
  576. }
  577. else
  578. {
  579. if (!checkslot(0, 25, slot))
  580. {
  581. reason = string.Format("Pick invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  582. return false;
  583. }
  584. }
  585. }
  586. else if(ModuleHelper.IsBuffer(chamber))
  587. {
  588. if (hand == Hand.Both)
  589. {
  590. if (!checkslot(0, 6 - Blade2Slots, slot)) //0-20 | 20,21,22,23,24
  591. {
  592. reason = string.Format("Pick invalid parameter,double hand, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  593. return false;
  594. }
  595. }
  596. else if (hand == Hand.Blade2)
  597. {
  598. if (!checkslot(0, 6 - Blade2Slots + 1, slot)) //0 - 21| 21,22,23,24
  599. {
  600. reason = string.Format("Pick invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  601. return false;
  602. }
  603. }
  604. else
  605. {
  606. if (!checkslot(0, 6, slot))
  607. {
  608. reason = string.Format("Pick invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  609. return false;
  610. }
  611. }
  612. }
  613. else
  614. {
  615. if (hand == Hand.Both)
  616. {
  617. reason = string.Format("Pick invalid parameter, double pick,chamber is {0}, slot is {1}", chamber.ToString(), slot);
  618. return false;
  619. }
  620. }
  621. return execute(_factory.Pick(chamber, slot, hand), out reason);
  622. }
  623. public bool PickExtend(ModuleName chamber, int slot, Hand hand, out string reason)
  624. {
  625. reason = string.Empty;
  626. if (chamber == ModuleName.Robot)
  627. {
  628. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  629. return false;
  630. }
  631. if (ModuleHelper.IsLoadPort(chamber))
  632. {
  633. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  634. return false;
  635. }
  636. if (ModuleHelper.IsBuffer(chamber))
  637. {
  638. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  639. return false;
  640. }
  641. if (hand == Hand.Both)
  642. {
  643. reason = string.Format("Place invalid parameter, double place,chamber is {0}, slot is {1}", chamber.ToString(), slot);
  644. return false;
  645. }
  646. return execute(_factory.PickExtend(chamber, slot, hand), out reason);
  647. }
  648. public bool PickRetract(ModuleName chamber, int slot, Hand hand, out string reason)
  649. {
  650. reason = string.Empty;
  651. if (chamber == ModuleName.Robot)
  652. {
  653. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  654. return false;
  655. }
  656. if (ModuleHelper.IsLoadPort(chamber))
  657. {
  658. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  659. return false;
  660. }
  661. if (ModuleHelper.IsBuffer(chamber))
  662. {
  663. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  664. return false;
  665. }
  666. if (hand == Hand.Both)
  667. {
  668. reason = string.Format("Place invalid parameter, double place,chamber is {0}, slot is {1}", chamber.ToString(), slot);
  669. return false;
  670. }
  671. return execute(_factory.PickRetract(chamber, slot, hand), out reason);
  672. }
  673. public bool Place(ModuleName chamber, int slot, Hand hand, out string reason)
  674. {
  675. reason = string.Empty;
  676. if (chamber == ModuleName.Robot)
  677. {
  678. reason = string.Format("Place invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  679. return false;
  680. }
  681. if (ModuleHelper.IsLoadPort(chamber))
  682. {
  683. if (hand == Hand.Both)
  684. {
  685. if (!checkslot(0, 25 - Blade2Slots, slot)) //0-20 | 20,21,22,23,24
  686. {
  687. reason = string.Format("Place invalid parameter,double hand, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  688. return false;
  689. }
  690. }
  691. else if (hand == Hand.Blade2)
  692. {
  693. if (!checkslot(0, 25 - Blade2Slots + 1, slot)) //0 - 21| 21,22,23,24
  694. {
  695. reason = string.Format("Pick invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  696. return false;
  697. }
  698. }
  699. else
  700. {
  701. if (!checkslot(0, 25, slot))
  702. {
  703. reason = string.Format("Place invalid parameter,place, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  704. return false;
  705. }
  706. }
  707. }
  708. else if (ModuleHelper.IsBuffer(chamber))
  709. {
  710. if (hand == Hand.Both)
  711. {
  712. if (!checkslot(0, 6 - Blade2Slots, slot)) //0-6
  713. {
  714. reason = string.Format("Place invalid parameter,double hand, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  715. return false;
  716. }
  717. }
  718. else if (hand == Hand.Blade2)
  719. {
  720. if (!checkslot(0, 6 - Blade2Slots + 1, slot)) //0 - 6
  721. {
  722. reason = string.Format("Pick invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  723. return false;
  724. }
  725. }
  726. else
  727. {
  728. if (!checkslot(0, 6, slot))
  729. {
  730. reason = string.Format("Place invalid parameter,place, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  731. return false;
  732. }
  733. }
  734. }
  735. else
  736. {
  737. if (hand == Hand.Both)
  738. {
  739. reason = string.Format("Place invalid parameter, double place,chamber is {0}, slot is {1}", chamber.ToString(), slot);
  740. return false;
  741. }
  742. }
  743. return execute(_factory.Place(chamber, slot, hand), out reason);
  744. }
  745. public bool PlaceExtend(ModuleName chamber, int slot, Hand hand, out string reason)
  746. {
  747. reason = string.Empty;
  748. if (chamber == ModuleName.Robot)
  749. {
  750. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  751. return false;
  752. }
  753. if (ModuleHelper.IsLoadPort(chamber))
  754. {
  755. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  756. return false;
  757. }
  758. if (ModuleHelper.IsBuffer(chamber))
  759. {
  760. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  761. return false;
  762. }
  763. if (hand == Hand.Both)
  764. {
  765. reason = string.Format("Place invalid parameter, double place,chamber is {0}, slot is {1}", chamber.ToString(), slot);
  766. return false;
  767. }
  768. return execute(_factory.PlaceExtend(chamber, slot, hand), out reason);
  769. }
  770. public bool PlaceRetract(ModuleName chamber, int slot, Hand hand, out string reason)
  771. {
  772. reason = string.Empty;
  773. if (chamber == ModuleName.Robot)
  774. {
  775. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  776. return false;
  777. }
  778. if (ModuleHelper.IsLoadPort(chamber))
  779. {
  780. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  781. return false;
  782. }
  783. if (ModuleHelper.IsBuffer(chamber))
  784. {
  785. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  786. return false;
  787. }
  788. if (hand == Hand.Both)
  789. {
  790. reason = string.Format("Place invalid parameter, double place,chamber is {0}, slot is {1}", chamber.ToString(), slot);
  791. return false;
  792. }
  793. return execute(_factory.PlaceRetract(chamber, slot, hand), out reason);
  794. }
  795. public bool Exchange(ModuleName chamber, int slot, Hand hand, out string reason)
  796. {
  797. reason = string.Empty;
  798. if (chamber == ModuleName.Robot)
  799. {
  800. reason = string.Format("Exchange invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  801. return false;
  802. }
  803. if (Blade2Slots > 1)
  804. {
  805. reason = string.Format("this robot don't support exchange operation.");
  806. return false;
  807. }
  808. if (hand == Hand.Both)
  809. {
  810. reason = string.Format("Exchange invalid parameter,double hand");
  811. return false;
  812. }
  813. if (ModuleHelper.IsLoadPort(chamber))
  814. {
  815. if (!checkslot(0, 25, slot))
  816. {
  817. reason = string.Format("Exchange invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  818. return false;
  819. }
  820. }
  821. return execute(_factory.Exchange(chamber, slot, hand), out reason);
  822. }
  823. public bool PickEx(ModuleName chamber, int slot, Hand hand, int x, int y, int z, out string reason)
  824. {
  825. reason = string.Empty;
  826. if (chamber == ModuleName.Robot)
  827. {
  828. reason = string.Format("Pick invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  829. return false;
  830. }
  831. if (ModuleHelper.IsLoadPort(chamber))
  832. {
  833. if (hand == Hand.Both)
  834. {
  835. if (!checkslot(0, 25 - Blade2Slots, slot)) //0-20 | 20,21,22,23,24
  836. {
  837. reason = string.Format("Pick invalid parameter,double hand, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  838. return false;
  839. }
  840. }
  841. else if (hand == Hand.Blade2)
  842. {
  843. if (!checkslot(0, 25 - Blade2Slots + 1, slot)) //0 - 21| 21,22,23,24
  844. {
  845. reason = string.Format("Pick invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  846. return false;
  847. }
  848. }
  849. else
  850. {
  851. if (!checkslot(0, 25, slot))
  852. {
  853. reason = string.Format("Pick invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  854. return false;
  855. }
  856. }
  857. }
  858. else
  859. {
  860. if (hand == Hand.Both)
  861. {
  862. reason = string.Format("Pick invalid parameter, double pick,chamber is {0}, slot is {1}", chamber.ToString(), slot);
  863. return false;
  864. }
  865. }
  866. return execute(_factory.PickEx(chamber, slot, hand, x,y,z), out reason);
  867. }
  868. public bool PlaceEx(ModuleName chamber, int slot, Hand hand, int x, int y, int z, out string reason)
  869. {
  870. reason = string.Empty;
  871. if (chamber == ModuleName.Robot)
  872. {
  873. reason = string.Format("Place invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  874. return false;
  875. }
  876. if (ModuleHelper.IsLoadPort(chamber))
  877. {
  878. if (hand == Hand.Both)
  879. {
  880. if (!checkslot(0, 25 - Blade2Slots, slot)) //0-20 | 20,21,22,23,24
  881. {
  882. reason = string.Format("Place invalid parameter,double hand, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  883. return false;
  884. }
  885. }
  886. else if (hand == Hand.Blade2)
  887. {
  888. if (!checkslot(0, 25 - Blade2Slots + 1, slot)) //0 - 21| 21,22,23,24
  889. {
  890. reason = string.Format("Pick invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  891. return false;
  892. }
  893. }
  894. else
  895. {
  896. if (!checkslot(0, 25, slot))
  897. {
  898. reason = string.Format("Place invalid parameter,place, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  899. return false;
  900. }
  901. }
  902. }
  903. else
  904. {
  905. if (hand == Hand.Both)
  906. {
  907. reason = string.Format("Place invalid parameter, double place,chamber is {0}, slot is {1}", chamber.ToString(), slot);
  908. return false;
  909. }
  910. }
  911. return execute(_factory.PlaceEx(chamber, slot, hand,x,y,z), out reason);
  912. }
  913. public bool ExchangeEx(ModuleName chamber, int slot, Hand hand, int x, int y, int z, out string reason)
  914. {
  915. reason = string.Empty;
  916. if (chamber == ModuleName.Robot)
  917. {
  918. reason = string.Format("Exchange invalid parameter, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  919. return false;
  920. }
  921. if (Blade2Slots > 1)
  922. {
  923. reason = string.Format("this robot don't support exchange operation.");
  924. return false;
  925. }
  926. if (hand == Hand.Both)
  927. {
  928. reason = string.Format("Exchange invalid parameter,double hand");
  929. return false;
  930. }
  931. if (ModuleHelper.IsLoadPort(chamber))
  932. {
  933. if (!checkslot(0, 25, slot))
  934. {
  935. reason = string.Format("Exchange invalid parameter,pick, chamber is {0}, slot is {1}", chamber.ToString(), slot);
  936. return false;
  937. }
  938. }
  939. return execute(_factory.ExchangeEx(chamber, slot, hand, x, y, z), out reason);
  940. }
  941. #endregion
  942. private bool execute(IHandler handler, out string reason)
  943. {
  944. reason = string.Empty;
  945. lock (_locker)
  946. {
  947. if (_foregroundHandler != null)
  948. {
  949. reason = "System busy, please wait or reset system.";
  950. EV.PostMessage(Name, EventEnum.DefaultWarning, string.Format("{0} {1} {2}", this.DeviceID, handler.ToString(), reason));
  951. return false;
  952. }
  953. if (_backgroundHandler != null && handler.IsBackground)
  954. {
  955. reason = "System busy,one background command is running, please wait or reset system.";
  956. EV.PostMessage(Name, EventEnum.DefaultWarning, string.Format("{0} {1} {2}", this.DeviceID, handler.ToString(), reason)); reason = "系统忙,后台命令正在处理,暂时不能处理新的后台命令";
  957. return false;
  958. }
  959. handler.Unit = (int)Unit.Robot;
  960. if(!handler.Execute(ref _socket))
  961. {
  962. reason = "Communication error,please check it.";
  963. return false;
  964. }
  965. if (handler.IsBackground)
  966. _backgroundHandler = handler;
  967. else
  968. _foregroundHandler = handler;
  969. }
  970. return true;
  971. }
  972. private void OnDataChanged(string package)
  973. {
  974. try
  975. {
  976. if (!package.Contains("Gb") && !package.Contains("Pb"))
  977. package = package.ToUpper();
  978. string[] msgs = Regex.Split(package, delimiter);
  979. foreach (string msg in msgs)
  980. {
  981. if (msg.Length > 0)
  982. {
  983. bool completed = false;
  984. string resp = msg;
  985. lock (_locker)
  986. {
  987. if (_foregroundHandler != null && _foregroundHandler.OnMessage(ref _socket, resp, out completed))
  988. {
  989. _foregroundHandler = null;
  990. }
  991. else if (_backgroundHandler != null && _backgroundHandler.OnMessage(ref _socket, resp, out completed))
  992. {
  993. if (completed)
  994. {
  995. string reason = string.Empty;
  996. QueryState(out reason);
  997. _backgroundHandler = null;
  998. }
  999. }
  1000. else
  1001. {
  1002. if (_eventHandler != null)
  1003. {
  1004. if (_eventHandler.OnMessage(ref _socket, resp, out completed))
  1005. {
  1006. if (completed)
  1007. {
  1008. EV.PostMessage("Robot", EventEnum.DefaultWarning, string.Format(" has error. {0:X}", ErrorCode));
  1009. _exceuteErr = true;
  1010. }
  1011. }
  1012. }
  1013. }
  1014. }
  1015. }
  1016. }
  1017. }
  1018. catch (ExcuteFailedException e)
  1019. {
  1020. EV.PostMessage("Robot", EventEnum.DefaultWarning, string.Format("executed failed. {0}",e.Message));
  1021. OnError();
  1022. _exceuteErr = false;
  1023. }
  1024. catch (InvalidPackageException e)
  1025. {
  1026. EV.PostMessage("Robot", EventEnum.DefaultWarning, string.Format("receive invalid package. {0}", e.Message));
  1027. OnError();
  1028. }
  1029. catch (System.Exception ex)
  1030. {
  1031. _commErr = true;
  1032. LOG.Write("Robot failed:" + ex.ToString());
  1033. }
  1034. }
  1035. private void OnErrorHandler(ErrorEventArgs args)
  1036. {
  1037. _commErr = true;
  1038. Initalized = false;
  1039. EV.PostMessage(Module, EventEnum.CommunicationError, Display, args.Reason);
  1040. OnError();
  1041. }
  1042. private bool checkslot(int min, int max, int slot)
  1043. {
  1044. return slot >= min && slot < max;
  1045. }
  1046. public void NotifySlotMapResult(ModuleName module, string slotMap)
  1047. {
  1048. if (OnSlotMapRead != null)
  1049. {
  1050. OnSlotMapRead(module, slotMap);
  1051. }
  1052. }
  1053. public void OnError()
  1054. {
  1055. EV.Notify(AlarmRobotError);
  1056. }
  1057. }
  1058. }