HongHuVR.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. private ModuleName _module;
  100. public HongHuVR(ModuleName module)
  101. {
  102. _module = module;
  103. _socket = new AsyncSocket("", EOF);
  104. _socket.Connect(SC.GetStringValue($"{_module}.IPAddress"));
  105. _socket.OnDataChanged += OnReceiveMessage;
  106. _socket.OnErrorHappened += OnErrorHappen;
  107. _status = RState.Init;
  108. _IsHomed = false;
  109. _StationNumbers[ModuleName.VCE1] = SC.GetValue<int>($"{_module}.VCE1StationNumber");
  110. _StationNumbers[ModuleName.VCEA] = SC.GetValue<int>($"{_module}.VCEAStationNumber");
  111. _StationNumbers[ModuleName.VCEB] = SC.GetValue<int>($"{_module}.VCEBStationNumber");
  112. _StationNumbers[ModuleName.VPA] = SC.GetValue<int>($"{_module}.VPAStationNumber");
  113. _StationNumbers[ModuleName.PMA] = SC.GetValue<int>($"{_module}.PMAStationNumber");
  114. _StationNumbers[ModuleName.PMB] = SC.GetValue<int>($"{_module}.PMBStationNumber");
  115. _StationNumbers[ModuleName.PMC] = SC.GetValue<int>($"{_module}.PMCStationNumber");
  116. _StationNumbers[ModuleName.PMD] = SC.GetValue<int>($"{_module}.PMDStationNumber");
  117. WaferManager.Instance.SubscribeLocation(ModuleName.TMRobot, 2);
  118. Task.Run(() =>
  119. {
  120. foreach (var data in blockingCollection.GetConsumingEnumerable())
  121. {
  122. _robotMoveInfo.Action = data.Action;
  123. _robotMoveInfo.ArmTarget = data.Hand == Hand.Blade1 ? RobotArm.ArmA : (data.Hand == Hand.Both ? RobotArm.Both : RobotArm.ArmB);
  124. _robotMoveInfo.BladeTarget = $"{_robotMoveInfo.ArmTarget}.{data.Target}";
  125. System.Threading.Thread.Sleep(300);
  126. }
  127. });
  128. }
  129. //初始化某个轴
  130. //1.清错
  131. //2.设备上电
  132. //3.各轴按顺序运动
  133. public bool Home()
  134. {
  135. _status = RState.Running;
  136. _currentStep = VRStep.Home;
  137. return _SendCommand("HOME ALL");
  138. }
  139. public bool Halt()
  140. {
  141. _status = RState.Running;
  142. _currentStep = VRStep.Halt;
  143. return _SendCommand("HALT");
  144. }
  145. public bool Release()
  146. {
  147. _status = RState.Running;
  148. _currentStep = VRStep.Release;
  149. return _SendCommand("RELEASE");
  150. }
  151. //public bool MOVE()
  152. //{
  153. //}
  154. public bool Pick(ModuleName station, int slot, Hand hand)
  155. {
  156. if (!CheckRobotStatus())
  157. return false;
  158. _currentStep = VRStep.Pick;
  159. _status = RState.Running;
  160. SetRobotMovingInfo(RobotAction.Picking, hand, station);
  161. return _SendCommand($"PICK {_StationNumbers[station]} ARM {Hand2Arm(hand)}");
  162. }
  163. public bool PickExtend(ModuleName station, int slot, Hand hand)
  164. {
  165. if (!CheckRobotStatus())
  166. return false;
  167. _currentStep = VRStep.PickExtend;
  168. _status = RState.Running;
  169. SetRobotMovingInfo(RobotAction.Extending, hand, station);
  170. return _SendCommand($"PICK {_StationNumbers[station]} ARM {Hand2Arm(hand)} ENRT NR");
  171. }
  172. public bool PickRetract(ModuleName station, int slot, Hand hand)
  173. {
  174. if (!CheckRobotStatus())
  175. return false;
  176. _currentStep = VRStep.PickRetract;
  177. _status = RState.Running;
  178. SetRobotMovingInfo(RobotAction.Retracting, hand, station);
  179. return _SendCommand($"PICK {_StationNumbers[station]} ARM {Hand2Arm(hand)} STRT NR");
  180. }
  181. public bool Place(ModuleName station, int slot, Hand hand)
  182. {
  183. if (!CheckRobotStatus())
  184. return false;
  185. _currentStep = VRStep.Place;
  186. _status = RState.Running;
  187. SetRobotMovingInfo(RobotAction.Placing, hand, station);
  188. return _SendCommand($"PLACE {_StationNumbers[station]} ARM {Hand2Arm(hand)}");
  189. }
  190. public bool PlaceExtend(ModuleName station, int slot, Hand hand)
  191. {
  192. if (!CheckRobotStatus())
  193. return false;
  194. _currentStep = VRStep.Place;
  195. _status = RState.Running;
  196. SetRobotMovingInfo(RobotAction.Extending, hand, station);
  197. return _SendCommand($"PLACE {_StationNumbers[station]} ARM {Hand2Arm(hand)} ENRT NR");
  198. }
  199. public bool PlaceRetract(ModuleName station, int slot, Hand hand)
  200. {
  201. if (!CheckRobotStatus())
  202. return false;
  203. _currentStep = VRStep.Place;
  204. _status = RState.Running;
  205. SetRobotMovingInfo(RobotAction.Retracting, hand, station);
  206. return _SendCommand($"PLACE {_StationNumbers[station]} ARM {Hand2Arm(hand)} STRT NR");
  207. }
  208. public bool Transfer(ModuleName fromstation, ModuleName tostation, Hand hand)
  209. {
  210. if (!CheckRobotStatus())
  211. return false;
  212. _currentStep = VRStep.Xfer;
  213. _status = RState.Running;
  214. return _SendCommand($"XFER ARM {Hand2Arm(hand)} {_StationNumbers[fromstation]} {_StationNumbers[tostation]}");
  215. }
  216. public bool CheckLoad(Hand hand = Hand.Blade1)
  217. {
  218. if (_currentStep != VRStep.Home && _currentStep != VRStep.CheckLoad_ArmA && !CheckRobotStatus())
  219. return false;
  220. _currentStep = hand == Hand.Blade2 ? VRStep.CheckLoad_ArmB : VRStep.CheckLoad_ArmA;
  221. _status = RState.Running;
  222. string arm = hand == Hand.Blade2 ? "B" : "A";
  223. return _SendCommand($"CHECK LOAD {_StationNumbers[ModuleName.VPA]} {arm}");
  224. }
  225. public bool QueryAwc()
  226. {
  227. return true;
  228. }
  229. public bool Goto(ModuleName station, int slot, Hand hand)
  230. {
  231. if (!CheckRobotStatus())
  232. return false;
  233. _currentStep = VRStep.Goto;
  234. _status = RState.Running;
  235. SetRobotMovingInfo(RobotAction.Rotating, hand, station);
  236. return _SendCommand($"Goto {_StationNumbers[station]} ARM {Hand2Arm(hand)}");
  237. }
  238. public bool MoveTo(ModuleName stnFrom, ModuleName stnTo, Hand hand)
  239. {
  240. _currentStep = VRStep.Move;
  241. _status = RState.Running;
  242. return _SendCommand($"MOVE R ABS 100 ARM {Hand2Arm(hand)}"); ;
  243. }
  244. public bool PickWithOffset(ModuleName station, int slot, Hand hand, int Roffset, int Toffset)
  245. {
  246. _currentStep = VRStep.Pick;
  247. _status = RState.Running;
  248. return _SendCommand($"PICK {_StationNumbers[station]} ARM {Hand2Arm(hand)} RO {Roffset} TO {Toffset}"); ;
  249. }
  250. public bool ReQueryLoadA()
  251. {
  252. _currentStep = VRStep.ReQueryLoadA;
  253. _status = RState.Running;
  254. return _SendCommand($"RQ LOAD ARM A");
  255. }
  256. public bool ReQueryLoadB()
  257. {
  258. _currentStep = VRStep.ReQueryLoadB;
  259. _status = RState.Running;
  260. return _SendCommand($"RQ LOAD ARM B");
  261. }
  262. private bool _SendCommand(string cmd)
  263. {
  264. LOG.WriteSingeLine(eEvent.INFO_TM_ROBOT, ModuleName.TMRobot, $"Send Command to TMRobot: {cmd},Connect:{_socket.IsConnected}");
  265. return _socket.Write(cmd + EOF);
  266. }
  267. private bool CheckRobotStatus()
  268. {
  269. if (Status == RState.Init)
  270. {
  271. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, "TMRobot is not homed, please home first.");
  272. return false;
  273. }
  274. else if (Status == RState.Running)
  275. {
  276. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, "TMRobot is busy, please wait a minute");
  277. return false;
  278. }
  279. else if (Status == RState.Failed || Status == RState.Timeout)
  280. {
  281. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, "TMRobot has a error, please check and fix the hardware issue and home it");
  282. return false;
  283. }
  284. return true;
  285. }
  286. private void OnReceiveMessage(string revMsg)
  287. {
  288. string[] ResMsgs = revMsg.Split('\n');
  289. foreach (string revRawMsg in ResMsgs)
  290. {
  291. if (string.IsNullOrWhiteSpace(revRawMsg)) continue;
  292. string RevMsg = revRawMsg.Trim();
  293. LOG.WriteSingeLine(eEvent.INFO_TM_ROBOT, ModuleName.TMRobot, $"Receive message from TMRobot: {RevMsg}, while {_currentStep}");
  294. if (_rex_error_code.IsMatch(RevMsg))
  295. {
  296. _IsHomed = false;
  297. _status = RState.Failed;
  298. var results = _rex_error_code.Match(RevMsg);
  299. ErrorMessageHandler(results.Groups[1].Value);
  300. return;
  301. }
  302. switch (_currentStep)
  303. {
  304. case VRStep.Goto:
  305. case VRStep.Halt:
  306. case VRStep.Move:
  307. case VRStep.Xfer:
  308. case VRStep.Pick:
  309. case VRStep.PickExtend:
  310. case VRStep.PickRetract:
  311. case VRStep.Place:
  312. case VRStep.PlaceExtend:
  313. case VRStep.PlaceRetract:
  314. {
  315. if (RevMsg.Trim() == "_RDY" || (RevMsg.Contains("_RDY") && !RevMsg.Contains("_ERR")))
  316. {
  317. _currentStep = VRStep.Idle;
  318. _status = RState.End;
  319. }
  320. else
  321. {
  322. ReportWrongMsg(RevMsg);
  323. }
  324. if (_currentStep != VRStep.PickExtend && _currentStep != VRStep.PlaceExtend)
  325. SetRobotMovingInfo(RobotAction.None, Hand.Both, ModuleName.TMRobot);
  326. }
  327. break;
  328. case VRStep.Home:
  329. {
  330. if (RevMsg.Trim() == "_RDY")
  331. {
  332. //CheckLoad(Hand.Blade1);
  333. _currentStep = VRStep.Idle;
  334. _status = RState.End;
  335. _IsHomed = true;
  336. SetRobotMovingInfo(RobotAction.Homing, Hand.Both, ModuleName.SETM);
  337. }
  338. else
  339. ReportWrongMsg(RevMsg);
  340. }
  341. break;
  342. case VRStep.CheckLoad_ArmA:
  343. {
  344. if (_rex_check_load.IsMatch(RevMsg))
  345. {
  346. GetCheckLoadResult(RevMsg);
  347. //CheckLoad(Hand.Blade2);
  348. }
  349. if (RevMsg.Trim() == "_RDY" || (RevMsg.Contains("_RDY") && !RevMsg.Contains("_ERR")))
  350. {
  351. _currentStep = VRStep.Idle;
  352. _status = RState.End;
  353. }
  354. }
  355. break;
  356. case VRStep.CheckLoad_ArmB:
  357. {
  358. if (_rex_check_load.IsMatch(RevMsg))
  359. {
  360. GetCheckLoadResult(RevMsg);
  361. //_currentStep = VRStep.Idle;
  362. //_status = RState.End;
  363. //_IsHomed = true;
  364. }
  365. if (RevMsg.Trim() == "_RDY" || (RevMsg.Contains("_RDY") && !RevMsg.Contains("_ERR")))
  366. {
  367. _currentStep = VRStep.Idle;
  368. _status = RState.End;
  369. }
  370. }
  371. break;
  372. case VRStep.ReQueryLoadA:
  373. if (_rex_rq_load_A.IsMatch(RevMsg))
  374. {
  375. string WaferStatus = RevMsg.Split(' ')[2];
  376. //LOG.Write(eEvent.WARN_DEFAULT_WARN, ModuleName.TMRobot, WaferStatus);
  377. if (WaferStatus.Contains("ON") && WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 0))
  378. {
  379. WaferManager.Instance.CreateWafer(ModuleName.TMRobot, 0, Aitex.Core.Common.WaferStatus.Normal);
  380. }
  381. if (WaferStatus.Contains("OFF"))
  382. {
  383. //LOG.Write(eEvent.WARN_DEFAULT_WARN, ModuleName.TMRobot, "contains off");
  384. WaferManager.Instance.DeleteWafer(ModuleName.TMRobot, 0);
  385. }
  386. }
  387. else
  388. {
  389. if (RevMsg.Trim() == "_RDY" || (RevMsg.Contains("_RDY") && !RevMsg.Contains("_ERR")))
  390. {
  391. _currentStep = VRStep.Idle;
  392. _status = RState.End;
  393. }
  394. else
  395. {
  396. ReportWrongMsg(RevMsg);
  397. }
  398. }
  399. break;
  400. case VRStep.ReQueryLoadB:
  401. if (_rex_rq_load_B.IsMatch(RevMsg))
  402. {
  403. string WaferStatus = RevMsg.Split(' ')[2];
  404. //LOG.Write(eEvent.WARN_DEFAULT_WARN, ModuleName.TMRobot, WaferStatus);
  405. if (WaferStatus.Contains("ON") && WaferManager.Instance.CheckNoWafer(ModuleName.TMRobot, 1))
  406. {
  407. WaferManager.Instance.CreateWafer(ModuleName.TMRobot, 1, Aitex.Core.Common.WaferStatus.Normal);
  408. }
  409. if (WaferStatus.Contains("OFF"))
  410. {
  411. WaferManager.Instance.DeleteWafer(ModuleName.TMRobot, 1);
  412. }
  413. }
  414. else
  415. {
  416. if (RevMsg.Trim() == "_RDY" || (RevMsg.Contains("_RDY") && !RevMsg.Contains("_ERR")))
  417. {
  418. _currentStep = VRStep.Idle;
  419. _status = RState.End;
  420. }
  421. else
  422. {
  423. ReportWrongMsg(RevMsg);
  424. }
  425. }
  426. break;
  427. default:
  428. if (!RevMsg.Contains("_EVENT"))
  429. ReportWrongMsg(RevMsg);
  430. break;
  431. }
  432. }
  433. }
  434. private void GetCheckLoadResult(string revMsg)
  435. {
  436. Match result = _rex_check_load.Match(revMsg);
  437. string Arm = result.Groups[1].Value;
  438. string WaferStatus = result.Groups[2].Value;
  439. if (WaferStatus == "ON")
  440. {
  441. WaferManager.Instance.CreateWafer(ModuleName.TMRobot, Arm == "A" ? 0 : 1, Aitex.Core.Common.WaferStatus.Unknown);
  442. }
  443. }
  444. private void ErrorMessageHandler(string errorcode)
  445. {
  446. string ErrorInfo;
  447. if (_error2msg.ContainsKey(errorcode))
  448. {
  449. ErrorInfo = _error2msg[errorcode];
  450. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, ErrorInfo);
  451. }
  452. else
  453. {
  454. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, $"Dictionary Not Contains error code:{errorcode}");
  455. }
  456. _IsHomed = false;
  457. }
  458. private void OnErrorHappen(ErrorEventArgs args)
  459. {
  460. //Singleton<RouteManager>.Instance.SETM.PostMsg(SETMEntity.MSG.Error);
  461. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, $"HongHu TMRobot Error: {args.Reason} while {_currentStep}");
  462. }
  463. private void ReportWrongMsg(string revMsg)
  464. {
  465. LOG.Write(eEvent.WARN_DEVICE_INFO, ModuleName.TMRobot, $"Receive wrong message:{revMsg} while {_currentStep}");
  466. }
  467. public void SetRobotMovingInfo(RobotAction action, Hand hand, ModuleName target)
  468. {
  469. _robotMoveInfo.Action = action;
  470. _robotMoveInfo.ArmTarget = hand == Hand.Blade1 ? RobotArm.ArmA : (hand == Hand.Both ? RobotArm.Both : RobotArm.ArmB);
  471. _robotMoveInfo.BladeTarget = $"{_robotMoveInfo.ArmTarget}.{target}";
  472. }
  473. }
  474. }