JelRobotWithX.cs 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using System.Text;
  6. using Aitex.Core.Common.DeviceData;
  7. using Aitex.Core.RT.Device;
  8. using Aitex.Core.RT.Device.Unit;
  9. using Aitex.Core.RT.Event;
  10. using Aitex.Core.RT.Log;
  11. using Aitex.Core.RT.OperationCenter;
  12. using Aitex.Core.RT.SCCore;
  13. using Aitex.Core.Util;
  14. using MECF.Framework.Common.Communications;
  15. using MECF.Framework.Common.Device.Bases;
  16. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Common;
  17. using Newtonsoft.Json;
  18. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.RobotBase;
  19. using MECF.Framework.Common.Equipment;
  20. using MECF.Framework.Common.SubstrateTrackings;
  21. using System.Threading;
  22. using Aitex.Core.Common;
  23. using Aitex.Core.RT.DataCenter;
  24. using System.Text.RegularExpressions;
  25. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
  26. using MECF.Framework.Common.CommonData;
  27. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.LoadPortBase;
  28. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.WalkingAixs;
  29. using Aitex.Core.RT.Routine;
  30. using static MECF.Framework.RT.EquipmentLibrary.HardwareUnits.WalkingAixs.WalkingAxisBaseDevice;
  31. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.JEL
  32. {
  33. public class JelRobotWithX : JelRobot, IConnection
  34. {
  35. protected WalkingAxisBaseDevice m_WalkingAxis { get; set; }
  36. protected bool m_IsNeedMoveWalkingAxis { get; set; }
  37. public JelRobotWithX(string module, string name, string scRoot,WalkingAxisBaseDevice xaxis=null) :
  38. base(module, name, scRoot)
  39. {
  40. m_WalkingAxis = xaxis;
  41. }
  42. public override bool OnTimer()
  43. {
  44. return base.OnTimer();
  45. }
  46. protected override bool fReset(object[] param)
  47. {
  48. m_WalkingAxis.Reset(null);
  49. return base.fReset(param);
  50. }
  51. protected override bool fMonitorReset(object[] param)
  52. {
  53. IsBusy = false;
  54. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  55. {
  56. HandlerError("ResetTimeout");
  57. return true;
  58. }
  59. if (!m_WalkingAxis.IsReady()) return false;
  60. return base.fMonitorReset(param);
  61. }
  62. protected override bool fStartGoTo(object[] param)
  63. {
  64. RobotArmEnum arm = (RobotArmEnum)param[0];
  65. ModuleName module = (ModuleName)Enum.Parse(typeof(ModuleName), param[1].ToString());
  66. BladeTarget = module;
  67. Blade1Target = module;
  68. Blade2Target = module;
  69. int slot = (int)param[2] + 1;
  70. RobotPostionEnum postype = (RobotPostionEnum)param[3];
  71. if (!m_WalkingAxis.IsReady())
  72. return false;
  73. m_WalkingAxis.MoveTo(module);
  74. int compaundcmdNO = SC.GetValue<int>($"Robot.{Name}.{arm}{postype}CmdNO");
  75. var wz = WaferManager.Instance.GetWaferSize(RobotModuleName, (int)arm);
  76. if (ModuleHelper.IsLoadPort(module))
  77. {
  78. var lp = DEVICE.GetDevice<LoadPortBaseDevice>(module.ToString());
  79. if (lp != null) lp.NoteTransferStart();
  80. }
  81. int bankno;
  82. int cassetteNO;
  83. if(!GetBankAndCassetteNumber(module, wz, out bankno, out cassetteNO))
  84. return false;
  85. lock (_locker)
  86. {
  87. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "BC", bankno.ToString("X")));
  88. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "WCP", cassetteNO.ToString()));
  89. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "WCD", slot.ToString()));
  90. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "BC"));
  91. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "WCP"));
  92. _lstMonitorHandler.AddLast(new JelRobotCompaundCommandHandler(this, compaundcmdNO.ToString()));
  93. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "G"));
  94. }
  95. _dtActionStart = DateTime.Now;
  96. return true;
  97. }
  98. protected override bool fMonitorGoTo(object[] param)
  99. {
  100. IsBusy = false;
  101. RobotArmEnum arm = (RobotArmEnum)CurrentParamter[0];
  102. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[1].ToString());
  103. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  104. {
  105. HandlerError($"Goto{BladeTarget}Timeout");
  106. return true;
  107. }
  108. if(m_WalkingAxis.DeviceState == WalkingAxisState.ERROR)
  109. {
  110. HandlerError($"WalkingAxisError");
  111. return true;
  112. }
  113. if (_lstMonitorHandler.Count == 0 && !_connection.IsBusy)
  114. {
  115. if (CurrentCompoundCommandStatus == JelCommandStatus.NormalEnd)
  116. {
  117. BladeTarget = ModuleName.System;
  118. Blade1Target = ModuleName.System;
  119. Blade2Target = ModuleName.System;
  120. CmdTarget = ModuleName.System;
  121. MoveInfo = new RobotMoveInfo()
  122. {
  123. Action = RobotAction.Picking,
  124. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  125. BladeTarget = BuildBladeTarget(),
  126. };
  127. if (ModuleHelper.IsLoadPort(tempmodule))
  128. {
  129. var lp = DEVICE.GetDevice<LoadPortBaseDevice>(tempmodule.ToString());
  130. if (lp != null) lp.NoteTransferStop();
  131. }
  132. return true;
  133. }
  134. if (CurrentCompoundCommandStatus == JelCommandStatus.InError)
  135. {
  136. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "GER"));
  137. HandlerError("GotoFailed");
  138. }
  139. }
  140. return false;
  141. }
  142. protected override bool fStartInit(object[] param)
  143. {
  144. _dtActionStart = DateTime.Now;
  145. int compaundcmdNO = SC.GetValue<int>($"Robot.{Name}.InitCmdNO");
  146. int Robotspeed = SC.GetValue<int>($"Robot.{Name}.RobotSpeed");
  147. if (Robotspeed < 0)
  148. Robotspeed = 0;
  149. if (Robotspeed > 9)
  150. Robotspeed = 9;
  151. lock (_locker)
  152. {
  153. _lstMonitorHandler.AddLast(new JelRobotCompaundCommandHandler(this, compaundcmdNO.ToString()));
  154. _lstMonitorHandler.AddLast(new JelRobotMoveHandler(this, ""));
  155. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "CS1"));
  156. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "BC")); // Read bank number
  157. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "WCP")); // Read Cassette Number
  158. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "6M", "1"));
  159. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "6M", "2"));
  160. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "6M", "3"));
  161. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "6M", "4"));
  162. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "SP", Robotspeed.ToString()));
  163. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "G"));
  164. }
  165. ResetRoutine();
  166. return true;
  167. }
  168. protected override bool fMonitorInit(object[] param)
  169. {
  170. IsBusy = false;
  171. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  172. {
  173. HandlerError("InitTimeout");
  174. return true;
  175. }
  176. WaitRobotCompleteMove((int)JelStep.Step1, "Wait robot home complete", RobotCommandTimeout);
  177. if (ExecuteResult.Item1)
  178. {
  179. return false;
  180. }
  181. HomeWalkingAxis((int)JelStep.Step2, "Home walking axis", RobotCommandTimeout);
  182. if (ExecuteResult.Item1)
  183. {
  184. return false;
  185. }
  186. Blade1Target = ModuleName.System;
  187. Blade2Target = ModuleName.System;
  188. CmdTarget = ModuleName.System;
  189. MoveInfo = new RobotMoveInfo()
  190. {
  191. Action = RobotAction.Moving,
  192. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  193. BladeTarget = BuildBladeTarget(),
  194. };
  195. return true;
  196. }
  197. public override bool IsReady()
  198. {
  199. return RobotState == RobotStateEnum.Idle && !IsBusy;
  200. }
  201. protected override bool fClear(object[] param)
  202. {
  203. return true;
  204. }
  205. protected override bool fStop(object[] param)
  206. {
  207. m_WalkingAxis.Stop(null);
  208. _lstMonitorHandler.Clear();
  209. _connection.ForceClear();
  210. _connection.Execute(new JelRobotSetHandler(this, "S"));
  211. return ReadStatus();
  212. }
  213. protected override bool fStartMapWafer(object[] param)
  214. {
  215. ResetRoutine();
  216. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), param[0].ToString());
  217. _dtActionStart = DateTime.Now;
  218. return true;
  219. }
  220. protected override bool fMonitorMap(object[] param)
  221. {
  222. IsBusy = false;
  223. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  224. {
  225. HandlerError("MapTimeout");
  226. return true;
  227. }
  228. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[0].ToString());
  229. WalkingAxisMoveToPos((int)JelStep.Step1, "Wait Axis move complete", tempmodule, RobotCommandTimeout);
  230. if (ExecuteResult.Item1)
  231. {
  232. return false;
  233. }
  234. RobotExecuteMap((int)JelStep.Step2);
  235. if (ExecuteResult.Item1)
  236. {
  237. return false;
  238. }
  239. WaitRobotCompleteMove((int)JelStep.Step3, "Wait mappingg complete", RobotCommandTimeout);
  240. if (ExecuteResult.Item1)
  241. {
  242. return false;
  243. }
  244. EV.PostInfoLog("Robot", $"{RobotModuleName} Map {tempmodule} complete. ");
  245. return true;
  246. }
  247. protected override bool fStartPickWafer(object[] param)
  248. {
  249. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[1].ToString());
  250. m_IsNeedMoveWalkingAxis = m_WalkingAxis.GetCurrentStation() != m_WalkingAxis.GetStationIndex(tempmodule);
  251. ResetRoutine();
  252. _dtActionStart = DateTime.Now;
  253. return true;
  254. }
  255. protected override bool fMonitorPick(object[] param)
  256. {
  257. IsBusy = false;
  258. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  259. {
  260. HandlerError("PickTimeout");
  261. return true;
  262. }
  263. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[1].ToString());
  264. int slotindex = int.Parse(CurrentParamter[2].ToString());
  265. if(m_IsNeedMoveWalkingAxis)
  266. {
  267. RobotExecutePickReady((int)JelStep.Step1);
  268. if (ExecuteResult.Item1)
  269. {
  270. return false;
  271. }
  272. WalkingAxisMoveToPos((int)JelStep.Step2, "Wait Axis move complete", tempmodule, RobotCommandTimeout);
  273. if (ExecuteResult.Item1)
  274. {
  275. return false;
  276. }
  277. WaitRobotCompleteMove((int)JelStep.Step3, "Wait picking complete", RobotCommandTimeout);
  278. if (ExecuteResult.Item1)
  279. {
  280. return false;
  281. }
  282. }
  283. RobotExecutePick((int)JelStep.Step4);
  284. if (ExecuteResult.Item1)
  285. {
  286. return false;
  287. }
  288. WaitRobotCompleteMove((int)JelStep.Step5, "Wait picking complete", RobotCommandTimeout);
  289. if (ExecuteResult.Item1)
  290. {
  291. return false;
  292. }
  293. RobotCompletePick((int)JelStep.Step7);
  294. EV.PostInfoLog("Robot", $"{RobotModuleName} pick wafer from {tempmodule}:{slotindex+1} complete. ");
  295. return true;
  296. }
  297. protected override bool fStartSwapWafer(object[] param)
  298. {
  299. ResetRoutine();
  300. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[1].ToString());
  301. m_IsNeedMoveWalkingAxis = m_WalkingAxis.GetCurrentStation() != m_WalkingAxis.GetStationIndex(tempmodule);
  302. _dtActionStart = DateTime.Now;
  303. return true;
  304. }
  305. protected override bool fMonitorSwap(object[] param)
  306. {
  307. IsBusy = false;
  308. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  309. {
  310. HandlerError("SwapTimeout");
  311. return true;
  312. }
  313. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[1].ToString());
  314. int slotindex = int.Parse(CurrentParamter[2].ToString());
  315. if (m_IsNeedMoveWalkingAxis)
  316. {
  317. RobotExecutePickReady((int)JelStep.Step1);
  318. if (ExecuteResult.Item1)
  319. {
  320. return false;
  321. }
  322. WalkingAxisMoveToPos((int)JelStep.Step2, "Wait Axis move complete", tempmodule, RobotCommandTimeout);
  323. if (ExecuteResult.Item1)
  324. {
  325. return false;
  326. }
  327. WaitRobotCompleteMove((int)JelStep.Step3, "Wait picking complete", RobotCommandTimeout);
  328. if (ExecuteResult.Item1)
  329. {
  330. return false;
  331. }
  332. }
  333. RobotExecuteSwap((int)JelStep.Step4);
  334. if (ExecuteResult.Item1)
  335. {
  336. return false;
  337. }
  338. WaitRobotCompleteMove((int)JelStep.Step5, "Wait swapping complete", RobotCommandTimeout);
  339. if (ExecuteResult.Item1)
  340. {
  341. return false;
  342. }
  343. RobotCompleteSwap((int)JelStep.Step6);
  344. EV.PostInfoLog("Robot", $"{RobotModuleName} Swap wafer from {tempmodule}:{slotindex + 1} complete. ");
  345. return true;
  346. }
  347. protected override bool fStartPlaceWafer(object[] param)
  348. {
  349. ResetRoutine();
  350. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[1].ToString());
  351. m_IsNeedMoveWalkingAxis = m_WalkingAxis.GetCurrentStation() != m_WalkingAxis.GetStationIndex(tempmodule);
  352. _dtActionStart = DateTime.Now;
  353. return true;
  354. }
  355. protected override bool fMonitorPlace(object[] param)
  356. {
  357. IsBusy = false;
  358. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  359. {
  360. HandlerError("PlaceTimeout");
  361. return true;
  362. }
  363. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[1].ToString());
  364. int slotindex = int.Parse(CurrentParamter[2].ToString());
  365. if (m_IsNeedMoveWalkingAxis)
  366. {
  367. RobotExecutePickReady((int)JelStep.Step1);
  368. if (ExecuteResult.Item1)
  369. {
  370. return false;
  371. }
  372. WalkingAxisMoveToPos((int)JelStep.Step2, "Wait Axis move complete", tempmodule, RobotCommandTimeout);
  373. if (ExecuteResult.Item1)
  374. {
  375. return false;
  376. }
  377. WaitRobotCompleteMove((int)JelStep.Step3, "Wait picking complete", RobotCommandTimeout);
  378. if (ExecuteResult.Item1)
  379. {
  380. return false;
  381. }
  382. }
  383. RobotExecutePlace((int)JelStep.Step4);
  384. if (ExecuteResult.Item1)
  385. {
  386. return false;
  387. }
  388. WaitRobotCompleteMove((int)JelStep.Step5, "Wait placing complete", RobotCommandTimeout);
  389. if (ExecuteResult.Item1)
  390. {
  391. return false;
  392. }
  393. RobotCompletePlace((int)JelStep.Step6);
  394. if (ExecuteResult.Item1)
  395. {
  396. return false;
  397. }
  398. EV.PostInfoLog("Robot", $"{RobotModuleName} place wafer to {tempmodule}:{slotindex + 1} complete. ");
  399. return true;
  400. }
  401. public override RobotArmWaferStateEnum GetWaferState(RobotArmEnum arm)
  402. {
  403. if (arm == RobotArmEnum.Lower)
  404. return IsLeftArmPressureSensorON? RobotArmWaferStateEnum.Present: RobotArmWaferStateEnum.Absent;
  405. if (arm == RobotArmEnum.Upper)
  406. return IsRightArmPressureSensorON ? RobotArmWaferStateEnum.Present : RobotArmWaferStateEnum.Absent; ;
  407. return RobotArmWaferStateEnum.ArmInvalid;
  408. }
  409. protected void RobotExecutePickReady(int id)
  410. {
  411. Tuple<bool, Result> ret = Execute(id, () =>
  412. {
  413. RobotArmEnum arm = (RobotArmEnum)CurrentParamter[0];
  414. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[1].ToString());
  415. int slotindex = int.Parse(CurrentParamter[2].ToString());
  416. var wz = WaferManager.Instance.GetWaferSize(tempmodule, slotindex);
  417. int bankno;
  418. int cassetteNO;
  419. if (!GetBankAndCassetteNumber(tempmodule, wz, out bankno, out cassetteNO))
  420. return false;
  421. int compaundcmdNO = SC.GetValue<int>($"Robot.{Name}.{arm}PickReadyCmdNO");
  422. lock (_locker)
  423. {
  424. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "BC", bankno.ToString("X")));
  425. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "WCP", cassetteNO.ToString()));
  426. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "WCD", (slotindex + 1).ToString()));
  427. _lstMonitorHandler.AddLast(new JelRobotCompaundCommandHandler(this, compaundcmdNO.ToString()));
  428. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "G"));
  429. }
  430. Blade1Target = tempmodule;
  431. Blade2Target = tempmodule;
  432. CmdTarget = tempmodule;
  433. MoveInfo = new RobotMoveInfo()
  434. {
  435. Action = RobotAction.Picking,
  436. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  437. BladeTarget = BuildBladeTarget(),
  438. };
  439. return true;
  440. });
  441. if (ret.Item1)
  442. {
  443. if (ret.Item2 == Result.FAIL)
  444. {
  445. OnError($"{RobotModuleName}GoPickReadyError.");
  446. }
  447. }
  448. }
  449. protected void RobotExecutePlaceReady(int id)
  450. {
  451. Tuple<bool, Result> ret = Execute(id, () =>
  452. {
  453. RobotArmEnum arm = (RobotArmEnum)CurrentParamter[0];
  454. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[1].ToString());
  455. int slotindex = int.Parse(CurrentParamter[2].ToString());
  456. var wz = WaferManager.Instance.GetWaferSize(RobotModuleName, arm == RobotArmEnum.Both ? 0 : (int)arm);
  457. int bankno;
  458. int cassetteNO;
  459. if(!GetBankAndCassetteNumber(tempmodule,wz, out bankno, out cassetteNO))
  460. return false;
  461. int compaundcmdNO = SC.GetValue<int>($"Robot.{Name}.{arm}PlaceReadyCmdNO");
  462. lock (_locker)
  463. {
  464. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "BC", bankno.ToString("X")));
  465. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "WCP", cassetteNO.ToString()));
  466. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "WCD", (slotindex + 1).ToString()));
  467. _lstMonitorHandler.AddLast(new JelRobotCompaundCommandHandler(this, compaundcmdNO.ToString()));
  468. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "G"));
  469. }
  470. Blade1Target = tempmodule;
  471. Blade2Target = tempmodule;
  472. CmdTarget = tempmodule;
  473. MoveInfo = new RobotMoveInfo()
  474. {
  475. Action = RobotAction.Moving,
  476. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  477. BladeTarget = BuildBladeTarget(),
  478. };
  479. return true;
  480. });
  481. if (ret.Item1)
  482. {
  483. if (ret.Item2 == Result.FAIL)
  484. {
  485. OnError($"{RobotModuleName}GoPlaceReadyError.");
  486. }
  487. }
  488. }
  489. protected void RobotExecuteMap(int id)
  490. {
  491. Tuple<bool, Result> ret = Execute(id, () =>
  492. {
  493. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[0].ToString());
  494. EV.PostInfoLog($"{RobotModuleName}", $"Start mapping wafer on {tempmodule}");
  495. WaferSize wz = WaferManager.Instance.GetWaferSize(tempmodule, 0);
  496. int bankno;
  497. int cassetteNO;
  498. if (!GetBankAndCassetteNumber(tempmodule, wz, out bankno, out cassetteNO))
  499. return false;
  500. int compaundcmdNO = SC.GetValue<int>($"Robot.{Name}.MapCmdNO");
  501. lock (_locker)
  502. {
  503. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "BC", bankno.ToString("X")));
  504. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "WCP", cassetteNO.ToString()));
  505. _lstMonitorHandler.AddLast(new JelRobotCompaundCommandHandler(this, compaundcmdNO.ToString()));
  506. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "G"));
  507. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, ""));
  508. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "WFW"));
  509. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "WFK"));
  510. }
  511. Blade1Target = tempmodule;
  512. Blade2Target = tempmodule;
  513. CmdTarget = tempmodule;
  514. MoveInfo = new RobotMoveInfo()
  515. {
  516. Action = RobotAction.Picking,
  517. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  518. BladeTarget = BuildBladeTarget(),
  519. };
  520. return true;
  521. });
  522. if (ret.Item1)
  523. {
  524. if (ret.Item2 == Result.FAIL)
  525. {
  526. OnError($"{RobotModuleName}MappingError.");
  527. }
  528. }
  529. }
  530. protected void RobotExecutePick(int id)
  531. {
  532. Tuple<bool, Result> ret = Execute(id, () =>
  533. {
  534. RobotArmEnum arm = (RobotArmEnum)CurrentParamter[0];
  535. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[1].ToString());
  536. int slotindex = int.Parse(CurrentParamter[2].ToString());
  537. var wz = WaferManager.Instance.GetWaferSize(tempmodule, slotindex);
  538. int bankno;
  539. int cassetteNO;
  540. if (!GetBankAndCassetteNumber(tempmodule, wz, out bankno, out cassetteNO))
  541. return false;
  542. int compaundcmdNO = SC.GetValue<int>($"Robot.{Name}.{arm}PickCmdNO");
  543. lock (_locker)
  544. {
  545. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "BC", bankno.ToString("X")));
  546. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "WCP", cassetteNO.ToString()));
  547. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "WCD", (slotindex + 1).ToString()));
  548. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "BC"));
  549. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "WCP"));
  550. _lstMonitorHandler.AddLast(new JelRobotCompaundCommandHandler(this, compaundcmdNO.ToString()));
  551. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "G"));
  552. }
  553. Blade1Target = tempmodule;
  554. Blade2Target = tempmodule;
  555. CmdTarget = tempmodule;
  556. MoveInfo = new RobotMoveInfo()
  557. {
  558. Action = RobotAction.Picking,
  559. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  560. BladeTarget = BuildBladeTarget(),
  561. };
  562. return true;
  563. });
  564. if (ret.Item1)
  565. {
  566. if (ret.Item2 == Result.FAIL)
  567. {
  568. OnError($"{RobotModuleName}PickingError.");
  569. }
  570. }
  571. }
  572. protected void RobotCompletePick(int id)
  573. {
  574. Tuple<bool, Result> ret = Execute(id, () =>
  575. {
  576. RobotArmEnum arm = (RobotArmEnum)CurrentParamter[0];
  577. ModuleName sourcemodule;
  578. if (!Enum.TryParse(CurrentParamter[1].ToString(), out sourcemodule)) return false;
  579. int SourceslotIndex;
  580. if (!int.TryParse(CurrentParamter[2].ToString(), out SourceslotIndex)) return false;
  581. if (arm == RobotArmEnum.Lower)
  582. WaferManager.Instance.WaferMoved(sourcemodule, SourceslotIndex, RobotModuleName, 0);
  583. if (arm == RobotArmEnum.Upper)
  584. WaferManager.Instance.WaferMoved(sourcemodule, SourceslotIndex, RobotModuleName, 1);
  585. if (arm == RobotArmEnum.Both)
  586. {
  587. WaferManager.Instance.WaferMoved(sourcemodule, SourceslotIndex, RobotModuleName, 0);
  588. WaferManager.Instance.WaferMoved(sourcemodule, SourceslotIndex, RobotModuleName, 1);
  589. }
  590. Blade1Target = ModuleName.System;
  591. Blade2Target = ModuleName.System;
  592. CmdTarget = ModuleName.System;
  593. MoveInfo = new RobotMoveInfo()
  594. {
  595. Action = RobotAction.Moving,
  596. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  597. BladeTarget = BuildBladeTarget(),
  598. };
  599. return true;
  600. });
  601. if (ret.Item1)
  602. {
  603. if (ret.Item2 == Result.FAIL)
  604. {
  605. OnError($"{RobotModuleName}CompletePickingError.");
  606. }
  607. }
  608. }
  609. protected void RobotExecutePlace(int id)
  610. {
  611. Tuple<bool, Result> ret = Execute(id, () =>
  612. {
  613. RobotArmEnum arm = (RobotArmEnum)CurrentParamter[0];
  614. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[1].ToString());
  615. int slotindex = int.Parse(CurrentParamter[2].ToString());
  616. var wz = WaferManager.Instance.GetWaferSize(RobotModuleName, arm == RobotArmEnum.Both ? 0 : (int)arm);
  617. int bankno;
  618. int cassetteNO;
  619. if (!GetBankAndCassetteNumber(tempmodule, wz, out bankno, out cassetteNO))
  620. return false;
  621. int compaundcmdNO = SC.GetValue<int>($"Robot.{Name}.{arm}PlaceCmdNO");
  622. lock (_locker)
  623. {
  624. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "BC", bankno.ToString("X")));
  625. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "WCP", cassetteNO.ToString()));
  626. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "WCD", (slotindex + 1).ToString()));
  627. _lstMonitorHandler.AddLast(new JelRobotCompaundCommandHandler(this, compaundcmdNO.ToString()));
  628. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "G"));
  629. }
  630. Blade1Target = tempmodule;
  631. Blade2Target = tempmodule;
  632. CmdTarget = tempmodule;
  633. MoveInfo = new RobotMoveInfo()
  634. {
  635. Action = RobotAction.Moving,
  636. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  637. BladeTarget = BuildBladeTarget(),
  638. };
  639. return true;
  640. });
  641. if (ret.Item1)
  642. {
  643. if (ret.Item2 == Result.FAIL)
  644. {
  645. OnError($"{RobotModuleName}PlacingError.");
  646. }
  647. }
  648. }
  649. protected void RobotCompletePlace(int id)
  650. {
  651. Tuple<bool, Result> ret = Execute(id, () =>
  652. {
  653. RobotArmEnum arm = (RobotArmEnum)CurrentParamter[0];
  654. ModuleName sourcemodule;
  655. if (!Enum.TryParse(CurrentParamter[1].ToString(), out sourcemodule)) return false;
  656. int Sourceslotindex;
  657. if (!int.TryParse(CurrentParamter[2].ToString(), out Sourceslotindex)) return false;
  658. if (arm == RobotArmEnum.Lower)
  659. WaferManager.Instance.WaferMoved(RobotModuleName, 0, sourcemodule, Sourceslotindex);
  660. if (arm == RobotArmEnum.Upper)
  661. WaferManager.Instance.WaferMoved(RobotModuleName, 1, sourcemodule, Sourceslotindex);
  662. if (arm == RobotArmEnum.Both)
  663. {
  664. WaferManager.Instance.WaferMoved(RobotModuleName, 0, sourcemodule, Sourceslotindex);
  665. WaferManager.Instance.WaferMoved(RobotModuleName, 1, sourcemodule, Sourceslotindex);
  666. }
  667. Blade1Target = ModuleName.System;
  668. Blade2Target = ModuleName.System;
  669. CmdTarget = ModuleName.System;
  670. MoveInfo = new RobotMoveInfo()
  671. {
  672. Action = RobotAction.Moving,
  673. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  674. BladeTarget = BuildBladeTarget(),
  675. };
  676. return true;
  677. });
  678. if (ret.Item1)
  679. {
  680. if (ret.Item2 == Result.FAIL)
  681. {
  682. OnError($"{RobotModuleName}CompletePickingError.");
  683. }
  684. }
  685. }
  686. protected void RobotExecuteSwap(int id)
  687. {
  688. Tuple<bool, Result> ret = Execute(id, () =>
  689. {
  690. RobotArmEnum arm = (RobotArmEnum)CurrentParamter[0];
  691. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), CurrentParamter[1].ToString());
  692. int slotindex = int.Parse(CurrentParamter[2].ToString());
  693. var wz = WaferManager.Instance.GetWaferSize(RobotModuleName, arm == RobotArmEnum.Both ? 0 : (int)arm);
  694. int bankno;
  695. int cassetteNO;
  696. if (!GetBankAndCassetteNumber(tempmodule, wz, out bankno, out cassetteNO))
  697. return false;
  698. int compaundcmdNO = SC.GetValue<int>($"Robot.{Name}.{arm}PickSwapCmdNO");
  699. lock (_locker)
  700. {
  701. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "BC", bankno.ToString("X")));
  702. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "WCP", cassetteNO.ToString()));
  703. _lstMonitorHandler.AddLast(new JelRobotSetHandler(this, "WCD", (slotindex + 1).ToString()));
  704. _lstMonitorHandler.AddLast(new JelRobotCompaundCommandHandler(this, compaundcmdNO.ToString()));
  705. _lstMonitorHandler.AddLast(new JelRobotReadHandler(this, "G"));
  706. }
  707. Blade1Target = tempmodule;
  708. Blade2Target = tempmodule;
  709. CmdTarget = tempmodule;
  710. MoveInfo = new RobotMoveInfo()
  711. {
  712. Action = RobotAction.Moving,
  713. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  714. BladeTarget = BuildBladeTarget(),
  715. };
  716. return true;
  717. });
  718. if (ret.Item1)
  719. {
  720. if (ret.Item2 == Result.FAIL)
  721. {
  722. OnError($"{RobotModuleName}SwappingError.");
  723. }
  724. }
  725. }
  726. protected void RobotCompleteSwap(int id)
  727. {
  728. Tuple<bool, Result> ret = Execute(id, () =>
  729. {
  730. RobotArmEnum arm = (RobotArmEnum)CurrentParamter[0];
  731. ModuleName sourcemodule;
  732. if (!Enum.TryParse(CurrentParamter[1].ToString(), out sourcemodule)) return false;
  733. int Sourceslotindex;
  734. if (!int.TryParse(CurrentParamter[2].ToString(), out Sourceslotindex)) return false;
  735. if (arm == RobotArmEnum.Lower)
  736. {
  737. WaferManager.Instance.WaferMoved(sourcemodule, Sourceslotindex, RobotModuleName, 0);
  738. WaferManager.Instance.WaferMoved(RobotModuleName, 1, sourcemodule, Sourceslotindex);
  739. }
  740. if (arm == RobotArmEnum.Upper)
  741. {
  742. WaferManager.Instance.WaferMoved(sourcemodule, Sourceslotindex, RobotModuleName, 1);
  743. WaferManager.Instance.WaferMoved(RobotModuleName, 0, sourcemodule, Sourceslotindex);
  744. }
  745. if (arm == RobotArmEnum.Both)
  746. {
  747. }
  748. Blade1Target = ModuleName.System;
  749. Blade2Target = ModuleName.System;
  750. CmdTarget = ModuleName.System;
  751. MoveInfo = new RobotMoveInfo()
  752. {
  753. Action = RobotAction.Moving,
  754. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  755. BladeTarget = BuildBladeTarget(),
  756. };
  757. return true;
  758. });
  759. if (ret.Item1)
  760. {
  761. if (ret.Item2 == Result.FAIL)
  762. {
  763. OnError($"{RobotModuleName}CompletePickingError.");
  764. }
  765. }
  766. }
  767. public void WaitRobotCompleteMove(int id, string name, int time)
  768. {
  769. var ret = ExecuteAndWait(id, () =>
  770. {
  771. EV.PostInfoLog($"{RobotModuleName}",name);
  772. return true;
  773. }, () =>
  774. {
  775. if (_lstMonitorHandler.Count >0 || _connection.IsBusy)
  776. {
  777. return Result.RUN;
  778. }
  779. if (CurrentCompoundCommandStatus == JelCommandStatus.InError)
  780. return Result.FAIL;
  781. if (CurrentCompoundCommandStatus == JelCommandStatus.NormalEnd)
  782. {
  783. Blade1Target = ModuleName.System;
  784. Blade2Target = ModuleName.System;
  785. CmdTarget = ModuleName.System;
  786. MoveInfo = new RobotMoveInfo()
  787. {
  788. Action = RobotAction.Moving,
  789. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  790. BladeTarget = BuildBladeTarget(),
  791. };
  792. return Result.DONE;
  793. }
  794. return Result.RUN;
  795. }, time * 1000);
  796. if (ret.Item1)
  797. {
  798. if (ret.Item2 == Result.FAIL)
  799. {
  800. OnError(string.Format("{0} occurred error", name));
  801. }
  802. if (ret.Item2 == Result.TIMEOUT) //timeout
  803. {
  804. OnError(string.Format("{0} timeout, than {1} seconds", name, time));
  805. }
  806. }
  807. }
  808. public void HomeWalkingAxis(int id, string name, int time)
  809. {
  810. var ret = ExecuteAndWait(id, () =>
  811. {
  812. EV.PostInfoLog($"{RobotModuleName}", name);
  813. m_WalkingAxis.Home(null);
  814. return true;
  815. }, () =>
  816. {
  817. if (m_WalkingAxis.IsReady())
  818. return Result.DONE;
  819. if (m_WalkingAxis.DeviceState == WalkingAxisState.ERROR)
  820. return Result.FAIL;
  821. return Result.RUN;
  822. }, time * 1000);
  823. if (ret.Item1)
  824. {
  825. if (ret.Item2 == Result.FAIL)
  826. {
  827. OnError(string.Format("{0} occurred error", name));
  828. }
  829. if (ret.Item2 == Result.TIMEOUT) //timeout
  830. {
  831. OnError(string.Format("{0} timeout, than {1} seconds", name, time));
  832. }
  833. }
  834. }
  835. public void WalkingAxisMoveToPos(int id, string name, ModuleName module, int time)
  836. {
  837. var ret = ExecuteAndWait(id, () =>
  838. {
  839. EV.PostInfoLog($"{m_WalkingAxis.Name}", name);
  840. return m_WalkingAxis.MoveTo(module);
  841. }, () =>
  842. {
  843. if (m_WalkingAxis.IsReady() && m_WalkingAxis.IsArrivedTarget)
  844. return Result.DONE;
  845. if (m_WalkingAxis.DeviceState == WalkingAxisState.ERROR)
  846. return Result.FAIL;
  847. return Result.RUN;
  848. }, time * 1000);
  849. if (ret.Item1)
  850. {
  851. if (ret.Item2 == Result.FAIL)
  852. {
  853. OnError(string.Format("{0} occurred error", name));
  854. }
  855. if (ret.Item2 == Result.TIMEOUT) //timeout
  856. {
  857. OnError(string.Format("{0} timeout, than {1} seconds", name, time));
  858. }
  859. }
  860. }
  861. protected DeviceTimer counter = new DeviceTimer();
  862. protected DeviceTimer delayTimer = new DeviceTimer();
  863. private enum STATE
  864. {
  865. IDLE,
  866. WAIT,
  867. }
  868. public int TokenId
  869. {
  870. get { return _id; }
  871. }
  872. private int _id; //step index
  873. private int _currentTokenId = -1;
  874. /// <summary>
  875. /// already done steps
  876. /// </summary>
  877. private Stack<int> _steps = new Stack<int>();
  878. private STATE state; //step state //idel,wait,
  879. //loop control
  880. private int loop = 0;
  881. private int loopCount = 0;
  882. private int loopID = 0;
  883. private DeviceTimer timer = new DeviceTimer();
  884. public int LoopCounter { get { return loop; } }
  885. public int LoopTotalTime { get { return loopCount; } }
  886. // public int Timeout { get { return (int)(timer.GetTotalTime() / 1000); } }
  887. //状态持续时间,单位为秒
  888. public int Elapsed { get { return (int)(timer.GetElapseTime() / 1000); } }
  889. protected RoutineResult RoutineToken = new RoutineResult() { Result = RoutineState.Running };
  890. protected Tuple<bool, Result> ExecuteResult;
  891. public void ResetRoutine()
  892. {
  893. _id = 0;
  894. _steps.Clear();
  895. loop = 0;
  896. loopCount = 0;
  897. state = STATE.IDLE;
  898. counter.Start(60 * 60 * 100); //默认1小时
  899. RoutineToken.Result = RoutineState.Running;
  900. _currentTokenId = -1;
  901. ExecuteResult = Tuple.Create(false, Result.DONE);
  902. }
  903. protected void PerformRoutineStep(int id, Func<RoutineState> execution, RoutineResult result)
  904. {
  905. if (!Acitve(id))
  906. return;
  907. result.Result = execution();
  908. }
  909. #region interface
  910. public void StopLoop()
  911. {
  912. loop = loopCount;
  913. }
  914. public Tuple<bool, Result> Loop<T>(T id, Func<bool> func, int count)
  915. {
  916. int idx = Convert.ToInt32(id);
  917. bool bActive = Acitve(idx);
  918. if (bActive)
  919. {
  920. if (!func())
  921. {
  922. return Tuple.Create(bActive, Result.FAIL); //执行错误
  923. }
  924. loopID = idx;
  925. loopCount = count;
  926. next();
  927. return Tuple.Create(true, Result.RUN);
  928. }
  929. return Tuple.Create(false, Result.RUN);
  930. }
  931. public Tuple<bool, Result> EndLoop<T>(T id, Func<bool> func)
  932. {
  933. int idx = Convert.ToInt32(id);
  934. bool bActive = Acitve(idx);
  935. if (bActive)
  936. {
  937. if (++loop >= loopCount) //Loop 结束
  938. {
  939. if (!func())
  940. {
  941. return Tuple.Create(bActive, Result.FAIL); //执行错误
  942. }
  943. loop = 0;
  944. loopCount = 0; // Loop 结束时,当前loop和loop总数都清零
  945. next();
  946. return Tuple.Create(true, Result.RUN);
  947. }
  948. //继续下一LOOP
  949. next(loopID);
  950. return Tuple.Create(true, Result.RUN);
  951. }
  952. return Tuple.Create(false, Result.RUN);
  953. }
  954. public Tuple<bool, Result> ExecuteAndWait<T>(T id, IRoutine routine)
  955. {
  956. int idx = Convert.ToInt32(id);
  957. bool bActive = Acitve(idx);
  958. if (bActive)
  959. {
  960. if (state == STATE.IDLE)
  961. {
  962. Result startRet = routine.Start();
  963. if (startRet == Result.FAIL)
  964. {
  965. return Tuple.Create(true, Result.FAIL); //执行错误
  966. }
  967. else if (startRet == Result.DONE)
  968. {
  969. next();
  970. return Tuple.Create(true, Result.DONE);
  971. }
  972. state = STATE.WAIT;
  973. }
  974. Result ret = routine.Monitor();
  975. if (ret == Result.DONE)
  976. {
  977. next();
  978. return Tuple.Create(true, Result.DONE);
  979. }
  980. else if (ret == Result.FAIL || ret == Result.TIMEOUT)
  981. {
  982. return Tuple.Create(true, Result.FAIL);
  983. }
  984. else
  985. {
  986. return Tuple.Create(true, Result.RUN);
  987. }
  988. }
  989. return Tuple.Create(false, Result.RUN);
  990. }
  991. public Tuple<bool, Result> ExecuteAndWait<T>(T id, List<IRoutine> routines)
  992. {
  993. int idx = Convert.ToInt32(id);
  994. bool bActive = Acitve(idx);
  995. if (bActive)
  996. {
  997. if (state == STATE.IDLE)
  998. {
  999. foreach (var item in routines)
  1000. {
  1001. if (item.Start() == Result.FAIL)
  1002. return Tuple.Create(true, Result.FAIL);
  1003. }
  1004. state = STATE.WAIT;
  1005. }
  1006. //wait all sub failed or completedboo
  1007. bool bFail = false;
  1008. bool bDone = true;
  1009. foreach (var item in routines)
  1010. {
  1011. Result ret = item.Monitor();
  1012. bDone &= (ret == Result.FAIL || ret == Result.DONE);
  1013. bFail |= ret == Result.FAIL;
  1014. }
  1015. if (bDone)
  1016. {
  1017. next();
  1018. if (bFail)
  1019. return Tuple.Create(true, Result.FAIL);
  1020. return Tuple.Create(true, Result.DONE);
  1021. }
  1022. return Tuple.Create(true, Result.RUN);
  1023. }
  1024. return Tuple.Create(false, Result.RUN);
  1025. }
  1026. public Tuple<bool, Result> Check<T>(T id, Func<bool> func) //顺序执行
  1027. {
  1028. return Check(Check(Convert.ToInt32(id), func));
  1029. }
  1030. public Tuple<bool, Result> Execute<T>(T id, Func<bool> func) //顺序执行
  1031. {
  1032. return Check(execute(Convert.ToInt32(id), func));
  1033. }
  1034. public Tuple<bool, Result> Wait<T>(T id, Func<bool> func, double timeout = int.MaxValue) //Wait condition
  1035. {
  1036. return Check(wait(Convert.ToInt32(id), func, timeout));
  1037. }
  1038. public Tuple<bool, Result> Wait<T>(T id, Func<bool?> func, double timeout = int.MaxValue) //Wait condition
  1039. {
  1040. return Check(wait(Convert.ToInt32(id), func, timeout));
  1041. }
  1042. public Tuple<bool, Result> ExecuteAndWait<T>(T id, Func<bool> execute, Func<Result?> check, double timeout = int.MaxValue)
  1043. {
  1044. int idx = Convert.ToInt32(id);
  1045. bool bActive = Acitve(idx);
  1046. Result? bExecute = Result.RUN;
  1047. if (bActive)
  1048. {
  1049. //if (idx != _currentTokenId && ExecuteResult.Item1) return ExecuteResult;
  1050. if (state == STATE.IDLE)
  1051. {
  1052. if (!execute())
  1053. {
  1054. ExecuteResult = Tuple.Create(bActive, Result.FAIL);
  1055. return Tuple.Create(bActive, Result.FAIL); //执行错误
  1056. }
  1057. timer.Start(timeout);
  1058. state = STATE.WAIT;
  1059. _currentTokenId = idx;
  1060. }
  1061. bExecute = check();
  1062. if (bExecute == null)
  1063. {
  1064. ExecuteResult = Tuple.Create(bActive, Result.FAIL);
  1065. return Tuple.Create(bActive, Result.FAIL); //Termianate
  1066. }
  1067. else
  1068. {
  1069. if (bExecute == Result.DONE) //检查Success, next
  1070. {
  1071. next();
  1072. ExecuteResult = Tuple.Create(true, Result.RUN);
  1073. return Tuple.Create(true, Result.RUN);
  1074. }
  1075. if (bExecute == Result.Succeed) //检查Success, next
  1076. {
  1077. next();
  1078. ExecuteResult = Tuple.Create(true, Result.RUN);
  1079. return Tuple.Create(true, Result.RUN);
  1080. }
  1081. if (bExecute == Result.FAIL) //检查 Fail 直接返回Fail
  1082. {
  1083. ExecuteResult = Tuple.Create(true, Result.FAIL);
  1084. return Tuple.Create(true, Result.FAIL);
  1085. }
  1086. }
  1087. if (timer.IsTimeout())
  1088. {
  1089. ExecuteResult = Tuple.Create(true, Result.TIMEOUT);
  1090. return Tuple.Create(true, Result.TIMEOUT);
  1091. }
  1092. ExecuteResult = Tuple.Create(true, Result.RUN);
  1093. return Tuple.Create(true, Result.RUN);
  1094. }
  1095. ExecuteResult = Tuple.Create(false, Result.RUN);
  1096. return Tuple.Create(false, Result.RUN);
  1097. }
  1098. public Tuple<bool, Result> Wait<T>(T id, IRoutine rt)
  1099. {
  1100. int idx = Convert.ToInt32(id);
  1101. bool bActive = Acitve(idx);
  1102. if (bActive)
  1103. {
  1104. if (state == STATE.IDLE)
  1105. {
  1106. rt.Start();
  1107. state = STATE.WAIT;
  1108. }
  1109. Result ret = rt.Monitor();
  1110. return Tuple.Create(true, ret);
  1111. }
  1112. return Tuple.Create(false, Result.RUN);
  1113. }
  1114. //Monitor
  1115. public Tuple<bool, Result> Monitor<T>(T id, Func<bool> func, Func<bool> check, double time)
  1116. {
  1117. int idx = Convert.ToInt32(id);
  1118. bool bActive = Acitve(idx);
  1119. bool bCheck = false;
  1120. if (bActive)
  1121. {
  1122. if (state == STATE.IDLE)
  1123. {
  1124. if ((func != null) && !func())
  1125. {
  1126. return Tuple.Create(true, Result.FAIL);
  1127. }
  1128. timer.Start(time);
  1129. state = STATE.WAIT;
  1130. }
  1131. bCheck = check();
  1132. if (!bCheck)
  1133. {
  1134. return Tuple.Create(true, Result.FAIL); //Termianate
  1135. }
  1136. if (timer.IsTimeout())
  1137. {
  1138. next();
  1139. }
  1140. return Tuple.Create(true, Result.RUN);
  1141. }
  1142. return Tuple.Create(false, Result.RUN);
  1143. }
  1144. //Delay
  1145. public Tuple<bool, Result> Delay<T>(T id, Func<bool> func, double time)
  1146. {
  1147. int idx = Convert.ToInt32(id);
  1148. bool bActive = Acitve(idx);
  1149. if (bActive)
  1150. {
  1151. //if (_currentTokenId != idx && !ExecuteResult.Item1) return ExecuteResult;
  1152. if (state == STATE.IDLE)
  1153. {
  1154. if ((func != null) && !func())
  1155. {
  1156. ExecuteResult = Tuple.Create(true, Result.FAIL);
  1157. return Tuple.Create(true, Result.FAIL);
  1158. }
  1159. _currentTokenId = idx;
  1160. timer.Start(time);
  1161. state = STATE.WAIT;
  1162. }
  1163. if (timer.IsTimeout())
  1164. {
  1165. next();
  1166. }
  1167. ExecuteResult = Tuple.Create(true, Result.RUN);
  1168. return Tuple.Create(true, Result.RUN);
  1169. }
  1170. ExecuteResult = Tuple.Create(false, Result.RUN);
  1171. return Tuple.Create(false, Result.RUN);
  1172. }
  1173. //先delay 再运行
  1174. public Tuple<bool, Result> DelayCheck<T>(T id, Func<bool> func, double time)
  1175. {
  1176. int idx = Convert.ToInt32(id);
  1177. bool bActive = Acitve(idx);
  1178. if (bActive)
  1179. {
  1180. if (state == STATE.IDLE)
  1181. {
  1182. timer.Start(time);
  1183. state = STATE.WAIT;
  1184. }
  1185. if (timer.IsTimeout())
  1186. {
  1187. if (func != null && !func())
  1188. {
  1189. return Tuple.Create(true, Result.FAIL);
  1190. }
  1191. next();
  1192. }
  1193. return Tuple.Create(true, Result.RUN);
  1194. }
  1195. return Tuple.Create(false, Result.RUN);
  1196. }
  1197. #endregion
  1198. private Tuple<bool, bool> execute(int id, Func<bool> func) //顺序执行
  1199. {
  1200. bool bActive = Acitve(id);
  1201. bool bExecute = false;
  1202. if (bActive)
  1203. {
  1204. //if (ExecuteResult.Item1) return Tuple.Create(true, true);
  1205. bExecute = func();
  1206. if (bExecute)
  1207. {
  1208. next();
  1209. }
  1210. }
  1211. return Tuple.Create(bActive, bExecute);
  1212. }
  1213. private Tuple<bool, bool> Check(int id, Func<bool> func) //check
  1214. {
  1215. bool bActive = Acitve(id);
  1216. bool bExecute = false;
  1217. if (bActive)
  1218. {
  1219. if (ExecuteResult.Item1) return Tuple.Create(true, true);
  1220. bExecute = func();
  1221. next();
  1222. }
  1223. return Tuple.Create(bActive, bExecute);
  1224. }
  1225. /// <summary>
  1226. /// </summary>
  1227. /// <param name="id"></param>
  1228. /// <param name="func"></param>
  1229. /// <param name="timeout"></param>
  1230. /// <returns>
  1231. /// item1 Active
  1232. /// item2 execute
  1233. /// item3 Timeout
  1234. ///</returns>
  1235. private Tuple<bool, bool, bool> wait(int id, Func<bool> func, double timeout = int.MaxValue) //Wait condition
  1236. {
  1237. bool bActive = Acitve(id);
  1238. bool bExecute = false;
  1239. bool bTimeout = false;
  1240. if (bActive)
  1241. {
  1242. if (state == STATE.IDLE)
  1243. {
  1244. timer.Start(timeout);
  1245. state = STATE.WAIT;
  1246. }
  1247. bExecute = func();
  1248. if (bExecute)
  1249. {
  1250. next();
  1251. }
  1252. bTimeout = timer.IsTimeout();
  1253. }
  1254. return Tuple.Create(bActive, bExecute, bTimeout);
  1255. }
  1256. private Tuple<bool, bool?, bool> wait(int id, Func<bool?> func, double timeout = int.MaxValue) //Wait condition && Check error
  1257. {
  1258. bool bActive = Acitve(id);
  1259. bool? bExecute = false;
  1260. bool bTimeout = false;
  1261. if (bActive)
  1262. {
  1263. if (state == STATE.IDLE)
  1264. {
  1265. timer.Start(timeout);
  1266. state = STATE.WAIT;
  1267. }
  1268. bExecute = func();
  1269. if (bExecute.HasValue && bExecute.Value)
  1270. {
  1271. next();
  1272. }
  1273. bTimeout = timer.IsTimeout();
  1274. }
  1275. return Tuple.Create(bActive, bExecute, bTimeout);
  1276. }
  1277. /// <summary>
  1278. /// </summary>
  1279. /// <param name="value"></param>
  1280. /// <returns>
  1281. /// item1 true, return item2
  1282. /// </returns>
  1283. private Tuple<bool, Result> Check(Tuple<bool, bool> value)
  1284. {
  1285. if (value.Item1)
  1286. {
  1287. if (!value.Item2)
  1288. {
  1289. ExecuteResult = Tuple.Create(true, Result.FAIL);
  1290. return Tuple.Create(true, Result.FAIL);
  1291. }
  1292. ExecuteResult = Tuple.Create(true, Result.RUN);
  1293. return Tuple.Create(true, Result.RUN);
  1294. }
  1295. ExecuteResult = Tuple.Create(false, Result.RUN);
  1296. return Tuple.Create(false, Result.RUN);
  1297. }
  1298. private Tuple<bool, Result> Check(Tuple<bool, bool, bool> value)
  1299. {
  1300. if (value.Item1) // 当前执行
  1301. {
  1302. if (CheckTimeout(value)) //timeout
  1303. {
  1304. return Tuple.Create(true, Result.TIMEOUT);
  1305. }
  1306. return Tuple.Create(true, Result.RUN);
  1307. }
  1308. return Tuple.Create(false, Result.RUN);
  1309. }
  1310. private Tuple<bool, Result> Check(Tuple<bool, bool?, bool> value)
  1311. {
  1312. if (value.Item1) // 当前执行
  1313. {
  1314. if (value.Item2 == null)
  1315. {
  1316. return Tuple.Create(true, Result.FAIL);
  1317. }
  1318. else
  1319. {
  1320. if (value.Item2 == false && value.Item3 == true) //timeout
  1321. {
  1322. return Tuple.Create(true, Result.TIMEOUT);
  1323. }
  1324. return Tuple.Create(true, Result.RUN);
  1325. }
  1326. }
  1327. return Tuple.Create(false, Result.RUN);
  1328. }
  1329. private bool CheckTimeout(Tuple<bool, bool, bool> value)
  1330. {
  1331. return value.Item1 == true && value.Item2 == false && value.Item3 == true;
  1332. }
  1333. private bool Acitve(int id) //
  1334. {
  1335. if (_steps.Contains(id))
  1336. return false;
  1337. this._id = id;
  1338. return true;
  1339. }
  1340. private void next()
  1341. {
  1342. _steps.Push(this._id);
  1343. state = STATE.IDLE;
  1344. }
  1345. private void next(int step) //loop
  1346. {
  1347. while (_steps.Pop() != step) ;
  1348. state = STATE.IDLE;
  1349. }
  1350. public void Delay(int id, double delaySeconds)
  1351. {
  1352. Tuple<bool, Result> ret = Delay(id, () =>
  1353. {
  1354. return true;
  1355. }, delaySeconds * 1000);
  1356. if (ret.Item1)
  1357. {
  1358. if (ret.Item2 == Result.RUN)
  1359. {
  1360. }
  1361. }
  1362. }
  1363. public bool IsActived(int id)
  1364. {
  1365. return _steps.Contains(id);
  1366. }
  1367. enum JelStep
  1368. {
  1369. Step1,
  1370. Step2,
  1371. Step3,
  1372. Step4,
  1373. Step5,
  1374. Step6,
  1375. Step7,
  1376. Step8,
  1377. Step9,
  1378. Step10,
  1379. Step11,
  1380. Step12,
  1381. }
  1382. }
  1383. }