HirataRobotII.cs 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393
  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.LoadPorts.LoadPortBase;
  25. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.WalkingAixs;
  26. using Aitex.Core.RT.Routine;
  27. using MECF.Framework.Common.CommonData;
  28. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.HirataRobotII
  29. {
  30. public class HirataRobotII : RobotBaseDevice, IConnection
  31. {
  32. public bool isSimulatorMode
  33. {
  34. get
  35. {
  36. return SC.ContainsItem("System.IsSimulatorMode") ? SC.GetValue<bool>("System.IsSimulatorMode") : false;
  37. }
  38. }
  39. public string Address { get { return _address; } }
  40. public string PortName;
  41. public bool IsConnected { get; }
  42. public bool Connect()
  43. {
  44. return _connection.Connect();
  45. }
  46. public bool Disconnect()
  47. {
  48. return _connection.Disconnect();
  49. }
  50. private HirataRobotIIConnection _connection;
  51. public HirataRobotIIConnection Connection
  52. {
  53. get { return _connection; }
  54. }
  55. private R_TRIG _trigError = new R_TRIG();
  56. private R_TRIG _trigCommunicationError = new R_TRIG();
  57. private R_TRIG _trigRetryConnect = new R_TRIG();
  58. private R_TRIG _trigActionDone = new R_TRIG();
  59. private PeriodicJob _thread;
  60. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  61. public List<IOResponse> IOResponseList { get; set; } = new List<IOResponse>();
  62. private object _locker = new object();
  63. private bool _enableLog;
  64. public int Axis { get; private set; }
  65. private float _armPitch { get; set; }
  66. private string _scRoot;
  67. public bool DIReadValue { get; set; }
  68. private bool[,] di_Values { get; set; } = new bool[8,8];
  69. protected WalkingAxisBaseDevice m_WalkingAxis { get; set; }
  70. public HirataRobotII(string module, string name, string scRoot, WalkingAxisBaseDevice walkaxis=null) : base(module, name)
  71. {
  72. _scRoot = scRoot;
  73. m_WalkingAxis = walkaxis;
  74. //base.Initialize();
  75. ResetPropertiesAndResponses();
  76. RegisterSpecialData();
  77. _address = SC.GetStringValue($"{_scRoot}.{Name}.Address");
  78. _enableLog = SC.GetValue<bool>($"{_scRoot}.{Name}.EnableLogMessage");
  79. Axis = SC.GetValue<int>($"{_scRoot}.{Name}.RobotAxis");
  80. _armPitch = SC.GetValue<int>($"{_scRoot}.{Name}.ArmPitch") / 100;
  81. PortName = SC.GetStringValue($"{_scRoot}.{Name}.PortName");
  82. _connection = new HirataRobotIIConnection(PortName,this);
  83. _connection.EnableLog(_enableLog);
  84. if (_connection.Connect())
  85. {
  86. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  87. }
  88. _thread = new PeriodicJob(50, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  89. //_address = SC.GetStringValue($"{_scRoot}.{Name}.DeviceAddress");
  90. }
  91. private void RegisterSpecialData()
  92. {
  93. DATA.Subscribe($"{Module}.{Name}.CurrentExtParaNO", () => CurrentReadExtParaNO);
  94. DATA.Subscribe($"{Module}.{Name}.CurrentExtParaValue", () => CurrentReadExtParaValue);
  95. DATA.Subscribe($"{Module}.{Name}.RobotIsOnline", () => IsOnLine);
  96. DATA.Subscribe($"{Module}.{Name}.RobotIsManual", () => IsManual);
  97. DATA.Subscribe($"{Module}.{Name}.RobotIsAuto", () => IsAuto);
  98. DATA.Subscribe($"{Module}.{Name}.RobotStopSignal", () => IsStop);
  99. DATA.Subscribe($"{Module}.{Name}.RobotEMSignal", () => IsES);
  100. DATA.Subscribe($"{Module}.{Name}.RobotZaxisSaftyZone", () => IsZaxisSafeZone);
  101. DATA.Subscribe($"{Module}.{Name}.RobotPositioningCompleted", () => MovingCompleted);
  102. DATA.Subscribe($"{Module}.{Name}.RobotACalCompleted", () => ACalCompleted);
  103. DATA.Subscribe($"{Module}.{Name}.RobotExecutionCompleted", () => ExecutionComplete);
  104. DATA.Subscribe($"{Module}.{Name}.RobotReadXPosition", () => ReadXPosition);
  105. DATA.Subscribe($"{Module}.{Name}.RobotReadYPosition", () => ReadYPosition);
  106. DATA.Subscribe($"{Module}.{Name}.RobotReadZPosition", () => ReadZPosition);
  107. DATA.Subscribe($"{Module}.{Name}.RobotReadWPosition", () => ReadWPosition);
  108. DATA.Subscribe($"{Module}.{Name}.RobotMdata", () => MData);
  109. DATA.Subscribe($"{Module}.{Name}.RobotFCode", () => FCode);
  110. DATA.Subscribe($"{Module}.{Name}.RobotSCode", () => SCode);
  111. DATA.Subscribe($"{Module}.{Name}.ExtPara_MapWaferCount", () => ExtPara_MapWaferCount);
  112. DATA.Subscribe($"{Module}.{Name}.ExtPara_WaferThickness", () => ExtPara_WaferThickness);
  113. DATA.Subscribe($"{Module}.{Name}.ExtPara_PositionRange", () => ExtPara_PositionRange);
  114. DATA.Subscribe($"{Module}.{Name}.ExtPara_Pitch", () => ExtPara_Pitch);
  115. DATA.Subscribe($"{Module}.{Name}.ExtPara_WaferMinThickness", () => ExtPara_WaferMinThickness);
  116. DATA.Subscribe($"{Module}.{Name}.ExtPara_Filter", () => ExtPara_Filter);
  117. DATA.Subscribe($"{Name}.CurrentExtParaNO", () => CurrentReadExtParaNO);
  118. DATA.Subscribe($"{Name}.CurrentExtParaValue", () => CurrentReadExtParaValue);
  119. DATA.Subscribe($"{Name}.RobotIsOnline", () => IsOnLine);
  120. DATA.Subscribe($"{Name}.RobotIsManual", () => IsManual);
  121. DATA.Subscribe($"{Name}.RobotIsAuto", () => IsAuto);
  122. DATA.Subscribe($"{Name}.RobotStopSignal", () => IsStop);
  123. DATA.Subscribe($"{Name}.RobotEMSignal", () => IsES);
  124. DATA.Subscribe($"{Name}.RobotZaxisSaftyZone", () => IsZaxisSafeZone);
  125. DATA.Subscribe($"{Name}.RobotPositioningCompleted", () => MovingCompleted);
  126. DATA.Subscribe($"{Name}.RobotACalCompleted", () => ACalCompleted);
  127. DATA.Subscribe($"{Name}.RobotExecutionCompleted", () => ExecutionComplete);
  128. DATA.Subscribe($"{Name}.RobotReadXPosition", () => ReadXPosition);
  129. DATA.Subscribe($"{Name}.RobotReadYPosition", () => ReadYPosition);
  130. DATA.Subscribe($"{Name}.RobotReadZPosition", () => ReadZPosition);
  131. DATA.Subscribe($"{Name}.RobotReadWPosition", () => ReadWPosition);
  132. DATA.Subscribe($"{Name}.RobotMdata", () => MData);
  133. DATA.Subscribe($"{Name}.RobotFCode", () => FCode);
  134. DATA.Subscribe($"{Name}.RobotSCode", () => SCode);
  135. DATA.Subscribe($"{Name}.ExtPara_MapWaferCount", () => ExtPara_MapWaferCount);
  136. DATA.Subscribe($"{Name}.ExtPara_WaferThickness", () => ExtPara_WaferThickness);
  137. DATA.Subscribe($"{Name}.ExtPara_PositionRange", () => ExtPara_PositionRange);
  138. DATA.Subscribe($"{Name}.ExtPara_Pitch", () => ExtPara_Pitch);
  139. DATA.Subscribe($"{Name}.ExtPara_WaferMinThickness", () => ExtPara_WaferMinThickness);
  140. DATA.Subscribe($"{Name}.ExtPara_Filter", () => ExtPara_Filter);
  141. }
  142. protected override bool Init()
  143. {
  144. return true;
  145. }
  146. private void ResetPropertiesAndResponses()
  147. {
  148. Connected = true;
  149. Error = null;
  150. RobotStatus = null;
  151. IsOnLine = false;
  152. IsManual = false;
  153. IsES = false;
  154. MovingCompleted = false;
  155. ACalCompleted = false;
  156. AddressOutSide = null;
  157. PositionOutSide = null;
  158. EmergencyStop = null;
  159. XLowerSide = null;
  160. XUpperSide = null;
  161. YLowerSide = null;
  162. YUpperSide = null;
  163. ZLowerSide = null;
  164. ZUpperSide = null;
  165. foreach (var ioResponse in IOResponseList)
  166. {
  167. ioResponse.ResonseContent = null;
  168. ioResponse.ResonseRecievedTime = DateTime.Now;
  169. }
  170. }
  171. public override bool IsReady()
  172. {
  173. return (!_connection.IsBusy && _lstHandler.Count == 0 && IsOnLine && ACalCompleted &&
  174. MovingCompleted&& !_connection.IsCommunicationError && !IsBusy && RobotState == RobotStateEnum.Idle);
  175. }
  176. private string _address;
  177. private bool OnTimer()
  178. {
  179. try
  180. {
  181. //return true;
  182. if (!_connection.IsConnected || _connection.IsCommunicationError)
  183. {
  184. lock (_locker)
  185. {
  186. _lstHandler.Clear();
  187. }
  188. _trigRetryConnect.CLK = !_connection.IsConnected;
  189. if (_trigRetryConnect.Q)
  190. {
  191. _connection.SetPortAddress(SC.GetStringValue($"{_scRoot}.{Name}.PortName"));
  192. if (!_connection.Connect())
  193. {
  194. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  195. }
  196. else
  197. {
  198. //_lstHandler.AddLast(new RobotHirataR4QueryPinHandler(this, _deviceAddress));
  199. //_lstHandler.AddLast(new RobotHirataR4SetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
  200. }
  201. }
  202. return true;
  203. }
  204. HandlerBase handler = null;
  205. lock (_locker)
  206. {
  207. if (_lstHandler.Count > 0)
  208. {
  209. if (!_connection.IsBusy)
  210. {
  211. if (IsOnLine && ACalCompleted && MovingCompleted)
  212. {
  213. handler = _lstHandler.First.Value;
  214. _lstHandler.RemoveFirst();
  215. }
  216. else
  217. {
  218. Thread.Sleep(100);
  219. handler = new HirataRobotIIMonitorRobotStatusHandler(this);
  220. }
  221. _connection.Execute(handler);
  222. }
  223. }
  224. if(_connection.IsBusy)
  225. {
  226. _connection.MonitorTimeout();
  227. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  228. if (_trigCommunicationError.Q)
  229. {
  230. _lstHandler.Clear();
  231. //EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  232. OnError($"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  233. //_trigActionDone.CLK = true;
  234. }
  235. }
  236. }
  237. }
  238. catch (Exception ex)
  239. {
  240. LOG.Write(ex);
  241. }
  242. return true;
  243. }
  244. public override void Monitor()
  245. {
  246. try
  247. {
  248. }
  249. catch (Exception ex)
  250. {
  251. LOG.Write(ex);
  252. }
  253. }
  254. public override void Reset()
  255. {
  256. _trigError.RST = true;
  257. _connection.SetCommunicationError(false, "");
  258. _trigCommunicationError.RST = true;
  259. _enableLog = SC.GetValue<bool>($"{_scRoot}.{Name}.EnableLogMessage");
  260. _connection.EnableLog(_enableLog);
  261. _trigRetryConnect.RST = true;
  262. //base.Reset();
  263. }
  264. #region Command Functions
  265. public void PerformRawCommand(string command, string comandArgument)
  266. {
  267. lock (_locker)
  268. {
  269. _lstHandler.AddLast(new HirataRobotIIRawCommandHandler(this, command, comandArgument));
  270. }
  271. }
  272. public void PerformRawCommand(string command)
  273. {
  274. lock (_locker)
  275. {
  276. _lstHandler.AddLast(new HirataRobotIIRawCommandHandler(this, command));
  277. }
  278. }
  279. public bool WritePosition(int address, float x, float y, float z, float w,
  280. string Mdata, string Fcode, string Scode)
  281. {
  282. int intFcode,intScode;
  283. string strMdata;
  284. if (!int.TryParse(Fcode, out intFcode)) return false;
  285. if (!int.TryParse(Scode, out intScode)) return false;
  286. if (intFcode < 0 || intFcode > 99 || intScode < 0 || intScode > 99)
  287. {
  288. EV.PostAlarmLog("Robot", "Robot postion data format is not correct");
  289. return false;
  290. }
  291. if (!int.TryParse(Mdata, out _)) strMdata = "??";
  292. else strMdata = (Convert.ToInt32(Mdata) < 0 || Convert.ToInt32(Mdata) > 99) ?
  293. "??" : Mdata.ToString().PadLeft(2, '0');
  294. lock (_locker)
  295. {
  296. _lstHandler.AddLast(new HirataRobotIIWritePositionHandler(this, address, x, y, z, w, 0, 0,
  297. "R", strMdata, Fcode.ToString().PadLeft(2, '0'),
  298. Scode.ToString().PadLeft(2, '0')));
  299. }
  300. return true;
  301. }
  302. protected override bool fStop(object[] param)
  303. {
  304. MoveStop();
  305. return true;
  306. }
  307. public void MoveStop()
  308. {
  309. lock (_locker)
  310. {
  311. _lstHandler.Clear();
  312. _connection.ForceClear();
  313. _connection.Execute(new HirataRobotIISimpleActionHandler(this, "GD"));
  314. _connection.ForceClear();
  315. }
  316. }
  317. public void MoveReset()
  318. {
  319. lock (_locker)
  320. {
  321. _lstHandler.AddLast(new HirataRobotIISimpleActionHandler(this, "GE"));
  322. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  323. }
  324. }
  325. public void ErrorClear()
  326. {
  327. lock (_locker)
  328. {
  329. _lstHandler.AddLast(new HirataRobotIISimpleActionHandler(this, "CL"));
  330. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  331. }
  332. }
  333. public void ReadPosition(int address)
  334. {
  335. _lstHandler.AddLast(new HirataRobotIIReadRobotPositionHandler(this, address));
  336. }
  337. #endregion
  338. #region Properties
  339. public bool Connected { get; private set; }
  340. public string Error { get; private set; }
  341. public string RobotStatus { get; private set; }
  342. public bool IsOnLine { get; private set; }
  343. public bool IsManual { get; private set; }
  344. public bool IsAuto { get; private set; }
  345. public bool IsStop { get; private set; }
  346. public bool IsES { get; private set; }
  347. public bool IsZaxisSafeZone { get; private set; }
  348. public bool MovingCompleted { get; private set; }
  349. public bool ACalCompleted { get; private set; }
  350. public bool ExecutionComplete { get; private set; }
  351. public string AddressOutSide { get; private set; }
  352. public string PositionOutSide { get; private set; }
  353. public string EmergencyStop { get; private set; }
  354. public string XLowerSide { get; private set; }
  355. public string XUpperSide { get; private set; }
  356. public string YLowerSide { get; private set; }
  357. public string YUpperSide { get; private set; }
  358. public string ZLowerSide { get; private set; }
  359. public string ZUpperSide { get; private set; }
  360. public string ReadRobotPositonData { get; private set; }
  361. public float ReadXPosition { get; private set; }
  362. public float ReadYPosition { get; private set; }
  363. public float ReadZPosition { get; private set; }
  364. public float ReadWPosition { get; private set; }
  365. public float ReadRPosition { get; private set; }
  366. public float ReadCPosition { get; private set; }
  367. public string RobotPosture { get; private set; }
  368. public string MData { get; private set; }
  369. public string FCode { get; private set; }
  370. public string SCode { get; private set; }
  371. public string ExtPara_MapWaferCount { get; private set; }
  372. public string ExtPara_WaferThickness { get; private set; }
  373. public string ExtPara_PositionRange { get; private set; }
  374. public string ExtPara_Pitch { get; private set; }
  375. public string ExtPara_WaferMinThickness { get; private set; }
  376. public string ExtPara_Filter { get; private set; }
  377. #endregion
  378. public string MapExistInfor { get; private set; }
  379. public string MapNGInfor { get; private set; }
  380. public string CurrentReadExtParaNO { get; private set; }
  381. public string CurrentReadExtParaValue { get; private set; }
  382. private ModuleName _currentMotionStation;
  383. private int _currentMotionSlot;
  384. private RobotArmEnum _currentMotionArm;
  385. #region Note Functions
  386. private R_TRIG _trigWarningMessage = new R_TRIG();
  387. public void NoteRobotDIByte(string data, int offset)
  388. {
  389. int tempvalue;
  390. if (!int.TryParse(data.Replace(Encoding.ASCII.GetString(new byte[] { 0x3}),""), out tempvalue)) return;
  391. di_Values[offset, 0] = ((tempvalue & 0x1) == 0x1);
  392. di_Values[offset, 1] = ((tempvalue & 0x2) == 0x2);
  393. di_Values[offset, 2] = ((tempvalue & 0x4) == 0x4);
  394. di_Values[offset, 3] = ((tempvalue & 0x8) == 0x8);
  395. di_Values[offset, 4] = ((tempvalue & 0x10) == 0x10);
  396. di_Values[offset, 5] = ((tempvalue & 0x10) == 0x20);
  397. di_Values[offset, 6] = ((tempvalue & 0x10) == 0x40);
  398. di_Values[offset, 7] = ((tempvalue & 0x10) == 0x80);
  399. }
  400. public void NoteError(string errorData)
  401. {
  402. if (errorData != null)
  403. {
  404. EV.PostAlarmLog("Robot", $"Robot occurred error:{errorData}");
  405. Error = errorData;
  406. ParseErrorData(errorData);
  407. }
  408. else
  409. {
  410. Error = null;
  411. }
  412. }
  413. private void ParseErrorData(string errorData)
  414. {
  415. errorData = errorData.Remove(0, 1);
  416. ParseStatusData(errorData);
  417. ErrorCode = errorData.Remove(0, 4);
  418. var statusArray = errorData.ToArray();
  419. char E1 = statusArray[5];
  420. char E2 = statusArray[4];
  421. char XErr = statusArray[6];
  422. char YErr = statusArray[7];
  423. char ZErr = statusArray[8];
  424. char WErr = statusArray[9];
  425. //char RErr = statusArray[10];
  426. //char CErr = statusArray[11];
  427. if (E2 == '0' && E1 == '9')
  428. {
  429. if (XErr == '1')
  430. {
  431. XLowerSide = "true";
  432. }
  433. else if (XErr == '2')
  434. {
  435. XUpperSide = "true";
  436. }
  437. if (YErr == '1')
  438. {
  439. YLowerSide = "true";
  440. }
  441. else if (YErr == '2')
  442. {
  443. YUpperSide = "true";
  444. }
  445. if (ZErr == '1')
  446. {
  447. ZLowerSide = "true";
  448. }
  449. else if (ZErr == '2')
  450. {
  451. ZUpperSide = "true";
  452. }
  453. }
  454. else if (E2 == '1' && E1 == '0')
  455. {
  456. EmergencyStop = "true";
  457. }
  458. else if (E2 == '2' && E1 == '0')
  459. {
  460. }
  461. else if (E2 == '3' && E1 == '0')
  462. {
  463. AddressOutSide = "true";
  464. }
  465. else if (E2 == '3' && E1 == '1')
  466. {
  467. }
  468. else if (E2 == '4' && E1 == '0')
  469. {
  470. PositionOutSide = "true";
  471. }
  472. OnError($"ErrorCode:{ErrorCode}");
  473. }
  474. public override void OnError(string errortext)
  475. {
  476. if (RobotState != RobotStateEnum.Error)
  477. {
  478. _lstHandler.Clear();
  479. _connection.ForceClear();
  480. base.OnError(errortext);
  481. }
  482. }
  483. public void NoteRobotStatus(string statusData)
  484. {
  485. if (statusData != null)
  486. {
  487. RobotStatus = statusData;
  488. ParseStatusData(statusData);
  489. }
  490. else
  491. {
  492. Error = null;
  493. }
  494. }
  495. private void ParseStatusData(string statusData)
  496. {
  497. var statusArray = statusData.ToArray();
  498. int S1 = statusArray[3] - '0';
  499. int S2 = statusArray[2] - '0';
  500. int S3 = statusArray[1] - '0';
  501. int S4 = statusArray[0] - '0';
  502. IsOnLine = ((S1 & 0x1)==0x1);
  503. IsManual = ((S1 & 0x2) ==0x2);
  504. IsAuto = ((S1 & 0x4) == 0x4);
  505. IsStop = ((S2 & 1)==1);
  506. IsES = (S2 & 2)==2;
  507. IsZaxisSafeZone = (S3 & 1) == 1;
  508. MovingCompleted = (S3 & 2)==2;
  509. ACalCompleted = (S3 & 4)==4;
  510. ExecutionComplete = (S4 & 4) != 4;
  511. }
  512. internal void NoteRobotPositon(string positionData)
  513. {
  514. ReadRobotPositonData = positionData;
  515. ParseRobotPositon(positionData);
  516. }
  517. //"123.45-123.45 123.45 123.45-123.45 123.45 R 0 1 90 0" "-123.45-123.45 123.45 123.45-123.45 123.45 R 0 1 90 0"
  518. private void ParseRobotPositon(string statusData)
  519. {
  520. string tempStr = statusData.Replace("-", " -").Replace("R", " R").Replace("L", " L");
  521. string[] strList = tempStr.Split(' ');
  522. try
  523. {
  524. LOG.Write($"Start to parse {Axis} axis robot position data:{statusData}.");
  525. //var statusArray = statusData.ToArray();
  526. //int segmentStartIndex = 0;
  527. //for (int index = 0; index < statusArray.Length; index++)
  528. //{
  529. // if ((statusArray[index] == '-' || statusArray[index] == ' ') && index > 0)
  530. // {
  531. // strList.Add(statusData.Substring(segmentStartIndex, index - segmentStartIndex).Trim());
  532. // segmentStartIndex = index;
  533. // }
  534. //}
  535. //strList.Add(statusData.Substring(segmentStartIndex, statusArray.Length - segmentStartIndex).Trim());
  536. if (Axis == 6)
  537. {
  538. float tempvalue;
  539. if (float.TryParse(strList[0], out tempvalue))
  540. ReadXPosition = tempvalue;
  541. if (float.TryParse(strList[1], out tempvalue))
  542. ReadYPosition = tempvalue;
  543. if (float.TryParse(strList[2], out tempvalue))
  544. ReadZPosition = tempvalue;
  545. if (float.TryParse(strList[3], out tempvalue))
  546. ReadWPosition = tempvalue;
  547. if (float.TryParse(strList[4], out tempvalue))
  548. ReadRPosition = tempvalue;
  549. if (float.TryParse(strList[5], out tempvalue))
  550. ReadCPosition = tempvalue;
  551. RobotPosture = strList[6];
  552. MData = strList[8];
  553. FCode = strList[9];
  554. SCode = strList[10];
  555. }
  556. else
  557. {
  558. float tempvalue;
  559. if (float.TryParse(strList[0], out tempvalue))
  560. ReadXPosition = tempvalue;
  561. if (float.TryParse(strList[1], out tempvalue))
  562. ReadYPosition = tempvalue;
  563. if (float.TryParse(strList[2], out tempvalue))
  564. ReadZPosition = tempvalue;
  565. if (float.TryParse(strList[3], out tempvalue))
  566. ReadWPosition = tempvalue;
  567. RobotPosture = strList[4];
  568. MData = strList[6];
  569. FCode = strList[7];
  570. SCode = strList[8];
  571. }
  572. }
  573. catch(Exception ex)
  574. {
  575. LOG.Write(ex);
  576. LOG.Write($"Failed to parse {Axis} axis robot position data:{tempStr} with length:{strList.Length}.");
  577. }
  578. }
  579. public void ParseReadData(string cmd,string paraname,string data)
  580. {
  581. switch(cmd)
  582. {
  583. case "LE":
  584. CurrentReadExtParaNO = paraname;
  585. CurrentReadExtParaValue = data;
  586. if (paraname == "100") MapExistInfor = ParSlotMapData(data);
  587. if (paraname == "101") MapNGInfor = ParSlotMapData(data);
  588. int intParaNO;
  589. if (!int.TryParse(paraname, out intParaNO)) break;
  590. if(intParaNO >= 112 && intParaNO <=154)
  591. {
  592. if ((intParaNO - 112) % 5 == 0) ExtPara_MapWaferCount = data;
  593. }
  594. if (intParaNO >= 610 && intParaNO <= 654)
  595. {
  596. if ((intParaNO - 610) % 5 == 0) ExtPara_WaferThickness = data;
  597. if ((intParaNO - 611) % 5 == 0) ExtPara_PositionRange = data;
  598. if ((intParaNO - 612) % 5 == 0) ExtPara_Pitch = data;
  599. if ((intParaNO - 613) % 5 == 0) ExtPara_WaferMinThickness = data;
  600. if ((intParaNO - 614) % 5 == 0) ExtPara_Filter = data;
  601. }
  602. break;
  603. case "LR":
  604. if (paraname == null) ParseCurrentPostion(data);
  605. break;
  606. default:
  607. break;
  608. }
  609. }
  610. private void ParseCurrentPostion(string data)
  611. {
  612. List<string> strList = new List<string>();
  613. var statusArray = data.ToArray();
  614. int segmentStartIndex = 0;
  615. for (int index = 0; index < statusArray.Length; index++)
  616. {
  617. if ((statusArray[index] == '-' || statusArray[index] == ' ') && index > 0)
  618. {
  619. strList.Add(data.Substring(segmentStartIndex, index - segmentStartIndex).Trim());
  620. segmentStartIndex = index;
  621. }
  622. }
  623. strList.Add(data.Substring(segmentStartIndex, statusArray.Length - segmentStartIndex).Trim());
  624. if (Axis == 6)
  625. {
  626. if(strList.Count <6)
  627. {
  628. EV.PostAlarmLog("Robot", $"Received wrong position data:{data}");
  629. return;
  630. }
  631. float tempvalue;
  632. if (float.TryParse(strList[0], out tempvalue))
  633. PositionAxis1 = tempvalue;
  634. if (float.TryParse(strList[1], out tempvalue))
  635. PositionAxis2 = tempvalue;
  636. if (float.TryParse(strList[2], out tempvalue))
  637. PositionAxis3 = tempvalue;
  638. if (float.TryParse(strList[3], out tempvalue))
  639. PositionAxis4 = tempvalue;
  640. if (float.TryParse(strList[4], out tempvalue))
  641. PositionAxis5 = tempvalue;
  642. if (float.TryParse(strList[5], out tempvalue))
  643. PositionAxis6 = tempvalue;
  644. }
  645. else
  646. {
  647. if (strList.Count < 4)
  648. {
  649. EV.PostAlarmLog("Robot", $"Received wrong position data:{data}");
  650. return;
  651. }
  652. float tempvalue;
  653. if (float.TryParse(strList[0], out tempvalue))
  654. PositionAxis1 = tempvalue;
  655. if (float.TryParse(strList[1], out tempvalue))
  656. PositionAxis2 = tempvalue;
  657. if (float.TryParse(strList[2], out tempvalue))
  658. PositionAxis3 = tempvalue;
  659. if (float.TryParse(strList[3], out tempvalue))
  660. PositionAxis4 = tempvalue;
  661. }
  662. }
  663. private string ParSlotMapData(string data)
  664. {
  665. string ret = "";
  666. int ivalue;
  667. string svalut = data.Replace(Encoding.ASCII.GetString(new byte[] { 0x3}), "");
  668. if (!int.TryParse(svalut, out ivalue)) return "0000000000000000000000000";
  669. for(int i=0;i<25;i++)
  670. {
  671. if ((ivalue & (int)Math.Pow(2, i)) == (int)Math.Pow(2, i))
  672. ret = ret + "1";
  673. else
  674. ret = ret + "0";
  675. }
  676. return ret;
  677. }
  678. internal void NoteRobotConnected(bool connected)
  679. {
  680. Connected = connected;
  681. }
  682. internal void NoteRawCommandInfo(string command, string data)
  683. {
  684. var curIOResponse = IOResponseList.Find(res => res.SourceCommandName == command);
  685. if (curIOResponse != null)
  686. {
  687. IOResponseList.Remove(curIOResponse);
  688. }
  689. IOResponseList.Add(new IOResponse() { SourceCommandName = command, ResonseContent = data, ResonseRecievedTime = DateTime.Now });
  690. }
  691. #endregion
  692. public override bool SetWaferSize(RobotArmEnum arm, WaferSize size)
  693. {
  694. Size = size;
  695. if(arm == RobotArmEnum.Both)
  696. {
  697. if (WaferManager.Instance.CheckHasWafer(RobotModuleName, 0))
  698. WaferManager.Instance.UpdateWaferSize(RobotModuleName, 0, size);
  699. if (WaferManager.Instance.CheckHasWafer(RobotModuleName, 1))
  700. WaferManager.Instance.UpdateWaferSize(RobotModuleName, 1, size);
  701. return true;
  702. }
  703. if(arm == RobotArmEnum.Lower || arm== RobotArmEnum.Blade1)
  704. {
  705. if (WaferManager.Instance.CheckHasWafer(RobotModuleName, 0))
  706. WaferManager.Instance.UpdateWaferSize(RobotModuleName, 0, size);
  707. return true;
  708. }
  709. if(arm == RobotArmEnum.Upper || arm == RobotArmEnum.Blade2)
  710. {
  711. if (WaferManager.Instance.CheckHasWafer(RobotModuleName, 1))
  712. WaferManager.Instance.UpdateWaferSize(RobotModuleName, 1, size);
  713. return true;
  714. }
  715. return true;
  716. }
  717. protected override bool fStartTransferWafer(object[] param)
  718. {
  719. return true;
  720. }
  721. protected override bool fStartUnGrip(object[] param)
  722. {
  723. _dtActionStart = DateTime.Now;
  724. RobotArmEnum arm = (RobotArmEnum)param[0];
  725. if (arm == RobotArmEnum.Lower)
  726. {
  727. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, true));
  728. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, false));
  729. }
  730. if(arm == RobotArmEnum.Upper)
  731. {
  732. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, true));
  733. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, false));
  734. }
  735. if(arm == RobotArmEnum.Both)
  736. {
  737. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, true));
  738. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, false));
  739. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, true));
  740. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, false));
  741. }
  742. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  743. return true;
  744. }
  745. protected override bool fMonitorUnGrip(object[] param)
  746. {
  747. IsBusy = false;
  748. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  749. {
  750. OnError("UnGripTimeout.");
  751. return true;
  752. }
  753. if (_lstHandler.Count == 0 && (!_connection.IsBusy) && IsOnLine && ACalCompleted && MovingCompleted)
  754. {
  755. EV.PostInfoLog("Robot", $"{RobotModuleName} ungrip wafer successfully.");
  756. return true;
  757. }
  758. return false;
  759. }
  760. protected override bool fMonitorGrip(object[] param)
  761. {
  762. IsBusy = false;
  763. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  764. {
  765. OnError("GripTimeout.");
  766. return true;
  767. }
  768. if (_lstHandler.Count == 0 && (!_connection.IsBusy) && IsOnLine && ACalCompleted && MovingCompleted)
  769. {
  770. EV.PostInfoLog("Robot", $"{RobotModuleName} grip wafer successfully.");
  771. return true;
  772. }
  773. return false;
  774. }
  775. protected override bool fStartGrip(object[] param)
  776. {
  777. _dtActionStart = DateTime.Now;
  778. RobotArmEnum arm = (RobotArmEnum)param[0];
  779. if (arm == RobotArmEnum.Lower)
  780. {
  781. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, true));
  782. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, false));
  783. }
  784. if (arm == RobotArmEnum.Upper)
  785. {
  786. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, true));
  787. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, false));
  788. }
  789. if (arm == RobotArmEnum.Both)
  790. {
  791. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, true));
  792. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, false));
  793. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, true));
  794. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, false));
  795. }
  796. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  797. return true;
  798. }
  799. protected override bool fStartGoTo(object[] param)
  800. {
  801. _dtActionStart = DateTime.Now;
  802. try
  803. {
  804. if (param.Length == 1)
  805. {
  806. int address = Convert.ToInt16(param[0]);
  807. lock (_locker)
  808. {
  809. _lstHandler.AddLast(new HirataRobotIIMoveToPositionHandler(this, address));
  810. }
  811. }
  812. if (param.Length >= 8)
  813. {
  814. RobotArmEnum arm = (RobotArmEnum)param[0];
  815. string station = param[1].ToString();
  816. int slotindex = (int)param[2];
  817. RobotPostionEnum pos = (RobotPostionEnum)param[3];
  818. float xoffset = (float)param[4];
  819. float yoffset = (float)param[5];
  820. float zoffset = (float)param[6];
  821. float woffset = (float)param[7];
  822. ModuleName stationname = (ModuleName)Enum.Parse(typeof(ModuleName), station);
  823. WaferSize wz = WaferManager.Instance.GetWaferSize(stationname, slotindex);
  824. int address = GetBaseAddress(stationname, wz);
  825. float waferpitch = GetWaferPitch(stationname, wz);
  826. float insertdistance = GetInsertDistance(stationname, wz);
  827. float slowupdistance = GetSlowUpDistance(stationname, wz);
  828. zoffset += waferpitch * slotindex;
  829. if (arm == RobotArmEnum.Lower || arm == RobotArmEnum.Both)
  830. zoffset += _armPitch;
  831. if (pos == RobotPostionEnum.PickReady)
  832. {
  833. lock (_locker)
  834. {
  835. _lstHandler.AddLast(new HirataRobotIIMoveToPositionWithDeviationHandler(this, "A",
  836. address, xoffset, yoffset, zoffset, woffset));
  837. }
  838. }
  839. else if (pos == RobotPostionEnum.PlaceReady)
  840. {
  841. zoffset += slowupdistance;
  842. lock (_locker)
  843. {
  844. _lstHandler.AddLast(new HirataRobotIIMoveToPositionWithDeviationHandler(this, "A",
  845. address, xoffset, yoffset, zoffset, woffset));
  846. }
  847. }
  848. else
  849. {
  850. EV.PostAlarmLog("Robot", $"Robot doesn't support goto {pos} now.");
  851. return false;
  852. }
  853. }
  854. if (param.Length == 6|| param.Length == 7)
  855. {
  856. string motion = param[0].ToString();
  857. string address = param[1].ToString();
  858. float x, y, z, w;
  859. if (!float.TryParse(param[2].ToString(), out x)) return false;
  860. if (!float.TryParse(param[3].ToString(), out y)) return false;
  861. if (!float.TryParse(param[4].ToString(), out z)) return false;
  862. if (!float.TryParse(param[5].ToString(), out w)) return false;
  863. if (param.Length >= 7 && param[6].ToString() == "-")
  864. {
  865. x = x * -1;
  866. y = y * -1;
  867. z = z * -1;
  868. w = w * -1;
  869. }
  870. lock (_locker)
  871. {
  872. if (address == "*")
  873. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, motion, x, y, z, w));
  874. else
  875. {
  876. int intaddress;
  877. if (!int.TryParse(address, out intaddress)) return false;
  878. _lstHandler.AddLast(new HirataRobotIIMoveToPositionWithDeviationHandler(this, motion, intaddress, x, y, z, w));
  879. }
  880. ReadCurrentPostion();
  881. }
  882. }
  883. }
  884. catch(Exception ex)
  885. {
  886. EV.PostAlarmLog("Robot","Robot Goto execution failed.");
  887. LOG.Write(ex);
  888. return false;
  889. }
  890. return true;
  891. }
  892. protected override bool fMonitorGoTo(object[] param)
  893. {
  894. IsBusy = false;
  895. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  896. {
  897. OnError("GripTimeout.");
  898. return true;
  899. }
  900. if (_lstHandler.Count == 0 && (!_connection.IsBusy) && IsOnLine && ACalCompleted && MovingCompleted)
  901. {
  902. EV.PostInfoLog("Robot", $"{RobotModuleName} grip wafer successfully.");
  903. return true;
  904. }
  905. return false;
  906. }
  907. private int _currentMapThicknessIndex;
  908. protected override bool fStartMapWafer(object[] param)
  909. {
  910. ResetRoutine();
  911. _dtActionStart = DateTime.Now;
  912. if (param.Length > 1)
  913. _currentMapThicknessIndex = Convert.ToInt32(param[1]);
  914. ModuleName module = (ModuleName)param[0];
  915. int[] mappingaddress = GetMappingAddress(module);
  916. if (mappingaddress == null || mappingaddress.Length != 6)
  917. {
  918. EV.PostAlarmLog("Robot", $"{RobotModuleName} map error: Invalid mapping setting.");
  919. return false;
  920. }
  921. Blade1Target = module;
  922. Blade2Target = module;
  923. CmdTarget = module;
  924. MoveInfo = new RobotMoveInfo()
  925. {
  926. Action = RobotAction.Moving,
  927. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  928. BladeTarget = BuildBladeTarget(),
  929. };
  930. if (ModuleHelper.IsLoadPort(CmdTarget))
  931. {
  932. var lp = DEVICE.GetDevice<LoadPortBaseDevice>(CmdTarget.ToString());
  933. if (lp != null)
  934. lp.NoteTransferStart();
  935. }
  936. return true;
  937. }
  938. protected override bool fMonitorMap(object[] param)
  939. {
  940. IsBusy = false;
  941. int[] mappingaddress = GetMappingAddress(CmdTarget);
  942. try
  943. {
  944. MoveWalkingAxis((int)RobotStepEnum.Step1,m_WalkingAxis.TimeLimitMove, CmdTarget, Notify, Stop);
  945. ReadRobotPosition((int)RobotStepEnum.Step2, m_WalkingAxis.TimeLimitMove, mappingaddress[1], Notify, Stop);
  946. MapWaferAndWaitComplete((int)RobotStepEnum.Step3, RobotCommandTimeout,CmdTarget, Notify, Stop);
  947. }
  948. catch (RoutineBreakException)
  949. {
  950. return false;
  951. }
  952. catch (RoutineFaildException)
  953. {
  954. OnError("Mapping failed.");
  955. return true;
  956. }
  957. char[] exists = MapExistInfor.ToArray();
  958. char[] ngs = MapNGInfor.ToArray();
  959. for (int i = 0; i < 25; i++)
  960. {
  961. if (ngs[i] != '0') exists[i] = '2';
  962. }
  963. NotifySlotMapResult(CmdTarget, new string(exists));
  964. Blade1Target = ModuleName.System;
  965. Blade2Target = ModuleName.System;
  966. if (ModuleHelper.IsLoadPort(CmdTarget))
  967. {
  968. var lp = DEVICE.GetDevice<LoadPortBaseDevice>(CmdTarget.ToString());
  969. if (lp != null)
  970. lp.NoteTransferStop();
  971. }
  972. CmdTarget = ModuleName.System;
  973. MoveInfo = new RobotMoveInfo()
  974. {
  975. Action = RobotAction.Moving,
  976. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  977. BladeTarget = BuildBladeTarget(),
  978. };
  979. return true;
  980. }
  981. protected override bool fStartSwapWafer(object[] param)
  982. {
  983. _dtActionStart = DateTime.Now;
  984. if (param.Length < 3) return false;
  985. RobotArmEnum arm = (RobotArmEnum)param[0];
  986. ModuleName station = (ModuleName)Enum.Parse(typeof(ModuleName), (string)param[1]);
  987. int slot = (int)param[2];
  988. _currentMotionStation = station;
  989. _currentMotionSlot = slot;
  990. _currentMotionArm = arm;
  991. float xoffset = 0;
  992. float yoffset = 0;
  993. float zoffset = 0;
  994. float woffset = 0;
  995. if (param.Length >= 7)
  996. {
  997. xoffset = (float)param[3];
  998. yoffset = (float)param[4];
  999. zoffset = (float)param[5];
  1000. woffset = (float)param[6];
  1001. }
  1002. WaferSize wafersize = WaferManager.Instance.GetWaferSize(station,slot);
  1003. int address = GetBaseAddress(station, wafersize);
  1004. zoffset = zoffset + (slot) * GetWaferPitch(station, wafersize);
  1005. float insertdistance = GetInsertDistance(station, wafersize);
  1006. float slowupdistance = GetSlowUpDistance(station, wafersize);
  1007. lock (_locker)
  1008. {
  1009. if (arm == RobotArmEnum.Lower)
  1010. {
  1011. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1012. _lstHandler.AddLast(new HirataRobotIIMoveToPositionWithDeviationHandler(this, "A", address, xoffset, yoffset, zoffset + _armPitch, woffset));
  1013. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1014. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", insertdistance, 0, 0, 0));
  1015. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1016. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "U", 0, 0, slowupdistance, 0));
  1017. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1018. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, true));
  1019. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, false));
  1020. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1021. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", (-1) * insertdistance, insertdistance, 0, 0));
  1022. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1023. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, true));
  1024. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, false));
  1025. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1026. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "U", 0, 0, -(slowupdistance+ _armPitch), 0));
  1027. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1028. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", 0, -insertdistance, 0, 0));
  1029. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1030. _lstHandler.AddLast(new HirataRobotIIReadDIByteHandler(this, 0));
  1031. //WaferManager.Instance.WaferMoved( station, slot, RobotModuleName, 0);
  1032. //WaferManager.Instance.WaferMoved(RobotModuleName, 1,station, slot);
  1033. }
  1034. if (arm == RobotArmEnum.Upper)
  1035. {
  1036. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1037. _lstHandler.AddLast(new HirataRobotIIMoveToPositionWithDeviationHandler(this, "A", address, xoffset, yoffset, zoffset, woffset));
  1038. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1039. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", 0, insertdistance, 0, 0));
  1040. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1041. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "U", 0, 0, slowupdistance+_armPitch, 0));
  1042. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1043. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, true));
  1044. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, false));
  1045. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1046. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", insertdistance, -insertdistance, 0, 0));
  1047. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1048. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, true));
  1049. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, false));
  1050. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1051. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "U", 0, 0, -slowupdistance, 0));
  1052. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1053. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", -insertdistance,0, 0, 0));
  1054. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1055. _lstHandler.AddLast(new HirataRobotIIReadDIByteHandler(this, 0));
  1056. //WaferManager.Instance.WaferMoved(station, slot, RobotModuleName, 1);
  1057. //WaferManager.Instance.WaferMoved(RobotModuleName, 0, station, slot);
  1058. }
  1059. ReadCurrentPostion();
  1060. Blade1Target = station;
  1061. Blade2Target = station;
  1062. CmdTarget = station;
  1063. MoveInfo = new RobotMoveInfo()
  1064. {
  1065. Action = RobotAction.Moving,
  1066. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  1067. BladeTarget = BuildBladeTarget(),
  1068. };
  1069. return true;
  1070. }
  1071. }
  1072. protected override bool fMonitorSwap(object[] param)
  1073. {
  1074. IsBusy = false;
  1075. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  1076. {
  1077. OnError("SwapTimeout.");
  1078. return true;
  1079. }
  1080. if (_lstHandler.Count == 0 && (!_connection.IsBusy) && IsOnLine && ACalCompleted && MovingCompleted)
  1081. {
  1082. RobotArmEnum otherarm = GetAnotherArm(_currentMotionArm);
  1083. if ((GetWaferState(_currentMotionArm) == RobotArmWaferStateEnum.Present && GetWaferState(otherarm) == RobotArmWaferStateEnum.Absent)
  1084. || isSimulatorMode)
  1085. {
  1086. WaferManager.Instance.WaferMoved(_currentMotionStation, _currentMotionSlot,
  1087. RobotModuleName, (int)_currentMotionArm);
  1088. WaferManager.Instance.WaferMoved(RobotModuleName, (int)otherarm, _currentMotionStation, _currentMotionSlot);
  1089. }
  1090. else
  1091. {
  1092. lock(_locker)
  1093. {
  1094. _lstHandler.AddLast(new HirataRobotIIReadDIByteHandler(this, 0));
  1095. EV.PostWarningLog("Robot",$"{RobotModuleName} detect wafer absent on {_currentMotionArm}");
  1096. }
  1097. return false;
  1098. }
  1099. EV.PostInfoLog("Robot", $"{RobotModuleName} swap wafer successfully.");
  1100. Blade1Target = ModuleName.System;
  1101. Blade2Target = ModuleName.System;
  1102. CmdTarget = ModuleName.System;
  1103. MoveInfo = new RobotMoveInfo()
  1104. {
  1105. Action = RobotAction.Moving,
  1106. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  1107. BladeTarget = BuildBladeTarget(),
  1108. };
  1109. return true;
  1110. }
  1111. return false;
  1112. }
  1113. protected override bool fStartPlaceWafer(object[] param)
  1114. {
  1115. _dtActionStart = DateTime.Now;
  1116. if (param.Length < 3) return false;
  1117. RobotArmEnum arm = (RobotArmEnum)param[0];
  1118. ModuleName station = (ModuleName)Enum.Parse(typeof(ModuleName), (string)param[1]); ;
  1119. int slot = (int)param[2];
  1120. _currentMotionStation = station;
  1121. _currentMotionSlot = slot;
  1122. _currentMotionArm = arm;
  1123. float xoffset = 0;
  1124. float yoffset = 0;
  1125. float zoffset = 0;
  1126. float woffset = 0;
  1127. if (param.Length >= 7)
  1128. {
  1129. xoffset = (float)param[3];
  1130. yoffset = (float)param[4];
  1131. zoffset = (float)param[5];
  1132. woffset = (float)param[6];
  1133. }
  1134. WaferSize wafersize = WaferManager.Instance.GetWaferSize(RobotModuleName,
  1135. (int)(arm == RobotArmEnum.Both ? RobotArmEnum.Lower : arm));
  1136. float insertdistance = GetInsertDistance(station, wafersize);
  1137. float slowupdistance = GetSlowUpDistance(station, wafersize);
  1138. int address = GetBaseAddress(station, wafersize);
  1139. zoffset = zoffset + (slot) * GetWaferPitch(station, wafersize) + slowupdistance;
  1140. lock (_locker)
  1141. {
  1142. if (arm == RobotArmEnum.Lower)
  1143. {
  1144. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1145. _lstHandler.AddLast(new HirataRobotIIMoveToPositionWithDeviationHandler(this, "A", address, xoffset, yoffset, zoffset+ _armPitch, woffset));
  1146. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1147. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", insertdistance, 0, 0, 0));
  1148. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1149. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, true));
  1150. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, false));
  1151. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1152. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "U", 0, 0, (-1)*slowupdistance, 0));
  1153. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1154. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, true));
  1155. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, false));
  1156. _lstHandler.AddLast(new HirataRobotIIWaferInforMoveHandler(this, 0, false));
  1157. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, true));
  1158. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, false));
  1159. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", (-1) * insertdistance, 0, 0, 0));
  1160. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1161. _lstHandler.AddLast(new HirataRobotIIReadDIByteHandler(this, 0));
  1162. //WaferManager.Instance.WaferMoved(RobotModuleName, 0, station, slot);
  1163. }
  1164. if (arm == RobotArmEnum.Upper)
  1165. {
  1166. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1167. _lstHandler.AddLast(new HirataRobotIIMoveToPositionWithDeviationHandler(this, "A", address, xoffset, yoffset, zoffset, woffset));
  1168. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1169. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", 0, insertdistance, 0, 0));
  1170. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1171. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, true));
  1172. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, false));
  1173. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1174. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "U", 0, 0, (-1)*slowupdistance, 0));
  1175. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1176. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, true));
  1177. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, false));
  1178. _lstHandler.AddLast(new HirataRobotIIWaferInforMoveHandler(this, 0, false));
  1179. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, true));
  1180. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, false));
  1181. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", 0, (-1) * insertdistance, 0, 0));
  1182. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1183. _lstHandler.AddLast(new HirataRobotIIReadDIByteHandler(this, 0));
  1184. //WaferManager.Instance.WaferMoved(RobotModuleName, 1, station, slot);
  1185. }
  1186. if (arm == RobotArmEnum.Both)
  1187. {
  1188. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1189. _lstHandler.AddLast(new HirataRobotIIMoveToPositionWithDeviationHandler(this, "A", address, xoffset, yoffset, zoffset + _armPitch, woffset));
  1190. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1191. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", insertdistance, insertdistance, 0, 0));
  1192. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1193. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, true));
  1194. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, false));
  1195. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, true));
  1196. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, false));
  1197. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1198. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "U", 0, 0, (-1)*slowupdistance, 0));
  1199. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1200. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, true));
  1201. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, false));
  1202. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, true));
  1203. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, false));
  1204. _lstHandler.AddLast(new HirataRobotIIWaferInforMoveHandler(this, 0, false));
  1205. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, true));
  1206. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, false));
  1207. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, true));
  1208. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, false));
  1209. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", (-1) * insertdistance, (-1) * insertdistance, 0, 0));
  1210. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1211. _lstHandler.AddLast(new HirataRobotIIReadDIByteHandler(this, 0));
  1212. //WaferManager.Instance.WaferMoved(RobotModuleName, 0, station, slot);
  1213. //WaferManager.Instance.WaferMoved(RobotModuleName, 1, station, slot+1);
  1214. }
  1215. ReadCurrentPostion();
  1216. Blade1Target = station;
  1217. Blade2Target = station;
  1218. CmdTarget = station;
  1219. MoveInfo = new RobotMoveInfo()
  1220. {
  1221. Action = RobotAction.Moving,
  1222. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  1223. BladeTarget = BuildBladeTarget(),
  1224. };
  1225. if (ModuleHelper.IsLoadPort(CmdTarget))
  1226. {
  1227. var lp = DEVICE.GetDevice<LoadPortBaseDevice>(CmdTarget.ToString());
  1228. if (lp != null)
  1229. lp.NoteTransferStart();
  1230. }
  1231. return true;
  1232. }
  1233. }
  1234. protected override bool fMonitorPlace(object[] param)
  1235. {
  1236. IsBusy = false;
  1237. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  1238. {
  1239. OnError("PlaceTimeout.");
  1240. return true;
  1241. }
  1242. if (_lstHandler.Count == 0 && (!_connection.IsBusy) && IsOnLine && ACalCompleted && MovingCompleted)
  1243. {
  1244. if (GetWaferState(_currentMotionArm) == RobotArmWaferStateEnum.Absent || isSimulatorMode)
  1245. {
  1246. }
  1247. else
  1248. {
  1249. lock(_locker)
  1250. _lstHandler.AddLast(new HirataRobotIIReadDIByteHandler(this, 0));
  1251. EV.PostWarningLog("Robot",$"Detect wafer present on {_currentMotionArm}");
  1252. return false;
  1253. }
  1254. EV.PostInfoLog("Robot", $"{RobotModuleName} place wafer to {CmdTarget} successfully.");
  1255. Blade1Target = ModuleName.System;
  1256. Blade2Target = ModuleName.System;
  1257. if (ModuleHelper.IsLoadPort(CmdTarget))
  1258. {
  1259. var lp = DEVICE.GetDevice<LoadPortBaseDevice>(CmdTarget.ToString());
  1260. if (lp != null)
  1261. lp.NoteTransferStop();
  1262. }
  1263. CmdTarget = ModuleName.System;
  1264. MoveInfo = new RobotMoveInfo()
  1265. {
  1266. Action = RobotAction.Moving,
  1267. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  1268. BladeTarget = BuildBladeTarget(),
  1269. };
  1270. return true;
  1271. }
  1272. return false;
  1273. }
  1274. public void MoveWaferInforToStation()
  1275. {
  1276. if (GetWaferState(_currentMotionArm) == RobotArmWaferStateEnum.Absent || isSimulatorMode)
  1277. {
  1278. if (_currentMotionArm == RobotArmEnum.Lower || _currentMotionArm == RobotArmEnum.Upper)
  1279. WaferManager.Instance.WaferMoved(RobotModuleName, (int)_currentMotionArm,
  1280. _currentMotionStation, _currentMotionSlot);
  1281. else
  1282. {
  1283. WaferManager.Instance.WaferMoved(RobotModuleName, 0,
  1284. _currentMotionStation, _currentMotionSlot);
  1285. WaferManager.Instance.WaferMoved(RobotModuleName, 1,
  1286. _currentMotionStation, _currentMotionSlot + 1);
  1287. }
  1288. }
  1289. else
  1290. {
  1291. _lstHandler.Clear();
  1292. OnError($"Detect wafer present on {_currentMotionArm}");
  1293. }
  1294. }
  1295. public void MoveWaferInforFromStation()
  1296. {
  1297. if (GetWaferState(_currentMotionArm) == RobotArmWaferStateEnum.Present || isSimulatorMode)
  1298. {
  1299. if (_currentMotionArm == RobotArmEnum.Lower || _currentMotionArm == RobotArmEnum.Upper)
  1300. WaferManager.Instance.WaferMoved(_currentMotionStation, _currentMotionSlot,
  1301. RobotModuleName, (int)_currentMotionArm);
  1302. else
  1303. {
  1304. WaferManager.Instance.WaferMoved(_currentMotionStation, _currentMotionSlot,
  1305. RobotModuleName, 0);
  1306. WaferManager.Instance.WaferMoved(_currentMotionStation, _currentMotionSlot + 1,
  1307. RobotModuleName, 1);
  1308. }
  1309. }
  1310. else
  1311. {
  1312. _lstHandler.Clear();
  1313. OnError($"Detect wafer absent on {_currentMotionArm}");
  1314. }
  1315. }
  1316. protected override bool fStartPickWafer(object[] param)
  1317. {
  1318. _dtActionStart = DateTime.Now;
  1319. if (param.Length < 3) return false;
  1320. RobotArmEnum arm = (RobotArmEnum)param[0];
  1321. ModuleName station = (ModuleName)Enum.Parse(typeof(ModuleName), (string)param[1]); ;
  1322. int slot = (int)param[2];
  1323. float xoffset = 0;
  1324. float yoffset = 0;
  1325. float zoffset = 0;
  1326. float woffset = 0;
  1327. if(param.Length >=7)
  1328. {
  1329. xoffset = (float)param[3];
  1330. yoffset = (float)param[4];
  1331. zoffset = (float)param[5];
  1332. woffset = (float)param[6];
  1333. }
  1334. WaferSize wafersize = WaferManager.Instance.GetWaferSize(station, slot);
  1335. int address = GetBaseAddress(station, wafersize);
  1336. zoffset = zoffset + slot * GetWaferPitch(station, wafersize);
  1337. float insertdistance = GetInsertDistance(station, wafersize);
  1338. float slowupdistance = GetSlowUpDistance(station, wafersize);
  1339. _currentMotionArm = arm;
  1340. _currentMotionStation = station;
  1341. _currentMotionSlot = slot;
  1342. lock (_locker)
  1343. {
  1344. if(arm == RobotArmEnum.Lower)
  1345. {
  1346. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1347. _lstHandler.AddLast(new HirataRobotIIMoveToPositionWithDeviationHandler(this, "A", address, xoffset, yoffset, zoffset + _armPitch, woffset));
  1348. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1349. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", insertdistance, 0, 0, 0));
  1350. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1351. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, true));
  1352. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1353. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, false));
  1354. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1355. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "U", 0, 0, slowupdistance, 0));
  1356. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1357. _lstHandler.AddLast(new HirataRobotIIWaferInforMoveHandler(this, 0, true));
  1358. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", (-1)*insertdistance, 0, 0, 0));
  1359. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1360. _lstHandler.AddLast(new HirataRobotIIReadDIByteHandler(this, 0));
  1361. //WaferManager.Instance.WaferMoved(station, slot, RobotModuleName, 0);
  1362. }
  1363. if(arm == RobotArmEnum.Upper)
  1364. {
  1365. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1366. _lstHandler.AddLast(new HirataRobotIIMoveToPositionWithDeviationHandler(this, "A", address, xoffset, yoffset, zoffset, woffset));
  1367. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1368. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", 0, insertdistance, 0, 0));
  1369. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1370. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, true));
  1371. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1372. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, false));
  1373. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "U", 0, 0, slowupdistance, 0));
  1374. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1375. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1376. _lstHandler.AddLast(new HirataRobotIIWaferInforMoveHandler(this, 0, true));
  1377. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", 0, (-1) * insertdistance, 0, 0));
  1378. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1379. _lstHandler.AddLast(new HirataRobotIIReadDIByteHandler(this, 0));
  1380. //WaferManager.Instance.WaferMoved(station, slot, RobotModuleName, 1);
  1381. }
  1382. if (arm == RobotArmEnum.Both)
  1383. {
  1384. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1385. _lstHandler.AddLast(new HirataRobotIIMoveToPositionWithDeviationHandler(this, "A", address, xoffset, yoffset, zoffset + _armPitch, woffset));
  1386. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1387. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", insertdistance, insertdistance, 0, 0));
  1388. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1389. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, true));
  1390. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, false));
  1391. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, true));
  1392. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, false));
  1393. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "U", 0, 0, slowupdistance, 0));
  1394. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1395. _lstHandler.AddLast(new HirataRobotIIWaferInforMoveHandler(this, 0, true));
  1396. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1397. _lstHandler.AddLast(new HirataRobotIIMoveDeviationHandler(this, "I", (-1) * insertdistance, (-1) * insertdistance, 0, 0));
  1398. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1399. _lstHandler.AddLast(new HirataRobotIIReadDIByteHandler(this, 0));
  1400. }
  1401. ReadCurrentPostion();
  1402. Blade1Target = station;
  1403. Blade2Target = station;
  1404. CmdTarget = station;
  1405. MoveInfo = new RobotMoveInfo()
  1406. {
  1407. Action = RobotAction.Moving,
  1408. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  1409. BladeTarget = BuildBladeTarget(),
  1410. };
  1411. if (ModuleHelper.IsLoadPort(station))
  1412. {
  1413. var lp = DEVICE.GetDevice<LoadPortBaseDevice>(station.ToString());
  1414. if (lp != null)
  1415. lp.NoteTransferStart();
  1416. }
  1417. return true;
  1418. }
  1419. }
  1420. protected override bool fMonitorPick(object[] param)
  1421. {
  1422. IsBusy = false;
  1423. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  1424. {
  1425. OnError("PickTimeout.");
  1426. return true;
  1427. }
  1428. if (_lstHandler.Count == 0 && (!_connection.IsBusy) && IsOnLine && ACalCompleted && MovingCompleted)
  1429. {
  1430. if (GetWaferState(_currentMotionArm) == RobotArmWaferStateEnum.Present || isSimulatorMode)
  1431. {
  1432. }
  1433. else
  1434. {
  1435. lock(_locker)
  1436. _lstHandler.AddLast(new HirataRobotIIReadDIByteHandler(this, 0));
  1437. EV.PostWarningLog("Robot",$"Detect wafer absent on {_currentMotionArm}");
  1438. return true;
  1439. }
  1440. EV.PostInfoLog("Robot", $"{RobotModuleName} pick wafer from {CmdTarget} successfully.");
  1441. Blade1Target = ModuleName.System;
  1442. Blade2Target = ModuleName.System;
  1443. if (ModuleHelper.IsLoadPort(CmdTarget))
  1444. {
  1445. var lp = DEVICE.GetDevice<LoadPortBaseDevice>(CmdTarget.ToString());
  1446. if (lp != null)
  1447. lp.NoteTransferStop();
  1448. }
  1449. CmdTarget = ModuleName.System;
  1450. MoveInfo = new RobotMoveInfo()
  1451. {
  1452. Action = RobotAction.Moving,
  1453. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  1454. BladeTarget = BuildBladeTarget(),
  1455. };
  1456. return true;
  1457. }
  1458. return false;
  1459. }
  1460. protected override bool fResetToReady(object[] param)
  1461. {
  1462. return true;
  1463. }
  1464. protected override bool fReset(object[] param)
  1465. {
  1466. _dtActionStart = DateTime.Now;
  1467. _connection.SetCommunicationError(false, "");
  1468. if (!_connection.IsConnected)
  1469. {
  1470. _connection.Disconnect();
  1471. _connection.Connect();
  1472. }
  1473. lock (_locker)
  1474. {
  1475. _lstHandler.AddLast(new HirataRobotIISimpleActionHandler(this, "GE"));
  1476. _lstHandler.AddLast(new HirataRobotIIRawCommandHandler(this, "CL"));
  1477. }
  1478. return true;// throw new NotImplementedException();
  1479. }
  1480. private DateTime _dtActionStart;
  1481. protected override bool fMonitorReset(object[] param)
  1482. {
  1483. IsBusy = false;
  1484. if(DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  1485. {
  1486. OnError("ResetTimeout.");
  1487. return true;
  1488. }
  1489. return _lstHandler.Count==0 && !_connection.IsBusy;
  1490. }
  1491. protected override bool fStartInit(object[] param)
  1492. {
  1493. ResetRoutine();
  1494. _dtActionStart = DateTime.Now;
  1495. return true;
  1496. }
  1497. protected override bool fMonitorInit(object[] param)
  1498. {
  1499. IsBusy = false;
  1500. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  1501. {
  1502. OnError("InitTimeout.");
  1503. return true;
  1504. }
  1505. try
  1506. {
  1507. OpenVacuum((int)RobotStepEnum.Step1, RobotCommandTimeout, Notify, Stop);
  1508. TimeDelay((int)RobotStepEnum.Step2, 0.5);
  1509. IdentifyWaferStatus((int)RobotStepEnum.Step3, RobotCommandTimeout, Notify, Stop);
  1510. HomeRobotArms((int)RobotStepEnum.Step4, RobotCommandTimeout, Notify, Stop);
  1511. }
  1512. catch (RoutineBreakException)
  1513. {
  1514. return false;
  1515. }
  1516. catch (RoutineFaildException)
  1517. {
  1518. OnError("Mapping failed.");
  1519. return true;
  1520. }
  1521. Blade1Target = ModuleName.System;
  1522. Blade2Target = ModuleName.System;
  1523. CmdTarget = ModuleName.System;
  1524. MoveInfo = new RobotMoveInfo()
  1525. {
  1526. Action = RobotAction.Moving,
  1527. ArmTarget = CmdRobotArm == RobotArmEnum.Lower ? RobotArm.ArmA : RobotArm.ArmB,
  1528. BladeTarget = BuildBladeTarget(),
  1529. };
  1530. EV.PostInfoLog("Robot", $"{RobotModuleName} homing completed successfully.");
  1531. return true;
  1532. }
  1533. protected override bool fStartExtendForPick(object[] param)
  1534. {
  1535. return true;
  1536. }
  1537. protected override bool fStartExtendForPlace(object[] param)
  1538. {
  1539. return true;
  1540. }
  1541. protected override bool fStartRetractFromPick(object[] param)
  1542. {
  1543. return true;
  1544. }
  1545. protected override bool fStartRetractFromPlace(object[] param)
  1546. {
  1547. return true;
  1548. }
  1549. protected override bool fClear(object[] param)
  1550. {
  1551. return true; ;
  1552. }
  1553. protected override bool fError(object[] param)
  1554. {
  1555. return true;
  1556. }
  1557. private void ReadCurrentPostion()
  1558. {
  1559. _lstHandler.AddLast(new HirataRobotIIReadHandler(this, "LR", null));
  1560. }
  1561. protected virtual float GetWaferPitch(ModuleName module, WaferSize wz)
  1562. {
  1563. if (ModuleHelper.IsLoadPort(module))
  1564. {
  1565. var lp = DEVICE.GetDevice<LoadPortBaseDevice>(module.ToString());
  1566. int carrierindex = lp.InfoPadCarrierIndex;
  1567. return SC.ContainsItem($"CarrierInfo.CarrierWaferPitch{carrierindex}") ?
  1568. (float)SC.GetValue<int>($"CarrierInfo.CarrierWaferPitch{carrierindex}") / 100 : 20;
  1569. }
  1570. if (wz == WaferSize.WS12)
  1571. return (float)SC.GetValue<int>($"CarrierInfo.{module}CarrierWaferPitch12") / 100;
  1572. if (wz == WaferSize.WS8)
  1573. return (float)SC.GetValue<int>($"CarrierInfo.{module.ToString()}CarrierWaferPitch8") / 100;
  1574. if (wz == WaferSize.WS6)
  1575. return (float)SC.GetValue<int>($"CarrierInfo.{module.ToString()}CarrierWaferPitch6") / 100;
  1576. if (wz == WaferSize.WS4)
  1577. return (float)SC.GetValue<int>($"CarrierInfo.{module.ToString()}CarrierWaferPitch4") / 100;
  1578. if (wz == WaferSize.WS3)
  1579. return (float)SC.GetValue<int>($"CarrierInfo.{module.ToString()}CarrierWaferPitch3") / 100;
  1580. return 20;
  1581. }
  1582. protected virtual float GetInsertDistance(ModuleName module, WaferSize wz)
  1583. {
  1584. if (ModuleHelper.IsLoadPort(module))
  1585. {
  1586. var lp = DEVICE.GetDevice<LoadPortBaseDevice>(module.ToString());
  1587. int carrierindex = lp.InfoPadCarrierIndex;
  1588. return SC.ContainsItem($"CarrierInfo.InsertDistance{carrierindex}") ?
  1589. (float)SC.GetValue<int>($"CarrierInfo.InsertDistance{carrierindex}") / 100 : 300;
  1590. }
  1591. if (wz == WaferSize.WS12)
  1592. return (float)SC.GetValue<int>($"CarrierInfo.{module}InsertDistance12") / 100;
  1593. if (wz == WaferSize.WS8)
  1594. return (float)SC.GetValue<int>($"CarrierInfo.{module}InsertDistance8") / 100;
  1595. if (wz == WaferSize.WS6)
  1596. return (float)SC.GetValue<int>($"CarrierInfo.{module}InsertDistance6") / 100;
  1597. if (wz == WaferSize.WS4)
  1598. return (float)SC.GetValue<int>($"CarrierInfo.{module}InsertDistance4") / 100;
  1599. if (wz == WaferSize.WS3)
  1600. return (float)SC.GetValue<int>($"CarrierInfo.{module}InsertDistance3") / 100;
  1601. return (float)SC.GetValue<int>($"CarrierInfo.{module}InsertDistance0") / 100;
  1602. }
  1603. protected virtual float GetSlowUpDistance(ModuleName module, WaferSize wz)
  1604. {
  1605. if (ModuleHelper.IsLoadPort(module))
  1606. {
  1607. var lp = DEVICE.GetDevice<LoadPortBaseDevice>(module.ToString());
  1608. int carrierindex = lp.InfoPadCarrierIndex;
  1609. return SC.ContainsItem($"CarrierInfo.SlowUpDistance{carrierindex}") ?
  1610. (float)SC.GetValue<int>($"CarrierInfo.SlowUpDistance{carrierindex}") / 100 : 10;
  1611. }
  1612. if (wz == WaferSize.WS12)
  1613. return (float)SC.GetValue<int>($"CarrierInfo.{module}SlowUpDistance12") / 100;
  1614. if (wz == WaferSize.WS8)
  1615. return (float)SC.GetValue<int>($"CarrierInfo.{module}SlowUpDistance8") / 100;
  1616. if (wz == WaferSize.WS6)
  1617. return (float)SC.GetValue<int>($"CarrierInfo.{module}SlowUpDistance6") / 100;
  1618. if (wz == WaferSize.WS4)
  1619. return (float)SC.GetValue<int>($"CarrierInfo.{module}SlowUpDistance4") / 100;
  1620. if (wz == WaferSize.WS3)
  1621. return (float)SC.GetValue<int>($"CarrierInfo.{module}SlowUpDistance3") / 100;
  1622. return (float)SC.GetValue<int>($"CarrierInfo.{module}SlowUpDistance0") / 100;
  1623. }
  1624. protected virtual int GetBaseAddress(ModuleName module, WaferSize wz)
  1625. {
  1626. if (ModuleHelper.IsLoadPort(module))
  1627. {
  1628. var lp = DEVICE.GetDevice<LoadPortBaseDevice>(module.ToString());
  1629. int carrierindex = lp.InfoPadCarrierIndex;
  1630. return SC.ContainsItem($"CarrierInfo.{module}Station{carrierindex}") ?
  1631. Convert.ToInt16(SC.GetStringValue($"CarrierInfo.{module}Station{carrierindex}")) : 0;
  1632. }
  1633. if (wz == WaferSize.WS12)
  1634. return Convert.ToInt16(SC.GetStringValue($"CarrierInfo.{module}Station12"));
  1635. if (wz == WaferSize.WS8)
  1636. return Convert.ToInt16(SC.GetStringValue($"CarrierInfo.{module}Station8"));
  1637. if (wz == WaferSize.WS6)
  1638. return Convert.ToInt16(SC.GetStringValue($"CarrierInfo.{module}Station6"));
  1639. if (wz == WaferSize.WS4)
  1640. return Convert.ToInt16(SC.GetStringValue($"CarrierInfo.{module}Station4"));
  1641. if (wz == WaferSize.WS3)
  1642. return Convert.ToInt16(SC.GetStringValue($"CarrierInfo.{module}Station3"));
  1643. return Convert.ToInt16(SC.GetStringValue($"CarrierInfo.{module}Station0"));
  1644. }
  1645. protected virtual int[] GetMappingAddress(ModuleName module)
  1646. {
  1647. var lp = DEVICE.GetDevice<LoadPortBaseDevice>(module.ToString());
  1648. int carrierindex = lp.InfoPadCarrierIndex;
  1649. string addresses = SC.ContainsItem($"CarrierInfo.{module}MappingStation{carrierindex}") ?
  1650. SC.GetStringValue($"CarrierInfo.{module}MappingStation{carrierindex}") : "";
  1651. List<int> ret = new List<int>();
  1652. if (addresses.Split(',').Length != 6)
  1653. {
  1654. EV.PostAlarmLog("Robot", "Mapping address setting error!");
  1655. return null;
  1656. }
  1657. foreach (var add in addresses.Split(','))
  1658. {
  1659. int nValue;
  1660. if (!int.TryParse(add, out nValue))
  1661. {
  1662. EV.PostAlarmLog("Robot", "Mapping address setting error!");
  1663. return null;
  1664. }
  1665. ret.Add(nValue);
  1666. }
  1667. return ret.ToArray();
  1668. }
  1669. public override RobotArmWaferStateEnum GetWaferState(RobotArmEnum arm)
  1670. {
  1671. switch(arm)
  1672. {
  1673. case RobotArmEnum.Lower:
  1674. if (di_Values[0, 0]) return RobotArmWaferStateEnum.Present;
  1675. else return RobotArmWaferStateEnum.Absent;
  1676. case RobotArmEnum.Upper:
  1677. if (di_Values[0, 2]) return RobotArmWaferStateEnum.Present;
  1678. else return RobotArmWaferStateEnum.Absent;
  1679. case RobotArmEnum.Both:
  1680. if(di_Values[0, 0] == di_Values[0, 2])
  1681. {
  1682. if (di_Values[0, 0]) return RobotArmWaferStateEnum.Present;
  1683. else return RobotArmWaferStateEnum.Absent;
  1684. }
  1685. break;
  1686. }
  1687. return RobotArmWaferStateEnum.Unknown;
  1688. }
  1689. private RobotArmEnum GetAnotherArm(RobotArmEnum arm)
  1690. {
  1691. if (arm == RobotArmEnum.Lower) return RobotArmEnum.Upper;
  1692. if (arm == RobotArmEnum.Upper) return RobotArmEnum.Lower;
  1693. return RobotArmEnum.Both;
  1694. }
  1695. protected override bool fStartReadData(object[] param)
  1696. {
  1697. _dtActionStart = DateTime.Now;
  1698. string datatype = param[0].ToString();
  1699. lock (_locker)
  1700. {
  1701. switch (datatype)
  1702. {
  1703. case "CurrentPositionData":
  1704. ReadCurrentPostion();
  1705. break;
  1706. case "ExtParameter":
  1707. if (param.Length < 2) return false;
  1708. _lstHandler.AddLast(new HirataRobotIIReadHandler(this, "LE", param[1].ToString()));
  1709. break;
  1710. case "CurrentStatus":
  1711. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  1712. break;
  1713. case "PositionData":
  1714. if (param.Length < 2) return false;
  1715. if (!uint.TryParse(param[1].ToString(), out _)) return false;
  1716. ReadPosition(int.Parse(param[1].ToString()));
  1717. break;
  1718. case "MappingData":
  1719. if (param.Length < 2) return false;
  1720. if (!param[1].ToString().Contains("M0")) return false;
  1721. int paraindex;
  1722. if (!int.TryParse(param[1].ToString().Replace("M", ""), out paraindex)) return false;
  1723. if (paraindex > 9 || paraindex < 0) return false;
  1724. lock (_locker)
  1725. {
  1726. _lstHandler.AddLast(new HirataRobotIIReadHandler(this, "LE", (112 + (5 * (paraindex - 1))).ToString()));
  1727. _lstHandler.AddLast(new HirataRobotIIReadHandler(this, "LE", (610 + (5 * (paraindex - 1))).ToString()));
  1728. _lstHandler.AddLast(new HirataRobotIIReadHandler(this, "LE", (611 + (5 * (paraindex - 1))).ToString()));
  1729. _lstHandler.AddLast(new HirataRobotIIReadHandler(this, "LE", (612 + (5 * (paraindex - 1))).ToString()));
  1730. _lstHandler.AddLast(new HirataRobotIIReadHandler(this, "LE", (613 + (5 * (paraindex - 1))).ToString()));
  1731. _lstHandler.AddLast(new HirataRobotIIReadHandler(this, "LE", (614 + (5 * (paraindex - 1))).ToString()));
  1732. _lstHandler.AddLast(new HirataRobotIIReadHandler(this, "LE", (615 + (5 * (paraindex - 1))).ToString()));
  1733. }
  1734. break;
  1735. }
  1736. }
  1737. return true;
  1738. }
  1739. protected override bool fMonitorReadData(object[] param)
  1740. {
  1741. IsBusy = false;
  1742. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  1743. {
  1744. OnError("ReadDataTimeout.");
  1745. return true;
  1746. }
  1747. if (_lstHandler.Count == 0 && (!_connection.IsBusy) && IsOnLine && ACalCompleted && MovingCompleted)
  1748. {
  1749. EV.PostInfoLog("Robot", $"{RobotModuleName} ReadData successfully.");
  1750. return true;
  1751. }
  1752. return false;
  1753. }
  1754. protected override bool fStartSetParameters(object[] param)
  1755. {
  1756. _dtActionStart = DateTime.Now;
  1757. string datatype = param[0].ToString();
  1758. if (datatype == "TransferSpeedLevel")
  1759. {
  1760. if (param.Length == 2)
  1761. {
  1762. int speed = 10;
  1763. if (param[1].ToString() == "3") speed = 10;
  1764. if (param[1].ToString() == "2") speed = 50;
  1765. if (param[1].ToString() == "1") speed = 100;
  1766. lock (_locker)
  1767. {
  1768. _lstHandler.AddLast(new HirataRobotIIRawCommandHandler(this, "SP", $"{speed} {speed}"));
  1769. }
  1770. }
  1771. if (param.Length == 4)
  1772. {
  1773. int ABWSpeed;
  1774. int ZSpeed;
  1775. if (!int.TryParse(param[2].ToString(), out ABWSpeed)) return false;
  1776. if (!int.TryParse(param[3].ToString(), out ZSpeed)) return false;
  1777. if (ABWSpeed > 100) ABWSpeed = 100;
  1778. if (ABWSpeed < 0) ABWSpeed = 0;
  1779. if (ZSpeed > 100) ZSpeed = 100;
  1780. if (ZSpeed < 0) ZSpeed = 0;
  1781. if (param[1].ToString() == "PTPSpeed")
  1782. {
  1783. lock (_locker)
  1784. {
  1785. _lstHandler.AddLast(new HirataRobotIIRawCommandHandler(this, "SP", $"{ABWSpeed} {ZSpeed}"));
  1786. }
  1787. }
  1788. if (param[1].ToString() == "ACC/DEC")
  1789. {
  1790. lock (_locker)
  1791. {
  1792. _lstHandler.AddLast(new HirataRobotIIRawCommandHandler(this, "SX", $"{ABWSpeed} {ZSpeed}"));
  1793. }
  1794. }
  1795. }
  1796. }
  1797. if (datatype == "ExtParameter")
  1798. {
  1799. if (param.Length < 3) return false;
  1800. if (!int.TryParse(param[1].ToString(), out _)) return false;
  1801. if (string.IsNullOrEmpty(param[2].ToString())) return false;
  1802. lock (_locker)
  1803. {
  1804. _lstHandler.AddLast(new HirataRobotIIWriteHandler(this, "SE", $"{param[1]} {param[2]}"));
  1805. }
  1806. }
  1807. if (datatype == "PositionData")
  1808. {
  1809. if (param.Length < 9)
  1810. {
  1811. EV.PostAlarmLog("Robot", "Robot postion data is not correct");
  1812. return false;
  1813. }
  1814. if (!int.TryParse(param[1].ToString(), out _))
  1815. {
  1816. EV.PostAlarmLog("Robot", "Robot postion data is not correct");
  1817. return false;
  1818. }
  1819. if (!float.TryParse(param[2].ToString(), out _))
  1820. {
  1821. EV.PostAlarmLog("Robot", "Robot postion data is not correct");
  1822. return false;
  1823. }
  1824. if (!float.TryParse(param[3].ToString(), out _))
  1825. {
  1826. EV.PostAlarmLog("Robot", "Robot postion data is not correct");
  1827. return false;
  1828. }
  1829. if (!float.TryParse(param[4].ToString(), out _))
  1830. {
  1831. EV.PostAlarmLog("Robot", "Robot postion data is not correct");
  1832. return false;
  1833. }
  1834. if (!float.TryParse(param[5].ToString(), out _))
  1835. {
  1836. EV.PostAlarmLog("Robot", "Robot postion data is not correct");
  1837. return false;
  1838. }
  1839. if (!int.TryParse(param[6].ToString(), out _))
  1840. {
  1841. EV.PostAlarmLog("Robot", "Robot postion data is not correct");
  1842. return false;
  1843. }
  1844. if (!int.TryParse(param[7].ToString(), out _))
  1845. {
  1846. EV.PostAlarmLog("Robot", "Robot postion data is not correct");
  1847. return false;
  1848. }
  1849. if (!int.TryParse(param[8].ToString(), out _))
  1850. {
  1851. EV.PostAlarmLog("Robot", "Robot postion data is not correct");
  1852. return false;
  1853. }
  1854. return WritePosition(int.Parse(param[1].ToString()), float.Parse(param[2].ToString()), float.Parse(param[3].ToString()),
  1855. float.Parse(param[4].ToString()), float.Parse(param[5].ToString()), param[6].ToString(), param[7].ToString(),
  1856. param[8].ToString());
  1857. }
  1858. if (datatype == "MappingData")
  1859. {
  1860. int paraindex;
  1861. if (!int.TryParse(param[1].ToString().Replace("M", ""), out paraindex)) return false;
  1862. if (paraindex > 9 || paraindex < 0) return false;
  1863. if (param.Length < 8) return false;
  1864. if(uint.TryParse(param[2].ToString(),out _))
  1865. lock (_locker)
  1866. {
  1867. _lstHandler.AddLast(new HirataRobotIIWriteHandler(this, "SE",
  1868. $"{(112 + (5 * (paraindex - 1)))} {param[2]}"));
  1869. }
  1870. if(float.TryParse(param[3].ToString(),out _))
  1871. lock (_locker)
  1872. {
  1873. _lstHandler.AddLast(new HirataRobotIIWriteHandler(this, "SE",
  1874. $"{(610 + (5 * (paraindex - 1)))} {param[3]}"));
  1875. }
  1876. if (float.TryParse(param[4].ToString(), out _))
  1877. lock (_locker)
  1878. {
  1879. _lstHandler.AddLast(new HirataRobotIIWriteHandler(this, "SE",
  1880. $"{(611 + (5 * (paraindex - 1)))} {param[4]}"));
  1881. }
  1882. if (float.TryParse(param[5].ToString(), out _))
  1883. lock (_locker)
  1884. {
  1885. _lstHandler.AddLast(new HirataRobotIIWriteHandler(this, "SE",
  1886. $"{(612 + (5 * (paraindex - 1)))} {param[5]}"));
  1887. }
  1888. if (float.TryParse(param[6].ToString(), out _))
  1889. lock (_locker)
  1890. {
  1891. _lstHandler.AddLast(new HirataRobotIIWriteHandler(this, "SE",
  1892. $"{(613 + (5 * (paraindex - 1)))} {param[6]}"));
  1893. }
  1894. if (!string.IsNullOrEmpty(param[7].ToString()))
  1895. lock (_locker)
  1896. {
  1897. _lstHandler.AddLast(new HirataRobotIIWriteHandler(this, "SE",
  1898. $"{(614 + (5 * (paraindex - 1)))} {param[7]}"));
  1899. }
  1900. }
  1901. if(datatype == "InsertMotionParameter")
  1902. {
  1903. if (param.Length < 3) return false;
  1904. float insertdistance;
  1905. int Decratio;
  1906. if (!float.TryParse(param[1].ToString(), out insertdistance)) return false;
  1907. if (!int.TryParse(param[2].ToString(), out Decratio)) return false;
  1908. if (Decratio > 99) Decratio = 99;
  1909. if (Decratio < 0) Decratio = 0;
  1910. _lstHandler.AddLast(new HirataRobotIIWriteHandler(this, "SI", $" {insertdistance} {Decratio}"));
  1911. }
  1912. if (datatype == "SlowUpMotionParameter")
  1913. {
  1914. if (param.Length < 3) return false;
  1915. float insertdistance;
  1916. int Decratio;
  1917. if (!float.TryParse(param[1].ToString(), out insertdistance)) return false;
  1918. if (!int.TryParse(param[2].ToString(), out Decratio)) return false;
  1919. if (Decratio > 99) Decratio = 99;
  1920. if (Decratio < 0) Decratio = 0;
  1921. _lstHandler.AddLast(new HirataRobotIIWriteHandler(this, "SJ", $" {insertdistance} {Decratio}"));
  1922. }
  1923. if(datatype == "MappingDataForLP")
  1924. {
  1925. if (param.Length < 4) return false;
  1926. string[] mappingstations;
  1927. if (param[3].ToString() == "WS4")
  1928. {
  1929. if (!SC.ContainsItem($"CarrierInfo.{param[1]}MappingStation3")) return false;
  1930. mappingstations = SC.GetStringValue($"CarrierInfo.{param[1]}MappingStation3").Split(',');
  1931. }
  1932. else if(param[3].ToString() == "WS6")
  1933. {
  1934. if (!SC.ContainsItem($"CarrierInfo.{param[1]}MappingStation2")) return false;
  1935. mappingstations = SC.GetStringValue($"CarrierInfo.{param[1]}MappingStation2").Split(',');
  1936. }
  1937. else
  1938. {
  1939. mappingstations = null;
  1940. return false;
  1941. }
  1942. if (mappingstations.Length < 6) return false;
  1943. for(int i =1;i<5;i++)
  1944. {
  1945. if (!uint.TryParse(mappingstations[i], out _)) return false;
  1946. lock(_locker)
  1947. {
  1948. _lstHandler.AddLast(new HirataRobotIIReadRobotPositionHandler(this, Convert.ToInt32(mappingstations[i])));
  1949. }
  1950. int waittime = 0;
  1951. while(_lstHandler.Count !=0 || _connection.IsBusy ||_connection.IsCommunicationError)
  1952. {
  1953. Thread.Sleep(200);
  1954. waittime++;
  1955. if (waittime > 10)
  1956. {
  1957. EV.PostAlarmLog("Robot", "Set LP mapping data failed.");
  1958. return false;
  1959. }
  1960. }
  1961. WritePosition(Convert.ToInt32(mappingstations[i]), ReadXPosition, ReadYPosition, ReadZPosition, ReadWPosition,
  1962. param[2].ToString().Replace("M", ""), FCode, SCode);
  1963. }
  1964. }
  1965. return true;
  1966. }
  1967. protected override bool fMonitorSetParamter(object[] param)
  1968. {
  1969. IsBusy = false;
  1970. if (DateTime.Now - _dtActionStart > TimeSpan.FromSeconds(RobotCommandTimeout))
  1971. {
  1972. OnError("SetParametersTimeout.");
  1973. return true;
  1974. }
  1975. if (_lstHandler.Count == 0 && (!_connection.IsBusy) && IsOnLine && ACalCompleted && MovingCompleted)
  1976. {
  1977. EV.PostInfoLog("Robot", $"{RobotModuleName} set parameters successfully.");
  1978. return true;
  1979. }
  1980. return false;
  1981. }
  1982. private string BuildBladeTarget()
  1983. {
  1984. return (CmdRobotArm == RobotArmEnum.Upper ? "ArmB" : "ArmA") + "." + CmdTarget;
  1985. }
  1986. private void ReadRobotPosition(int id, int time, int address, Action<string> notify, Action<string> error)
  1987. {
  1988. var ret = ExecuteAndWait(id, () =>
  1989. {
  1990. notify($"Start read address data on {address}.");
  1991. lock (_locker)
  1992. {
  1993. _lstHandler.AddLast(new HirataRobotIIReadRobotPositionHandler(this, address));
  1994. }
  1995. return true;
  1996. }, () =>
  1997. {
  1998. return (_lstHandler.Count == 0 && !_connection.IsBusy);
  1999. }, time * 1000);
  2000. if (ret.Item1)
  2001. {
  2002. if (ret.Item2 == Result.FAIL)
  2003. {
  2004. throw new RoutineFaildException();
  2005. }
  2006. else if (ret.Item2 == Result.TIMEOUT) //timeout
  2007. {
  2008. error($"Wait to read position data on {address} timeout after {time} seconds");
  2009. throw new RoutineFaildException();
  2010. }
  2011. else
  2012. {
  2013. throw new RoutineBreakException();
  2014. }
  2015. }
  2016. }
  2017. private void MapWaferAndWaitComplete(int id, int time, ModuleName module, Action<string> notify, Action<string> error)
  2018. {
  2019. var ret = ExecuteAndWait(id, () =>
  2020. {
  2021. notify($"Start map wafer on {module}.");
  2022. int[] mappingaddress = GetMappingAddress(module);
  2023. if (mappingaddress == null || mappingaddress.Length != 6) return false;
  2024. int thicknessIndex = _currentMapThicknessIndex;
  2025. EV.PostInfoLog(Module, $"Start mapping, {module} thickness type {thicknessIndex} ");
  2026. lock (_locker)
  2027. {
  2028. if (thicknessIndex > 0 && thicknessIndex < 10)
  2029. {
  2030. _lstHandler.AddLast(new HirataRobotIIWriteMdataOnCurrenPositionHandler(this, mappingaddress[1], $"0{thicknessIndex}"));
  2031. }
  2032. _lstHandler.AddLast(new HirataRobotIIMoveToPositionHandler(this, mappingaddress[0], null));
  2033. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  2034. _lstHandler.AddLast(new HirataRobotIIMoveToPositionHandler(this, mappingaddress[1], null));
  2035. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  2036. _lstHandler.AddLast(new HirataRobotIIRawCommandHandler(this, "SM", "1"));
  2037. _lstHandler.AddLast(new HirataRobotIIMoveToPositionHandler(this, mappingaddress[2], null));
  2038. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  2039. _lstHandler.AddLast(new HirataRobotIIMoveToPositionHandler(this, mappingaddress[3], null));
  2040. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  2041. _lstHandler.AddLast(new HirataRobotIIMoveToPositionHandler(this, mappingaddress[4], null));
  2042. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  2043. _lstHandler.AddLast(new HirataRobotIIReadHandler(this, "LE", "100"));
  2044. _lstHandler.AddLast(new HirataRobotIIReadHandler(this, "LE", "101"));
  2045. _lstHandler.AddLast(new HirataRobotIIRawCommandHandler(this, "SM", "0")); //Close laser
  2046. _lstHandler.AddLast(new HirataRobotIIMoveToPositionHandler(this, mappingaddress[5], null));
  2047. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  2048. ReadCurrentPostion();
  2049. }
  2050. return true;
  2051. }, () =>
  2052. {
  2053. if (_lstHandler.Count == 0 && (!_connection.IsBusy) && IsOnLine && ACalCompleted &&MovingCompleted)
  2054. return true;
  2055. return false;
  2056. }, time * 1000);
  2057. if (ret.Item1)
  2058. {
  2059. if (ret.Item2 == Result.FAIL)
  2060. {
  2061. throw new RoutineFaildException();
  2062. }
  2063. else if (ret.Item2 == Result.TIMEOUT) //timeout
  2064. {
  2065. error($"Wait wafer map on {module} timeout after {time} seconds");
  2066. throw new RoutineFaildException();
  2067. }
  2068. else
  2069. {
  2070. throw new RoutineBreakException();
  2071. }
  2072. }
  2073. }
  2074. public void TimeDelay(int id, double time)
  2075. {
  2076. Tuple<bool, Result> ret = Delay(id, () =>
  2077. {
  2078. Notify($"Delay {time} seconds");
  2079. return true;
  2080. }, time * 1000);
  2081. if (ret.Item1)
  2082. {
  2083. if (ret.Item2 == Result.RUN)
  2084. {
  2085. throw (new RoutineBreakException());
  2086. }
  2087. }
  2088. }
  2089. private void OpenVacuum(int id, int time, Action<string> notify, Action<string> error)
  2090. {
  2091. var ret = ExecuteAndWait(id, () =>
  2092. {
  2093. notify($"Start open vacuum valve.");
  2094. lock (_locker)
  2095. {
  2096. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, true));
  2097. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, false));
  2098. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, true));
  2099. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, false));
  2100. }
  2101. return true;
  2102. }, () =>
  2103. {
  2104. if (_lstHandler.Count == 0 && (!_connection.IsBusy) && IsOnLine && ACalCompleted && MovingCompleted)
  2105. return true;
  2106. return false;
  2107. }, time * 1000);
  2108. if (ret.Item1)
  2109. {
  2110. if (ret.Item2 == Result.FAIL)
  2111. {
  2112. throw new RoutineFaildException();
  2113. }
  2114. else if (ret.Item2 == Result.TIMEOUT) //timeout
  2115. {
  2116. error($"Open blade vacuum timeout after {time} seconds");
  2117. throw new RoutineFaildException();
  2118. }
  2119. else
  2120. {
  2121. throw new RoutineBreakException();
  2122. }
  2123. }
  2124. }
  2125. private void IdentifyWaferStatus(int id, int time, Action<string> notify, Action<string> error)
  2126. {
  2127. var ret = ExecuteAndWait(id, () =>
  2128. {
  2129. notify($"Start check wafer status.");
  2130. lock (_locker)
  2131. {
  2132. ReadCurrentPostion();
  2133. _lstHandler.AddLast(new HirataRobotIIReadDIByteForInitHandler(this, 0));
  2134. }
  2135. return true;
  2136. }, () =>
  2137. {
  2138. if (_lstHandler.Count == 0 && (!_connection.IsBusy) && IsOnLine && ACalCompleted && MovingCompleted)
  2139. {
  2140. if (GetWaferState(RobotArmEnum.Lower) == RobotArmWaferStateEnum.Present)
  2141. {
  2142. if (WaferManager.Instance.CheckNoWafer(RobotModuleName, 0))
  2143. WaferManager.Instance.CreateWafer(RobotModuleName, 0, WaferStatus.Normal);
  2144. }
  2145. if (GetWaferState(RobotArmEnum.Lower) == RobotArmWaferStateEnum.Absent)
  2146. {
  2147. if (WaferManager.Instance.CheckHasWafer(RobotModuleName, 0))
  2148. WaferManager.Instance.DeleteWafer(RobotModuleName, 0);
  2149. }
  2150. if (GetWaferState(RobotArmEnum.Upper) == RobotArmWaferStateEnum.Present)
  2151. {
  2152. if (WaferManager.Instance.CheckNoWafer(RobotModuleName, 1))
  2153. WaferManager.Instance.CreateWafer(RobotModuleName, 1, WaferStatus.Normal);
  2154. }
  2155. if (GetWaferState(RobotArmEnum.Upper) == RobotArmWaferStateEnum.Absent)
  2156. {
  2157. if (WaferManager.Instance.CheckHasWafer(RobotModuleName, 1))
  2158. WaferManager.Instance.DeleteWafer(RobotModuleName, 1);
  2159. }
  2160. return true;
  2161. }
  2162. return false;
  2163. }, time * 1000);
  2164. if (ret.Item1)
  2165. {
  2166. if (ret.Item2 == Result.FAIL)
  2167. {
  2168. throw new RoutineFaildException();
  2169. }
  2170. else if (ret.Item2 == Result.TIMEOUT) //timeout
  2171. {
  2172. error($"Open blade vacuum timeout after {time} seconds");
  2173. throw new RoutineFaildException();
  2174. }
  2175. else
  2176. {
  2177. throw new RoutineBreakException();
  2178. }
  2179. }
  2180. }
  2181. private void HomeRobotArms(int id, int time, Action<string> notify, Action<string> error)
  2182. {
  2183. var ret = ExecuteAndWait(id, () =>
  2184. {
  2185. notify($"Start home robot arms.");
  2186. lock (_locker)
  2187. {
  2188. if (WaferManager.Instance.CheckNoWafer(RobotModuleName, 0))
  2189. {
  2190. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 1, true));
  2191. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 0, false));
  2192. }
  2193. if (WaferManager.Instance.CheckNoWafer(RobotModuleName, 1))
  2194. {
  2195. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 3, true));
  2196. _lstHandler.AddLast(new HirataRobotIIWriteDOHandler(this, 2, false));
  2197. }
  2198. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  2199. _lstHandler.AddLast(new HirataRobotIIMoveToPositionHandler(this, 0));
  2200. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  2201. _lstHandler.AddLast(new HirataRobotIIMoveToPositionHandler(this, 1, "A"));
  2202. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  2203. }
  2204. return true;
  2205. }, () =>
  2206. {
  2207. if (_lstHandler.Count == 0 && (!_connection.IsBusy) && IsOnLine && ACalCompleted && MovingCompleted)
  2208. {
  2209. return true;
  2210. }
  2211. return false;
  2212. }, time * 1000);
  2213. if (ret.Item1)
  2214. {
  2215. if (ret.Item2 == Result.FAIL)
  2216. {
  2217. throw new RoutineFaildException();
  2218. }
  2219. else if (ret.Item2 == Result.TIMEOUT) //timeout
  2220. {
  2221. error($"Home robot timeout after {time} seconds");
  2222. throw new RoutineFaildException();
  2223. }
  2224. else
  2225. {
  2226. throw new RoutineBreakException();
  2227. }
  2228. }
  2229. }
  2230. private void MoveWalkingAxis(int id, int time, ModuleName module, Action<string> notify, Action<string> error)
  2231. {
  2232. var ret = ExecuteAndWait(id, () =>
  2233. {
  2234. if(m_WalkingAxis == null)
  2235. {
  2236. error("No valid walking axis.");
  2237. return false;
  2238. }
  2239. if(!m_WalkingAxis.IsReady())
  2240. {
  2241. error("Walking axis is not ready.");
  2242. return false;
  2243. }
  2244. notify($"Start move walking axis to {module}.");
  2245. return m_WalkingAxis.MoveTo(module);
  2246. }, () =>
  2247. {
  2248. if (m_WalkingAxis.IsReady() && m_WalkingAxis.IsArrivedTarget)
  2249. return true;
  2250. return false;
  2251. }, time * 1000);
  2252. if (ret.Item1)
  2253. {
  2254. if (ret.Item2 == Result.FAIL)
  2255. {
  2256. throw new RoutineFaildException();
  2257. }
  2258. else if (ret.Item2 == Result.TIMEOUT) //timeout
  2259. {
  2260. error($"Wait walking axis move to {module} timeout after {time} seconds");
  2261. throw new RoutineFaildException();
  2262. }
  2263. else
  2264. {
  2265. throw new RoutineBreakException();
  2266. }
  2267. }
  2268. }
  2269. private void WaitRobotToReady(int id, int time, Action<string> notify, Action<string> error)
  2270. {
  2271. var ret = ExecuteAndWait(id, () =>
  2272. {
  2273. lock(_locker)
  2274. {
  2275. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  2276. }
  2277. return true;
  2278. }, () =>
  2279. {
  2280. if(_lstHandler.Count == 0 && !_connection.IsBusy)
  2281. {
  2282. if (IsOnLine && ACalCompleted && MovingCompleted)
  2283. return true;
  2284. else
  2285. {
  2286. lock (_locker)
  2287. {
  2288. _lstHandler.AddLast(new HirataRobotIIMonitorRobotStatusHandler(this));
  2289. }
  2290. }
  2291. }
  2292. return false;
  2293. }, time * 1000);
  2294. if (ret.Item1)
  2295. {
  2296. if (ret.Item2 == Result.FAIL)
  2297. {
  2298. throw new RoutineFaildException();
  2299. }
  2300. else if (ret.Item2 == Result.TIMEOUT) //timeout
  2301. {
  2302. error($"Wait robot to be ready timeout after {time} seconds");
  2303. throw new RoutineFaildException();
  2304. }
  2305. else
  2306. {
  2307. throw new RoutineBreakException();
  2308. }
  2309. }
  2310. }
  2311. #region Sequence Routine
  2312. protected void Notify(string message)
  2313. {
  2314. EV.PostMessage(Name, EventEnum.GeneralInfo, string.Format("{0}:{1}", Name, message));
  2315. }
  2316. protected void Stop(string failReason)
  2317. {
  2318. OnError(string.Format("Failed {0}, {1} ", Name, failReason));
  2319. }
  2320. private enum RobotStepEnum
  2321. {
  2322. Step1,
  2323. Step2,
  2324. Step3,
  2325. Step4,
  2326. Step5,
  2327. Step6,
  2328. Step7,
  2329. Step8,
  2330. Step9,
  2331. Step10,
  2332. Step11,
  2333. Step12,
  2334. Step13,
  2335. Step14,
  2336. Step15,
  2337. Step16,
  2338. }
  2339. //timer, 计算routine时间
  2340. protected DeviceTimer counter = new DeviceTimer();
  2341. protected DeviceTimer delayTimer = new DeviceTimer();
  2342. private enum STATE
  2343. {
  2344. IDLE,
  2345. WAIT,
  2346. }
  2347. public int TokenId
  2348. {
  2349. get { return _id; }
  2350. }
  2351. private int _id; //step index
  2352. /// <summary>
  2353. /// already done steps
  2354. /// </summary>
  2355. private Stack<int> _steps = new Stack<int>();
  2356. private STATE state; //step state //idel,wait,
  2357. //loop control
  2358. private int loop = 0;
  2359. private int loopCount = 0;
  2360. private int loopID = 0;
  2361. private DeviceTimer timer = new DeviceTimer();
  2362. public int LoopCounter { get { return loop; } }
  2363. public int LoopTotalTime { get { return loopCount; } }
  2364. // public int Timeout { get { return (int)(timer.GetTotalTime() / 1000); } }
  2365. //状态持续时间,单位为秒
  2366. public int Elapsed { get { return (int)(timer.GetElapseTime() / 1000); } }
  2367. protected RoutineResult RoutineToken = new RoutineResult() { Result = RoutineState.Running };
  2368. public void ResetRoutine()
  2369. {
  2370. _id = 0;
  2371. _steps.Clear();
  2372. loop = 0;
  2373. loopCount = 0;
  2374. state = STATE.IDLE;
  2375. counter.Start(60 * 60 * 100); //默认1小时
  2376. RoutineToken.Result = RoutineState.Running;
  2377. }
  2378. protected void PerformRoutineStep(int id, Func<RoutineState> execution, RoutineResult result)
  2379. {
  2380. if (!Acitve(id))
  2381. return;
  2382. result.Result = execution();
  2383. }
  2384. #region interface
  2385. public void StopLoop()
  2386. {
  2387. loop = loopCount;
  2388. }
  2389. public Tuple<bool, Result> Loop<T>(T id, Func<bool> func, int count)
  2390. {
  2391. int idx = Convert.ToInt32(id);
  2392. bool bActive = Acitve(idx);
  2393. if (bActive)
  2394. {
  2395. if (!func())
  2396. {
  2397. return Tuple.Create(bActive, Result.FAIL); //执行错误
  2398. }
  2399. loopID = idx;
  2400. loopCount = count;
  2401. next();
  2402. return Tuple.Create(true, Result.RUN);
  2403. }
  2404. return Tuple.Create(false, Result.RUN);
  2405. }
  2406. public Tuple<bool, Result> EndLoop<T>(T id, Func<bool> func)
  2407. {
  2408. int idx = Convert.ToInt32(id);
  2409. bool bActive = Acitve(idx);
  2410. if (bActive)
  2411. {
  2412. if (++loop >= loopCount) //Loop 结束
  2413. {
  2414. if (!func())
  2415. {
  2416. return Tuple.Create(bActive, Result.FAIL); //执行错误
  2417. }
  2418. loop = 0;
  2419. loopCount = 0; // Loop 结束时,当前loop和loop总数都清零
  2420. next();
  2421. return Tuple.Create(true, Result.RUN);
  2422. }
  2423. //继续下一LOOP
  2424. next(loopID);
  2425. return Tuple.Create(true, Result.RUN);
  2426. }
  2427. return Tuple.Create(false, Result.RUN);
  2428. }
  2429. public Tuple<bool, Result> ExecuteAndWait<T>(T id, IRoutine routine)
  2430. {
  2431. int idx = Convert.ToInt32(id);
  2432. bool bActive = Acitve(idx);
  2433. if (bActive)
  2434. {
  2435. if (state == STATE.IDLE)
  2436. {
  2437. Result startRet = routine.Start();
  2438. if (startRet == Result.FAIL)
  2439. {
  2440. return Tuple.Create(true, Result.FAIL); //执行错误
  2441. }
  2442. else if (startRet == Result.DONE)
  2443. {
  2444. next();
  2445. return Tuple.Create(true, Result.DONE);
  2446. }
  2447. state = STATE.WAIT;
  2448. }
  2449. Result ret = routine.Monitor();
  2450. if (ret == Result.DONE)
  2451. {
  2452. next();
  2453. return Tuple.Create(true, Result.DONE);
  2454. }
  2455. else if (ret == Result.FAIL || ret == Result.TIMEOUT)
  2456. {
  2457. return Tuple.Create(true, Result.FAIL);
  2458. }
  2459. else
  2460. {
  2461. return Tuple.Create(true, Result.RUN);
  2462. }
  2463. }
  2464. return Tuple.Create(false, Result.RUN);
  2465. }
  2466. public Tuple<bool, Result> ExecuteAndWait<T>(T id, List<IRoutine> routines)
  2467. {
  2468. int idx = Convert.ToInt32(id);
  2469. bool bActive = Acitve(idx);
  2470. if (bActive)
  2471. {
  2472. if (state == STATE.IDLE)
  2473. {
  2474. foreach (var item in routines)
  2475. {
  2476. if (item.Start() == Result.FAIL)
  2477. return Tuple.Create(true, Result.FAIL);
  2478. }
  2479. state = STATE.WAIT;
  2480. }
  2481. //wait all sub failed or completedboo
  2482. bool bFail = false;
  2483. bool bDone = true;
  2484. foreach (var item in routines)
  2485. {
  2486. Result ret = item.Monitor();
  2487. bDone &= (ret == Result.FAIL || ret == Result.DONE);
  2488. bFail |= ret == Result.FAIL;
  2489. }
  2490. if (bDone)
  2491. {
  2492. next();
  2493. if (bFail)
  2494. return Tuple.Create(true, Result.FAIL);
  2495. return Tuple.Create(true, Result.DONE);
  2496. }
  2497. return Tuple.Create(true, Result.RUN);
  2498. }
  2499. return Tuple.Create(false, Result.RUN);
  2500. }
  2501. public Tuple<bool, Result> Check<T>(T id, Func<bool> func) //顺序执行
  2502. {
  2503. return Check(Check(Convert.ToInt32(id), func));
  2504. }
  2505. public Tuple<bool, Result> Execute<T>(T id, Func<bool> func) //顺序执行
  2506. {
  2507. return Check(execute(Convert.ToInt32(id), func));
  2508. }
  2509. public Tuple<bool, Result> Wait<T>(T id, Func<bool> func, double timeout = int.MaxValue) //Wait condition
  2510. {
  2511. return Check(wait(Convert.ToInt32(id), func, timeout));
  2512. }
  2513. public Tuple<bool, Result> Wait<T>(T id, Func<bool?> func, double timeout = int.MaxValue) //Wait condition
  2514. {
  2515. return Check(wait(Convert.ToInt32(id), func, timeout));
  2516. }
  2517. public Tuple<bool, Result> ExecuteAndWait<T>(T id, Func<bool> execute, Func<bool?> check, double timeout = int.MaxValue)
  2518. {
  2519. int idx = Convert.ToInt32(id);
  2520. bool bActive = Acitve(idx);
  2521. bool? bExecute = false;
  2522. if (bActive)
  2523. {
  2524. if (state == STATE.IDLE)
  2525. {
  2526. if (!execute())
  2527. {
  2528. return Tuple.Create(bActive, Result.FAIL); //执行错误
  2529. }
  2530. timer.Start(timeout);
  2531. state = STATE.WAIT;
  2532. }
  2533. bExecute = check();
  2534. if (bExecute == null)
  2535. {
  2536. return Tuple.Create(bActive, Result.FAIL); //Termianate
  2537. }
  2538. else
  2539. {
  2540. if (bExecute.Value) //检查Success, next
  2541. {
  2542. next();
  2543. return Tuple.Create(true, Result.RUN);
  2544. }
  2545. }
  2546. if (timer.IsTimeout())
  2547. return Tuple.Create(true, Result.TIMEOUT);
  2548. return Tuple.Create(true, Result.RUN);
  2549. }
  2550. return Tuple.Create(false, Result.RUN);
  2551. }
  2552. public Tuple<bool, Result> ExecuteAndWait<T>(T id, Func<bool> execute, Func<bool?> check, Func<double> time)
  2553. {
  2554. int idx = Convert.ToInt32(id);
  2555. bool bActive = Acitve(idx);
  2556. bool? bExecute = false;
  2557. double timeout = 0;
  2558. if (bActive)
  2559. {
  2560. if (state == STATE.IDLE)
  2561. {
  2562. timeout = time();
  2563. if (!execute())
  2564. {
  2565. return Tuple.Create(true, Result.FAIL); //执行错误
  2566. }
  2567. timer.Start(timeout);
  2568. state = STATE.WAIT;
  2569. }
  2570. bExecute = check();
  2571. if (bExecute == null)
  2572. {
  2573. return Tuple.Create(true, Result.FAIL); //Termianate
  2574. }
  2575. if (bExecute.Value) //检查Success, next
  2576. {
  2577. next();
  2578. return Tuple.Create(true, Result.RUN);
  2579. }
  2580. if (timer.IsTimeout())
  2581. return Tuple.Create(true, Result.TIMEOUT);
  2582. return Tuple.Create(true, Result.RUN);
  2583. }
  2584. return Tuple.Create(false, Result.RUN);
  2585. }
  2586. public Tuple<bool, Result> Wait<T>(T id, IRoutine rt)
  2587. {
  2588. int idx = Convert.ToInt32(id);
  2589. bool bActive = Acitve(idx);
  2590. if (bActive)
  2591. {
  2592. if (state == STATE.IDLE)
  2593. {
  2594. rt.Start();
  2595. state = STATE.WAIT;
  2596. }
  2597. Result ret = rt.Monitor();
  2598. return Tuple.Create(true, ret);
  2599. }
  2600. return Tuple.Create(false, Result.RUN);
  2601. }
  2602. //Monitor
  2603. public Tuple<bool, Result> Monitor<T>(T id, Func<bool> func, Func<bool> check, double time)
  2604. {
  2605. int idx = Convert.ToInt32(id);
  2606. bool bActive = Acitve(idx);
  2607. bool bCheck = false;
  2608. if (bActive)
  2609. {
  2610. if (state == STATE.IDLE)
  2611. {
  2612. if ((func != null) && !func())
  2613. {
  2614. return Tuple.Create(true, Result.FAIL);
  2615. }
  2616. timer.Start(time);
  2617. state = STATE.WAIT;
  2618. }
  2619. bCheck = check();
  2620. if (!bCheck)
  2621. {
  2622. return Tuple.Create(true, Result.FAIL); //Termianate
  2623. }
  2624. if (timer.IsTimeout())
  2625. {
  2626. next();
  2627. }
  2628. return Tuple.Create(true, Result.RUN);
  2629. }
  2630. return Tuple.Create(false, Result.RUN);
  2631. }
  2632. //Delay
  2633. public Tuple<bool, Result> Delay<T>(T id, Func<bool> func, double time)
  2634. {
  2635. int idx = Convert.ToInt32(id);
  2636. bool bActive = Acitve(idx);
  2637. if (bActive)
  2638. {
  2639. if (state == STATE.IDLE)
  2640. {
  2641. if ((func != null) && !func())
  2642. {
  2643. return Tuple.Create(true, Result.FAIL);
  2644. }
  2645. timer.Start(time);
  2646. state = STATE.WAIT;
  2647. }
  2648. if (timer.IsTimeout())
  2649. {
  2650. next();
  2651. }
  2652. return Tuple.Create(true, Result.RUN);
  2653. }
  2654. return Tuple.Create(false, Result.RUN);
  2655. }
  2656. //先delay 再运行
  2657. public Tuple<bool, Result> DelayCheck<T>(T id, Func<bool> func, double time)
  2658. {
  2659. int idx = Convert.ToInt32(id);
  2660. bool bActive = Acitve(idx);
  2661. if (bActive)
  2662. {
  2663. if (state == STATE.IDLE)
  2664. {
  2665. timer.Start(time);
  2666. state = STATE.WAIT;
  2667. }
  2668. if (timer.IsTimeout())
  2669. {
  2670. if (func != null && !func())
  2671. {
  2672. return Tuple.Create(true, Result.FAIL);
  2673. }
  2674. next();
  2675. }
  2676. return Tuple.Create(true, Result.RUN);
  2677. }
  2678. return Tuple.Create(false, Result.RUN);
  2679. }
  2680. #endregion
  2681. private Tuple<bool, bool> execute(int id, Func<bool> func) //顺序执行
  2682. {
  2683. bool bActive = Acitve(id);
  2684. bool bExecute = false;
  2685. if (bActive)
  2686. {
  2687. bExecute = func();
  2688. if (bExecute)
  2689. {
  2690. next();
  2691. }
  2692. }
  2693. return Tuple.Create(bActive, bExecute);
  2694. }
  2695. private Tuple<bool, bool> Check(int id, Func<bool> func) //check
  2696. {
  2697. bool bActive = Acitve(id);
  2698. bool bExecute = false;
  2699. if (bActive)
  2700. {
  2701. bExecute = func();
  2702. next();
  2703. }
  2704. return Tuple.Create(bActive, bExecute);
  2705. }
  2706. /// <summary>
  2707. /// </summary>
  2708. /// <param name="id"></param>
  2709. /// <param name="func"></param>
  2710. /// <param name="timeout"></param>
  2711. /// <returns>
  2712. /// item1 Active
  2713. /// item2 execute
  2714. /// item3 Timeout
  2715. ///</returns>
  2716. private Tuple<bool, bool, bool> wait(int id, Func<bool> func, double timeout = int.MaxValue) //Wait condition
  2717. {
  2718. bool bActive = Acitve(id);
  2719. bool bExecute = false;
  2720. bool bTimeout = false;
  2721. if (bActive)
  2722. {
  2723. if (state == STATE.IDLE)
  2724. {
  2725. timer.Start(timeout);
  2726. state = STATE.WAIT;
  2727. }
  2728. bExecute = func();
  2729. if (bExecute)
  2730. {
  2731. next();
  2732. }
  2733. bTimeout = timer.IsTimeout();
  2734. }
  2735. return Tuple.Create(bActive, bExecute, bTimeout);
  2736. }
  2737. private Tuple<bool, bool?, bool> wait(int id, Func<bool?> func, double timeout = int.MaxValue) //Wait condition && Check error
  2738. {
  2739. bool bActive = Acitve(id);
  2740. bool? bExecute = false;
  2741. bool bTimeout = false;
  2742. if (bActive)
  2743. {
  2744. if (state == STATE.IDLE)
  2745. {
  2746. timer.Start(timeout);
  2747. state = STATE.WAIT;
  2748. }
  2749. bExecute = func();
  2750. if (bExecute.HasValue && bExecute.Value)
  2751. {
  2752. next();
  2753. }
  2754. bTimeout = timer.IsTimeout();
  2755. }
  2756. return Tuple.Create(bActive, bExecute, bTimeout);
  2757. }
  2758. /// <summary>
  2759. /// </summary>
  2760. /// <param name="value"></param>
  2761. /// <returns>
  2762. /// item1 true, return item2
  2763. /// </returns>
  2764. private Tuple<bool, Result> Check(Tuple<bool, bool> value)
  2765. {
  2766. if (value.Item1)
  2767. {
  2768. if (!value.Item2)
  2769. {
  2770. return Tuple.Create(true, Result.FAIL);
  2771. }
  2772. return Tuple.Create(true, Result.RUN);
  2773. }
  2774. return Tuple.Create(false, Result.RUN);
  2775. }
  2776. private Tuple<bool, Result> Check(Tuple<bool, bool, bool> value)
  2777. {
  2778. if (value.Item1) // 当前执行
  2779. {
  2780. if (CheckTimeout(value)) //timeout
  2781. {
  2782. return Tuple.Create(true, Result.TIMEOUT);
  2783. }
  2784. return Tuple.Create(true, Result.RUN);
  2785. }
  2786. return Tuple.Create(false, Result.RUN);
  2787. }
  2788. private Tuple<bool, Result> Check(Tuple<bool, bool?, bool> value)
  2789. {
  2790. if (value.Item1) // 当前执行
  2791. {
  2792. if (value.Item2 == null)
  2793. {
  2794. return Tuple.Create(true, Result.FAIL);
  2795. }
  2796. else
  2797. {
  2798. if (value.Item2 == false && value.Item3 == true) //timeout
  2799. {
  2800. return Tuple.Create(true, Result.TIMEOUT);
  2801. }
  2802. return Tuple.Create(true, Result.RUN);
  2803. }
  2804. }
  2805. return Tuple.Create(false, Result.RUN);
  2806. }
  2807. private bool CheckTimeout(Tuple<bool, bool, bool> value)
  2808. {
  2809. return value.Item1 == true && value.Item2 == false && value.Item3 == true;
  2810. }
  2811. private bool Acitve(int id) //
  2812. {
  2813. if (_steps.Contains(id))
  2814. return false;
  2815. this._id = id;
  2816. return true;
  2817. }
  2818. private void next()
  2819. {
  2820. _steps.Push(this._id);
  2821. state = STATE.IDLE;
  2822. }
  2823. private void next(int step) //loop
  2824. {
  2825. while (_steps.Pop() != step) ;
  2826. state = STATE.IDLE;
  2827. }
  2828. public void Delay(int id, double delaySeconds)
  2829. {
  2830. Tuple<bool, Result> ret = Delay(id, () =>
  2831. {
  2832. return true;
  2833. }, delaySeconds * 1000);
  2834. if (ret.Item1)
  2835. {
  2836. if (ret.Item2 == Result.RUN)
  2837. {
  2838. throw (new RoutineBreakException());
  2839. }
  2840. }
  2841. }
  2842. public bool IsActived(int id)
  2843. {
  2844. return _steps.Contains(id);
  2845. }
  2846. #endregion
  2847. }
  2848. }