GAPlcRobot.cs 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  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 MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot;
  25. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.LoadPortBase;
  26. using System.Xml;
  27. using Aitex.Core.RT.IOCore;
  28. using Aitex.Core.RT.Routine;
  29. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots;
  30. using MECF.Framework.Common.CommonData;
  31. using EventType = Aitex.Core.RT.Event.EventType;
  32. namespace Aitex.Core.RT.Device.Unit
  33. {
  34. public class GAPlcRobot:RobotBaseDevice
  35. {
  36. public GAPlcRobot(string module,XmlElement node,string ioModule =""): base(node.GetAttribute("module"), node.GetAttribute("id"))
  37. {
  38. base.Module = node.GetAttribute("module");//string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module");
  39. base.Name = node.GetAttribute("id");
  40. ioModule = node.GetAttribute("ioModule");
  41. _aiCurrentXPos_L = ParseAiNode("AI_CurrentXPos_L", node, ioModule);
  42. _aiCurrentXPos_H = ParseAiNode("AI_CurrentXPos_H", node, ioModule);
  43. _aiCurrentYPos_L = ParseAiNode("AI_CurrentYPos_L", node, ioModule);
  44. _aiCurrentYPos_H = ParseAiNode("AI_CurrentYPos_H", node, ioModule);
  45. _aiCurrentZPos_L = ParseAiNode("AI_CurrentZPos_L", node, ioModule);
  46. _aiCurrentZPos_H = ParseAiNode("AI_CurrentZPos_H", node, ioModule);
  47. _aiZAxisPitch_L = ParseAiNode("AI_ZAxisPitch_L", node, ioModule);
  48. _aiZAxisPitch_H = ParseAiNode("AI_ZAxisPitch_H", node, ioModule);
  49. _aiZAxisFirstPos_L = ParseAiNode("AI_ZAxisFirstPos_L", node, ioModule);
  50. _aiZAxisFirstPos_H = ParseAiNode("AI_ZAxisFirstPos_H", node, ioModule);
  51. _aiZAxisPlacePos_L = ParseAiNode("AI_ZAxisPlacePos_L", node, ioModule);
  52. _aiZAxisPlacePos_H = ParseAiNode("AI_ZAxisPlacePos_H", node, ioModule);
  53. _aiXAxisMoveDistance_L = ParseAiNode("AI_XAxisMoveDistance_L", node, ioModule);
  54. _aiXAxisMoveDistance_H = ParseAiNode("AI_XAxisMoveDistance_H", node, ioModule);
  55. _aiYAxisMoveDistance_L = ParseAiNode("AI_YAxisMoveDistance_L", node, ioModule);
  56. _aiYAxisMoveDistance_H = ParseAiNode("AI_YAxisMoveDistance_H", node, ioModule);
  57. _aiZAxisMoveSpeed_L = ParseAiNode("AI_ZAxisMoveSpeed_L", node, ioModule);
  58. _aiZAxisMoveSpeed_H = ParseAiNode("AI_ZAxisMoveSpeed_H", node, ioModule);
  59. _aiXAxisMoveSpeed_L = ParseAiNode("AI_XAxisMoveSpeed_L", node, ioModule);
  60. _aiXAxisMoveSpeed_H = ParseAiNode("AI_XAxisMoveSpeed_H", node, ioModule);
  61. _aiYAxisMoveSpeed_L = ParseAiNode("AI_YAxisMoveSpeed_L", node, ioModule);
  62. _aiYAxisMoveSpeed_H = ParseAiNode("AI_YAxisMoveSpeed_H", node, ioModule);
  63. _aiTargetPos = ParseAiNode("AI_TargetPos", node, ioModule);
  64. _aoZAxisPitch_L = ParseAoNode("AO_ZAxisPitch_L", node, ioModule);
  65. _aoZAxisPitch_H = ParseAoNode("AO_ZAxisPitch_H", node, ioModule);
  66. _aoZAxisFirstPos_L = ParseAoNode("AO_ZAxisFirstPos_L", node, ioModule);
  67. _aoZAxisFirstPos_H = ParseAoNode("AO_ZAxisFirstPos_H", node, ioModule);
  68. _aoZAxisPlacePos_L = ParseAoNode("AO_ZAxisPlacePos_L", node, ioModule);
  69. _aoZAxisPlacePos_H = ParseAoNode("AO_ZAxisPlacePos_H", node, ioModule);
  70. _aoXAxisMoveDistance_L = ParseAoNode("AO_XAxisMoveDistance_L", node, ioModule);
  71. _aoXAxisMoveDistance_H = ParseAoNode("AO_XAxisMoveDistance_H", node, ioModule);
  72. _aoYAxisMoveDistance_L = ParseAoNode("AO_YAxisMoveDistance_L", node, ioModule);
  73. _aoYAxisMoveDistance_H = ParseAoNode("AO_YAxisMoveDistance_H", node, ioModule);
  74. _aoZAxisMoveSpeed_L = ParseAoNode("AO_ZAxisMoveSpeed_L", node, ioModule);
  75. _aoZAxisMoveSpeed_H = ParseAoNode("AO_ZAxisMoveSpeed_H", node, ioModule);
  76. _aoXAxisMoveSpeed_L = ParseAoNode("AO_XAxisMoveSpeed_L", node, ioModule);
  77. _aoXAxisMoveSpeed_H = ParseAoNode("AO_XAxisMoveSpeed_H", node, ioModule);
  78. _aoYAxisMoveSpeed_L = ParseAoNode("AO_YAxisMoveSpeed_L", node, ioModule);
  79. _aoYAxisMoveSpeed_H = ParseAoNode("AO_YAxisMoveSpeed_H", node, ioModule);
  80. _aoTargetPos = ParseAoNode("AO_TargetPos", node, ioModule);
  81. _aoXMoveFwdDistance_L = ParseAoNode("AO_XMoveFwdDistance_L", node, ioModule);
  82. _aoXMoveFwdDistance_H = ParseAoNode("AO_XMoveFwdDistance_H", node, ioModule);
  83. _aoYMoveFwdDistance_L = ParseAoNode("AO_YMoveFwdDistance_L", node, ioModule);
  84. _aoYMoveFwdDistance_H = ParseAoNode("AO_YMoveFwdDistance_H", node, ioModule);
  85. _aoZMoveFwdDistance_L = ParseAoNode("AO_ZMoveFwdDistance_L", node, ioModule);
  86. _aoZMoveFwdDistance_H = ParseAoNode("AO_ZMoveFwdDistance_H", node, ioModule);
  87. _aoXMoveRevDistance_L = ParseAoNode("AO_XMoveRevDistance_L", node, ioModule);
  88. _aoXMoveRevDistance_H = ParseAoNode("AO_XMoveRevDistance_H", node, ioModule);
  89. _aoYMoveRevDistance_L = ParseAoNode("AO_YMoveRevDistance_L", node, ioModule);
  90. _aoYMoveRevDistance_H = ParseAoNode("AO_YMoveRevDistance_H", node, ioModule);
  91. _aoZMoveRevDistance_L = ParseAoNode("AO_ZMoveRevDistance_L", node, ioModule);
  92. _aoZMoveRevDistance_H = ParseAoNode("AO_ZMoveRevDistance_H", node, ioModule);
  93. _diReset_Ack = ParseDiNode("DI_Reset_Ack", node, ioModule);
  94. _diHome_Ack = ParseDiNode("DI_Home_Ack", node, ioModule);
  95. _diSave_Ack = ParseDiNode("DI_Save_Ack", node, ioModule);
  96. _diPick_Ack = ParseDiNode("DI_Pick_Ack", node, ioModule);
  97. _diPlace_Ack = ParseDiNode("DI_Place_Ack", node, ioModule);
  98. _diX_Fwd_Ack = ParseDiNode("DI_X_Fwd_Ack", node, ioModule);
  99. _diX_Rev_Ack = ParseDiNode("DI_X_Rev_Ack", node, ioModule);
  100. _diY_Fwd_Ack = ParseDiNode("DI_Y_Fwd_Ack", node, ioModule);
  101. _diY_Rev_Ack = ParseDiNode("DI_Y_Rev_Ack", node, ioModule);
  102. _diZ_Fwd_Ack = ParseDiNode("DI_Z_Fwd_Ack", node, ioModule);
  103. _diZ_Rev_Ack = ParseDiNode("DI_Z_Rev_Ack", node, ioModule);
  104. _diPlugError = ParseDiNode("DI_PlugError", node, ioModule);
  105. _diZMotionError = ParseDiNode("DI_ZMotionError", node, ioModule);
  106. _diXMotionError = ParseDiNode("DI_XMotionError", node, ioModule);
  107. _diYMotionError = ParseDiNode("DI_YMotionError", node, ioModule);
  108. _diZMotionOutLimit = ParseDiNode("DI_ZMotionOutLimit", node, ioModule);
  109. _diXMotionOutLimit = ParseDiNode("DI_XMotionOutLimit", node, ioModule);
  110. _diYMotionOutLimit = ParseDiNode("DI_YMotionOutLimit", node, ioModule);
  111. _diPLCReady = ParseDiNode("DI_PLCReady", node, ioModule);
  112. _diMotionIdle = ParseDiNode("DI_MotionIdle", node, ioModule);
  113. _doResetCmd = ParseDoNode("DO_ResetCmd", node, ioModule);
  114. _doHomeCmd = ParseDoNode("DO_HomeCmd", node, ioModule);
  115. _doSaveCmd = ParseDoNode("DO_SaveCmd", node, ioModule);
  116. _doPickCmd = ParseDoNode("DO_PickCmd", node, ioModule);
  117. _doPlaceCmd = ParseDoNode("DO_PlaceCmd", node, ioModule);
  118. _doX_FwdCmd = ParseDoNode("DO_X_FwdCmd", node, ioModule);
  119. _doX_RevCmd = ParseDoNode("DO_X_RevCmd", node, ioModule);
  120. _doY_FwdCmd = ParseDoNode("DO_Y_FwdCmd", node, ioModule);
  121. _doY_RevCmd = ParseDoNode("DO_Y_RevCmd", node, ioModule);
  122. _doZ_FwdCmd = ParseDoNode("DO_Z_FwdCmd", node, ioModule);
  123. _doZ_RevCmd = ParseDoNode("DO_Z_RevCmd", node, ioModule);
  124. EV.Subscribe(new EventItem("Alarm", AlarmRobotError, $"Robot occurred error.", EventLevel.Alarm, EventType.EventUI_Notify));
  125. DATA.Subscribe($"{Name}.PLCReady", () => _diPLCReady.Value);
  126. DATA.Subscribe($"{Name}.MotionIdle", () => _diMotionIdle.Value);
  127. DATA.Subscribe($"{Name}.SlotPitch", () => _slotPitch);
  128. DATA.Subscribe($"{Name}.FirstZPosition", () => _zFirstPosition);
  129. DATA.Subscribe($"{Name}.PlaceWaferZPosition", () => _zPutWaferPosition);
  130. DATA.Subscribe($"{Name}.XMoveDistance", () => _xMoveDistance);
  131. DATA.Subscribe($"{Name}.YMoveDistance", () => _yMoveDistance);
  132. DATA.Subscribe($"{Name}.ZMoveSpeed", () => _zMoveSpeed);
  133. DATA.Subscribe($"{Name}.YMoveSpeed", () => _yMoveSpeed);
  134. DATA.Subscribe($"{Name}.XMoveSpeed", () => _xMoveSpeed);
  135. DATA.Subscribe($"{Name}.CurrentXPosition", () => _currentXPosition);
  136. DATA.Subscribe($"{Name}.CurrentYPosition", () => _currentYPosition);
  137. DATA.Subscribe($"{Name}.CurrentZPosition", () => _currentZPosition);
  138. _trigErrorOccurred = new R_TRIG();
  139. _thread = new PeriodicJob(10, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  140. }
  141. private string AlarmRobotError = "RobotOccurredError";
  142. private AIAccessor _aiZAxisPitch_L;
  143. private AIAccessor _aiZAxisPitch_H;
  144. private AIAccessor _aiZAxisFirstPos_L;
  145. private AIAccessor _aiZAxisFirstPos_H;
  146. private AIAccessor _aiZAxisPlacePos_L;
  147. private AIAccessor _aiZAxisPlacePos_H;
  148. private AIAccessor _aiXAxisMoveDistance_L;
  149. private AIAccessor _aiXAxisMoveDistance_H;
  150. private AIAccessor _aiYAxisMoveDistance_L;
  151. private AIAccessor _aiYAxisMoveDistance_H;
  152. private AIAccessor _aiZAxisMoveSpeed_L;
  153. private AIAccessor _aiZAxisMoveSpeed_H;
  154. private AIAccessor _aiXAxisMoveSpeed_L;
  155. private AIAccessor _aiXAxisMoveSpeed_H;
  156. private AIAccessor _aiYAxisMoveSpeed_L;
  157. private AIAccessor _aiYAxisMoveSpeed_H;
  158. private AIAccessor _aiTargetPos;
  159. private AIAccessor _aiCurrentXPos_L;
  160. private AIAccessor _aiCurrentXPos_H;
  161. private AIAccessor _aiCurrentYPos_L;
  162. private AIAccessor _aiCurrentYPos_H;
  163. private AIAccessor _aiCurrentZPos_L;
  164. private AIAccessor _aiCurrentZPos_H;
  165. private AOAccessor _aoZAxisPitch_L;
  166. private AOAccessor _aoZAxisPitch_H;
  167. private AOAccessor _aoZAxisFirstPos_L;
  168. private AOAccessor _aoZAxisFirstPos_H;
  169. private AOAccessor _aoZAxisPlacePos_L;
  170. private AOAccessor _aoZAxisPlacePos_H;
  171. private AOAccessor _aoXAxisMoveDistance_L;
  172. private AOAccessor _aoXAxisMoveDistance_H;
  173. private AOAccessor _aoYAxisMoveDistance_L;
  174. private AOAccessor _aoYAxisMoveDistance_H;
  175. private AOAccessor _aoZAxisMoveSpeed_L;
  176. private AOAccessor _aoZAxisMoveSpeed_H;
  177. private AOAccessor _aoXAxisMoveSpeed_L;
  178. private AOAccessor _aoXAxisMoveSpeed_H;
  179. private AOAccessor _aoYAxisMoveSpeed_L;
  180. private AOAccessor _aoYAxisMoveSpeed_H;
  181. private AOAccessor _aoTargetPos;
  182. private AOAccessor _aoXMoveFwdDistance_L;
  183. private AOAccessor _aoXMoveFwdDistance_H;
  184. private AOAccessor _aoYMoveFwdDistance_L;
  185. private AOAccessor _aoYMoveFwdDistance_H;
  186. private AOAccessor _aoZMoveFwdDistance_L;
  187. private AOAccessor _aoZMoveFwdDistance_H;
  188. private AOAccessor _aoXMoveRevDistance_L;
  189. private AOAccessor _aoXMoveRevDistance_H;
  190. private AOAccessor _aoYMoveRevDistance_L;
  191. private AOAccessor _aoYMoveRevDistance_H;
  192. private AOAccessor _aoZMoveRevDistance_L;
  193. private AOAccessor _aoZMoveRevDistance_H;
  194. private DIAccessor _diReset_Ack;
  195. private DIAccessor _diHome_Ack;
  196. private DIAccessor _diSave_Ack;
  197. private DIAccessor _diPick_Ack;
  198. private DIAccessor _diPlace_Ack;
  199. private DIAccessor _diX_Fwd_Ack;
  200. private DIAccessor _diX_Rev_Ack;
  201. private DIAccessor _diY_Fwd_Ack;
  202. private DIAccessor _diY_Rev_Ack;
  203. private DIAccessor _diZ_Fwd_Ack;
  204. private DIAccessor _diZ_Rev_Ack;
  205. private DIAccessor _diPlugError;
  206. private DIAccessor _diZMotionError;
  207. private DIAccessor _diXMotionError;
  208. private DIAccessor _diYMotionError;
  209. private DIAccessor _diZMotionOutLimit;
  210. private DIAccessor _diXMotionOutLimit;
  211. private DIAccessor _diYMotionOutLimit;
  212. private DIAccessor _diPLCReady;
  213. private DIAccessor _diMotionIdle;
  214. private DOAccessor _doResetCmd;
  215. private DOAccessor _doHomeCmd;
  216. private DOAccessor _doSaveCmd;
  217. private DOAccessor _doPickCmd;
  218. private DOAccessor _doPlaceCmd;
  219. private DOAccessor _doX_FwdCmd;
  220. private DOAccessor _doX_RevCmd;
  221. private DOAccessor _doY_FwdCmd;
  222. private DOAccessor _doY_RevCmd;
  223. private DOAccessor _doZ_FwdCmd;
  224. private DOAccessor _doZ_RevCmd;
  225. private PeriodicJob _thread;
  226. private R_TRIG _trigErrorOccurred;
  227. private int _currentXPosition
  228. {
  229. get
  230. {
  231. return TwoShortConvInt(_aiCurrentXPos_L.Value, _aiCurrentXPos_H.Value);
  232. }
  233. }
  234. private int _currentYPosition
  235. {
  236. get
  237. {
  238. return TwoShortConvInt(_aiCurrentYPos_L.Value, _aiCurrentYPos_H.Value);
  239. }
  240. }
  241. private int _currentZPosition
  242. {
  243. get
  244. {
  245. return TwoShortConvInt(_aiCurrentZPos_L.Value, _aiCurrentZPos_H.Value);
  246. }
  247. }
  248. private int _slotPitch
  249. {
  250. get
  251. {
  252. return TwoShortConvInt(_aiZAxisPitch_L.Value, _aiZAxisPitch_H.Value);
  253. }
  254. }
  255. private int _zFirstPosition
  256. {
  257. get
  258. {
  259. return TwoShortConvInt(_aiZAxisFirstPos_L .Value, _aiZAxisFirstPos_H.Value);
  260. }
  261. }
  262. private int _zPutWaferPosition
  263. {
  264. get
  265. {
  266. return TwoShortConvInt(_aiZAxisPlacePos_L.Value, _aiZAxisPlacePos_H.Value);
  267. }
  268. }
  269. private int _xMoveDistance
  270. {
  271. get
  272. {
  273. return TwoShortConvInt(_aiXAxisMoveDistance_L.Value, _aiXAxisMoveDistance_H.Value);
  274. }
  275. }
  276. private int _yMoveDistance
  277. {
  278. get
  279. {
  280. return TwoShortConvInt(_aiYAxisMoveDistance_L.Value, _aiYAxisMoveDistance_H.Value);
  281. }
  282. }
  283. private int _zMoveSpeed
  284. {
  285. get
  286. {
  287. return TwoShortConvInt(_aiZAxisMoveSpeed_L.Value, _aiZAxisMoveSpeed_H.Value);
  288. }
  289. }
  290. private int _yMoveSpeed
  291. {
  292. get
  293. {
  294. return TwoShortConvInt(_aiYAxisMoveSpeed_L.Value, _aiYAxisMoveSpeed_H.Value);
  295. }
  296. }
  297. private int _xMoveSpeed
  298. {
  299. get
  300. {
  301. return TwoShortConvInt(_aiXAxisMoveSpeed_L.Value, _aiXAxisMoveSpeed_H.Value);
  302. }
  303. }
  304. private bool OnTimer()
  305. {
  306. _trigErrorOccurred.CLK =
  307. _diPlugError.Value || _diZMotionError.Value
  308. || _diXMotionError.Value || _diYMotionError.Value || _diZMotionOutLimit.Value
  309. || _diXMotionOutLimit.Value || _diYMotionOutLimit.Value;
  310. if(_trigErrorOccurred.Q)
  311. {
  312. EV.Notify(AlarmRobotError);
  313. if (_diPlugError.Value)
  314. {
  315. EV.PostAlarmLog("Robot", $"Robot occurred error: _diPlugError.");
  316. }
  317. if (_diZMotionError.Value)
  318. {
  319. EV.PostAlarmLog("Robot", $"Robot occurred error: _diZMotionError.");
  320. }
  321. if (_diXMotionError.Value)
  322. {
  323. EV.PostAlarmLog("Robot", $"Robot occurred error: _diXMotionError.");
  324. }
  325. if (_diYMotionError.Value)
  326. {
  327. EV.PostAlarmLog("Robot", $"Robot occurred error: _diYMotionError.");
  328. }
  329. if (_diZMotionOutLimit.Value)
  330. {
  331. EV.PostAlarmLog("Robot", $"Robot occurred error: _diZMotionOutLimit.");
  332. }
  333. if (_diYMotionOutLimit.Value)
  334. {
  335. EV.PostAlarmLog("Robot", $"Robot occurred error: _diYMotionOutLimit.");
  336. }
  337. if (_diXMotionOutLimit.Value)
  338. {
  339. EV.PostAlarmLog("Robot", $"Robot occurred error: _diXMotionOutLimit.");
  340. }
  341. OnError("Robot Error");
  342. }
  343. return true;
  344. }
  345. #region Override Robot base function
  346. private enum RobotStepEnum
  347. {
  348. ActionStep1,
  349. ActionStep2,
  350. ActionStep3,
  351. ActionStep4,
  352. ActionStep5,
  353. ActionStep6,
  354. ActionStep7,
  355. ActionStep8,
  356. ActionStep9,
  357. ActionStep10,
  358. ActionStep11,
  359. ActionStep12,
  360. ActionStep13,
  361. ActionStep14,
  362. ActionStep15,
  363. ActionStep16,
  364. }
  365. private DateTime _dtActionStart;
  366. protected override bool fClear(object[] param)
  367. {
  368. return true;
  369. }
  370. protected override bool fStartReadData(object[] param)
  371. {
  372. return true;
  373. }
  374. protected override bool fStartSetParameters(object[] param)
  375. {
  376. try
  377. {
  378. _aoZAxisPitch_L.Value = _aiZAxisPitch_L.Value;
  379. _aoZAxisPitch_H.Value = _aiZAxisPitch_H.Value;
  380. _aoZAxisFirstPos_L.Value = _aiZAxisFirstPos_L.Value;
  381. _aoZAxisFirstPos_H.Value = _aiZAxisFirstPos_H.Value;
  382. _aoZAxisPlacePos_L.Value = _aiZAxisPlacePos_L.Value;
  383. _aoZAxisPlacePos_H.Value = _aiZAxisPlacePos_H.Value;
  384. _aoXAxisMoveDistance_L.Value = _aiXAxisMoveDistance_L.Value;
  385. _aoXAxisMoveDistance_H.Value = _aiXAxisMoveDistance_H.Value;
  386. _aoYAxisMoveDistance_L.Value = _aiYAxisMoveDistance_L.Value;
  387. _aoYAxisMoveDistance_H.Value = _aiYAxisMoveDistance_H.Value;
  388. _aoZAxisMoveSpeed_L.Value = _aiZAxisMoveSpeed_L.Value;
  389. _aoZAxisMoveSpeed_H.Value = _aiZAxisMoveSpeed_H.Value;
  390. _aoXAxisMoveSpeed_L.Value = _aiXAxisMoveSpeed_L.Value;
  391. _aoXAxisMoveSpeed_H.Value = _aiXAxisMoveSpeed_H.Value;
  392. _aoYAxisMoveSpeed_L.Value = _aiYAxisMoveSpeed_L.Value;
  393. _aoYAxisMoveSpeed_H.Value = _aiYAxisMoveSpeed_H.Value;
  394. string setcommand = param[0].ToString();
  395. int setvalue = Convert.ToInt32(param[1].ToString());
  396. short lowbit=0, highbit=0;
  397. IntConvTwoShort(setvalue, ref lowbit, ref highbit);
  398. _dtActionStart = DateTime.Now;
  399. ResetRoutine();
  400. switch (setcommand)
  401. {
  402. case "SlotPitch":
  403. _aoZAxisPitch_L.Value = lowbit;
  404. _aoZAxisPitch_H.Value = highbit;
  405. break;
  406. case "FirstZPosition":
  407. _aoZAxisFirstPos_L.Value = lowbit;
  408. _aoZAxisFirstPos_H.Value = highbit;
  409. break;
  410. case "PlaceWaferZPosition":
  411. _aoZAxisPlacePos_L.Value = lowbit;
  412. _aoZAxisPlacePos_H.Value = highbit;
  413. break;
  414. case "XMoveDistance":
  415. _aoXAxisMoveDistance_L.Value = lowbit;
  416. _aoXAxisMoveDistance_H.Value = highbit;
  417. break;
  418. case "YMoveDistance":
  419. _aoYAxisMoveDistance_L.Value = lowbit;
  420. _aoYAxisMoveDistance_H.Value = highbit;
  421. break;
  422. case "ZMoveSpeed":
  423. _aoZAxisMoveSpeed_L.Value = lowbit;
  424. _aoZAxisMoveSpeed_H.Value = highbit;
  425. break;
  426. case "XMoveSpeed":
  427. _aoXAxisMoveSpeed_L.Value = lowbit;
  428. _aoXAxisMoveSpeed_H.Value = highbit;
  429. break;
  430. case "YMoveSpeed":
  431. _aoYAxisMoveSpeed_L.Value = lowbit;
  432. _aoYAxisMoveSpeed_H.Value = highbit;
  433. break;
  434. default:
  435. return false;
  436. }
  437. }
  438. catch(Exception ex)
  439. {
  440. LOG.Write(ex);
  441. }
  442. return true;
  443. }
  444. protected override bool fMonitorSetParamter(object[] param)
  445. {
  446. IsBusy = false;
  447. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  448. {
  449. OnError("Command execution timeout");
  450. return true;
  451. }
  452. try
  453. {
  454. SetDoState((int)RobotStepEnum.ActionStep2, _doSaveCmd, true, Notify);
  455. WaitDiState((int)RobotStepEnum.ActionStep3, RobotCommandTimeout, _diSave_Ack, true, Notify, Stop);
  456. SetDoState((int)RobotStepEnum.ActionStep4, _doSaveCmd, false, Notify);
  457. WaitDiState((int)RobotStepEnum.ActionStep5, RobotCommandTimeout, _diSave_Ack, false, Notify, Stop);
  458. }
  459. catch (RoutineBreakException)
  460. {
  461. return false;
  462. }
  463. catch (RoutineFaildException)
  464. {
  465. EV.PostAlarmLog("Alarm", "Pick wafer failed.");
  466. OnError("PickFailed");
  467. }
  468. return true;
  469. }
  470. protected override bool fStartTransferWafer(object[] param)
  471. {
  472. return true;
  473. }
  474. protected override bool fStartUnGrip(object[] param)
  475. {
  476. return false;
  477. }
  478. protected override bool fStartGrip(object[] param)
  479. {
  480. return false;
  481. }
  482. protected override bool fStartGoTo(object[] param)
  483. {
  484. return false;
  485. }
  486. protected override bool fStartMapWafer(object[] param)
  487. {
  488. return false;
  489. }
  490. protected override bool fStartSwapWafer(object[] param)
  491. {
  492. return false;
  493. }
  494. protected override bool fStartPlaceWafer(object[] param)
  495. {
  496. if(!_diPLCReady.Value)
  497. {
  498. EV.PostAlarmLog("Robot", "PLC ready signal is OFF.");
  499. return false;
  500. }
  501. if(!_diMotionIdle.Value)
  502. {
  503. EV.PostAlarmLog("Robot", "Motion idle signal is OFF.");
  504. return false;
  505. }
  506. _dtActionStart = DateTime.Now;
  507. ResetRoutine();
  508. try
  509. {
  510. //RobotArmEnum arm = (RobotArmEnum)param[0];
  511. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), param[1].ToString());
  512. int slotindex = int.Parse(param[2].ToString());
  513. if (ModuleHelper.IsLoadPort(tempmodule))
  514. {
  515. _aoTargetPos.Value = (short)(slotindex + 1);
  516. }
  517. else
  518. _aoTargetPos.Value = 26;
  519. Blade1Target = tempmodule;
  520. Blade2Target = tempmodule;
  521. CmdTarget = tempmodule;
  522. MoveInfo = new RobotMoveInfo()
  523. {
  524. Action = RobotAction.Picking,
  525. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  526. BladeTarget = BuildBladeTarget(),
  527. };
  528. }
  529. catch (Exception ex)
  530. {
  531. LOG.Write(ex);
  532. return false;
  533. }
  534. return true;
  535. }
  536. protected override bool fMonitorPlace(object[] param)
  537. {
  538. IsBusy = false;
  539. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  540. {
  541. OnError("Command execution timeout");
  542. return true;
  543. }
  544. try
  545. {
  546. WaitAiValue((int)RobotStepEnum.ActionStep1, RobotCommandTimeout, _aiTargetPos, _aoTargetPos.Value, Notify, Stop);
  547. SetDoState((int)RobotStepEnum.ActionStep2, _doPlaceCmd, true, Notify);
  548. WaitDiState((int)RobotStepEnum.ActionStep3, RobotCommandTimeout, _diPlace_Ack, true, Notify, Stop);
  549. SetDoState((int)RobotStepEnum.ActionStep4, _doPlaceCmd, false, Notify);
  550. WaitDiState((int)RobotStepEnum.ActionStep5, RobotCommandTimeout, _diPlace_Ack, false, Notify, Stop);
  551. }
  552. catch (RoutineBreakException)
  553. {
  554. return false;
  555. }
  556. catch (RoutineFaildException)
  557. {
  558. EV.PostAlarmLog("Alarm", "Pick wafer failed.");
  559. OnError("PickFailed");
  560. return true;
  561. }
  562. Blade1Target = ModuleName.System;
  563. Blade2Target = ModuleName.System;
  564. CmdTarget = ModuleName.System;
  565. MoveInfo = new RobotMoveInfo()
  566. {
  567. Action = RobotAction.Picking,
  568. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  569. BladeTarget = BuildBladeTarget(),
  570. };
  571. ModuleName sourcemodule;
  572. if (!Enum.TryParse(CurrentParamter[1].ToString(), out sourcemodule)) return false;
  573. int SourceslotIndex;
  574. if (!int.TryParse(CurrentParamter[2].ToString(), out SourceslotIndex)) return false;
  575. WaferManager.Instance.WaferMoved(RobotModuleName, 0,sourcemodule, SourceslotIndex);
  576. return true;
  577. }
  578. protected override bool fMonitorPick(object[] param)
  579. {
  580. IsBusy = false;
  581. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  582. {
  583. OnError("Command execution timeout");
  584. return true;
  585. }
  586. try
  587. {
  588. WaitAiValue((int)RobotStepEnum.ActionStep1, RobotCommandTimeout, _aiTargetPos, _aoTargetPos.Value, Notify, Stop);
  589. SetDoState((int)RobotStepEnum.ActionStep2, _doPickCmd, true, Notify);
  590. WaitDiState((int)RobotStepEnum.ActionStep3, RobotCommandTimeout, _diPick_Ack, true, Notify, Stop);
  591. SetDoState((int)RobotStepEnum.ActionStep4, _doPickCmd, false, Notify);
  592. WaitDiState((int)RobotStepEnum.ActionStep5, RobotCommandTimeout, _diPick_Ack, false, Notify, Stop);
  593. }
  594. catch (RoutineBreakException)
  595. {
  596. return false;
  597. }
  598. catch (RoutineFaildException)
  599. {
  600. EV.PostAlarmLog("Alarm","Pick wafer failed.");
  601. OnError("PickFailed");
  602. return true;
  603. }
  604. Blade1Target = ModuleName.System;
  605. Blade2Target = ModuleName.System;
  606. CmdTarget = ModuleName.System;
  607. MoveInfo = new RobotMoveInfo()
  608. {
  609. Action = RobotAction.Picking,
  610. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  611. BladeTarget = BuildBladeTarget(),
  612. };
  613. ModuleName sourcemodule;
  614. if (!Enum.TryParse(CurrentParamter[1].ToString(), out sourcemodule)) return false;
  615. int SourceslotIndex;
  616. if (!int.TryParse(CurrentParamter[2].ToString(), out SourceslotIndex)) return false;
  617. WaferManager.Instance.WaferMoved(sourcemodule, SourceslotIndex, RobotModuleName, 0);
  618. return true;
  619. }
  620. protected override bool fStartPickWafer(object[] param)
  621. {
  622. if (!_diPLCReady.Value)
  623. {
  624. EV.PostAlarmLog("Robot", "PLC ready signal is OFF.");
  625. return false;
  626. }
  627. if (!_diMotionIdle.Value)
  628. {
  629. EV.PostAlarmLog("Robot", "Motion idle signal is OFF.");
  630. return false;
  631. }
  632. _dtActionStart = DateTime.Now;
  633. ResetRoutine();
  634. try
  635. {
  636. ModuleName tempmodule = (ModuleName)Enum.Parse(typeof(ModuleName), param[1].ToString());
  637. int slotindex = int.Parse(param[2].ToString());
  638. if (ModuleHelper.IsLoadPort(tempmodule))
  639. {
  640. _aoTargetPos.Value = (short)(slotindex + 1);
  641. }
  642. else
  643. _aoTargetPos.Value = 26;
  644. Blade1Target = tempmodule;
  645. Blade2Target = tempmodule;
  646. CmdTarget = tempmodule;
  647. MoveInfo = new RobotMoveInfo()
  648. {
  649. Action = RobotAction.Picking,
  650. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  651. BladeTarget = BuildBladeTarget(),
  652. };
  653. }
  654. catch (Exception ex)
  655. {
  656. LOG.Write(ex);
  657. return false;
  658. }
  659. return true;
  660. }
  661. protected override bool fResetToReady(object[] param)
  662. {
  663. return true;
  664. }
  665. protected override bool fReset(object[] param)
  666. {
  667. _dtActionStart = DateTime.Now;
  668. ResetRoutine();
  669. return true;
  670. }
  671. protected override bool fMonitorReset(object[] param)
  672. {
  673. IsBusy = false;
  674. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  675. {
  676. OnError("Command execution timeout");
  677. return true;
  678. }
  679. try
  680. {
  681. SetDoState((int)RobotStepEnum.ActionStep1, _doPickCmd, false, Notify);
  682. SetDoState((int)RobotStepEnum.ActionStep2, _doPlaceCmd, false, Notify);
  683. SetDoState((int)RobotStepEnum.ActionStep3, _doHomeCmd, false, Notify);
  684. SetDoState((int)RobotStepEnum.ActionStep4, _doSaveCmd, false, Notify);
  685. SetDoState((int)RobotStepEnum.ActionStep5, _doX_FwdCmd, false, Notify);
  686. SetDoState((int)RobotStepEnum.ActionStep6, _doX_RevCmd, false, Notify);
  687. SetDoState((int)RobotStepEnum.ActionStep7, _doY_FwdCmd, false, Notify);
  688. SetDoState((int)RobotStepEnum.ActionStep8, _doY_RevCmd, false, Notify);
  689. SetDoState((int)RobotStepEnum.ActionStep9, _doZ_FwdCmd, false, Notify);
  690. SetDoState((int)RobotStepEnum.ActionStep10, _doZ_RevCmd, false, Notify);
  691. SetDoState((int)RobotStepEnum.ActionStep11, _doResetCmd, true, Notify);
  692. WaitDiState((int)RobotStepEnum.ActionStep12, RobotCommandTimeout, _diReset_Ack, true, Notify, Stop);
  693. SetDoState((int)RobotStepEnum.ActionStep13, _doResetCmd, false, Notify);
  694. WaitDiState((int)RobotStepEnum.ActionStep14, RobotCommandTimeout, _diReset_Ack, false, Notify, Stop);
  695. }
  696. catch (RoutineBreakException)
  697. {
  698. return false;
  699. }
  700. catch (RoutineFaildException)
  701. {
  702. EV.PostAlarmLog("Alarm", "Pick wafer failed.");
  703. OnError("PickFailed");
  704. return true;
  705. }
  706. return true;
  707. }
  708. protected override bool fStartInit(object[] param)
  709. {
  710. _dtActionStart = DateTime.Now;
  711. ResetRoutine();
  712. return true;
  713. }
  714. protected override bool fMonitorInit(object[] param)
  715. {
  716. IsBusy = false;
  717. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  718. {
  719. OnError("Command execution timeout");
  720. return true;
  721. }
  722. try
  723. {
  724. SetDoState((int)RobotStepEnum.ActionStep1, _doPickCmd, false, Notify);
  725. SetDoState((int)RobotStepEnum.ActionStep2, _doPlaceCmd, false, Notify);
  726. SetDoState((int)RobotStepEnum.ActionStep3, _doResetCmd, false, Notify);
  727. SetDoState((int)RobotStepEnum.ActionStep4, _doSaveCmd, false, Notify);
  728. SetDoState((int)RobotStepEnum.ActionStep5, _doX_FwdCmd, false, Notify);
  729. SetDoState((int)RobotStepEnum.ActionStep6, _doX_RevCmd, false, Notify);
  730. SetDoState((int)RobotStepEnum.ActionStep7, _doY_FwdCmd, false, Notify);
  731. SetDoState((int)RobotStepEnum.ActionStep8, _doY_RevCmd, false, Notify);
  732. SetDoState((int)RobotStepEnum.ActionStep9, _doZ_FwdCmd, false, Notify);
  733. SetDoState((int)RobotStepEnum.ActionStep10, _doZ_RevCmd, false, Notify);
  734. SetDoState((int)RobotStepEnum.ActionStep11, _doHomeCmd, true, Notify);
  735. WaitDiState((int)RobotStepEnum.ActionStep12, RobotCommandTimeout, _diHome_Ack, true, Notify, Stop);
  736. SetDoState((int)RobotStepEnum.ActionStep13, _doHomeCmd, false, Notify);
  737. WaitDiState((int)RobotStepEnum.ActionStep14, RobotCommandTimeout, _diHome_Ack, false, Notify, Stop);
  738. }
  739. catch (RoutineBreakException)
  740. {
  741. return false;
  742. }
  743. catch (RoutineFaildException)
  744. {
  745. EV.PostAlarmLog("Alarm", "Pick wafer failed.");
  746. OnError("PickFailed");
  747. return true;
  748. }
  749. Blade1Target = ModuleName.System;
  750. Blade2Target = ModuleName.System;
  751. CmdTarget = ModuleName.System;
  752. MoveInfo = new RobotMoveInfo()
  753. {
  754. Action = RobotAction.Picking,
  755. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  756. BladeTarget = BuildBladeTarget(),
  757. };
  758. return true;
  759. }
  760. protected override bool fStartExecuteCommand(object[] param)
  761. {
  762. CurrentExecuteCmd = param[0].ToString();
  763. if (string.IsNullOrEmpty(CurrentExecuteCmd))
  764. return false;
  765. _dtActionStart = DateTime.Now;
  766. ResetRoutine();
  767. return true;
  768. }
  769. public string CurrentExecuteCmd { get; private set; }
  770. protected override bool fMonitorExecuting(object[] param)
  771. {
  772. string command = CurrentParamter[0].ToString();
  773. Int32 pnum = Convert.ToInt32(CurrentParamter[1]);
  774. short high16bit = (short)(pnum >> 16);
  775. short low16bit = (short)(pnum & ushort.MaxValue);
  776. try
  777. {
  778. switch (command)
  779. {
  780. case "XMoveFwd":
  781. SetAoValue((int)RobotStepEnum.ActionStep1, _aoXMoveFwdDistance_H, high16bit, Notify);
  782. SetAoValue((int)RobotStepEnum.ActionStep2, _aoXMoveFwdDistance_L, low16bit, Notify);
  783. SetDoState((int)RobotStepEnum.ActionStep3, _doX_FwdCmd, true, Notify);
  784. WaitDiState((int)RobotStepEnum.ActionStep4, RobotCommandTimeout, _diX_Fwd_Ack, true, Notify,Stop);
  785. SetDoState((int)RobotStepEnum.ActionStep5, _doX_FwdCmd, false, Notify);
  786. WaitDiState((int)RobotStepEnum.ActionStep6, RobotCommandTimeout, _diX_Fwd_Ack, false, Notify, Stop);
  787. SetAoValue((int)RobotStepEnum.ActionStep7, _aoXMoveFwdDistance_H, 0, Notify);
  788. SetAoValue((int)RobotStepEnum.ActionStep8, _aoXMoveFwdDistance_L, 0, Notify);
  789. break;
  790. case "XMoveRev":
  791. high16bit = (short)((-1*pnum) >> 16);
  792. low16bit = (short)((-1 * pnum) & ushort.MaxValue);
  793. SetAoValue((int)RobotStepEnum.ActionStep1, _aoXMoveRevDistance_H, high16bit, Notify);
  794. SetAoValue((int)RobotStepEnum.ActionStep2, _aoXMoveRevDistance_L, low16bit, Notify);
  795. SetDoState((int)RobotStepEnum.ActionStep3, _doX_RevCmd, true, Notify);
  796. WaitDiState((int)RobotStepEnum.ActionStep4, RobotCommandTimeout, _diX_Rev_Ack, true, Notify, Stop);
  797. SetDoState((int)RobotStepEnum.ActionStep5, _doX_RevCmd, false, Notify);
  798. WaitDiState((int)RobotStepEnum.ActionStep6, RobotCommandTimeout, _diX_Rev_Ack, false, Notify, Stop);
  799. SetAoValue((int)RobotStepEnum.ActionStep7, _aoXMoveRevDistance_H, 0, Notify);
  800. SetAoValue((int)RobotStepEnum.ActionStep8, _aoXMoveRevDistance_L, 0, Notify);
  801. break;
  802. case "YMoveFwd":
  803. SetAoValue((int)RobotStepEnum.ActionStep1, _aoYMoveFwdDistance_H, high16bit, Notify);
  804. SetAoValue((int)RobotStepEnum.ActionStep2, _aoYMoveFwdDistance_L, low16bit, Notify);
  805. SetDoState((int)RobotStepEnum.ActionStep3, _doY_FwdCmd, true, Notify);
  806. WaitDiState((int)RobotStepEnum.ActionStep4, RobotCommandTimeout, _diY_Fwd_Ack, true, Notify, Stop);
  807. SetDoState((int)RobotStepEnum.ActionStep5, _doY_FwdCmd, false, Notify);
  808. WaitDiState((int)RobotStepEnum.ActionStep6, RobotCommandTimeout, _diY_Fwd_Ack, false, Notify, Stop);
  809. SetAoValue((int)RobotStepEnum.ActionStep7, _aoYMoveFwdDistance_H, 0, Notify);
  810. SetAoValue((int)RobotStepEnum.ActionStep8, _aoYMoveFwdDistance_L, 0, Notify);
  811. break;
  812. case "YMoveRev":
  813. high16bit = (short)((-1 * pnum) >> 16);
  814. low16bit = (short)((-1 * pnum) & ushort.MaxValue);
  815. SetAoValue((int)RobotStepEnum.ActionStep1, _aoYMoveRevDistance_H, high16bit, Notify);
  816. SetAoValue((int)RobotStepEnum.ActionStep2, _aoYMoveRevDistance_L, low16bit, Notify);
  817. SetDoState((int)RobotStepEnum.ActionStep3, _doY_RevCmd, true, Notify);
  818. WaitDiState((int)RobotStepEnum.ActionStep4, RobotCommandTimeout, _diY_Rev_Ack, true, Notify, Stop);
  819. SetDoState((int)RobotStepEnum.ActionStep5, _doY_RevCmd, false, Notify);
  820. WaitDiState((int)RobotStepEnum.ActionStep6, RobotCommandTimeout, _diY_Rev_Ack, false, Notify, Stop);
  821. SetAoValue((int)RobotStepEnum.ActionStep7, _aoYMoveRevDistance_H, 0, Notify);
  822. SetAoValue((int)RobotStepEnum.ActionStep8, _aoYMoveRevDistance_L, 0, Notify);
  823. break;
  824. case "ZMoveFwd":
  825. SetAoValue((int)RobotStepEnum.ActionStep1, _aoZMoveFwdDistance_H, high16bit, Notify);
  826. SetAoValue((int)RobotStepEnum.ActionStep2, _aoZMoveFwdDistance_L, low16bit, Notify);
  827. SetDoState((int)RobotStepEnum.ActionStep3, _doZ_FwdCmd, true, Notify);
  828. WaitDiState((int)RobotStepEnum.ActionStep4, RobotCommandTimeout, _diZ_Fwd_Ack, true, Notify, Stop);
  829. SetDoState((int)RobotStepEnum.ActionStep5, _doZ_FwdCmd, false, Notify);
  830. WaitDiState((int)RobotStepEnum.ActionStep6, RobotCommandTimeout, _diZ_Fwd_Ack, false, Notify, Stop);
  831. SetAoValue((int)RobotStepEnum.ActionStep7, _aoZMoveFwdDistance_H, 0, Notify);
  832. SetAoValue((int)RobotStepEnum.ActionStep8, _aoZMoveFwdDistance_L, 0, Notify);
  833. break;
  834. case "ZMoveRev":
  835. high16bit = (short)((-1 * pnum) >> 16);
  836. low16bit = (short)((-1 * pnum) & ushort.MaxValue);
  837. SetAoValue((int)RobotStepEnum.ActionStep1, _aoZMoveRevDistance_H, high16bit, Notify);
  838. SetAoValue((int)RobotStepEnum.ActionStep2, _aoZMoveRevDistance_L, low16bit, Notify);
  839. SetDoState((int)RobotStepEnum.ActionStep3, _doZ_RevCmd, true, Notify);
  840. WaitDiState((int)RobotStepEnum.ActionStep4, RobotCommandTimeout, _diZ_Rev_Ack, true, Notify, Stop);
  841. SetDoState((int)RobotStepEnum.ActionStep5, _doZ_RevCmd, false, Notify);
  842. WaitDiState((int)RobotStepEnum.ActionStep6, RobotCommandTimeout, _diZ_Rev_Ack, false, Notify, Stop);
  843. SetAoValue((int)RobotStepEnum.ActionStep7, _aoZMoveRevDistance_H, 0, Notify);
  844. SetAoValue((int)RobotStepEnum.ActionStep8, _aoZMoveRevDistance_L, 0, Notify);
  845. break;
  846. default:
  847. break;
  848. }
  849. }
  850. catch (RoutineBreakException)
  851. {
  852. return false;
  853. }
  854. catch (RoutineFaildException)
  855. {
  856. EV.PostAlarmLog("Alarm", $"Execute command:{command} failed.");
  857. OnError($"Execute {command} failed");
  858. return true;
  859. }
  860. return true;
  861. }
  862. protected override bool fError(object[] param)
  863. {
  864. return true;
  865. }
  866. protected override bool fStartExtendForPick(object[] param)
  867. {
  868. return true;
  869. }
  870. protected override bool fStartExtendForPlace(object[] param)
  871. {
  872. return true;
  873. }
  874. protected override bool fStartRetractFromPick(object[] param)
  875. {
  876. return true;
  877. }
  878. protected override bool fStartRetractFromPlace(object[] param)
  879. {
  880. return true;
  881. }
  882. public override RobotArmWaferStateEnum GetWaferState(RobotArmEnum arm)
  883. {
  884. if (WaferManager.Instance.CheckHasWafer(RobotModuleName, 0))
  885. return RobotArmWaferStateEnum.Present;
  886. return RobotArmWaferStateEnum.Absent;
  887. }
  888. private string BuildBladeTarget()
  889. {
  890. return (CmdRobotArm == RobotArmEnum.Upper ? "ArmB" : "ArmA") + "." + CmdTarget;
  891. }
  892. private static int TwoShortConvInt(short low16bit, short high16bit)
  893. {
  894. return (high16bit << 16) + low16bit + (low16bit<0 ? UInt16.MaxValue+1 :0);
  895. }
  896. private static void IntConvTwoShort(int num,ref short low16bit,ref short high16bit)
  897. {
  898. high16bit = (short)(num >> 16);
  899. low16bit = (short)(num & ushort.MaxValue);
  900. }
  901. #endregion
  902. public DOAccessor ParseDoNode(string name, XmlElement node, string ioModule = "")
  903. {
  904. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  905. return IO.DO[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  906. return null;
  907. }
  908. public DIAccessor ParseDiNode(string name, XmlElement node, string ioModule = "")
  909. {
  910. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  911. return IO.DI[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  912. return null;
  913. }
  914. public AOAccessor ParseAoNode(string name, XmlElement node, string ioModule = "")
  915. {
  916. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  917. return IO.AO[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  918. return null;
  919. }
  920. public AIAccessor ParseAiNode(string name, XmlElement node, string ioModule = "")
  921. {
  922. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  923. return IO.AI[string.IsNullOrEmpty(ioModule) ? node.GetAttribute(name).Trim() : $"{ioModule}.{node.GetAttribute(name).Trim()}"];
  924. return null;
  925. }
  926. public SCConfigItem ParseScNode(string name, XmlElement node, string ioModule = "", string defaultScPath = "")
  927. {
  928. SCConfigItem result = null;
  929. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  930. result = SC.GetConfigItem(node.GetAttribute(name));
  931. if (result == null && !string.IsNullOrEmpty(defaultScPath) && SC.ContainsItem(defaultScPath))
  932. result = SC.GetConfigItem(defaultScPath);
  933. return result;
  934. }
  935. public static T ParseDeviceNode<T>(string name, XmlElement node) where T : class, IDevice
  936. {
  937. if (!string.IsNullOrEmpty(node.GetAttribute(name).Trim()))
  938. return DEVICE.GetDevice<T>(node.GetAttribute(name));
  939. LOG.Write(string.Format("{0},未定义{1}", node.InnerXml, name));
  940. return null;
  941. }
  942. public static T ParseDeviceNode<T>(string module, string name, XmlElement node) where T : class, IDevice
  943. {
  944. string device_id = node.GetAttribute(name);
  945. if (!string.IsNullOrEmpty(device_id) && !string.IsNullOrEmpty(device_id.Trim()))
  946. {
  947. return DEVICE.GetDevice<T>($"{module}.{device_id}");
  948. }
  949. LOG.Write(string.Format("{0},undefined {1}", node.InnerXml, name));
  950. return null;
  951. }
  952. public enum CarrierMode
  953. {
  954. Foup,
  955. Fosb,
  956. OpenCassette,
  957. }
  958. private void SetAoValue(int id, AOAccessor ao, short value, Action<string> notify)
  959. {
  960. var ret = Execute(id, () =>
  961. {
  962. notify($"{RobotModuleName} set {ao.Name} to {value}.");
  963. ao.Value = value;
  964. return true;
  965. });
  966. if (ret.Item1)
  967. {
  968. if (ret.Item2 == Result.FAIL)
  969. {
  970. throw new RoutineFaildException();
  971. }
  972. }
  973. }
  974. private void WaitAiValue(int id, int time, AIAccessor ai, short value, Action<string> notify, Action<string> error)
  975. {
  976. var ret = ExecuteAndWait(id, () =>
  977. {
  978. notify($"Wait {RobotModuleName} {ai.Name} to be {value}");
  979. return true;
  980. }, () =>
  981. {
  982. if (ai.Value == value)
  983. return true;
  984. return false;
  985. }, time * 1000);
  986. if (ret.Item1)
  987. {
  988. if (ret.Item2 == Result.FAIL)
  989. {
  990. throw new RoutineFaildException();
  991. }
  992. else if (ret.Item2 == Result.TIMEOUT) //timeout
  993. {
  994. //EV.Notify(RobotModuleName);
  995. error($"Wait {RobotModuleName} {ai.Name} to be {value} timeout after {time} seconds");
  996. throw new RoutineFaildException();
  997. }
  998. else
  999. {
  1000. throw new RoutineBreakException();
  1001. }
  1002. }
  1003. }
  1004. private void WaitDiState(int id, int time, DIAccessor di, bool state, Action<string> notify, Action<string> error)
  1005. {
  1006. var ret = ExecuteAndWait(id, () =>
  1007. {
  1008. notify($"Wait {RobotModuleName} {di.Name} to be {state}");
  1009. return true;
  1010. }, () =>
  1011. {
  1012. if (di.Value == state)
  1013. return true;
  1014. return false;
  1015. }, time * 1000);
  1016. if (ret.Item1)
  1017. {
  1018. if (ret.Item2 == Result.FAIL)
  1019. {
  1020. throw new RoutineFaildException();
  1021. }
  1022. else if (ret.Item2 == Result.TIMEOUT) //timeout
  1023. {
  1024. //EV.Notify();
  1025. error($"Wait {RobotModuleName} {di.Name} to be {state} timeout after {time} seconds");
  1026. throw new RoutineFaildException();
  1027. }
  1028. else
  1029. {
  1030. throw new RoutineBreakException();
  1031. }
  1032. }
  1033. }
  1034. private void SetDoState(int id, DOAccessor _do, bool state, Action<string> notify)
  1035. {
  1036. var ret = Execute(id, () =>
  1037. {
  1038. notify($"{RobotModuleName} start set {_do.Name} to {state}.");
  1039. //if (_do.Value == state)
  1040. //{
  1041. // _do.Value = !state;
  1042. // Thread.Sleep(500);
  1043. //}
  1044. return _do.SetValue(state, out _);
  1045. });
  1046. if (ret.Item1)
  1047. {
  1048. if (ret.Item2 == Result.FAIL)
  1049. {
  1050. throw new RoutineFaildException();
  1051. }
  1052. }
  1053. }
  1054. protected void Notify(string message)
  1055. {
  1056. EV.PostMessage(Name, EventEnum.GeneralInfo, string.Format("{0}:{1}", Name, message));
  1057. }
  1058. protected void Stop(string failReason)
  1059. {
  1060. OnError(string.Format("Failed {0}, {1} ", Name, failReason));
  1061. }
  1062. #region Routine Executor
  1063. //timer, 计算routine时间
  1064. protected DeviceTimer counter = new DeviceTimer();
  1065. protected DeviceTimer delayTimer = new DeviceTimer();
  1066. private enum STATE
  1067. {
  1068. IDLE,
  1069. WAIT,
  1070. }
  1071. public int TokenId
  1072. {
  1073. get { return _id; }
  1074. }
  1075. private int _id; //step index
  1076. /// <summary>
  1077. /// already done steps
  1078. /// </summary>
  1079. private Stack<int> _steps = new Stack<int>();
  1080. private STATE state; //step state //idel,wait,
  1081. //loop control
  1082. private int loop = 0;
  1083. private int loopCount = 0;
  1084. private int loopID = 0;
  1085. private DeviceTimer timer = new DeviceTimer();
  1086. public int LoopCounter { get { return loop; } }
  1087. public int LoopTotalTime { get { return loopCount; } }
  1088. // public int Timeout { get { return (int)(timer.GetTotalTime() / 1000); } }
  1089. //状态持续时间,单位为秒
  1090. public int Elapsed { get { return (int)(timer.GetElapseTime() / 1000); } }
  1091. protected RoutineResult RoutineToken = new RoutineResult() { Result = RoutineState.Running };
  1092. public void ResetRoutine()
  1093. {
  1094. _id = 0;
  1095. _steps.Clear();
  1096. loop = 0;
  1097. loopCount = 0;
  1098. state = STATE.IDLE;
  1099. counter.Start(60 * 60 * 100); //默认1小时
  1100. RoutineToken.Result = RoutineState.Running;
  1101. }
  1102. protected void PerformRoutineStep(int id, Func<RoutineState> execution, RoutineResult result)
  1103. {
  1104. if (!Acitve(id))
  1105. return;
  1106. result.Result = execution();
  1107. }
  1108. #region interface
  1109. public void StopLoop()
  1110. {
  1111. loop = loopCount;
  1112. }
  1113. public Tuple<bool, Result> Loop<T>(T id, Func<bool> func, int count)
  1114. {
  1115. int idx = Convert.ToInt32(id);
  1116. bool bActive = Acitve(idx);
  1117. if (bActive)
  1118. {
  1119. if (!func())
  1120. {
  1121. return Tuple.Create(bActive, Result.FAIL); //执行错误
  1122. }
  1123. loopID = idx;
  1124. loopCount = count;
  1125. next();
  1126. return Tuple.Create(true, Result.RUN);
  1127. }
  1128. return Tuple.Create(false, Result.RUN);
  1129. }
  1130. public Tuple<bool, Result> EndLoop<T>(T id, Func<bool> func)
  1131. {
  1132. int idx = Convert.ToInt32(id);
  1133. bool bActive = Acitve(idx);
  1134. if (bActive)
  1135. {
  1136. if (++loop >= loopCount) //Loop 结束
  1137. {
  1138. if (!func())
  1139. {
  1140. return Tuple.Create(bActive, Result.FAIL); //执行错误
  1141. }
  1142. loop = 0;
  1143. loopCount = 0; // Loop 结束时,当前loop和loop总数都清零
  1144. next();
  1145. return Tuple.Create(true, Result.RUN);
  1146. }
  1147. //继续下一LOOP
  1148. next(loopID);
  1149. return Tuple.Create(true, Result.RUN);
  1150. }
  1151. return Tuple.Create(false, Result.RUN);
  1152. }
  1153. public Tuple<bool, Result> ExecuteAndWait<T>(T id, IRoutine routine)
  1154. {
  1155. int idx = Convert.ToInt32(id);
  1156. bool bActive = Acitve(idx);
  1157. if (bActive)
  1158. {
  1159. if (state == STATE.IDLE)
  1160. {
  1161. Result startRet = routine.Start();
  1162. if (startRet == Result.FAIL)
  1163. {
  1164. return Tuple.Create(true, Result.FAIL); //执行错误
  1165. }
  1166. else if (startRet == Result.DONE)
  1167. {
  1168. next();
  1169. return Tuple.Create(true, Result.DONE);
  1170. }
  1171. state = STATE.WAIT;
  1172. }
  1173. Result ret = routine.Monitor();
  1174. if (ret == Result.DONE)
  1175. {
  1176. next();
  1177. return Tuple.Create(true, Result.DONE);
  1178. }
  1179. else if (ret == Result.FAIL || ret == Result.TIMEOUT)
  1180. {
  1181. return Tuple.Create(true, Result.FAIL);
  1182. }
  1183. else
  1184. {
  1185. return Tuple.Create(true, Result.RUN);
  1186. }
  1187. }
  1188. return Tuple.Create(false, Result.RUN);
  1189. }
  1190. public Tuple<bool, Result> ExecuteAndWait<T>(T id, List<IRoutine> routines)
  1191. {
  1192. int idx = Convert.ToInt32(id);
  1193. bool bActive = Acitve(idx);
  1194. if (bActive)
  1195. {
  1196. if (state == STATE.IDLE)
  1197. {
  1198. foreach (var item in routines)
  1199. {
  1200. if (item.Start() == Result.FAIL)
  1201. return Tuple.Create(true, Result.FAIL);
  1202. }
  1203. state = STATE.WAIT;
  1204. }
  1205. //wait all sub failed or completedboo
  1206. bool bFail = false;
  1207. bool bDone = true;
  1208. foreach (var item in routines)
  1209. {
  1210. Result ret = item.Monitor();
  1211. bDone &= (ret == Result.FAIL || ret == Result.DONE);
  1212. bFail |= ret == Result.FAIL;
  1213. }
  1214. if (bDone)
  1215. {
  1216. next();
  1217. if (bFail)
  1218. return Tuple.Create(true, Result.FAIL);
  1219. return Tuple.Create(true, Result.DONE);
  1220. }
  1221. return Tuple.Create(true, Result.RUN);
  1222. }
  1223. return Tuple.Create(false, Result.RUN);
  1224. }
  1225. public Tuple<bool, Result> Check<T>(T id, Func<bool> func) //顺序执行
  1226. {
  1227. return Check(Check(Convert.ToInt32(id), func));
  1228. }
  1229. public Tuple<bool, Result> Execute<T>(T id, Func<bool> func) //顺序执行
  1230. {
  1231. return Check(execute(Convert.ToInt32(id), func));
  1232. }
  1233. public Tuple<bool, Result> Wait<T>(T id, Func<bool> func, double timeout = int.MaxValue) //Wait condition
  1234. {
  1235. return Check(wait(Convert.ToInt32(id), func, timeout));
  1236. }
  1237. public Tuple<bool, Result> Wait<T>(T id, Func<bool?> func, double timeout = int.MaxValue) //Wait condition
  1238. {
  1239. return Check(wait(Convert.ToInt32(id), func, timeout));
  1240. }
  1241. public Tuple<bool, Result> ExecuteAndWait<T>(T id, Func<bool> execute, Func<bool?> check, double timeout = int.MaxValue)
  1242. {
  1243. int idx = Convert.ToInt32(id);
  1244. bool bActive = Acitve(idx);
  1245. bool? bExecute = false;
  1246. if (bActive)
  1247. {
  1248. if (state == STATE.IDLE)
  1249. {
  1250. if (!execute())
  1251. {
  1252. return Tuple.Create(bActive, Result.FAIL); //执行错误
  1253. }
  1254. timer.Start(timeout);
  1255. state = STATE.WAIT;
  1256. }
  1257. bExecute = check();
  1258. if (bExecute == null)
  1259. {
  1260. return Tuple.Create(bActive, Result.FAIL); //Termianate
  1261. }
  1262. else
  1263. {
  1264. if (bExecute.Value) //检查Success, next
  1265. {
  1266. next();
  1267. return Tuple.Create(true, Result.RUN);
  1268. }
  1269. }
  1270. if (timer.IsTimeout())
  1271. return Tuple.Create(true, Result.TIMEOUT);
  1272. return Tuple.Create(true, Result.RUN);
  1273. }
  1274. return Tuple.Create(false, Result.RUN);
  1275. }
  1276. public Tuple<bool, Result> ExecuteAndWait<T>(T id, Func<bool> execute, Func<bool?> check, Func<double> time)
  1277. {
  1278. int idx = Convert.ToInt32(id);
  1279. bool bActive = Acitve(idx);
  1280. bool? bExecute = false;
  1281. double timeout = 0;
  1282. if (bActive)
  1283. {
  1284. if (state == STATE.IDLE)
  1285. {
  1286. timeout = time();
  1287. if (!execute())
  1288. {
  1289. return Tuple.Create(true, Result.FAIL); //执行错误
  1290. }
  1291. timer.Start(timeout);
  1292. state = STATE.WAIT;
  1293. }
  1294. bExecute = check();
  1295. if (bExecute == null)
  1296. {
  1297. return Tuple.Create(true, Result.FAIL); //Termianate
  1298. }
  1299. if (bExecute.Value) //检查Success, next
  1300. {
  1301. next();
  1302. return Tuple.Create(true, Result.RUN);
  1303. }
  1304. if (timer.IsTimeout())
  1305. return Tuple.Create(true, Result.TIMEOUT);
  1306. return Tuple.Create(true, Result.RUN);
  1307. }
  1308. return Tuple.Create(false, Result.RUN);
  1309. }
  1310. public Tuple<bool, Result> Wait<T>(T id, IRoutine rt)
  1311. {
  1312. int idx = Convert.ToInt32(id);
  1313. bool bActive = Acitve(idx);
  1314. if (bActive)
  1315. {
  1316. if (state == STATE.IDLE)
  1317. {
  1318. rt.Start();
  1319. state = STATE.WAIT;
  1320. }
  1321. Result ret = rt.Monitor();
  1322. return Tuple.Create(true, ret);
  1323. }
  1324. return Tuple.Create(false, Result.RUN);
  1325. }
  1326. //Monitor
  1327. public Tuple<bool, Result> Monitor<T>(T id, Func<bool> func, Func<bool> check, double time)
  1328. {
  1329. int idx = Convert.ToInt32(id);
  1330. bool bActive = Acitve(idx);
  1331. bool bCheck = false;
  1332. if (bActive)
  1333. {
  1334. if (state == STATE.IDLE)
  1335. {
  1336. if ((func != null) && !func())
  1337. {
  1338. return Tuple.Create(true, Result.FAIL);
  1339. }
  1340. timer.Start(time);
  1341. state = STATE.WAIT;
  1342. }
  1343. bCheck = check();
  1344. if (!bCheck)
  1345. {
  1346. return Tuple.Create(true, Result.FAIL); //Termianate
  1347. }
  1348. if (timer.IsTimeout())
  1349. {
  1350. next();
  1351. }
  1352. return Tuple.Create(true, Result.RUN);
  1353. }
  1354. return Tuple.Create(false, Result.RUN);
  1355. }
  1356. //Delay
  1357. public Tuple<bool, Result> Delay<T>(T id, Func<bool> func, double time)
  1358. {
  1359. int idx = Convert.ToInt32(id);
  1360. bool bActive = Acitve(idx);
  1361. if (bActive)
  1362. {
  1363. if (state == STATE.IDLE)
  1364. {
  1365. if ((func != null) && !func())
  1366. {
  1367. return Tuple.Create(true, Result.FAIL);
  1368. }
  1369. timer.Start(time);
  1370. state = STATE.WAIT;
  1371. }
  1372. if (timer.IsTimeout())
  1373. {
  1374. next();
  1375. }
  1376. return Tuple.Create(true, Result.RUN);
  1377. }
  1378. return Tuple.Create(false, Result.RUN);
  1379. }
  1380. //先delay 再运行
  1381. public Tuple<bool, Result> DelayCheck<T>(T id, Func<bool> func, double time)
  1382. {
  1383. int idx = Convert.ToInt32(id);
  1384. bool bActive = Acitve(idx);
  1385. if (bActive)
  1386. {
  1387. if (state == STATE.IDLE)
  1388. {
  1389. timer.Start(time);
  1390. state = STATE.WAIT;
  1391. }
  1392. if (timer.IsTimeout())
  1393. {
  1394. if (func != null && !func())
  1395. {
  1396. return Tuple.Create(true, Result.FAIL);
  1397. }
  1398. next();
  1399. }
  1400. return Tuple.Create(true, Result.RUN);
  1401. }
  1402. return Tuple.Create(false, Result.RUN);
  1403. }
  1404. #endregion
  1405. private Tuple<bool, bool> execute(int id, Func<bool> func) //顺序执行
  1406. {
  1407. bool bActive = Acitve(id);
  1408. bool bExecute = false;
  1409. if (bActive)
  1410. {
  1411. bExecute = func();
  1412. if (bExecute)
  1413. {
  1414. next();
  1415. }
  1416. }
  1417. return Tuple.Create(bActive, bExecute);
  1418. }
  1419. private Tuple<bool, bool> Check(int id, Func<bool> func) //check
  1420. {
  1421. bool bActive = Acitve(id);
  1422. bool bExecute = false;
  1423. if (bActive)
  1424. {
  1425. bExecute = func();
  1426. next();
  1427. }
  1428. return Tuple.Create(bActive, bExecute);
  1429. }
  1430. /// <summary>
  1431. /// </summary>
  1432. /// <param name="id"></param>
  1433. /// <param name="func"></param>
  1434. /// <param name="timeout"></param>
  1435. /// <returns>
  1436. /// item1 Active
  1437. /// item2 execute
  1438. /// item3 Timeout
  1439. ///</returns>
  1440. private Tuple<bool, bool, bool> wait(int id, Func<bool> func, double timeout = int.MaxValue) //Wait condition
  1441. {
  1442. bool bActive = Acitve(id);
  1443. bool bExecute = false;
  1444. bool bTimeout = false;
  1445. if (bActive)
  1446. {
  1447. if (state == STATE.IDLE)
  1448. {
  1449. timer.Start(timeout);
  1450. state = STATE.WAIT;
  1451. }
  1452. bExecute = func();
  1453. if (bExecute)
  1454. {
  1455. next();
  1456. }
  1457. bTimeout = timer.IsTimeout();
  1458. }
  1459. return Tuple.Create(bActive, bExecute, bTimeout);
  1460. }
  1461. private Tuple<bool, bool?, bool> wait(int id, Func<bool?> func, double timeout = int.MaxValue) //Wait condition && Check error
  1462. {
  1463. bool bActive = Acitve(id);
  1464. bool? bExecute = false;
  1465. bool bTimeout = false;
  1466. if (bActive)
  1467. {
  1468. if (state == STATE.IDLE)
  1469. {
  1470. timer.Start(timeout);
  1471. state = STATE.WAIT;
  1472. }
  1473. bExecute = func();
  1474. if (bExecute.HasValue && bExecute.Value)
  1475. {
  1476. next();
  1477. }
  1478. bTimeout = timer.IsTimeout();
  1479. }
  1480. return Tuple.Create(bActive, bExecute, bTimeout);
  1481. }
  1482. /// <summary>
  1483. /// </summary>
  1484. /// <param name="value"></param>
  1485. /// <returns>
  1486. /// item1 true, return item2
  1487. /// </returns>
  1488. private Tuple<bool, Result> Check(Tuple<bool, bool> value)
  1489. {
  1490. if (value.Item1)
  1491. {
  1492. if (!value.Item2)
  1493. {
  1494. return Tuple.Create(true, Result.FAIL);
  1495. }
  1496. return Tuple.Create(true, Result.RUN);
  1497. }
  1498. return Tuple.Create(false, Result.RUN);
  1499. }
  1500. private Tuple<bool, Result> Check(Tuple<bool, bool, bool> value)
  1501. {
  1502. if (value.Item1) // 当前执行
  1503. {
  1504. if (CheckTimeout(value)) //timeout
  1505. {
  1506. return Tuple.Create(true, Result.TIMEOUT);
  1507. }
  1508. return Tuple.Create(true, Result.RUN);
  1509. }
  1510. return Tuple.Create(false, Result.RUN);
  1511. }
  1512. private Tuple<bool, Result> Check(Tuple<bool, bool?, bool> value)
  1513. {
  1514. if (value.Item1) // 当前执行
  1515. {
  1516. if (value.Item2 == null)
  1517. {
  1518. return Tuple.Create(true, Result.FAIL);
  1519. }
  1520. else
  1521. {
  1522. if (value.Item2 == false && value.Item3 == true) //timeout
  1523. {
  1524. return Tuple.Create(true, Result.TIMEOUT);
  1525. }
  1526. return Tuple.Create(true, Result.RUN);
  1527. }
  1528. }
  1529. return Tuple.Create(false, Result.RUN);
  1530. }
  1531. private bool CheckTimeout(Tuple<bool, bool, bool> value)
  1532. {
  1533. return value.Item1 == true && value.Item2 == false && value.Item3 == true;
  1534. }
  1535. private bool Acitve(int id) //
  1536. {
  1537. if (_steps.Contains(id))
  1538. return false;
  1539. this._id = id;
  1540. return true;
  1541. }
  1542. private void next()
  1543. {
  1544. _steps.Push(this._id);
  1545. state = STATE.IDLE;
  1546. }
  1547. private void next(int step) //loop
  1548. {
  1549. while (_steps.Pop() != step) ;
  1550. state = STATE.IDLE;
  1551. }
  1552. public void Delay(int id, double delaySeconds)
  1553. {
  1554. Tuple<bool, Result> ret = Delay(id, () =>
  1555. {
  1556. return true;
  1557. }, delaySeconds * 1000);
  1558. if (ret.Item1)
  1559. {
  1560. if (ret.Item2 == Result.RUN)
  1561. {
  1562. throw (new RoutineBreakException());
  1563. }
  1564. }
  1565. }
  1566. public bool IsActived(int id)
  1567. {
  1568. return _steps.Contains(id);
  1569. }
  1570. #endregion
  1571. }
  1572. }