HongHuVR.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.RT.SCCore;
  3. using Aitex.Core.Util;
  4. using Aitex.Sorter.Common;
  5. using MECF.Framework.Common.CommonData;
  6. using MECF.Framework.Common.Equipment;
  7. using MECF.Framework.Common.SubstrateTrackings;
  8. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot;
  9. using System;
  10. using System.Collections.Concurrent;
  11. using System.Collections.Generic;
  12. using System.ComponentModel;
  13. using System.Diagnostics.Tracing;
  14. using System.Linq;
  15. using System.Runtime.InteropServices;
  16. using System.Text;
  17. using System.Text.RegularExpressions;
  18. using System.Threading.Tasks;
  19. using Venus_Core;
  20. using Venus_RT.Modules;
  21. namespace Venus_RT.Devices.VCE
  22. {
  23. //泓浒
  24. enum VRStep
  25. {
  26. Idle,
  27. Home,
  28. Move,
  29. Halt,
  30. Release,
  31. Goto,
  32. Pick,
  33. PickExtend,
  34. PickRetract,
  35. Place,
  36. PlaceExtend,
  37. PlaceRetract,
  38. Xfer,
  39. CheckLoad_ArmA,
  40. CheckLoad_ArmB,
  41. SetLoad,
  42. ReQueryLoadA,
  43. ReQueryLoadB,
  44. }
  45. public class HongHuVR : ITransferRobot
  46. {
  47. private readonly AsyncSocket _socket;
  48. private const string EOF = "\n";
  49. private RState _status;
  50. private bool _IsHomed;
  51. private VRStep _currentStep = VRStep.Idle;
  52. private Dictionary<ModuleName, int> _StationNumbers = new Dictionary<ModuleName, int>();
  53. public RState Status { get { return _status; } }
  54. public bool IsHomed { get { return _IsHomed; } }
  55. private string Hand2Arm(Hand hand) => hand == Hand.Blade2 ? "B" : "A";
  56. //private readonly Regex _rex_check_load = new Regex(@"LOAD\s+(A|B)\s+(\w+)\s*");
  57. private readonly Regex _rex_check_load = new Regex(@"CHECK LOAD\s*");
  58. private readonly Regex _rex_rq_load_A = new Regex(@"LOAD\sA\s.*");
  59. private readonly Regex _rex_rq_load_B = new Regex(@"LOAD\sB\s.*");
  60. private readonly Regex _rex_error_code = new Regex(@"_ERR\s+(\d+)\s*");
  61. private RobotMoveInfo _robotMoveInfo = new RobotMoveInfo();
  62. public RobotMoveInfo TMRobotMoveInfo { get { return _robotMoveInfo; } }
  63. public double Offset_X => 0;
  64. public double Offset_Y => 0;
  65. public double Offset_D => 0;
  66. public Dictionary<string, string> _error2msg = new Dictionary<string, string>()
  67. {
  68. {"221" , "手臂选择无效,检查指令中的对应参数" },
  69. {"222" , "无效的 Pan 参数"},
  70. {"305" , "无法识别的命令,检查指令" },
  71. {"309" , "不支持的指令,检查指令" },
  72. {"350" , "解析器错误,堆栈溢出,检查指令" },
  73. {"402" , "槽位参数异常,检查指令中的对应参数" },
  74. {"407" , "T轴参数异常,检查指令中的对应参数" },
  75. {"408" , "R轴参数异常,检查指令中的对应参数" },
  76. {"409" , "Z轴参数异常,检查指令中的对应参数" },
  77. {"416" , "站点未初始化,检查指令中的对应参数" },
  78. {"417" , "offset 值过大,检查指令中的对应参数" },
  79. {"418" , "AWC 功能检测到误差过大,检查晶圆是否过偏或检查 AWC 传感器标定参数" },
  80. {"550" , "站参数超出范围,检查站点配置参数" },
  81. {"551" , "ARM 不在站点,检查当前手臂实际位置与逻辑位置" },
  82. {"560" , "机械臂处于 HALT 状态,拒绝接受新的指令" },
  83. {"561" , "当前机械臂状态机与指令不匹配,当前状态机不满足执行该指令的条件" },
  84. {"600" , "PAligner通讯异常,检查与 PA 的通讯电缆,上电情况等" },
  85. {"603" , "收到急停指令" },
  86. {"608" , "机械臂急停中" },
  87. {"700" , "当前手臂有晶圆,检查指令动作要求的晶圆状态,与手臂内部存储的晶圆状态是否匹配" },
  88. {"701" , "当前手臂无晶圆,检查指令动作要求的晶圆状态,与手臂内部存储的晶圆状态是否匹配"},
  89. {"711" , "站点互锁错误,检查站点的“允许机械臂进入信号”是否就绪"},
  90. {"721" , "取片失败,检查手臂晶圆状态,或者晶圆检测传感器状态"},
  91. {"722" , "放片失败,检查手臂晶圆状态,或者晶圆检测传感器状态"},
  92. {"791" , "查询 AWC 数据失败,未曾执行过 AWC 相关功能的操作"},
  93. {"799" , "放片失败,检查手臂晶圆状态,或者晶圆检测传感器状态"},
  94. {"803" , "机械臂上电失败 HOME ALL 指令,或者其他上电操作,驱动器未能正常响应上电"},
  95. {"1100" , "无法安全回 HOME 操作,检查机械臂是否在安全 HOME 半径内,常见于双臂机械臂,双臂同时伸出超过安全位"},
  96. {"13004" , "PA报错,检查 PA 运行状态,PA 扫片结果等"},
  97. };
  98. private BlockingCollection<RobotAnimationData> blockingCollection = new BlockingCollection<RobotAnimationData>();
  99. public HongHuVR()
  100. {
  101. _socket = new AsyncSocket("", EOF);
  102. _socket.Connect(SC.GetStringValue($"SETM.IPAddress"));
  103. _socket.OnDataChanged += OnReceiveMessage;
  104. _socket.OnErrorHappened += OnErrorHappen;
  105. _status = RState.Init;
  106. _IsHomed = false;
  107. _StationNumbers[ModuleName.VCE1] = SC.GetValue<int>("SETM.VCE1StationNumber");
  108. _StationNumbers[ModuleName.VPA] = SC.GetValue<int>("SETM.VPAStationNumber");
  109. _StationNumbers[ModuleName.PMA] = SC.GetValue<int>("SETM.PMAStationNumber");
  110. _StationNumbers[ModuleName.PMB] = SC.GetValue<int>("SETM.PMBStationNumber");
  111. _StationNumbers[ModuleName.PMC] = SC.GetValue<int>("SETM.PMCStationNumber");
  112. _StationNumbers[ModuleName.PMD] = SC.GetValue<int>("SETM.PMDStationNumber");
  113. WaferManager.Instance.SubscribeLocation(ModuleName.TMRobot, 2);
  114. Task.Run(() =>
  115. {
  116. foreach (var data in blockingCollection.GetConsumingEnumerable())
  117. {
  118. _robotMoveInfo.Action = data.Action;
  119. _robotMoveInfo.ArmTarget = data.Hand == Hand.Blade1 ? RobotArm.ArmA : (data.Hand == Hand.Both ? RobotArm.Both : RobotArm.ArmB);
  120. _robotMoveInfo.BladeTarget = $"{_robotMoveInfo.ArmTarget}.{data.Target}";
  121. System.Threading.Thread.Sleep(300);
  122. }
  123. });
  124. }
  125. //初始化某个轴
  126. //1.清错
  127. //2.设备上电
  128. //3.各轴按顺序运动
  129. public bool Home()
  130. {
  131. _status = RState.Running;
  132. _currentStep = VRStep.Home;
  133. return _SendCommand("HOME ALL");
  134. }
  135. public bool Halt()
  136. {
  137. _status = RState.Running;
  138. _currentStep = VRStep.Halt;
  139. return _SendCommand("HALT");
  140. }
  141. public bool Release()
  142. {
  143. _status = RState.Running;
  144. _currentStep = VRStep.Release;
  145. return _SendCommand("RELEASE");
  146. }
  147. //public bool MOVE()
  148. //{
  149. //}
  150. public bool Pick(ModuleName station, int slot, Hand hand)
  151. {
  152. if (!CheckRobotStatus())
  153. return false;
  154. _currentStep = VRStep.Pick;
  155. _status = RState.Running;
  156. SetRobotMovingInfo(RobotAction.Picking, hand, station);
  157. return _SendCommand($"PICK {_StationNumbers[station]} ARM {Hand2Arm(hand)}");
  158. }
  159. public bool PickExtend(ModuleName station, int slot, Hand hand)
  160. {
  161. if (!CheckRobotStatus())
  162. return false;
  163. _currentStep = VRStep.PickExtend;
  164. _status = RState.Running;
  165. SetRobotMovingInfo(RobotAction.Extending, hand, station);
  166. return _SendCommand($"PICK {_StationNumbers[station]} ARM {Hand2Arm(hand)} ENRT NR");
  167. }
  168. public bool PickRetract(ModuleName station, int slot, Hand hand)
  169. {
  170. if (!CheckRobotStatus())
  171. return false;
  172. _currentStep = VRStep.PickRetract;
  173. _status = RState.Running;
  174. SetRobotMovingInfo(RobotAction.Retracting, hand, station);
  175. return _SendCommand($"PICK {_StationNumbers[station]} ARM {Hand2Arm(hand)} STRT NR");
  176. }
  177. public bool Place(ModuleName station, int slot, Hand hand)
  178. {
  179. if (!CheckRobotStatus())
  180. return false;
  181. _currentStep = VRStep.Place;
  182. _status = RState.Running;
  183. SetRobotMovingInfo(RobotAction.Placing, hand, station);
  184. return _SendCommand($"PLACE {_StationNumbers[station]} ARM {Hand2Arm(hand)}");
  185. }
  186. public bool PlaceExtend(ModuleName station, int slot, Hand hand)
  187. {
  188. if (!CheckRobotStatus())
  189. return false;
  190. _currentStep = VRStep.Place;
  191. _status = RState.Running;
  192. SetRobotMovingInfo(RobotAction.Extending, hand, station);
  193. return _SendCommand($"PLACE {_StationNumbers[station]} ARM {Hand2Arm(hand)} ENRT NR");
  194. }
  195. public bool PlaceRetract(ModuleName station, int slot, Hand hand)
  196. {
  197. if (!CheckRobotStatus())
  198. return false;
  199. _currentStep = VRStep.Place;
  200. _status = RState.Running;
  201. SetRobotMovingInfo(RobotAction.Retracting, hand, station);
  202. return _SendCommand($"PLACE {_StationNumbers[station]} ARM {Hand2Arm(hand)} STRT NR");
  203. }
  204. public bool Transfer(ModuleName fromstation, ModuleName tostation, Hand hand)
  205. {
  206. if (!CheckRobotStatus())
  207. return false;
  208. _currentStep = VRStep.Xfer;
  209. _status = RState.Running;
  210. return _SendCommand($"XFER ARM {Hand2Arm(hand)} {_StationNumbers[fromstation]} {_StationNumbers[tostation]}");
  211. }
  212. public bool CheckLoad(Hand hand = Hand.Blade1)
  213. {
  214. if (_currentStep != VRStep.Home && _currentStep != VRStep.CheckLoad_ArmA && !CheckRobotStatus())
  215. return false;
  216. _currentStep = hand == Hand.Blade2 ? VRStep.CheckLoad_ArmB : VRStep.CheckLoad_ArmA;
  217. _status = RState.Running;
  218. string arm = hand == Hand.Blade2 ? "B" : "A";
  219. return _SendCommand($"CHECK LOAD {_StationNumbers[ModuleName.VPA]} {arm}");
  220. }
  221. public bool QueryAwc()
  222. {
  223. return true;
  224. }
  225. public bool Goto(ModuleName station, int slot, Hand hand)
  226. {
  227. if (!CheckRobotStatus())
  228. return false;
  229. _currentStep = VRStep.Goto;
  230. _status = RState.Running;
  231. SetRobotMovingInfo(RobotAction.Rotating, hand, station);
  232. return _SendCommand($"Goto {_StationNumbers[station]} ARM {Hand2Arm(hand)}");
  233. }
  234. public bool MoveTo(ModuleName stnFrom, ModuleName stnTo, Hand hand)
  235. {
  236. _currentStep = VRStep.Move;
  237. _status = RState.Running;
  238. return _SendCommand($"MOVE R ABS 100 ARM {Hand2Arm(hand)}"); ;
  239. }
  240. public bool PickWithOffset(ModuleName station, int slot, Hand hand, int Roffset, int Toffset)
  241. {
  242. _currentStep = VRStep.Pick;
  243. _status = RState.Running;
  244. return _SendCommand($"PICK {_StationNumbers[station]} ARM {Hand2Arm(hand)} RO {Roffset} TO {Toffset}"); ;
  245. }
  246. public bool ReQueryLoadA()
  247. {
  248. _currentStep = VRStep.ReQueryLoadA;
  249. _status = RState.Running;
  250. return _SendCommand($"RQ LOAD ARM A");
  251. }
  252. public bool ReQueryLoadB()
  253. {
  254. _currentStep = VRStep.ReQueryLoadB;
  255. _status = RState.Running;
  256. return _SendCommand($"RQ LOAD ARM B");
  257. }
  258. private bool _SendCommand(string cmd)
  259. {
  260. LOG.WriteSingeLine(eEvent.INFO_TM_ROBOT, ModuleName.TMRobot, $"Send Command to TMRobot: {cmd},Connect:{_socket.IsConnected}");
  261. return _socket.Write(cmd + EOF);
  262. }
  263. private bool CheckRobotStatus()
  264. {
  265. if (Status == RState.Init)
  266. {
  267. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, "TMRobot is not homed, please home first.");
  268. return false;
  269. }
  270. else if (Status == RState.Running)
  271. {
  272. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, "TMRobot is busy, please wait a minute");
  273. return false;
  274. }
  275. else if (Status == RState.Failed || Status == RState.Timeout)
  276. {
  277. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, "TMRobot has a error, please check and fix the hardware issue and home it");
  278. return false;
  279. }
  280. return true;
  281. }
  282. private void OnReceiveMessage(string revMsg)
  283. {
  284. string[] ResMsgs = revMsg.Split('\n');
  285. foreach (string revRawMsg in ResMsgs)
  286. {
  287. if (string.IsNullOrWhiteSpace(revRawMsg)) continue;
  288. string RevMsg = revRawMsg.Trim();
  289. LOG.WriteSingeLine(eEvent.INFO_TM_ROBOT, ModuleName.TMRobot, $"Receive message from TMRobot: {RevMsg}, while {_currentStep}");
  290. if (_rex_error_code.IsMatch(RevMsg))
  291. {
  292. _IsHomed = false;
  293. _status = RState.Failed;
  294. var results = _rex_error_code.Match(RevMsg);
  295. ErrorMessageHandler(results.Groups[1].Value);
  296. return;
  297. }
  298. switch (_currentStep)
  299. {
  300. case VRStep.Goto:
  301. case VRStep.Halt:
  302. case VRStep.Move:
  303. case VRStep.Xfer:
  304. case VRStep.Pick:
  305. case VRStep.PickExtend:
  306. case VRStep.PickRetract:
  307. case VRStep.Place:
  308. case VRStep.PlaceExtend:
  309. case VRStep.PlaceRetract:
  310. {
  311. if (RevMsg.Trim() == "_RDY" || (RevMsg.Contains("_RDY") && !RevMsg.Contains("_ERR")))
  312. {
  313. _currentStep = VRStep.Idle;
  314. _status = RState.End;
  315. }
  316. else
  317. {
  318. ReportWrongMsg(RevMsg);
  319. }
  320. if (_currentStep != VRStep.PickExtend && _currentStep != VRStep.PlaceExtend)
  321. SetRobotMovingInfo(RobotAction.None, Hand.Both, ModuleName.TMRobot);
  322. }
  323. break;
  324. case VRStep.Home:
  325. {
  326. if (RevMsg.Trim() == "_RDY")
  327. {
  328. //CheckLoad(Hand.Blade1);
  329. _currentStep = VRStep.Idle;
  330. _status = RState.End;
  331. _IsHomed = true;
  332. SetRobotMovingInfo(RobotAction.Homing, Hand.Both, ModuleName.SETM);
  333. }
  334. else
  335. ReportWrongMsg(RevMsg);
  336. }
  337. break;
  338. case VRStep.CheckLoad_ArmA:
  339. {
  340. if (_rex_check_load.IsMatch(RevMsg))
  341. {
  342. GetCheckLoadResult(RevMsg);
  343. //CheckLoad(Hand.Blade2);
  344. }
  345. if (RevMsg.Trim() == "_RDY" || (RevMsg.Contains("_RDY") && !RevMsg.Contains("_ERR")))
  346. {
  347. _currentStep = VRStep.Idle;
  348. _status = RState.End;
  349. }
  350. }
  351. break;
  352. case VRStep.CheckLoad_ArmB:
  353. {
  354. if (_rex_check_load.IsMatch(RevMsg))
  355. {
  356. GetCheckLoadResult(RevMsg);
  357. //_currentStep = VRStep.Idle;
  358. //_status = RState.End;
  359. //_IsHomed = true;
  360. }
  361. if (RevMsg.Trim() == "_RDY" || (RevMsg.Contains("_RDY") && !RevMsg.Contains("_ERR")))
  362. {
  363. _currentStep = VRStep.Idle;
  364. _status = RState.End;
  365. }
  366. }
  367. break;
  368. case VRStep.ReQueryLoadA:
  369. if (_rex_rq_load_A.IsMatch(RevMsg))
  370. {
  371. string WaferStatus = RevMsg.Split(' ')[2];
  372. //LOG.Write(eEvent.WARN_DEFAULT_WARN, ModuleName.TMRobot, WaferStatus);
  373. if (WaferStatus.Contains("ON") && WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 0))
  374. {
  375. WaferManager.Instance.CreateWafer(ModuleName.TMRobot, 0, Aitex.Core.Common.WaferStatus.Normal);
  376. }
  377. if (WaferStatus.Contains("OFF"))
  378. {
  379. //LOG.Write(eEvent.WARN_DEFAULT_WARN, ModuleName.TMRobot, "contains off");
  380. WaferManager.Instance.DeleteWafer(ModuleName.TMRobot, 0);
  381. }
  382. }
  383. else
  384. {
  385. if (RevMsg.Trim() == "_RDY" || (RevMsg.Contains("_RDY") && !RevMsg.Contains("_ERR")))
  386. {
  387. _currentStep = VRStep.Idle;
  388. _status = RState.End;
  389. }
  390. else
  391. {
  392. ReportWrongMsg(RevMsg);
  393. }
  394. }
  395. break;
  396. case VRStep.ReQueryLoadB:
  397. if (_rex_rq_load_B.IsMatch(RevMsg))
  398. {
  399. string WaferStatus = RevMsg.Split(' ')[2];
  400. //LOG.Write(eEvent.WARN_DEFAULT_WARN, ModuleName.TMRobot, WaferStatus);
  401. if (WaferStatus.Contains("ON") && WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 1))
  402. {
  403. WaferManager.Instance.CreateWafer(ModuleName.TMRobot, 1, Aitex.Core.Common.WaferStatus.Normal);
  404. }
  405. if (WaferStatus.Contains("OFF"))
  406. {
  407. WaferManager.Instance.DeleteWafer(ModuleName.TMRobot, 1);
  408. }
  409. }
  410. else
  411. {
  412. if (RevMsg.Trim() == "_RDY" || (RevMsg.Contains("_RDY") && !RevMsg.Contains("_ERR")))
  413. {
  414. _currentStep = VRStep.Idle;
  415. _status = RState.End;
  416. }
  417. else
  418. {
  419. ReportWrongMsg(RevMsg);
  420. }
  421. }
  422. break;
  423. default:
  424. if (!RevMsg.Contains("_EVENT"))
  425. ReportWrongMsg(RevMsg);
  426. break;
  427. }
  428. }
  429. }
  430. private void GetCheckLoadResult(string revMsg)
  431. {
  432. Match result = _rex_check_load.Match(revMsg);
  433. string Arm = result.Groups[1].Value;
  434. string WaferStatus = result.Groups[2].Value;
  435. if (WaferStatus == "ON")
  436. {
  437. WaferManager.Instance.CreateWafer(ModuleName.TMRobot, Arm == "A" ? 0 : 1, Aitex.Core.Common.WaferStatus.Unknown);
  438. }
  439. }
  440. private void ErrorMessageHandler(string errorcode)
  441. {
  442. string ErrorInfo;
  443. if (_error2msg.ContainsKey(errorcode))
  444. {
  445. ErrorInfo = _error2msg[errorcode];
  446. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, ErrorInfo);
  447. }
  448. else
  449. {
  450. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, $"Dictionary Not Contains error code:{errorcode}");
  451. }
  452. _IsHomed = false;
  453. }
  454. private void OnErrorHappen(ErrorEventArgs args)
  455. {
  456. //Singleton<RouteManager>.Instance.SETM.PostMsg(SETMEntity.MSG.Error);
  457. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, $"HongHu TMRobot Error: {args.Reason} while {_currentStep}");
  458. }
  459. private void ReportWrongMsg(string revMsg)
  460. {
  461. LOG.Write(eEvent.WARN_DEVICE_INFO, ModuleName.TMRobot, $"Receive wrong message:{revMsg} while {_currentStep}");
  462. }
  463. public void SetRobotMovingInfo(RobotAction action, Hand hand, ModuleName target)
  464. {
  465. _robotMoveInfo.Action = action;
  466. _robotMoveInfo.ArmTarget = hand == Hand.Blade1 ? RobotArm.ArmA : (hand == Hand.Both ? RobotArm.Both : RobotArm.ArmB);
  467. _robotMoveInfo.BladeTarget = $"{_robotMoveInfo.ArmTarget}.{target}";
  468. }
  469. }
  470. }