HongHuVR.cs 22 KB

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