GalilSocketSimulator.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.Beckhoff.AxisProvider;
  5. using MECF.Framework.Common.Device.Galil;
  6. using MECF.Framework.Common.Net;
  7. using MECF.Framework.Common.Simulator;
  8. using MECF.Framework.Simulator.Core.Driver;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Text;
  13. namespace CyberX8_Simulator.Devices
  14. {
  15. public class GalilSocketSimulator : SocketDeviceSimulator
  16. {
  17. #region 常量
  18. //最大运动轴数量(abcdefg)
  19. private const int MAX_AXIS_NUM = 8;
  20. //GalilControllerData最大数据长度
  21. private const int MAX_CONTROL_DATA_LENGTH_21 = 264;
  22. private const int MAX_CONTROL_DATA_LENGTH_40 = 366;
  23. //GalilAxisData最大数据长度
  24. private const int MAX_AXIS_DATA_LENGTH_21 = 28;
  25. private const int MAX_AXIS_DATA_LENGTH_40 = 36;
  26. //GalilType
  27. private const string GALIL_21 = "Galil21";
  28. private const string GALIL_40 = "Galil40";
  29. private const string TARGET_VELOCITY = "TargetVelocity";
  30. private const string TARGET_ACCEL = "TargetAcceleration";
  31. private const string TARGET_DECEL = "TargetDeceleration";
  32. private const string TARGET_POSITION = "TargetPosition";
  33. private const string SWITCH_SIGNAL = "SwitchSignal";
  34. private const string ACTUAL_POSITION = "ActualPosition";
  35. private const string AUXILIARY_POSITION = "AuxiliaryPosition";
  36. private const string HOMING_SIGNAL = "HomingSignal";
  37. private const string MOTION_SIGNAL = "MotionSignal";
  38. private const string STOP_SIGNAL = "StopSignal";
  39. #endregion
  40. #region 内部变量
  41. private IByteTransform byteTransform = new BigEndianByteTransformBase();
  42. /// <summary>
  43. /// Galil数据
  44. /// </summary>
  45. private GalilControllerData _galilControlData = new GalilControllerData();
  46. /// <summary>
  47. /// Axis名称字典(key:axisName, value:index)
  48. /// </summary>
  49. private Dictionary<string, int> _axisNameIndexDic = new Dictionary<string, int>();
  50. /// <summary>
  51. /// Axis名称列表(index - axisName)
  52. /// </summary>
  53. private string[] _nameAxisList = new string[MAX_AXIS_NUM];
  54. /// <summary>
  55. /// ModuleName
  56. /// </summary>
  57. /// <param name="port"></param>
  58. private string _moduleName;
  59. /// <summary>
  60. /// Galil Type
  61. /// </summary>
  62. private string _galilType;
  63. /// <summary>
  64. /// GalilControllerData数据长度
  65. /// </summary>
  66. private int _controlDataLength;
  67. /// <summary>
  68. /// GalilAxisDatas数据长度
  69. /// </summary>
  70. private int _axisDataLength;
  71. /// <summary>
  72. /// MotorPosition比例系数字典(index - motorPositionRate)
  73. /// </summary>
  74. private Dictionary<string, double> _motorPositionRateDic = new Dictionary<string, double>();
  75. /// <summary>
  76. /// Key:dataName - Value:GalilDI
  77. /// </summary>
  78. public Dictionary<string, GalilDI> InputDataNameDIDic = new Dictionary<string, GalilDI>();
  79. #endregion
  80. /// <summary>
  81. /// 构造函数
  82. /// </summary>
  83. /// <param name="port"></param>
  84. public GalilSocketSimulator(int port) :base(port)
  85. {
  86. InitData(port);
  87. }
  88. /// <summary>
  89. /// 解析信息
  90. /// </summary>
  91. /// <param name="data"></param>
  92. protected override void ProcessUnsplitMessage(byte[] data)
  93. {
  94. string cmdStr = ASCIIEncoding.ASCII.GetString(data);
  95. if (CheckCmdValid(cmdStr))
  96. {
  97. var cmdOperation = AnalyseCommand(cmdStr);
  98. byte[] response;
  99. if(cmdOperation.Item1 == "QR")
  100. {
  101. //Read
  102. response = CreateReadResponse(_galilControlData);
  103. }
  104. else
  105. {
  106. //Write
  107. SetOperation(cmdOperation.Item1, cmdOperation.Item2, cmdOperation.Item3);
  108. response = CreateWriteResponse();
  109. }
  110. OnWriteMessage(response);
  111. }
  112. else
  113. {
  114. OnWriteMessage(CreateError());
  115. }
  116. }
  117. /// <summary>
  118. /// 读回复
  119. /// </summary>
  120. /// <param name="flag"></param>
  121. /// <param name="channel"></param>
  122. /// <param name="command"></param>
  123. /// <param name="registerCount"></param>
  124. /// <param name="values"></param>
  125. /// <returns></returns>
  126. private byte[] CreateReadResponse(GalilControllerData data)
  127. {
  128. //数据头(4 bytes)
  129. int headLength = 4;
  130. byte[] result = new byte[headLength + _controlDataLength];
  131. result[0] = 0x00;
  132. result[1] = 0x00;
  133. short dataLength = (short)(headLength + _controlDataLength);
  134. Array.Copy(byteTransform.GetBytes(dataLength), 0, result, 2, 2);
  135. //数据体(MAX_CONTROL_DATA_LENGTH bytes)
  136. switch (_galilType)
  137. {
  138. case GALIL_21:
  139. Array.Copy(CodeControlData21(data), 0, result, 4, MAX_CONTROL_DATA_LENGTH_21);
  140. break;
  141. case GALIL_40:
  142. Array.Copy(CodeControlData40(data), 0, result, 4, MAX_CONTROL_DATA_LENGTH_40);
  143. break;
  144. default:
  145. break;
  146. }
  147. return result;
  148. }
  149. /// <summary>
  150. /// 写回复
  151. /// </summary>
  152. /// <param name="flag"></param>
  153. /// <param name="channel"></param>
  154. /// <param name="command"></param>
  155. /// <param name="startAddress"></param>
  156. /// <param name="value"></param>
  157. /// <returns></returns>
  158. private byte[] CreateWriteResponse()
  159. {
  160. //数据头(1 bytes)
  161. int headLength = 1;
  162. byte[] result = new byte[headLength];
  163. result[0] = 0x3A;
  164. return result;
  165. }
  166. /// <summary>
  167. /// 错误回复
  168. /// </summary>
  169. /// <param name="flag"></param>
  170. /// <param name="channel"></param>
  171. /// <param name="command"></param>
  172. /// <param name="error"></param>
  173. /// <returns></returns>
  174. private byte[] CreateError()
  175. {
  176. int headLength = 1;
  177. byte[] result = new byte[headLength];
  178. //Command not valid in program
  179. result[0] = 0x03;
  180. return result;
  181. }
  182. /// <summary>
  183. /// 初始化
  184. /// </summary>
  185. private void InitData(int port)
  186. {
  187. //初始化AxisData(最大MAX_AXIS_NUM个axis)
  188. _galilControlData.GalilAxisDatas = new List<GalilAxisData>();
  189. for (int i = 0; i < MAX_AXIS_NUM; i++)
  190. {
  191. _galilControlData.GalilAxisDatas.Add(new GalilAxisData());
  192. _galilControlData.GalilAxisDatas[i].Status = 0x00;
  193. }
  194. _galilControlData.Inputs = new byte[10];
  195. _galilControlData.Outputs = new byte[10];
  196. //电机数据更新事件
  197. MotorSimulator.Instance.OnUpdateMotionDatasChanged += UpdateRealTimeMotionData;
  198. MotorSimulator.Instance.OnUpdateInputDatasChanged += UpdateInputData;
  199. //加载GalilControllerCfg-Simulator.xml
  200. try
  201. {
  202. string oldXmlPath = PathManager.GetCfgDir();
  203. string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Devices\\GalilControllerCfg-Simulator.xml";
  204. GalilControllerCfg cfg = CustomXmlSerializer.Deserialize<GalilControllerCfg>(new FileInfo(newXmlPath));
  205. if(cfg != null)
  206. {
  207. foreach (GalilDeviceConfig config in cfg.GalilDeviceConfigs)
  208. {
  209. if (port == config.Port)
  210. {
  211. _moduleName = config.Module;
  212. _galilType = config.GalilType;
  213. foreach (GalilAxisConfig item in config.GalilAxises)
  214. {
  215. _axisNameIndexDic[$"{config.Module}.{item.Name}"] = item.Index;
  216. _nameAxisList[item.Index] = $"{config.Module}.{item.Name}";
  217. }
  218. if (config.GalilDigIn != null && config.GalilDigIn.DIs.Count != 0)
  219. {
  220. foreach (var items in config.GalilDigIn.DIs)
  221. {
  222. InputDataNameDIDic[items.Name] = items;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. catch
  230. {
  231. LOG.WriteLog(eEvent.ERR_GALIL, "Galil", "Load galil GalilControllerCfg-Simulator.xml failed");
  232. }
  233. //加载AxisProviderCfg.xml
  234. try
  235. {
  236. string oldXmlPath = PathManager.GetCfgDir();
  237. string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Devices\\AxisProviderCfg.xml";
  238. BeckhoffAxisProviderCfg axisProviderCfg = CustomXmlSerializer.Deserialize<BeckhoffAxisProviderCfg>(new FileInfo(newXmlPath));
  239. if (axisProviderCfg != null)
  240. {
  241. foreach (BeckhoffProviderAxis item in axisProviderCfg.Axes)
  242. {
  243. for (int i = 0; i < MAX_AXIS_NUM; i++)
  244. {
  245. if (_nameAxisList[i] == item.Name)
  246. {
  247. _motorPositionRateDic[item.Name] = item.MotorPositionRate;
  248. }
  249. }
  250. }
  251. }
  252. }
  253. catch
  254. {
  255. LOG.WriteLog(eEvent.ERR_AXIS, "axisProvider", "Load AxisProviderCfg.xml failed");
  256. }
  257. //初始化对应Galiltype数据
  258. if (_galilType == GALIL_40)
  259. {
  260. _controlDataLength = MAX_CONTROL_DATA_LENGTH_40;
  261. _axisDataLength = MAX_AXIS_DATA_LENGTH_40;
  262. _galilControlData.SBlocks = new byte[10];
  263. _galilControlData.TBlocks = new byte[10];
  264. }
  265. else
  266. {
  267. _controlDataLength = MAX_CONTROL_DATA_LENGTH_21;
  268. _axisDataLength = MAX_AXIS_DATA_LENGTH_21;
  269. _galilControlData.SBlocks = new byte[8];
  270. _galilControlData.TBlocks = new byte[8];
  271. }
  272. }
  273. #region Galil编码
  274. /// <summary>
  275. /// GalilControllerData编码(Galil21)
  276. /// </summary>
  277. /// <returns></returns>
  278. private byte[] CodeControlData21(GalilControllerData ctrlData)
  279. {
  280. byte[] result = new byte[MAX_CONTROL_DATA_LENGTH_21];
  281. int index = 0;
  282. //Sample(2 bytes)
  283. Array.Copy(BitConverter.GetBytes(ctrlData.Sample), 0, result, index, 2);
  284. index += 2;
  285. //Inputs(1*10 bytes)
  286. Array.Copy(ctrlData.Inputs, 0, result, index, 10);
  287. index += 10;
  288. //Outputs(1*10 bytes)
  289. Array.Copy(ctrlData.Outputs, 0, result, index, 10);
  290. index += 10;
  291. //Error Code(1 bytes)
  292. result[13] = ctrlData.ErrorCode;
  293. index++;
  294. //General Status(1 bytes)
  295. result[14] = ctrlData.Status;
  296. index++;
  297. //S Block(2+2+4=8 bytes)
  298. Array.Copy(ctrlData.SBlocks, 0, result, 15, 8);
  299. index += 8;
  300. //T Block(2+2+4=8 bytes)
  301. Array.Copy(ctrlData.TBlocks, 0, result, 23, 8);
  302. index += 8;
  303. //Axis Datas(28 * 8 bytes)
  304. for (int i = 0; i < MAX_AXIS_NUM; i++)
  305. {
  306. Array.Copy(CodeAxisData21(ctrlData.GalilAxisDatas[i]), 0, result, index, MAX_AXIS_DATA_LENGTH_21);
  307. index += MAX_AXIS_DATA_LENGTH_21;
  308. }
  309. return result;
  310. }
  311. /// <summary>
  312. /// GalilAxisData编码(Galil21)
  313. /// </summary>
  314. /// <param name="axisData"></param>
  315. /// <returns></returns>
  316. private byte[] CodeAxisData21(GalilAxisData axisData)
  317. {
  318. byte[] result = new byte[MAX_AXIS_DATA_LENGTH_21];
  319. int index = 0;
  320. //Status(2 bytes)
  321. Array.Copy(byteTransform.GetBytes(axisData.Status), 0, result, index, 2);
  322. index += 2;
  323. //Switches(1 bytes)
  324. result[index] = axisData.Switches;
  325. index++;
  326. //Stop Code(1 bytes)
  327. result[index] = axisData.StopCode;
  328. index++;
  329. //Reference Position(4 bytes)
  330. Array.Copy(BitConverter.GetBytes(axisData.ReferencePosition), 0, result, index, 4);
  331. index += 4;
  332. //Motor Position(4 bytes)
  333. Array.Copy(BitConverter.GetBytes(axisData.MotorPosition), 0, result, index, 4);
  334. index += 4;
  335. //Position Error
  336. Array.Copy(BitConverter.GetBytes(axisData.PositionError), 0, result, index, 4);
  337. index += 4;
  338. //Auxiliary Position
  339. Array.Copy(BitConverter.GetBytes(axisData.AuxiliaryPosition), 0, result, index, 4);
  340. index += 4;
  341. //Velocity
  342. Array.Copy(BitConverter.GetBytes(axisData.Velocity), 0, result, index, 4);
  343. index += 4;
  344. //Torque
  345. Array.Copy(BitConverter.GetBytes(axisData.Torque), 0, result, index, 2);
  346. index += 2;
  347. //Analog
  348. Array.Copy(BitConverter.GetBytes(axisData.Res), 0, result, index, 2);
  349. return result;
  350. }
  351. /// <summary>
  352. /// GalilControllerData编码(Galil40)
  353. /// </summary>
  354. /// <returns></returns>
  355. private byte[] CodeControlData40(GalilControllerData ctrlData)
  356. {
  357. byte[] result = new byte[MAX_CONTROL_DATA_LENGTH_40];
  358. int index = 0;
  359. //Sample(2 bytes)
  360. Array.Copy(BitConverter.GetBytes(ctrlData.Sample), 0, result, index, 2);
  361. index += 2;
  362. //Inputs(1*10 bytes)
  363. Array.Copy(ctrlData.Inputs, 0, result, index, 10);
  364. index += 10;
  365. //Outputs(1*10 bytes)
  366. Array.Copy(ctrlData.Outputs, 0, result, index, 10);
  367. index += 10;
  368. //reservers
  369. byte[] reservers = new byte[16];
  370. Array.Copy(reservers, 0, result, index, 16);
  371. index += 16;
  372. //ethernet
  373. byte[] ethernet = new byte[8];
  374. Array.Copy(ethernet, 0, result, index, 8);
  375. index += 8;
  376. //ethernet
  377. //Error Code(1 bytes)
  378. result[13] = ctrlData.ErrorCode;
  379. index++;
  380. //General Status(1 bytes)
  381. result[14] = ctrlData.Status;
  382. index++;
  383. //ampliferStatus
  384. byte[] ampliferStatus = new byte[4];
  385. Array.Copy(ampliferStatus, 0, result, index, 4);
  386. index += 4;
  387. //counterModel
  388. byte[] counterModel = new byte[6];
  389. Array.Copy(counterModel, 0, result, index, 6);
  390. index += 6;
  391. //S Block(10 bytes)
  392. Array.Copy(ctrlData.SBlocks, 0, result, index, 10);
  393. index += 10;
  394. //T Block(10 bytes)
  395. Array.Copy(ctrlData.TBlocks, 0, result, index, 10);
  396. index += 10;
  397. //Axis Datas(36 * 8 bytes)
  398. for (int i = 0; i < MAX_AXIS_NUM; i++)
  399. {
  400. Array.Copy(CodeAxisData40(ctrlData.GalilAxisDatas[i]), 0, result, index, MAX_AXIS_DATA_LENGTH_40);
  401. index += MAX_AXIS_DATA_LENGTH_40;
  402. }
  403. return result;
  404. }
  405. /// <summary>
  406. /// GalilAxisData编码(Galil40)
  407. /// </summary>
  408. /// <param name="axisData"></param>
  409. /// <returns></returns>
  410. private byte[] CodeAxisData40(GalilAxisData axisData)
  411. {
  412. byte[] result = new byte[MAX_AXIS_DATA_LENGTH_40];
  413. int index = 0;
  414. //Status(2 bytes)
  415. Array.Copy(byteTransform.GetBytes(axisData.Status), 0, result, index, 2);
  416. index += 2;
  417. //Switches(1 bytes)
  418. result[index] = axisData.Switches;
  419. index++;
  420. //Stop Code(1 bytes)
  421. result[index] = axisData.StopCode;
  422. index++;
  423. //Reference Position(4 bytes)
  424. Array.Copy(BitConverter.GetBytes(axisData.ReferencePosition), 0, result, index, 4);
  425. index += 4;
  426. //Motor Position(4 bytes)
  427. Array.Copy(BitConverter.GetBytes(axisData.MotorPosition), 0, result, index, 4);
  428. index += 4;
  429. //Position Error
  430. Array.Copy(BitConverter.GetBytes(axisData.PositionError), 0, result, index, 4);
  431. index += 4;
  432. //Auxiliary Position
  433. Array.Copy(BitConverter.GetBytes(axisData.AuxiliaryPosition), 0, result, index, 4);
  434. index += 4;
  435. //Velocity
  436. Array.Copy(BitConverter.GetBytes(axisData.Velocity), 0, result, index, 4);
  437. index += 4;
  438. //Torque
  439. Array.Copy(BitConverter.GetBytes(axisData.Torque), 0, result, index, 4);
  440. index += 4;
  441. //Analog
  442. Array.Copy(BitConverter.GetBytes(axisData.Res), 0, result, index, 2);
  443. index += 2;
  444. byte hallInput = 0x00;
  445. result[index] = hallInput;
  446. index ++;
  447. byte reserved = 0x00;
  448. result[index] = reserved;
  449. index ++;
  450. byte[] userVariables = new byte[4];
  451. Array.Copy(userVariables, 0, result, index, 4);
  452. return result;
  453. }
  454. #endregion
  455. /// <summary>
  456. /// 解析指令(操作符, 运动轴, 操作数)
  457. /// </summary>
  458. /// <param name="cmd"></param>
  459. /// <returns></returns>
  460. private (string, char, int) AnalyseCommand(string cmdStr)
  461. {
  462. var result = ("", ' ', -1);
  463. //操作符
  464. result.Item1 = cmdStr.Substring(0, 2);
  465. //运动轴
  466. if (cmdStr.Length >= 4)
  467. {
  468. result.Item2 = Convert.ToChar(cmdStr.Substring(2, 1));
  469. }
  470. //操作数
  471. if (cmdStr.Length >= 5 && int.TryParse(cmdStr.Substring(4, cmdStr.Length - 5), out int tmp))
  472. {
  473. result.Item3 = tmp;
  474. }
  475. return result;
  476. }
  477. /// <summary>
  478. /// CMD校验
  479. /// </summary>
  480. /// <returns></returns>
  481. private bool CheckCmdValid(string cmdStr)
  482. {
  483. //长度
  484. if(cmdStr.Length < 3) return false;
  485. //;结尾
  486. if (!cmdStr.EndsWith(";")) return false;
  487. //第1位为A~Z
  488. if (cmdStr[0] < 'A' || cmdStr[0] > 'Z') return false;
  489. //第2位为A~Z
  490. if (cmdStr[1] < 'A' || cmdStr[1] > 'Z') return false;
  491. if(cmdStr[0] == 'C'&& cmdStr[1] == 'N')
  492. {
  493. return true;
  494. }
  495. if (cmdStr.Length >= 4)
  496. {
  497. //axis名称为A~H
  498. if (cmdStr[2] < 'A' || cmdStr[2] > 'H') return false;
  499. //=
  500. if(cmdStr.Length > 4 && !cmdStr.Contains("=")) return false;
  501. }
  502. return true;
  503. }
  504. /// <summary>
  505. /// 设置操作
  506. /// </summary>
  507. /// <param name="cmd"></param>
  508. /// <param name="axis"></param>
  509. /// <param name="value"></param>
  510. /// <returns></returns>
  511. private bool SetOperation(string cmd, char axis, int value)
  512. {
  513. switch (cmd)
  514. {
  515. case "SH"://上电
  516. SwitchMotor(axis, true);
  517. break;
  518. case "MO"://下电
  519. SwitchMotor(axis, false);
  520. break;
  521. case "PR"://相对位置
  522. SetTargetRelativePosition(axis, value);
  523. break;
  524. case "PA"://绝对位置
  525. SetTargetAbsolutePosition(axis, value);
  526. break;
  527. case "SP"://速度
  528. SetSpeed(axis, value);
  529. break;
  530. case "AC"://加速度
  531. SetAcceleration(axis, value);
  532. break;
  533. case "DC"://减速度
  534. SetDeceleration(axis, value);
  535. break;
  536. case "ST"://停止
  537. ControlMotion(axis, false);
  538. break;
  539. case "BG"://启动
  540. ControlMotion(axis, true);
  541. break;
  542. case "HM"://回零
  543. Home(axis);
  544. break;
  545. case "DP"://设定motor position
  546. SetMotorPosition(axis, value);
  547. break;
  548. case "DE"://设定auxiliary position
  549. SetAuxiliaryPosition(axis, value);
  550. break;
  551. case "CN":
  552. break;
  553. default:
  554. break;
  555. }
  556. return true;
  557. }
  558. /// <summary>
  559. /// 实时更新电机数据
  560. /// </summary>
  561. /// <param name="datasDic"></param>
  562. private void UpdateRealTimeMotionData(Dictionary<string, SimulatorMotionData> datasDic)
  563. {
  564. foreach(var dataItem in datasDic)
  565. {
  566. if (_axisNameIndexDic.ContainsKey(dataItem.Key))
  567. {
  568. UpdateAxisData(_axisNameIndexDic[dataItem.Key], dataItem.Value);
  569. }
  570. }
  571. }
  572. /// <summary>
  573. /// 更新Galil Input datas
  574. /// </summary>
  575. private void UpdateInputData(string module, string VariableName, bool value)
  576. {
  577. if(_moduleName == module && InputDataNameDIDic.ContainsKey(VariableName))
  578. {
  579. int tmp = InputDataNameDIDic[VariableName].Invert ? (!value ? 1 : 0) : (value ? 1 : 0);
  580. _galilControlData.Inputs[InputDataNameDIDic[VariableName].Address] = (byte)(tmp << InputDataNameDIDic[VariableName].Bit);
  581. }
  582. }
  583. /// <summary>
  584. /// 更新对应Axis数据
  585. /// </summary>
  586. /// <param name="index"></param>
  587. private void UpdateAxisData(int index, SimulatorMotionData data)
  588. {
  589. _galilControlData.GalilAxisDatas[index].Velocity = data.ActualVelocity;
  590. _galilControlData.GalilAxisDatas[index].MotorPosition = (int)(data.ActualPosition / _motorPositionRateDic[_nameAxisList[index]]);
  591. _galilControlData.GalilAxisDatas[index].ReferencePosition = data.TargetPosition;
  592. _galilControlData.GalilAxisDatas[index].Torque = data.ActualTorque;
  593. _galilControlData.GalilAxisDatas[index].StopCode = data.StopCode;
  594. }
  595. /// <summary>
  596. /// 模拟器UI更新inputs数据
  597. /// </summary>
  598. /// <param name="name"></param>
  599. /// <param name="value"></param>
  600. public void UpdateInputByte(string name, int value)
  601. {
  602. if (InputDataNameDIDic.ContainsKey(name))
  603. {
  604. GalilDI galilDI = InputDataNameDIDic[name];
  605. short byteValue = (short)(_galilControlData.Inputs[galilDI.Address] & ~(1 << galilDI.Bit));
  606. short tmp = (short)(value << galilDI.Bit);
  607. _galilControlData.Inputs[galilDI.Address] = (byte)(byteValue | tmp);
  608. }
  609. }
  610. /// <summary>
  611. /// 模拟器获取inputs数据
  612. /// </summary>
  613. /// <param name="name"></param>
  614. public int GetInputData(string name)
  615. {
  616. int result = 0;
  617. if (!InputDataNameDIDic.ContainsKey(name)) return 0;
  618. GalilDI galilDI = InputDataNameDIDic[name];
  619. int tmp = _galilControlData.Inputs[galilDI.Address] & (1 << galilDI.Bit);
  620. result = (tmp == (1 << galilDI.Bit)) ? 1 : 0;
  621. return result;
  622. }
  623. #region AxisOperation
  624. /// <summary>
  625. /// 开/关电机
  626. /// </summary>
  627. /// <param name="axis"></param>
  628. /// <param name="flag"></param>
  629. private void SwitchMotor(char axis, bool flag)
  630. {
  631. byte[] data = byteTransform.GetBytes(_galilControlData.GalilAxisDatas[axis - 'A'].Status);
  632. data[0] &= 0xFE;
  633. data[0] |= (byte)(flag ? 0x00 : 0x01);
  634. _galilControlData.GalilAxisDatas[axis - 'A'].Status = byteTransform.TransUInt16(data, 0);
  635. MotorSimulator.Instance.SetMotionData(_nameAxisList[axis - 'A'], SWITCH_SIGNAL, flag);
  636. }
  637. /// <summary>
  638. /// 设置速度
  639. /// </summary>
  640. private void SetSpeed(char axis, int value)
  641. {
  642. MotorSimulator.Instance.SetMotionData(_nameAxisList[axis - 'A'], TARGET_VELOCITY, value);
  643. }
  644. /// <summary>
  645. /// 设置加速度
  646. /// </summary>
  647. private void SetAcceleration(char axis, int value)
  648. {
  649. MotorSimulator.Instance.SetMotionData(_nameAxisList[axis - 'A'], TARGET_ACCEL, value);
  650. }
  651. /// <summary>
  652. /// 设置减速度
  653. /// </summary>
  654. private void SetDeceleration(char axis, int value)
  655. {
  656. MotorSimulator.Instance.SetMotionData(_nameAxisList[axis - 'A'], TARGET_DECEL, value);
  657. }
  658. /// <summary>
  659. /// 设置目标绝对位置(设定位置)
  660. /// </summary>
  661. private void SetTargetAbsolutePosition(char axis, int value)
  662. {
  663. _galilControlData.GalilAxisDatas[axis - 'A'].ReferencePosition = value;
  664. MotorSimulator.Instance.SetMotionData(_nameAxisList[axis - 'A'], TARGET_POSITION, value);
  665. }
  666. /// <summary>
  667. /// 设置目标相对位置(设定位置)
  668. /// </summary>
  669. private void SetTargetRelativePosition(char axis, int value)
  670. {
  671. _galilControlData.GalilAxisDatas[axis - 'A'].ReferencePosition = _galilControlData.GalilAxisDatas[axis - 'A'].MotorPosition + value;
  672. MotorSimulator.Instance.SetMotionData(_nameAxisList[axis - 'A'], TARGET_POSITION, _galilControlData.GalilAxisDatas[axis - 'A'].ReferencePosition);
  673. }
  674. /// <summary>
  675. /// 设置 Motor Position(实际位置)
  676. /// </summary>
  677. /// <param name="axis"></param>
  678. /// <param name="value"></param>
  679. private void SetMotorPosition(char axis, int value)
  680. {
  681. MotorSimulator.Instance.SetMotionData(_nameAxisList[axis - 'A'], ACTUAL_POSITION, value);
  682. }
  683. /// <summary>
  684. /// 设置 Auxiliary Position
  685. /// </summary>
  686. /// <param name="axis"></param>
  687. /// <param name="value"></param>
  688. private void SetAuxiliaryPosition(char axis, int value)
  689. {
  690. MotorSimulator.Instance.SetMotionData(_nameAxisList[axis - 'A'], AUXILIARY_POSITION, value);
  691. }
  692. /// <summary>
  693. /// 控制运动
  694. /// </summary>
  695. private void ControlMotion(char axis, bool value)
  696. {
  697. MotorSimulator.Instance.SetMotionData(_nameAxisList[axis - 'A'], value ? MOTION_SIGNAL : STOP_SIGNAL, value);
  698. }
  699. /// <summary>
  700. /// 电机回零
  701. /// </summary>
  702. /// <param name="axis"></param>
  703. private void Home(char axis)
  704. {
  705. MotorSimulator.Instance.SetMotionData(_nameAxisList[axis - 'A'], HOMING_SIGNAL, true);
  706. }
  707. #endregion
  708. }
  709. }