HirataRobotIIHandler.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Log;
  3. using MECF.Framework.Common.Communications;
  4. using MECF.Framework.Common.SubstrateTrackings;
  5. using Newtonsoft.Json.Linq;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.HirataRobotII
  11. {
  12. public abstract class HirataRobotIIHandler : HandlerBase
  13. {
  14. public HirataRobotII Device { get; }
  15. protected string _command;
  16. public string _parameter;
  17. //protected static char _stx = '\u0002';
  18. //protected static char _etx = '\u0003';
  19. //protected static char _space = '\u0020';
  20. protected static string _stx = "\x02"; // Start of Text
  21. protected static string _etx = "\x03"; // End of Text
  22. protected static string _sp = "\x20";
  23. //byte[] myBytes = System.Text.Encoding.ASCII.GetBytes("\x02Hello, world!");
  24. //socket.Send(myBytes);
  25. protected HirataRobotIIHandler(HirataRobotII device, string command, string parameter = null)
  26. : base(BuildMessage(device.Address,command, parameter))
  27. {
  28. Device = device;
  29. _command = command;
  30. _parameter = parameter;
  31. Name = command;
  32. }
  33. private static string BuildMessage(string address, string command, string parameter)
  34. {
  35. string msg1 = parameter!= null ? $"{address}{_sp}{command}{_sp}{parameter}{_etx}": $"{address}{_sp}{command}{_etx}";
  36. byte lrc = CalcLRC(Encoding.ASCII.GetBytes(msg1).ToList());
  37. string msg = $"{_stx}{msg1}{(char)(lrc)}";
  38. return msg;// +"\0";
  39. }
  40. private static byte CalcLRC(List<byte> data)
  41. {
  42. byte ret = 0x00;
  43. for (var i = 0; i < data.Count; i++)
  44. {
  45. ret ^= data[i];
  46. }
  47. return ret;
  48. }
  49. protected static string F2S(float value)
  50. {
  51. return value < 0 ? value.ToString() : " " + value.ToString();
  52. }
  53. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  54. {
  55. HirataRobotIIMessage response = msg as HirataRobotIIMessage;
  56. ResponseMessage = msg;
  57. if(!msg.IsResponse)
  58. {
  59. transactionComplete = false;
  60. return false;
  61. }
  62. if (msg.IsError)
  63. {
  64. Device.NoteError(response.Data);
  65. }
  66. else
  67. {
  68. Device.NoteRobotStatus(response.Data);
  69. }
  70. if (response.IsComplete)
  71. {
  72. SetState(EnumHandlerState.Completed);
  73. transactionComplete = true;
  74. return true;
  75. }
  76. transactionComplete = false;
  77. return false;
  78. }
  79. protected virtual void ParseData(HirataRobotIIMessage msg)
  80. {
  81. if (msg.Data[0] != 0)
  82. {
  83. var reason = TranslateCsrCode(msg.Data[0]);
  84. Device.NoteError(reason);
  85. }
  86. }
  87. public void SendAck()
  88. {
  89. Device.Connection.SendMessage(new byte[] { 0x06 });
  90. }
  91. protected string TranslateCsrCode(int csrCode)
  92. {
  93. string ret = csrCode.ToString();
  94. switch (csrCode)
  95. {
  96. case 0:
  97. ret = null;//"Command accepted";
  98. break;
  99. case 1:
  100. ret = "Control Code Is Incorrect";
  101. break;
  102. case 2:
  103. ret = "Output Is On(Change Not Allowed)";
  104. break;
  105. case 4:
  106. ret = "Data Is Out Of Range";
  107. break;
  108. case 7:
  109. ret = "Active Fault(s) Exist";
  110. break;
  111. case 9:
  112. ret = "Data Byte Count Is Incorrect";
  113. break;
  114. case 19:
  115. ret = "Recipe Is Active(Change Not Allowed)";
  116. break;
  117. case 50:
  118. ret = "The Frequency Is Out Of Range";
  119. break;
  120. case 51:
  121. ret = "The Duty Cycle Is Out Of Range";
  122. break;
  123. case 53:
  124. ret = "The Device Controlled By The Command Is Not Detected";
  125. break;
  126. case 99:
  127. ret = "Command Not Accepted(There Is No Such Command)";
  128. break;
  129. default:
  130. break;
  131. }
  132. return ret;
  133. }
  134. }
  135. public class HirataRobotIIRawCommandHandler : HirataRobotIIHandler
  136. {
  137. public HirataRobotIIRawCommandHandler(HirataRobotII device, string command, string parameter = null)
  138. : base(device, command, parameter)
  139. {
  140. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  141. LOG.Write($"{device.Name} execute raw command {command} {temp} in byte.");
  142. }
  143. public override bool HandleMessage(MessageBase msg, out bool handled)
  144. {
  145. if (base.HandleMessage(msg, out handled))
  146. {
  147. var result = msg as HirataRobotIIMessage;
  148. Device.NoteRawCommandInfo(_command, result.RawMessage);
  149. ResponseMessage = msg;
  150. handled = true;
  151. }
  152. return true;
  153. }
  154. }
  155. public class HirataRobotIIWritePositionHandler : HirataRobotIIHandler
  156. {
  157. public HirataRobotIIWritePositionHandler(HirataRobotII device, int address, float x, float y, float z, float w,
  158. float r, float c, string posture,string Mdata,string Fcode,string Scode)
  159. : base(device, "SD", BuildParameter(address, x, y, z, w, r, c, posture, device.Axis,Mdata,Fcode,Scode))
  160. {
  161. }
  162. private static string BuildParameter(int address, float x, float y, float z, float w, float r, float c,
  163. string posture, int axis, string Mdata, string Fcode, string Scode)
  164. {
  165. if(axis == 6)
  166. return F2S(address) + F2S(x) + F2S(y) + F2S(z) + F2S(w) + F2S(r) + F2S(c) + posture +
  167. $" 0 {Mdata} {Fcode} {Scode}";
  168. else
  169. return F2S(address) + F2S(x) + F2S(y) + F2S(z) + F2S(w) + posture +
  170. $" 0 {Mdata} {Fcode} {Scode}";
  171. }
  172. }
  173. public class HirataRobotIIMoveToPositionHandler : HirataRobotIIHandler
  174. {
  175. public HirataRobotIIMoveToPositionHandler(HirataRobotII device, int address, string motion = null)
  176. : base(device, BuildCommand(motion), address.ToString())
  177. {
  178. }
  179. private static string BuildCommand(string motion)
  180. {
  181. if (motion == null)
  182. {
  183. return "GP";
  184. }
  185. else
  186. {
  187. return "GP" + motion;
  188. }
  189. }
  190. }
  191. public class HirataRobotIIMoveToPositionWithDeviationHandler : HirataRobotIIHandler
  192. {
  193. public HirataRobotIIMoveToPositionWithDeviationHandler(HirataRobotII device, string motion, int address, float xoffset, float yoffset, float zoffset, float woffset)
  194. : base(device, BuildCommand(motion), BuildParameter(address,xoffset, yoffset, zoffset, woffset))
  195. {
  196. LOG.Write($"{device.Name} move to postion:{address} on motion {motion} with deviation x:{xoffset},y:{yoffset},z:{zoffset},w:{woffset}");
  197. }
  198. private static string BuildCommand(string motion)
  199. {
  200. if (motion == null)
  201. {
  202. return "GP";
  203. }
  204. else
  205. {
  206. return "GP" + motion;
  207. }
  208. }
  209. private static string BuildParameter(int address,float x, float y, float z, float w)
  210. {
  211. return address.ToString()+ _sp+ "("+ F2S(x)+ F2S(y)+ F2S(z)+ F2S(w)+ ")";
  212. }
  213. }
  214. public class HirataRobotIIMoveDeviationHandler : HirataRobotIIHandler
  215. {
  216. public HirataRobotIIMoveDeviationHandler(HirataRobotII device, string motion,float xoffset, float yoffset, float zoffset, float woffset)
  217. : base(device, BuildCommand(motion), BuildParameter(xoffset, yoffset, zoffset, woffset))
  218. {
  219. LOG.Write($"{device.Name} move deviation on motion {motion} with deviation x:{xoffset},y:{yoffset},z:{zoffset},w:{woffset}");
  220. }
  221. private static string BuildCommand(string motion)
  222. {
  223. if (motion == null)
  224. {
  225. return "GP";
  226. }
  227. else
  228. {
  229. return "GP" + motion;
  230. }
  231. }
  232. private static string BuildParameter(float x, float y, float z, float w)
  233. {
  234. return "*" + _sp + "("+ F2S(x)+ F2S(y)+ F2S(z)+ F2S(w)+ ")";
  235. }
  236. }
  237. public class HirataRobotIISimpleActionHandler : HirataRobotIIHandler
  238. {
  239. public HirataRobotIISimpleActionHandler(HirataRobotII device, string command)
  240. : base(device, command)
  241. {
  242. LOG.Write($"{device.Name} execute the command:{command}");
  243. }
  244. }
  245. public class HirataRobotIIMonitorRobotStatusHandler : HirataRobotIIHandler
  246. {
  247. public HirataRobotIIMonitorRobotStatusHandler(HirataRobotII device)
  248. : base(device, "LS", null)
  249. {
  250. }
  251. }
  252. public class HirataRobotIIReadRobotPositionHandler : HirataRobotIIHandler
  253. {
  254. public HirataRobotIIReadRobotPositionHandler(HirataRobotII device, int address)
  255. : base(device, "LD", F2S(address))
  256. {
  257. }
  258. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  259. {
  260. HirataRobotIIMessage response = msg as HirataRobotIIMessage;
  261. ResponseMessage = msg;
  262. if (!msg.IsResponse)
  263. {
  264. transactionComplete = false;
  265. return false;
  266. }
  267. if (msg.IsError)
  268. {
  269. Device.NoteError(response.Data);
  270. }
  271. else
  272. {
  273. Device.NoteError(null);
  274. Device.NoteRobotPositon(response.Data);
  275. }
  276. if (response.IsComplete)
  277. {
  278. SetState(EnumHandlerState.Completed);
  279. transactionComplete = true;
  280. return true;
  281. }
  282. transactionComplete = false;
  283. return false;
  284. }
  285. }
  286. public class HirataRobotIIMonitorRobotConnectedHandler : HirataRobotIIHandler
  287. {
  288. public HirataRobotIIMonitorRobotConnectedHandler(HirataRobotII device)
  289. : base(device, "CO", null)
  290. {
  291. }
  292. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  293. {
  294. HirataRobotIIMessage response = msg as HirataRobotIIMessage;
  295. ResponseMessage = msg;
  296. if (msg.IsError)
  297. {
  298. Device.NoteError(response.Data);
  299. }
  300. else
  301. {
  302. Device.NoteError(null);
  303. Device.NoteRobotConnected(true);
  304. }
  305. if (response.IsComplete)
  306. {
  307. SetState(EnumHandlerState.Completed);
  308. transactionComplete = true;
  309. return true;
  310. }
  311. transactionComplete = false;
  312. return false;
  313. }
  314. }
  315. public class HirataRobotIIWriteDOHandler : HirataRobotIIHandler
  316. {
  317. public HirataRobotIIWriteDOHandler(HirataRobotII device,int offset,bool value)
  318. : base(device, "SOB", BuildParamter(offset,value))
  319. {
  320. LOG.Write($"{device.Name} write DO {offset} in byte.");
  321. }
  322. private static string BuildParamter(int offset,bool value)
  323. {
  324. return $"{offset}{_sp}{(value ? 1 : 0)}";
  325. }
  326. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  327. {
  328. HirataRobotIIMessage response = msg as HirataRobotIIMessage;
  329. ResponseMessage = msg;
  330. if (!msg.IsResponse)
  331. {
  332. transactionComplete = false;
  333. return false;
  334. }
  335. if (msg.IsError)
  336. {
  337. Device.NoteError(response.Data);
  338. }
  339. else
  340. {
  341. Device.NoteError(null);
  342. Device.NoteRobotConnected(true);
  343. }
  344. if (response.IsComplete)
  345. {
  346. SetState(EnumHandlerState.Completed);
  347. transactionComplete = true;
  348. return true;
  349. }
  350. transactionComplete = false;
  351. return false;
  352. }
  353. }
  354. public class HirataRobotIIReadSingleDIHandler : HirataRobotIIHandler
  355. {
  356. public HirataRobotIIReadSingleDIHandler(HirataRobotII device, int offset)
  357. : base(device, "LIB", BuildParamter(offset))
  358. {
  359. LOG.Write($"{device.Name} read single DI {offset} in byte.");
  360. }
  361. private static string BuildParamter(int offset)
  362. {
  363. return offset.ToString();
  364. }
  365. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  366. {
  367. HirataRobotIIMessage response = msg as HirataRobotIIMessage;
  368. ResponseMessage = msg;
  369. if (!msg.IsResponse)
  370. {
  371. transactionComplete = false;
  372. return false;
  373. }
  374. if (msg.IsError)
  375. {
  376. Device.NoteError(response.Data);
  377. }
  378. else
  379. {
  380. Device.NoteError(null);
  381. Device.NoteRobotConnected(true);
  382. }
  383. if (response.IsComplete)
  384. {
  385. SetState(EnumHandlerState.Completed);
  386. transactionComplete = true;
  387. if (response.Data == "0") Device.DIReadValue = false;
  388. if (response.Data == "1") Device.DIReadValue = true;
  389. return true;
  390. }
  391. transactionComplete = false;
  392. return false;
  393. }
  394. }
  395. public class HirataRobotIIReadDIByteHandler : HirataRobotIIHandler
  396. {
  397. private int _offset;
  398. public HirataRobotIIReadDIByteHandler(HirataRobotII device, int offset)
  399. : base(device, "LID", BuildParamter(offset))
  400. {
  401. LOG.Write($"{device.Name} read DI {offset} in byte.");
  402. _offset = offset;
  403. }
  404. private static string BuildParamter(int offset)
  405. {
  406. return offset.ToString();
  407. }
  408. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  409. {
  410. HirataRobotIIMessage response = msg as HirataRobotIIMessage;
  411. ResponseMessage = msg;
  412. if (!msg.IsResponse)
  413. {
  414. transactionComplete = false;
  415. return false;
  416. }
  417. if (msg.IsError)
  418. {
  419. Device.NoteError(response.Data);
  420. }
  421. else
  422. {
  423. Device.NoteError(null);
  424. Device.NoteRobotConnected(true);
  425. }
  426. if (response.IsComplete)
  427. {
  428. SetState(EnumHandlerState.Completed);
  429. transactionComplete = true;
  430. Device.NoteRobotDIByte(response.Data, _offset);
  431. return true;
  432. }
  433. transactionComplete = false;
  434. return false;
  435. }
  436. }
  437. public class HirataRobotIIReadHandler : HirataRobotIIHandler
  438. {
  439. public HirataRobotIIReadHandler(HirataRobotII device, string command,string parameter)
  440. : base(device, command, parameter)
  441. {
  442. LOG.Write($"{device.Name} read command {command} {parameter}");
  443. }
  444. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  445. {
  446. HirataRobotIIMessage response = msg as HirataRobotIIMessage;
  447. ResponseMessage = msg;
  448. if (!msg.IsResponse)
  449. {
  450. transactionComplete = false;
  451. return false;
  452. }
  453. if (msg.IsError)
  454. {
  455. Device.NoteError(response.Data);
  456. }
  457. else
  458. {
  459. Device.NoteError(null);
  460. Device.NoteRobotConnected(true);
  461. }
  462. if (response.IsComplete)
  463. {
  464. SetState(EnumHandlerState.Completed);
  465. Device.ParseReadData(_command, _parameter, response.Data);
  466. transactionComplete = true;
  467. return true;
  468. }
  469. transactionComplete = false;
  470. return false;
  471. }
  472. }
  473. public class HirataRobotIIWriteHandler : HirataRobotIIHandler
  474. {
  475. public HirataRobotIIWriteHandler(HirataRobotII device, string command, string parameter)
  476. : base(device, command, parameter)
  477. {
  478. LOG.Write($"{device.Name} Write command {command} {parameter}");
  479. }
  480. }
  481. public class HirataRobotIIOperateVacDependsOnWaferHandler : HirataRobotIIHandler
  482. {
  483. public HirataRobotIIOperateVacDependsOnWaferHandler(HirataRobotII device, int offset)
  484. : base(device, "SOB", BuildParamter(offset, device))
  485. {
  486. LOG.Write($"{device.Name} write DO {offset} in byte.");
  487. }
  488. private static string BuildParamter(int offset, HirataRobotII device)
  489. {
  490. bool value = false;
  491. if(offset == 0)
  492. {
  493. value = device.GetWaferState(RobotArmEnum.Lower) == RobotArmWaferStateEnum.Present;
  494. }
  495. if(offset == 1)
  496. {
  497. value = device.GetWaferState(RobotArmEnum.Lower) == RobotArmWaferStateEnum.Absent;
  498. }
  499. if (offset == 2)
  500. {
  501. value = device.GetWaferState(RobotArmEnum.Upper) == RobotArmWaferStateEnum.Present;
  502. }
  503. if (offset == 3)
  504. {
  505. value = device.GetWaferState(RobotArmEnum.Upper) == RobotArmWaferStateEnum.Absent;
  506. }
  507. return $"{offset}{_sp}{(value ? 1 : 0)}";
  508. }
  509. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  510. {
  511. HirataRobotIIMessage response = msg as HirataRobotIIMessage;
  512. ResponseMessage = msg;
  513. if (!msg.IsResponse)
  514. {
  515. transactionComplete = false;
  516. return false;
  517. }
  518. if (msg.IsError)
  519. {
  520. Device.NoteError(response.Data);
  521. }
  522. else
  523. {
  524. Device.NoteError(null);
  525. Device.NoteRobotConnected(true);
  526. }
  527. if (response.IsComplete)
  528. {
  529. SetState(EnumHandlerState.Completed);
  530. transactionComplete = true;
  531. return true;
  532. }
  533. transactionComplete = false;
  534. return false;
  535. }
  536. }
  537. public class HirataRobotIIWaferInforMoveHandler : HirataRobotIIHandler
  538. {
  539. private int _offset;
  540. private bool _isPick;
  541. public HirataRobotIIWaferInforMoveHandler(HirataRobotII device,int offset,bool isPickNoPlace)
  542. : base(device, "LID", BuildParamter(offset))
  543. {
  544. _isPick = isPickNoPlace;
  545. LOG.Write($"{device.Name} read DI {offset} and move wafer infor.");
  546. _offset = 0;
  547. }
  548. private static string BuildParamter(int offset)
  549. {
  550. Thread.Sleep(300);
  551. return offset.ToString();
  552. }
  553. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  554. {
  555. HirataRobotIIMessage response = msg as HirataRobotIIMessage;
  556. ResponseMessage = msg;
  557. if (!msg.IsResponse)
  558. {
  559. transactionComplete = false;
  560. return false;
  561. }
  562. if (msg.IsError)
  563. {
  564. Device.NoteError(response.Data);
  565. }
  566. else
  567. {
  568. Device.NoteError(null);
  569. Device.NoteRobotConnected(true);
  570. }
  571. if (response.IsComplete)
  572. {
  573. SetState(EnumHandlerState.Completed);
  574. transactionComplete = true;
  575. Device.NoteRobotDIByte(response.Data, _offset);
  576. if (_isPick)
  577. Device.MoveWaferInforFromStation();
  578. else
  579. Device.MoveWaferInforToStation();
  580. return true;
  581. }
  582. transactionComplete = false;
  583. return false;
  584. }
  585. }
  586. public class HirataRobotIIReadDIByteForInitHandler : HirataRobotIIHandler
  587. {
  588. private int _offset;
  589. public HirataRobotIIReadDIByteForInitHandler(HirataRobotII device, int offset)
  590. : base(device, "LID", BuildParamter(offset))
  591. {
  592. LOG.Write($"{device.Name} read DI {offset} in byte.");
  593. _offset = offset;
  594. }
  595. private static string BuildParamter(int offset)
  596. {
  597. Thread.Sleep(1000);
  598. return offset.ToString();
  599. }
  600. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  601. {
  602. HirataRobotIIMessage response = msg as HirataRobotIIMessage;
  603. ResponseMessage = msg;
  604. if (!msg.IsResponse)
  605. {
  606. transactionComplete = false;
  607. return false;
  608. }
  609. if (msg.IsError)
  610. {
  611. Device.NoteError(response.Data);
  612. }
  613. else
  614. {
  615. Device.NoteError(null);
  616. Device.NoteRobotConnected(true);
  617. }
  618. if (response.IsComplete)
  619. {
  620. SetState(EnumHandlerState.Completed);
  621. transactionComplete = true;
  622. Device.NoteRobotDIByte(response.Data, _offset);
  623. return true;
  624. }
  625. transactionComplete = false;
  626. return false;
  627. }
  628. }
  629. public class HirataRobotIIWriteMdataOnCurrenPositionHandler : HirataRobotIIHandler
  630. {
  631. public HirataRobotIIWriteMdataOnCurrenPositionHandler(HirataRobotII device, int address, string Mdata)
  632. : base(device, "SD", BuildParameter(device,address,Mdata))
  633. {
  634. }
  635. private static string BuildParameter(HirataRobotII device, int address, string Mdata)
  636. {
  637. return F2S(address) + F2S(device.ReadXPosition) + F2S(device.ReadYPosition) + F2S(device.ReadZPosition) +
  638. F2S(device.ReadWPosition) + device.RobotPosture + $" 0 {Mdata} {device.FCode} {device.SCode}";
  639. }
  640. }
  641. }