HongHuVR.cs 14 KB

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