GalilSocketSimulator.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.RT.Routine;
  4. using Aitex.Core.Util;
  5. using CyberX8_Core;
  6. using MECF.Framework.Common.CommonData.PUF;
  7. using MECF.Framework.Common.Device.Festo;
  8. using MECF.Framework.Common.Device.Galil;
  9. using MECF.Framework.Common.Net;
  10. using MECF.Framework.Common.Simulator;
  11. using MECF.Framework.Simulator.Core.Driver;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Text;
  16. using Xceed.Wpf.Toolkit.Panels;
  17. namespace CyberX8_Simulator.Devices
  18. {
  19. public class GalilSocketSimulator : SocketDeviceSimulator
  20. {
  21. #region 常量
  22. //最大运动轴数量(abcdefg)
  23. private const int MAX_AXIS_NUM = 8;
  24. //GalilControllerData最大数据长度
  25. private const int MAX_CONTROL_DATA_LENGTH = 264;
  26. //GalilAxisData最大数据长度
  27. private const int MAX_AXIS_DATA_LENGTH = 28;
  28. #endregion
  29. #region 内部变量
  30. private IByteTransform byteTransform = new BigEndianByteTransformBase();
  31. /// <summary>
  32. /// Galil数据
  33. /// </summary>
  34. private GalilControllerData _galilControlData = new GalilControllerData();
  35. /// <summary>
  36. /// Axis名称字典(key:axisName, value:index)
  37. /// </summary>
  38. private Dictionary<string, int> _axisNameIndexDic = new Dictionary<string, int>();
  39. /// <summary>
  40. /// ModuleName
  41. /// </summary>
  42. /// <param name="port"></param>
  43. private string _moduleName;
  44. #endregion
  45. /// <summary>
  46. /// 构造函数
  47. /// </summary>
  48. /// <param name="port"></param>
  49. public GalilSocketSimulator(int port) :base(port)
  50. {
  51. InitData(port);
  52. }
  53. /// <summary>
  54. /// 解析信息
  55. /// </summary>
  56. /// <param name="data"></param>
  57. protected override void ProcessUnsplitMessage(byte[] data)
  58. {
  59. string cmdStr = ASCIIEncoding.ASCII.GetString(data);
  60. if (CheckCmdValid(cmdStr))
  61. {
  62. var cmdOperation = AnalyseCommand(cmdStr);
  63. byte[] response;
  64. if(cmdOperation.Item1 == "QR")
  65. {
  66. //Read
  67. response = CreateReadResponse(_galilControlData);
  68. }
  69. else
  70. {
  71. //Write
  72. SetOperation(cmdOperation.Item1, cmdOperation.Item2, cmdOperation.Item3);
  73. response = CreateWriteResponse();
  74. }
  75. OnWriteMessage(response);
  76. }
  77. else
  78. {
  79. OnWriteMessage(CreateError());
  80. }
  81. }
  82. /// <summary>
  83. /// 读回复
  84. /// </summary>
  85. /// <param name="flag"></param>
  86. /// <param name="channel"></param>
  87. /// <param name="command"></param>
  88. /// <param name="registerCount"></param>
  89. /// <param name="values"></param>
  90. /// <returns></returns>
  91. private byte[] CreateReadResponse(GalilControllerData data)
  92. {
  93. //数据头(4 bytes)
  94. int headLength = 4;
  95. byte[] result = new byte[headLength + MAX_CONTROL_DATA_LENGTH];
  96. result[0] = 0x00;
  97. result[1] = 0x00;
  98. short dataLength = (short)(headLength + MAX_CONTROL_DATA_LENGTH);
  99. Array.Copy(byteTransform.GetBytes(dataLength), 0, result, 2, 2);
  100. //数据体(MAX_CONTROL_DATA_LENGTH bytes)
  101. Array.Copy(CodeControlData(data), 0, result, 4, MAX_CONTROL_DATA_LENGTH);
  102. return result;
  103. }
  104. /// <summary>
  105. /// 写回复
  106. /// </summary>
  107. /// <param name="flag"></param>
  108. /// <param name="channel"></param>
  109. /// <param name="command"></param>
  110. /// <param name="startAddress"></param>
  111. /// <param name="value"></param>
  112. /// <returns></returns>
  113. private byte[] CreateWriteResponse()
  114. {
  115. //数据头(1 bytes)
  116. int headLength = 1;
  117. byte[] result = new byte[headLength];
  118. result[0] = 0x3A;
  119. return result;
  120. }
  121. /// <summary>
  122. /// 错误回复
  123. /// </summary>
  124. /// <param name="flag"></param>
  125. /// <param name="channel"></param>
  126. /// <param name="command"></param>
  127. /// <param name="error"></param>
  128. /// <returns></returns>
  129. private byte[] CreateError()
  130. {
  131. int headLength = 1;
  132. byte[] result = new byte[headLength];
  133. //Command not valid in program
  134. result[0] = 0x03;
  135. return result;
  136. }
  137. /// <summary>
  138. /// 初始化
  139. /// </summary>
  140. private void InitData(int port)
  141. {
  142. //初始化AxisData(最大MAX_AXIS_NUM个axis)
  143. _galilControlData.GalilAxisDatas = new List<GalilAxisData>();
  144. for (int i = 0; i < MAX_AXIS_NUM; i++)
  145. {
  146. _galilControlData.GalilAxisDatas.Add(new GalilAxisData());
  147. _galilControlData.GalilAxisDatas[i].Status = 0x01;
  148. }
  149. _galilControlData.Inputs = new byte[10];
  150. _galilControlData.Outputs = new byte[10];
  151. _galilControlData.SBlocks = new byte[8];
  152. _galilControlData.TBlocks = new byte[8];
  153. //电机数据更新事件
  154. MotorSimulator.Instance.OnUpdateVariableValueChanged += UpdataRealTimeMotionData;
  155. //加载对应配置文件 GalilControllerCfg-Simulator.xml
  156. string oldXmlPath = PathManager.GetCfgDir();
  157. string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Devices\\GalilControllerCfg-Simulator.xml";
  158. GalilControllerCfg cfg = CustomXmlSerializer.Deserialize<GalilControllerCfg>(new FileInfo(newXmlPath));
  159. foreach (GalilDeviceConfig config in cfg.GalilDeviceConfigs)
  160. {
  161. if (port == config.Port)
  162. {
  163. _moduleName = config.Module;
  164. foreach (GalilAxisConfig item in config.GalilAxises)
  165. {
  166. _axisNameIndexDic[$"{config.Module}.{item.Name}"] = item.Index;
  167. }
  168. }
  169. }
  170. }
  171. /// <summary>
  172. /// GalilControllerData编码
  173. /// </summary>
  174. /// <returns></returns>
  175. private byte[] CodeControlData(GalilControllerData ctrlData)
  176. {
  177. byte[] result = new byte[MAX_CONTROL_DATA_LENGTH];
  178. int index = 0;
  179. //Sample(2 bytes)
  180. Array.Copy(byteTransform.GetBytes(ctrlData.Sample), 0, result, index, 2);
  181. index += 2;
  182. //Inputs(1*10 bytes)
  183. Array.Copy(ctrlData.Inputs, 0, result, index, 10);
  184. index += 10;
  185. //Outputs(1*10 bytes)
  186. Array.Copy(ctrlData.Outputs, 0, result, index, 10);
  187. index += 10;
  188. //Error Code(1 bytes)
  189. result[13] = ctrlData.ErrorCode;
  190. index ++;
  191. //General Status(1 bytes)
  192. result[14] = ctrlData.Status;
  193. index ++;
  194. //S Block(2+2+4=8 bytes)
  195. Array.Copy(ctrlData.SBlocks, 0, result, 15, 8);
  196. index += 8;
  197. //T Block(2+2+4=8 bytes)
  198. Array.Copy(ctrlData.TBlocks, 0, result, 23, 8);
  199. index += 8;
  200. //Axis Datas(28 * 8 bytes)
  201. for(int i = 0;i < MAX_AXIS_NUM; i++)
  202. {
  203. Array.Copy(CodeAxisData(ctrlData.GalilAxisDatas[i]), 0, result, index, MAX_AXIS_DATA_LENGTH);
  204. index += MAX_AXIS_DATA_LENGTH;
  205. }
  206. return result;
  207. }
  208. /// <summary>
  209. /// GalilAxisData编码
  210. /// </summary>
  211. /// <param name="axisData"></param>
  212. /// <returns></returns>
  213. private byte[] CodeAxisData(GalilAxisData axisData)
  214. {
  215. byte[] result = new byte[MAX_AXIS_DATA_LENGTH];
  216. int index = 0;
  217. //Status(2 bytes)
  218. Array.Copy(byteTransform.GetBytes(axisData.Status), 0, result, index, 2);
  219. index += 2;
  220. //Switches(1 bytes)
  221. result[index] = axisData.Switches;
  222. index ++;
  223. //Stop Code(1 bytes)
  224. result[index] = axisData.StopCode;
  225. index ++;
  226. //Reference Position(4 bytes)
  227. Array.Copy(byteTransform.GetBytes(axisData.ReferencePosition), 0, result, index, 4);
  228. index += 4;
  229. //Motor Position(4 bytes)
  230. Array.Copy(byteTransform.GetBytes(axisData.MotorPosition), 0, result, index, 4);
  231. index += 4;
  232. //Position Error
  233. Array.Copy(byteTransform.GetBytes(axisData.PositionError), 0, result, index, 4);
  234. index += 4;
  235. //Auxiliary Position
  236. Array.Copy(byteTransform.GetBytes(axisData.AuxiliaryPosition), 0, result, index, 4);
  237. index += 4;
  238. //Velocity
  239. Array.Copy(byteTransform.GetBytes(axisData.Velocity), 0, result, index, 4);
  240. index += 4;
  241. //Torque
  242. Array.Copy(byteTransform.GetBytes(axisData.Torque), 0, result, index, 2);
  243. index += 2;
  244. //Analog
  245. Array.Copy(byteTransform.GetBytes(axisData.Res), 0, result, index, 2);
  246. return result;
  247. }
  248. /// <summary>
  249. /// 解析指令(操作符, 运动轴, 操作数)
  250. /// </summary>
  251. /// <param name="cmd"></param>
  252. /// <returns></returns>
  253. private (string, char, int) AnalyseCommand(string cmdStr)
  254. {
  255. var result = ("", ' ', -1);
  256. //操作符
  257. result.Item1 = cmdStr.Substring(0, 2);
  258. //运动轴
  259. if (cmdStr.Length >= 4)
  260. {
  261. result.Item2 = Convert.ToChar(cmdStr.Substring(2, 1));
  262. }
  263. //操作数
  264. if (cmdStr.Length >= 5 && int.TryParse(cmdStr.Substring(4, cmdStr.Length - 5), out int tmp))
  265. {
  266. result.Item3 = tmp;
  267. }
  268. return result;
  269. }
  270. /// <summary>
  271. /// CMD校验
  272. /// </summary>
  273. /// <returns></returns>
  274. private bool CheckCmdValid(string cmdStr)
  275. {
  276. //长度
  277. if(cmdStr.Length < 3) return false;
  278. //;结尾
  279. if (!cmdStr.EndsWith(";")) return false;
  280. //第1位为A~Z
  281. if (cmdStr[0] < 'A' || cmdStr[0] > 'Z') return false;
  282. //第2位为A~Z
  283. if (cmdStr[1] < 'A' || cmdStr[1] > 'Z') return false;
  284. if (cmdStr.Length >= 4)
  285. {
  286. //axis名称为A~H
  287. if (cmdStr[2] < 'A' || cmdStr[2] > 'H') return false;
  288. //=
  289. if(cmdStr.Length > 4 && !cmdStr.Contains("=")) return false;
  290. }
  291. return true;
  292. }
  293. /// <summary>
  294. /// 设置操作
  295. /// </summary>
  296. /// <param name="cmd"></param>
  297. /// <param name="axis"></param>
  298. /// <param name="value"></param>
  299. /// <returns></returns>
  300. private bool SetOperation(string cmd, char axis, int value)
  301. {
  302. switch (cmd)
  303. {
  304. case "SH":
  305. SwitchMotor(axis, true);
  306. break;
  307. case "MO":
  308. SwitchMotor(axis, false);
  309. break;
  310. case "PR":
  311. SetTargetRelativePosition(axis, value);
  312. break;
  313. case "PA":
  314. SetTargetAbsolutePosition(axis, value);
  315. break;
  316. case "SP":
  317. break;
  318. case "AC":
  319. break;
  320. case "DC":
  321. break;
  322. case "ST":
  323. break;
  324. case "BG":
  325. break;
  326. case "HM":
  327. break;
  328. case "DP":
  329. break;
  330. case "DE":
  331. break;
  332. default:
  333. break;
  334. }
  335. return true;
  336. }
  337. /// <summary>
  338. /// 实时更新电机数据
  339. /// </summary>
  340. /// <param name="datasDic"></param>
  341. private void UpdataRealTimeMotionData(Dictionary<string, CommandMotionData> datasDic)
  342. {
  343. foreach(var dataItem in datasDic)
  344. {
  345. if (_axisNameIndexDic.ContainsKey(dataItem.Key))
  346. {
  347. UpdateAxisData(_axisNameIndexDic[dataItem.Key], dataItem.Value);
  348. }
  349. }
  350. }
  351. /// <summary>
  352. /// 更新对应Axis数据
  353. /// </summary>
  354. /// <param name="index"></param>
  355. private void UpdateAxisData(int index, CommandMotionData data)
  356. {
  357. _galilControlData.GalilAxisDatas[index].Velocity = (int)data.ActualVelocity;
  358. _galilControlData.GalilAxisDatas[index].MotorPosition = (int)data.MotorPosition;
  359. _galilControlData.GalilAxisDatas[index].ReferencePosition = (int)data.TargetPosition;
  360. _galilControlData.GalilAxisDatas[index].Torque = (short)data.ActualTorque;
  361. }
  362. #region AxisOperation
  363. /// <summary>
  364. /// 开/关电机
  365. /// </summary>
  366. /// <param name="axis"></param>
  367. /// <param name="flag"></param>
  368. private void SwitchMotor(char axis, bool flag)
  369. {
  370. byte[] data = byteTransform.GetBytes(_galilControlData.GalilAxisDatas[axis - 'A'].Status);
  371. data[0] &= 0xFE;
  372. data[0] |= (byte)(flag ? 0x00 : 0x01);
  373. _galilControlData.GalilAxisDatas[axis - 'A'].Status = byteTransform.TransUInt16(data, 0);
  374. }
  375. /// <summary>
  376. /// 设置目标位置
  377. /// </summary>
  378. private void SetSpeed(char axis, int value)
  379. {
  380. _galilControlData.GalilAxisDatas[axis - 'A'].Velocity = value;
  381. }
  382. /// <summary>
  383. /// 设置目标绝对位置
  384. /// </summary>
  385. private void SetTargetAbsolutePosition(char axis, int value)
  386. {
  387. _galilControlData.GalilAxisDatas[axis - 'A'].ReferencePosition = value;
  388. }
  389. /// <summary>
  390. /// 设置目标相对位置
  391. /// </summary>
  392. private void SetTargetRelativePosition(char axis, int value)
  393. {
  394. _galilControlData.GalilAxisDatas[axis - 'A'].ReferencePosition += value;
  395. }
  396. #endregion
  397. }
  398. }