HongHuVR.cs 16 KB

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