HongHuVR.cs 12 KB

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