HongHuVR.cs 18 KB

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