HiwinRobot.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. using Aitex.Core.Common;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.RT.Event;
  4. using Aitex.Core.RT.Log;
  5. using Aitex.Core.RT.OperationCenter;
  6. using Aitex.Core.RT.SCCore;
  7. using Aitex.Core.Util;
  8. using MECF.Framework.Common.CommonData;
  9. using MECF.Framework.Common.Communications;
  10. using MECF.Framework.Common.Equipment;
  11. using MECF.Framework.Common.SubstrateTrackings;
  12. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Common;
  13. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.RobotBase;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.HiWin
  20. {
  21. //上银机械手
  22. public class HiwinRobot:RobotBaseDevice,IConnection
  23. {
  24. public string Address => Connection.Address;
  25. public bool IsConnected => Connection.IsConnected && !_connection.IsCommunicationError;
  26. public bool Connect()
  27. {
  28. return _connection.Connect();
  29. }
  30. public bool Disconnect()
  31. {
  32. return _connection.Disconnect();
  33. }
  34. public string PortStatus { get; set; } = "Closed";
  35. public Dictionary<string, string> ModuleAssociateStationDic
  36. {
  37. get => _moduleAssociateStationDic;
  38. set
  39. {
  40. _moduleAssociateStationDic = value;
  41. }
  42. }
  43. public Dictionary<string,string> ModuleStationDic { get; set; }
  44. private HiwinRobotConnection _connection;
  45. public HiwinRobotConnection Connection
  46. {
  47. get { return _connection; }
  48. }
  49. public bool SevoOnOff { get; private set; }
  50. public string MapResult { get; set; }
  51. private R_TRIG _trigError = new R_TRIG();
  52. private bool _isAlarm;
  53. private R_TRIG _trigCommunicationError = new R_TRIG();
  54. private R_TRIG _trigRetryConnect = new R_TRIG();
  55. private PeriodicJob _thread;
  56. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  57. public List<IOResponse> IOResponseList { get; set; } = new List<IOResponse>();
  58. private Dictionary<string, string> _errorCodeReferenceDic;
  59. private Dictionary<string, string> _moduleAssociateStationDic;
  60. private object _locker = new object();
  61. private SCConfigItem _scHomeTimeout;
  62. private SCConfigItem _scMotionTimeout;
  63. //private SCConfigItem _scBladeSlots;
  64. public int BladeSlots => 1 ;
  65. private bool _enableLog;
  66. private string _scRoot;
  67. public HiwinRobot(string module, string name, string scRoot, string endof = "\r\n") : base(module, name,scRoot)
  68. {
  69. string deviceIP = SC.GetStringValue($"{scRoot}.Address");
  70. _connection = new HiwinRobotConnection(deviceIP, endof);
  71. _scRoot = scRoot;
  72. _connection.EnableLog(SC.GetValue<bool>($"{scRoot}.EnableLogMessage"));
  73. if (_connection.Connect())
  74. {
  75. PortStatus = "Open";
  76. EV.PostInfoLog(Module, $"{Module} connected");
  77. }
  78. else
  79. {
  80. PortStatus = "Close";
  81. EV.PostInfoLog(Module, $"{Module} connect failed");
  82. }
  83. _thread = new PeriodicJob(50, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  84. _errorCodeReferenceDic = new Dictionary<string, string>()
  85. {
  86. };
  87. }
  88. private bool OnTimer()
  89. {
  90. try
  91. {
  92. //return true;
  93. _connection.MonitorTimeout();
  94. if (!_connection.IsConnected || _connection.IsCommunicationError)
  95. {
  96. lock (_locker)
  97. {
  98. _lstHandler.Clear();
  99. }
  100. _trigRetryConnect.CLK = !_connection.IsConnected;
  101. if (_trigRetryConnect.Q)
  102. {
  103. if (!_connection.Connect())
  104. {
  105. EV.PostAlarmLog(Module, $"Can not connect with {Module} {_connection.Address}");
  106. }
  107. else
  108. {
  109. //_lstHandler.AddLast(new SunwayRobotQueryPinHandler(this, _deviceAddress));
  110. //_lstHandler.AddLast(new SunwayRobotSetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
  111. }
  112. }
  113. return true;
  114. }
  115. HandlerBase handler = null;
  116. if (!_connection.IsBusy)
  117. {
  118. lock (_locker)
  119. {
  120. if (_lstHandler.Count > 0)
  121. {
  122. handler = _lstHandler.First.Value;
  123. _lstHandler.RemoveFirst();
  124. }
  125. }
  126. if (handler != null)
  127. {
  128. _connection.Execute(handler);
  129. }
  130. }
  131. }
  132. catch (Exception ex)
  133. {
  134. LOG.Write(ex);
  135. }
  136. return true;
  137. }
  138. public override bool IsReady()
  139. {
  140. return RobotState == RobotStateEnum.Idle && !IsBusy && !_connection.IsBusy && _lstHandler.Count == 0;
  141. }
  142. protected override void SubscribeWaferLocation()
  143. {
  144. }
  145. internal void SetRobotSV(string axis,bool isOn)
  146. {
  147. lock (_locker)
  148. {
  149. _lstHandler.AddLast(new HiwinRobotOpeOrCloseMotorHandler(this, RobotModuleName, isOn, axis));
  150. }
  151. }
  152. public override void Reset()
  153. {
  154. _trigError.RST = true;
  155. _connection.SetCommunicationError(false, "");
  156. _trigCommunicationError.RST = true;
  157. _trigWarningMessage.RST = true;
  158. //_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  159. _trigRetryConnect.RST = true;
  160. base.Reset();
  161. }
  162. #region Note Functions
  163. private R_TRIG _trigWarningMessage = new R_TRIG();
  164. public void NoteError(string reason)
  165. {
  166. if (reason != null)
  167. {
  168. _trigWarningMessage.CLK = true;
  169. var content = reason;
  170. if (_errorCodeReferenceDic.ContainsKey(reason))
  171. content = _errorCodeReferenceDic[reason];
  172. if (_trigWarningMessage.Q)
  173. {
  174. lock (_locker)
  175. {
  176. _lstHandler.AddLast(new HiwinRobotGetAllErrHandler(this));
  177. }
  178. if (RobotHasError == null)
  179. {
  180. EV.PostAlarmLog(Module, $"{Module} error, {reason} {content}");
  181. }
  182. else
  183. {
  184. //RobotHasError.Description = $"{Module}.{Name} error, {reason} {content}";
  185. RobotHasError.Set($"{Module} error, {reason} {content}");
  186. }
  187. }
  188. _isAlarm = true;
  189. ErrorCode = reason;
  190. }
  191. else
  192. {
  193. _isAlarm = false;
  194. }
  195. }
  196. protected override bool fStartInit(object[] param)
  197. {
  198. _connection.EnableLog(SC.GetValue<bool>($"{_scRoot}.EnableLogMessage"));
  199. lock (_locker)
  200. {
  201. _lstHandler.AddLast(new HiwinRobotHomeALLHandler(this, RobotModuleName));
  202. _lstHandler.AddLast(new HiwinRobotSetSpeedHandler(this, SC.GetValue<int>($"{_scRoot}.Speed")));
  203. _lstHandler.AddLast(new HiwinRobotQueryWaferPresentHandler(this));
  204. _lstHandler.AddLast(new HiwinRobotGetAllErrHandler(this));
  205. }
  206. return true;
  207. }
  208. protected override bool fMonitorInit(object[] param)
  209. {
  210. if (_lstHandler.Count == 0
  211. && !_connection.IsBusy)
  212. {
  213. IsBusy = false;
  214. return true;
  215. }
  216. return false;
  217. }
  218. protected override bool fClear(object[] param)
  219. {
  220. //lock (_locker)
  221. //{
  222. // _lstHandler.AddLast(new SunwayRobotResetHandler(this));
  223. //}
  224. return true;
  225. }
  226. public override bool ReadParameter(object[] param)
  227. {
  228. IsBusy = true;
  229. return CheckToPostMessage((int)RobotMsg.ReadData, param);
  230. }
  231. public override bool GoTo(object[] param)
  232. {
  233. return false;
  234. }
  235. protected override bool fStartReadData(object[] param)
  236. {
  237. return true;
  238. }
  239. protected override bool fMonitorReadData(object[] param)
  240. {
  241. IsBusy = false;
  242. return _lstHandler.Count == 0 && !_connection.IsBusy;
  243. }
  244. protected override bool fStartSetParameters(object[] param)
  245. {
  246. return true;
  247. }
  248. protected override bool fStartTransferWafer(object[] param)
  249. {
  250. return true;
  251. }
  252. protected override bool fStartUnGrip(object[] param)
  253. {
  254. return true;
  255. }
  256. protected override bool fStartGrip(object[] param)
  257. {
  258. return true;
  259. }
  260. //(arm, module, slot, isRetract, isZaxisDown)
  261. protected override bool fStartGoTo(object[] param)
  262. {
  263. return true;
  264. }
  265. protected override bool fMonitorGoTo(object[] param)
  266. {
  267. if (_lstHandler.Count == 0 && !_connection.IsBusy)
  268. {
  269. IsBusy = false;
  270. return true;
  271. }
  272. return false;
  273. }
  274. protected override bool fStartMapWafer(object[] param)
  275. {
  276. IsBusy = true;
  277. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), param[0].ToString());
  278. if (!ModuleStationDic.ContainsKey($"{ tempmodule}.Map"))
  279. {
  280. EV.PostAlarmLog(Module, $"{Module} target module {tempmodule} is not teach");
  281. }
  282. string station = ModuleStationDic[$"{ tempmodule}.Map"];
  283. lock (_locker)
  284. {
  285. _lstHandler.AddLast(new HiWinWaferScanActionHandler(this, $"{station}"));
  286. _lstHandler.AddLast(new HiwinRobotGetMapResultHandler(this));
  287. _lstHandler.AddLast(new HiwinRobotGetAllErrHandler(this));
  288. }
  289. return true;
  290. }
  291. protected override bool fMonitorMap(object[] param)
  292. {
  293. if (_lstHandler.Count == 0 && !_connection.IsBusy)
  294. {
  295. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[0].ToString());
  296. var wafers = WaferManager.Instance.GetWafers(tempmodule);
  297. if(wafers != null && wafers.Any())
  298. {
  299. foreach(var wafer in wafers)
  300. {
  301. if (wafer == null || wafer.IsEmpty)
  302. continue;
  303. WaferManager.Instance.DeleteWafer(tempmodule, wafer.OriginSlot);
  304. }
  305. }
  306. if(!string.IsNullOrEmpty(MapResult))
  307. {
  308. for(int i=0; i<MapResult.Length; i++)
  309. {
  310. switch(MapResult[i])
  311. {
  312. case 'X'://unkonwn wafer
  313. WaferManager.Instance.CreateWafer(tempmodule, i, WaferStatus.Unknown);
  314. EV.PostAlarmLog(Module, $"{Module} {tempmodule} map result slot={i+1} has unkonwn wafer");
  315. break;
  316. case '0'://no wafer
  317. break;
  318. case '1'://has wafer
  319. WaferManager.Instance.CreateWafer(tempmodule, i, WaferStatus.Normal);
  320. break;
  321. case '2'://cross wafer
  322. WaferManager.Instance.CreateWafer(tempmodule, i, WaferStatus.Crossed);
  323. EV.PostAlarmLog(Module, $"{Module} {tempmodule} map result slot={i + 1} has cross wafer");
  324. break;
  325. case '3'://double wafer
  326. WaferManager.Instance.CreateWafer(tempmodule, i, WaferStatus.Double);
  327. EV.PostAlarmLog(Module, $"{Module} {tempmodule} map result slot={i + 1} has double wafer");
  328. break;
  329. case '4'://薄 wafer
  330. WaferManager.Instance.CreateWafer(tempmodule, i, WaferStatus.Unknown);
  331. EV.PostAlarmLog(Module, $"{Module} {tempmodule} map result slot={i + 1} has thin wafer");
  332. break;
  333. }
  334. }
  335. }
  336. IsBusy = false;
  337. return true;
  338. }
  339. return false;
  340. }
  341. protected override bool fStartSwapWafer(object[] param)
  342. {
  343. return false;
  344. }
  345. //(arm, module, slot)
  346. protected override bool fStartPlaceWafer(object[] param)
  347. {
  348. IsBusy = true;
  349. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), param[0].ToString());
  350. int slotindex = int.Parse(param[1].ToString()) + 1;
  351. RobotArmEnum arm = (RobotArmEnum)param[2];
  352. CmdRobotArm = arm;
  353. if (!ModuleStationDic.ContainsKey($"{ tempmodule}.Place"))
  354. {
  355. EV.PostAlarmLog(Module, $"{Module} target module {tempmodule} is not teach");
  356. }
  357. string station = ModuleStationDic[$"{ tempmodule}.Place"];
  358. lock (_locker)
  359. {
  360. _lstHandler.AddLast(new SunwayRobotRawCommandHandler(this, "PUT", $"{station},{slotindex}"));
  361. _lstHandler.AddLast(new HiwinRobotQueryWaferPresentHandler(this));
  362. _lstHandler.AddLast(new HiwinRobotGetAllErrHandler(this));
  363. }
  364. Blade1Target = tempmodule;
  365. Blade2Target = tempmodule;
  366. CmdTarget = tempmodule;
  367. MoveInfo = new RobotMoveInfo()
  368. {
  369. Action = RobotAction.Moving,
  370. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  371. BladeTarget = BuildBladeTarget(),
  372. };
  373. return true;
  374. }
  375. protected override bool fMonitorPlace(object[] param)
  376. {
  377. if (_lstHandler.Count == 0 && !_connection.IsBusy)
  378. {
  379. ModuleName destModule;
  380. if (!Enum.TryParse(CurrentParamter[0].ToString(), out destModule)) return false;
  381. int destSlotIndex;
  382. if (!int.TryParse(CurrentParamter[1].ToString(), out destSlotIndex)) return false;
  383. RobotArmEnum arm = (RobotArmEnum)CurrentParamter[2];
  384. if (arm == RobotArmEnum.Lower || arm == RobotArmEnum.Blade1)
  385. WaferManager.Instance.WaferMoved(RobotModuleName, 0, destModule, destSlotIndex);
  386. if (arm == RobotArmEnum.Upper || arm == RobotArmEnum.Blade2)
  387. WaferManager.Instance.WaferMoved(RobotModuleName, 1, destModule, destSlotIndex);
  388. if (arm == RobotArmEnum.Both)
  389. {
  390. WaferManager.Instance.WaferMoved(RobotModuleName, 0, destModule, destSlotIndex);
  391. WaferManager.Instance.WaferMoved(RobotModuleName, 1, destModule, destSlotIndex);
  392. }
  393. Blade1Target = ModuleName.System;
  394. Blade2Target = ModuleName.System;
  395. CmdTarget = ModuleName.System;
  396. MoveInfo = new RobotMoveInfo()
  397. {
  398. Action = RobotAction.Moving,
  399. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  400. BladeTarget = BuildBladeTarget(),
  401. };
  402. IsBusy = false;
  403. return true;
  404. }
  405. return false;
  406. }
  407. //(arm, module, slot)
  408. protected override bool fStartPickWafer(object[] param)
  409. {
  410. IsBusy = true;
  411. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), param[0].ToString());
  412. int slotindex = int.Parse(param[1].ToString()) + 1;
  413. RobotArmEnum arm = (RobotArmEnum)param[2];
  414. CmdRobotArm = arm;
  415. if (!ModuleStationDic.ContainsKey($"{ tempmodule}.Pick"))
  416. {
  417. EV.PostAlarmLog(Module, $"{Module} target module {tempmodule} is not teach");
  418. }
  419. string station = ModuleStationDic[$"{ tempmodule}.Pick"];
  420. lock (_locker)
  421. {
  422. _lstHandler.AddLast(new SunwayRobotRawCommandHandler(this, "GET", $"{station},{slotindex}"));
  423. _lstHandler.AddLast(new HiwinRobotQueryWaferPresentHandler(this));
  424. _lstHandler.AddLast(new HiwinRobotGetAllErrHandler(this));
  425. }
  426. Blade1Target = tempmodule;
  427. Blade2Target = tempmodule;
  428. CmdTarget = tempmodule;
  429. MoveInfo = new RobotMoveInfo()
  430. {
  431. Action = RobotAction.Moving,
  432. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  433. BladeTarget = BuildBladeTarget(),
  434. };
  435. return true;
  436. }
  437. protected override bool fMonitorPick(object[] param)
  438. {
  439. if (_lstHandler.Count == 0 && !_connection.IsBusy)
  440. {
  441. ModuleName sourcemodule;
  442. if (!Enum.TryParse(CurrentParamter[0].ToString(), out sourcemodule)) return false;
  443. int SourceslotIndex;
  444. if (!int.TryParse(CurrentParamter[1].ToString(), out SourceslotIndex)) return false;
  445. RobotArmEnum arm = (RobotArmEnum)CurrentParamter[2];
  446. if (arm == RobotArmEnum.Lower || arm == RobotArmEnum.Blade1)
  447. WaferManager.Instance.WaferMoved(sourcemodule, SourceslotIndex, RobotModuleName, 0);
  448. if (arm == RobotArmEnum.Upper || arm == RobotArmEnum.Blade2)
  449. WaferManager.Instance.WaferMoved(sourcemodule, SourceslotIndex, RobotModuleName, 1);
  450. if (arm == RobotArmEnum.Both)
  451. {
  452. WaferManager.Instance.WaferMoved(sourcemodule, SourceslotIndex, RobotModuleName, 0);
  453. WaferManager.Instance.WaferMoved(sourcemodule, SourceslotIndex, RobotModuleName, 1);
  454. }
  455. Blade1Target = ModuleName.System;
  456. Blade2Target = ModuleName.System;
  457. CmdTarget = ModuleName.System;
  458. MoveInfo = new RobotMoveInfo()
  459. {
  460. Action = RobotAction.Moving,
  461. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  462. BladeTarget = BuildBladeTarget(),
  463. };
  464. IsBusy = false;
  465. return true;
  466. }
  467. return false;
  468. }
  469. protected override bool fResetToReady(object[] param)
  470. {
  471. return true;
  472. }
  473. protected override bool fReset(object[] param)
  474. {
  475. _trigError.RST = true;
  476. _trigWarningMessage.RST = true;
  477. _connection.SetCommunicationError(false, "");
  478. _trigCommunicationError.RST = true;
  479. //_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  480. _trigRetryConnect.RST = true;
  481. if (_isAlarm)
  482. {
  483. //lock (_locker)
  484. //{
  485. // _lstHandler.AddLast(new SunwayRobotResetHandler(this));
  486. //}
  487. //Thread.Sleep(100);
  488. CheckToPostMessage((int)RobotMsg.ActionDone);
  489. }
  490. IsBusy = false;
  491. return true;
  492. }
  493. protected override bool fMonitorReset(object[] param)
  494. {
  495. IsBusy = false;
  496. return true;
  497. }
  498. protected override bool fError(object[] param)
  499. {
  500. return true;
  501. }
  502. public override RobotArmWaferStateEnum GetWaferState(RobotArmEnum arm)
  503. {
  504. if (arm == RobotArmEnum.Blade1)
  505. {
  506. return IsWaferPresenceOnBlade1 ? RobotArmWaferStateEnum.Present : RobotArmWaferStateEnum.Absent;
  507. }
  508. if (arm == RobotArmEnum.Blade2)
  509. {
  510. return IsWaferPresenceOnBlade2 ? RobotArmWaferStateEnum.Present : RobotArmWaferStateEnum.Absent;
  511. }
  512. return RobotArmWaferStateEnum.Unknown;
  513. }
  514. protected override bool fStop(object[] param)
  515. {
  516. lock (_locker)
  517. {
  518. _lstHandler.AddLast(new HiwinRobotAxisStopMoveHandler(this, RobotModuleName));
  519. }
  520. return true;
  521. }
  522. protected override bool fStartExtendForPick(object[] param)
  523. {
  524. throw new NotImplementedException();
  525. }
  526. protected override bool fStartExtendForPlace(object[] param)
  527. {
  528. throw new NotImplementedException();
  529. }
  530. protected override bool fStartRetractFromPick(object[] param)
  531. {
  532. throw new NotImplementedException();
  533. }
  534. protected override bool fStartRetractFromPlace(object[] param)
  535. {
  536. throw new NotImplementedException();
  537. }
  538. protected string BuildBladeTarget()
  539. {
  540. return (CmdRobotArm == RobotArmEnum.Upper ? "ArmB" : "ArmA") + "." + CmdTarget;
  541. }
  542. #endregion
  543. }
  544. }