HirataR4RobotHandler.cs 22 KB

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