LzMatch_Ethercat.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using Aitex.Core.Backend;
  2. using Aitex.Core.Common.DeviceData;
  3. using Aitex.Core.RT.DataCenter;
  4. using Aitex.Core.RT.Log;
  5. using Aitex.Core.RT.OperationCenter;
  6. using MECF.Framework.Common.Device.Bases;
  7. using MECF.Framework.Common.Equipment;
  8. using System;
  9. using System.Diagnostics;
  10. using System.Windows;
  11. using Venus_Core;
  12. using Venus_RT.Devices.AdLinkEthercat;
  13. namespace Venus_RT.Devices
  14. {
  15. public class LzMatch_Ethercat : RfMatchBase
  16. {
  17. //Int32 v_card_name = 0;
  18. Int32 v_board_id = -1;
  19. //Int32 v_StartAxisID = 0;
  20. Int32 BUS_No = 0;
  21. Int32 Mod_ID = 1;
  22. private readonly VenusDevice _device;
  23. private Stopwatch _stopWatch = new Stopwatch();
  24. private string WorkMode { get; set; } = "";
  25. public LzMatch_Ethercat(ModuleName mod, VenusDevice venusDevice, Int32 board_id) : base(mod.ToString(), venusDevice.ToString())
  26. {
  27. _device = venusDevice;
  28. v_board_id = board_id;
  29. _stopWatch.Start();
  30. }
  31. public override bool Initialize()
  32. {
  33. base.Initialize();
  34. DATA.Subscribe($"{Module}.{Name}.C1", () => TunePosition1);
  35. DATA.Subscribe($"{Module}.{Name}.C2", () => TunePosition2);
  36. DATA.Subscribe($"{Module}.{Name}.WorkMode", () => WorkMode);
  37. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchPosition}", (out string reason, int time, object[] param) =>
  38. {
  39. SetMatchPosition((float)Convert.ToDouble(param[0]), (float)Convert.ToDouble(param[1]), out reason);
  40. return true;
  41. });
  42. OP.Subscribe($"{Module}.{Name}.{AITRfOperation.SetMatchProcessMode}", (out string reason, int time, object[] param) =>
  43. {
  44. SetMatchMode((string)param[0] == "Auto" ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual, out reason);
  45. return true;
  46. });
  47. return true;
  48. }
  49. public override void Monitor()
  50. {
  51. if (_stopWatch.ElapsedMilliseconds > 500)
  52. {
  53. TunePosition1 = GetPositionC1();
  54. TunePosition2 = GetPositionC2();
  55. WorkMode = GetWorkMode();
  56. _stopWatch.Restart();
  57. }
  58. }
  59. public override void SetMatchPosition(float c1, float c2, out string reason)
  60. {
  61. base.SetMatchPosition(c1, c2, out reason);
  62. executeMatchPostion(c1, c2);
  63. reason = "";
  64. }
  65. private void executeMatchPostion(float c1, float c2)
  66. {
  67. SetWorkMode(EnumRfMatchTuneMode.Manual);
  68. SetPosition(c1, c2);
  69. }
  70. private void SetPosition(float c1val, float c2val)
  71. {
  72. SetPositionC1(c1val);
  73. SetPositionC2(c2val);
  74. }
  75. private void SetPositionC1(float c1val)
  76. {
  77. Int32 ret = 0;
  78. Byte[] Data = new Byte[32];
  79. UInt16 OD_index = 0x8000;
  80. UInt16 OD_Subindex = 3;
  81. UInt32 DataLength = 32;
  82. UInt32 TimeOut = 10000;
  83. UInt32 Flags = 0;
  84. Data = BitConverter.GetBytes(c1val);
  85. ret = APS168.APS_set_field_bus_sdo(v_board_id, BUS_No, Mod_ID, OD_index, OD_Subindex, Data, DataLength, TimeOut, Flags);
  86. if (ret != 0)
  87. {
  88. LOG.Write(eEvent.ERR_RF, Module, $"{_device} Set C1 {c1val} Fail!");
  89. }
  90. else
  91. {
  92. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"{_device} Set C1 {c1val} Success!");
  93. }
  94. }
  95. private void SetPositionC2(float c1va2)
  96. {
  97. Int32 ret = 0;
  98. Byte[] Data = new Byte[32];
  99. UInt16 OD_index = 0x8000;
  100. UInt16 OD_Subindex = 2;
  101. UInt32 DataLength = 32;
  102. UInt32 TimeOut = 10000;
  103. UInt32 Flags = 0;
  104. Data = BitConverter.GetBytes(c1va2);
  105. ret = APS168.APS_set_field_bus_sdo(v_board_id, BUS_No, Mod_ID, OD_index, OD_Subindex, Data, DataLength, TimeOut, Flags);
  106. if (ret != 0)
  107. {
  108. LOG.Write(eEvent.ERR_RF, Module, $"{_device} Set C2 {c1va2} Fail!");
  109. }
  110. else
  111. {
  112. LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"{_device} Set C2 {c1va2} Success!");
  113. }
  114. }
  115. private float GetPositionC1()
  116. {
  117. Int32 ret;
  118. Int32 BUS_No = 0;
  119. UInt16 ByteOffset = 22;
  120. UInt16 Size = 4;
  121. uint value = 0;
  122. ret = APS168.APS_get_field_bus_pdo(v_board_id, BUS_No, ByteOffset, Size, ref value);
  123. float f;
  124. if (ret != 0)
  125. {
  126. f = -1;
  127. LOG.Write(eEvent.ERR_MATCH, Module, $"{_device} Get C1 Value Fail");
  128. }
  129. else
  130. {
  131. f = BitConverter.ToSingle(BitConverter.GetBytes(value), 0);
  132. //LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"{_device} Get C1 Value Success");
  133. }
  134. return f;
  135. }
  136. private float GetPositionC2()
  137. {
  138. Int32 ret;
  139. Int32 BUS_No = 0;
  140. UInt16 ByteOffset = 18;
  141. UInt16 Size = 4;
  142. uint value = 0;
  143. ret = APS168.APS_get_field_bus_pdo(v_board_id, BUS_No, ByteOffset, Size, ref value);
  144. float f;
  145. if (ret != 0)
  146. {
  147. f = -1;
  148. LOG.Write(eEvent.ERR_MATCH, Module, $"{_device} Get C2 Value Fail");
  149. }
  150. else
  151. {
  152. f = BitConverter.ToSingle(BitConverter.GetBytes(value), 0);
  153. //LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"{_device} Get C2 Value Success");
  154. }
  155. return f;
  156. }
  157. public override bool SetMatchMode(EnumRfMatchTuneMode enumRfMatchTuneMode, out string reason)
  158. {
  159. reason = string.Empty;
  160. SetWorkMode(enumRfMatchTuneMode);
  161. return true;
  162. }
  163. private void SetWorkMode(EnumRfMatchTuneMode mode)
  164. {
  165. Int32 ret = 0;
  166. Byte[] Data = new Byte[32];
  167. UInt16 OD_index = 0x8000;
  168. UInt16 OD_Subindex = 1;
  169. UInt32 DataLength = 1;
  170. UInt32 TimeOut = 10000;
  171. Int32 Datatemp;
  172. UInt32 Flags = 0;
  173. if (mode == EnumRfMatchTuneMode.Auto)
  174. {
  175. Datatemp = 0;
  176. }
  177. else
  178. {
  179. Datatemp = 1;
  180. }
  181. for (Int32 i = 0; i < 8; i++)
  182. {
  183. Data[i] = (Byte)((Datatemp >> (i * 8)) & 0xFF);
  184. }
  185. ret = APS168.APS_set_field_bus_sdo(v_board_id, BUS_No, Mod_ID, OD_index, OD_Subindex, Data, DataLength, TimeOut, Flags);
  186. if (ret != 0)
  187. {
  188. LOG.Write(eEvent.ERR_MATCH, Module, $"{_device} set work mode {mode} Fail!");
  189. }
  190. else
  191. {
  192. //LOG.Write(eEvent.EV_DEVICE_INFO, Module, $"{_device} set work mode {mode} Success!");
  193. }
  194. }
  195. private string GetWorkMode()
  196. {
  197. Int32 ret = 0;
  198. Byte[] Data = new Byte[32];
  199. UInt16 OD_index = 0x8000;
  200. UInt16 OD_Subindex = 1;
  201. UInt32 DataLength = 1;
  202. UInt32 TimeOut = 10000;
  203. UInt32 OutDataLength = 0;
  204. UInt32 Flags = 0;
  205. ret = APS168.APS_get_field_bus_sdo(v_board_id, BUS_No, Mod_ID, OD_index, OD_Subindex, Data, DataLength, ref OutDataLength, TimeOut, Flags);
  206. if (ret != 0)
  207. {
  208. LOG.Write(eEvent.ERR_MATCH, Module, $"{_device} Get Work Mode Fail");
  209. }
  210. else
  211. {
  212. if (Data[0] == 0)
  213. {
  214. return "Auto";
  215. }
  216. else if (Data[0] == 1)
  217. {
  218. return "Manual";
  219. }
  220. }
  221. return "";
  222. }
  223. }
  224. }