HongHuVR.cs 13 KB

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