HongHuVR.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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.Linq;
  13. using System.Runtime.InteropServices;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. using System.Threading.Tasks;
  17. using Venus_Core;
  18. using Venus_RT.Modules;
  19. namespace Venus_RT.Devices.VCE
  20. {
  21. //泓浒
  22. enum VRStep
  23. {
  24. Idle,
  25. Home,
  26. Move,
  27. Halt,
  28. Release,
  29. Goto,
  30. Pick,
  31. PickExtend,
  32. PickRetract,
  33. Place,
  34. PlaceExtend,
  35. PlaceRetract,
  36. Xfer,
  37. CheckLoad_ArmA,
  38. CheckLoad_ArmB,
  39. SetLoad,
  40. ReQueryLoad,
  41. }
  42. public class HongHuVR : ITransferRobot
  43. {
  44. private readonly AsyncSocket _socket;
  45. private const string EOF = "\r";
  46. private RState _status;
  47. private bool _IsHomed;
  48. private VRStep _currentStep = VRStep.Idle;
  49. private Dictionary<ModuleName, int> _StationNumbers = new Dictionary<ModuleName, int>();
  50. public RState Status { get { return _status; } }
  51. public bool IsHomed { get { return _IsHomed; } }
  52. private string Hand2Arm(Hand hand) => hand == Hand.Blade1 ? "B" : "A";
  53. //private readonly Regex _rex_check_load = new Regex(@"LOAD\s+(A|B)\s+(\w+)\s*");
  54. private readonly Regex _rex_check_load = new Regex(@"CHECK LOAD\s*");
  55. private readonly Regex _rex_error_code = new Regex(@"_ERR\s+(\d+)\s*");
  56. private RobotMoveInfo _robotMoveInfo = new RobotMoveInfo();
  57. public RobotMoveInfo TMRobotMoveInfo { get { return _robotMoveInfo; } }
  58. public double Offset_X => 0;
  59. public double Offset_Y => 0;
  60. public double Offset_D => 0;
  61. public Dictionary<string, string> _error2msg = new Dictionary<string, string>()
  62. {
  63. { "701" , "设备检查互锁,发现无法执行"},
  64. { "722" , "设备动作,但是发生异常"},
  65. };
  66. private BlockingCollection<RobotAnimationData> blockingCollection = new BlockingCollection<RobotAnimationData>();
  67. public HongHuVR()
  68. {
  69. _socket = new AsyncSocket("", EOF);
  70. _socket.Connect(SC.GetStringValue($"SETM.IPAddress"));
  71. _socket.OnDataChanged += OnReceiveMessage;
  72. _socket.OnErrorHappened += OnErrorHappen;
  73. _status = RState.Init;
  74. _IsHomed = false;
  75. _StationNumbers[ModuleName.VCE1] = SC.GetValue<int>("SETM.VCE1StationNumber");
  76. _StationNumbers[ModuleName.VPA] = SC.GetValue<int>("SETM.VPAStationNumber");
  77. _StationNumbers[ModuleName.PMA] = SC.GetValue<int>("SETM.PMAStationNumber");
  78. _StationNumbers[ModuleName.PMB] = SC.GetValue<int>("SETM.PMBStationNumber");
  79. _StationNumbers[ModuleName.PMC] = SC.GetValue<int>("SETM.PMCStationNumber");
  80. _StationNumbers[ModuleName.PMD] = SC.GetValue<int>("SETM.PMDStationNumber");
  81. WaferManager.Instance.SubscribeLocation(ModuleName.TMRobot, 2);
  82. Task.Run(() =>
  83. {
  84. foreach (var data in blockingCollection.GetConsumingEnumerable())
  85. {
  86. _robotMoveInfo.Action = data.Action;
  87. _robotMoveInfo.ArmTarget = data.Hand == Hand.Blade1 ? RobotArm.ArmA : (data.Hand == Hand.Both ? RobotArm.Both : RobotArm.ArmB);
  88. _robotMoveInfo.BladeTarget = $"{_robotMoveInfo.ArmTarget}.{data.Target}";
  89. System.Threading.Thread.Sleep(300);
  90. }
  91. });
  92. }
  93. //初始化某个轴
  94. //1.清错
  95. //2.设备上电
  96. //3.各轴按顺序运动
  97. public bool Home()
  98. {
  99. _status = RState.Running;
  100. _currentStep = VRStep.Home;
  101. return _SendCommand("HOME ALL");
  102. }
  103. public bool Halt()
  104. {
  105. _status = RState.Running;
  106. _currentStep = VRStep.Halt;
  107. return _SendCommand("HALT");
  108. }
  109. public bool Release()
  110. {
  111. _status = RState.Running;
  112. _currentStep = VRStep.Release;
  113. return _SendCommand("RELEASE");
  114. }
  115. //public bool MOVE()
  116. //{
  117. //}
  118. public bool Pick(ModuleName station, int slot, Hand hand)
  119. {
  120. if (!CheckRobotStatus())
  121. return false;
  122. _currentStep = VRStep.Pick;
  123. _status = RState.Running;
  124. SetRobotMovingInfo(RobotAction.Picking, hand, station);
  125. return _SendCommand($"PICK {_StationNumbers[station]} ARM {Hand2Arm(hand)}");
  126. }
  127. public bool PickExtend(ModuleName station, int slot, Hand hand)
  128. {
  129. if (!CheckRobotStatus())
  130. return false;
  131. _currentStep = VRStep.PickExtend;
  132. _status = RState.Running;
  133. SetRobotMovingInfo(RobotAction.Extending, hand, station);
  134. return _SendCommand($"PICK {_StationNumbers[station]} ARM {Hand2Arm(hand)} ENRT NR");
  135. }
  136. public bool PickRetract(ModuleName station, int slot, Hand hand)
  137. {
  138. if (!CheckRobotStatus())
  139. return false;
  140. _currentStep = VRStep.PickRetract;
  141. _status = RState.Running;
  142. SetRobotMovingInfo(RobotAction.Retracting, hand, station);
  143. return _SendCommand($"PICK {_StationNumbers[station]} ARM {Hand2Arm(hand)} STRT NR");
  144. }
  145. public bool Place(ModuleName station, int slot, Hand hand)
  146. {
  147. if (!CheckRobotStatus())
  148. return false;
  149. _currentStep = VRStep.Place;
  150. _status = RState.Running;
  151. SetRobotMovingInfo(RobotAction.Placing, hand, station);
  152. return _SendCommand($"PLACE {_StationNumbers[station]} ARM {Hand2Arm(hand)}");
  153. }
  154. public bool PlaceExtend(ModuleName station, int slot, Hand hand)
  155. {
  156. if (!CheckRobotStatus())
  157. return false;
  158. _currentStep = VRStep.Place;
  159. _status = RState.Running;
  160. SetRobotMovingInfo(RobotAction.Extending, hand, station);
  161. return _SendCommand($"PLACE {_StationNumbers[station]} ARM {Hand2Arm(hand)} ENRT NR");
  162. }
  163. public bool PlaceRetract(ModuleName station, int slot, Hand hand)
  164. {
  165. if (!CheckRobotStatus())
  166. return false;
  167. _currentStep = VRStep.Place;
  168. _status = RState.Running;
  169. SetRobotMovingInfo(RobotAction.Retracting, hand, station);
  170. return _SendCommand($"PLACE {_StationNumbers[station]} ARM {Hand2Arm(hand)} STRT NR");
  171. }
  172. public bool Transfer(ModuleName fromstation, ModuleName tostation, Hand hand)
  173. {
  174. if (!CheckRobotStatus())
  175. return false;
  176. _currentStep = VRStep.Xfer;
  177. _status = RState.Running;
  178. return _SendCommand($"XFER ARM {Hand2Arm(hand)} {_StationNumbers[fromstation]} {_StationNumbers[tostation]}");
  179. }
  180. public bool CheckLoad(Hand hand = Hand.Blade1)
  181. {
  182. if (_currentStep != VRStep.Home && _currentStep != VRStep.CheckLoad_ArmA && !CheckRobotStatus())
  183. return false;
  184. _currentStep = hand == Hand.Blade2 ? VRStep.CheckLoad_ArmB : VRStep.CheckLoad_ArmA;
  185. _status = RState.Running;
  186. return _SendCommand($"CHECK LOAD {Hand2Arm(hand)}");
  187. }
  188. public bool QueryAwc()
  189. {
  190. return true;
  191. }
  192. public bool Goto(ModuleName station, int slot, Hand hand)
  193. {
  194. _currentStep = VRStep.Goto;
  195. _status = RState.Running;
  196. return _SendCommand($"GOTO N {_StationNumbers[station]} ARM {Hand2Arm(hand)}");
  197. }
  198. public bool MoveTo(ModuleName stnFrom, ModuleName stnTo, Hand hand)
  199. {
  200. _currentStep = VRStep.Move;
  201. _status = RState.Running;
  202. return _SendCommand($"MOVE R ABS 100 ARM {Hand2Arm(hand)}"); ;
  203. }
  204. private bool _SendCommand(string cmd)
  205. {
  206. LOG.WriteSingeLine(eEvent.INFO_TM_ROBOT, ModuleName.TMRobot, $"Send Command to HongHu TMRobot: {cmd}");
  207. return _socket.Write(cmd + EOF);
  208. }
  209. private bool CheckRobotStatus()
  210. {
  211. if (Status == RState.Init)
  212. {
  213. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, "TMRobot is not homed, please home first.");
  214. return false;
  215. }
  216. else if (Status == RState.Running)
  217. {
  218. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, "TMRobot is busy, please wait a minute");
  219. return false;
  220. }
  221. else if (Status == RState.Failed || Status == RState.Timeout)
  222. {
  223. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, "TMRobot has a error, please check and fix the hardware issue and home it");
  224. return false;
  225. }
  226. return true;
  227. }
  228. private void OnReceiveMessage(string RevMsg)
  229. {
  230. LOG.WriteSingeLine(eEvent.INFO_TM_ROBOT, ModuleName.TMRobot, $"Receive message from HongHu TMRobot: {RevMsg}, while {_currentStep}");
  231. RevMsg = RevMsg.Trim();
  232. if (_rex_error_code.IsMatch(RevMsg))
  233. {
  234. _IsHomed = false;
  235. _status = RState.Failed;
  236. var results = _rex_error_code.Match(RevMsg);
  237. ErrorMessageHandler(results.Groups[1].Value);
  238. return;
  239. }
  240. switch (_currentStep)
  241. {
  242. case VRStep.Goto:
  243. case VRStep.Halt:
  244. case VRStep.Move:
  245. case VRStep.Xfer:
  246. case VRStep.Pick:
  247. case VRStep.PickExtend:
  248. case VRStep.PickRetract:
  249. case VRStep.Place:
  250. case VRStep.PlaceExtend:
  251. case VRStep.PlaceRetract:
  252. {
  253. if (RevMsg.Trim() == "_RDY" || (RevMsg.Contains("_RDY") && !RevMsg.Contains("_ERR")))
  254. {
  255. _currentStep = VRStep.Idle;
  256. _status = RState.End;
  257. }
  258. else
  259. {
  260. ReportWrongMsg(RevMsg);
  261. }
  262. if (_currentStep != VRStep.PickExtend && _currentStep != VRStep.PlaceExtend)
  263. SetRobotMovingInfo(RobotAction.None, Hand.Both, ModuleName.TMRobot);
  264. }
  265. break;
  266. case VRStep.Home:
  267. {
  268. if (RevMsg.Trim() == "_RDY")
  269. {
  270. //CheckLoad(Hand.Blade1);
  271. _currentStep = VRStep.Idle;
  272. _status = RState.End;
  273. _IsHomed = true;
  274. }
  275. else
  276. ReportWrongMsg(RevMsg);
  277. }
  278. break;
  279. case VRStep.CheckLoad_ArmA:
  280. {
  281. if (_rex_check_load.IsMatch(RevMsg))
  282. {
  283. GetCheckLoadResult(RevMsg);
  284. CheckLoad(Hand.Blade2);
  285. }
  286. else
  287. ReportWrongMsg(RevMsg);
  288. }
  289. break;
  290. case VRStep.CheckLoad_ArmB:
  291. {
  292. if (_rex_check_load.IsMatch(RevMsg))
  293. {
  294. GetCheckLoadResult(RevMsg);
  295. _currentStep = VRStep.Idle;
  296. _status = RState.End;
  297. _IsHomed = true;
  298. }
  299. }
  300. break;
  301. default:
  302. if (!RevMsg.Contains("_EVENT"))
  303. ReportWrongMsg(RevMsg);
  304. break;
  305. }
  306. }
  307. private void GetCheckLoadResult(string revMsg)
  308. {
  309. Match result = _rex_check_load.Match(revMsg);
  310. string Arm = result.Groups[1].Value;
  311. string WaferStatus = result.Groups[2].Value;
  312. if (WaferStatus == "ON")
  313. {
  314. WaferManager.Instance.CreateWafer(ModuleName.TMRobot, Arm == "A" ? 0 : 1, Aitex.Core.Common.WaferStatus.Unknown);
  315. }
  316. }
  317. private void ErrorMessageHandler(string errorcode)
  318. {
  319. string ErrorInfo;
  320. if (_error2msg.ContainsKey(errorcode))
  321. {
  322. ErrorInfo = _error2msg[errorcode];
  323. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, ErrorInfo);
  324. }
  325. else
  326. {
  327. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, $"Dictionary Not Contains error code:{errorcode}");
  328. }
  329. }
  330. private void OnErrorHappen(ErrorEventArgs args)
  331. {
  332. //Singleton<RouteManager>.Instance.SETM.PostMsg(SETMEntity.MSG.Error);
  333. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, $"HongHu TMRobot Error: {args.Reason} while {_currentStep}");
  334. }
  335. private void ReportWrongMsg(string revMsg)
  336. {
  337. LOG.Write(eEvent.ERR_DEVICE_INFO, ModuleName.TMRobot, $"Receive wrong message:{revMsg} while {_currentStep}");
  338. }
  339. public void SetRobotMovingInfo(RobotAction action, Hand hand, ModuleName target)
  340. {
  341. _robotMoveInfo.Action = action;
  342. _robotMoveInfo.ArmTarget = hand == Hand.Blade1 ? RobotArm.ArmA : (hand == Hand.Both ? RobotArm.Both : RobotArm.ArmB);
  343. _robotMoveInfo.BladeTarget = $"{_robotMoveInfo.ArmTarget}.{target}";
  344. }
  345. }
  346. }