HongHuVR.cs 13 KB

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