HongHuVR.cs 18 KB

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