GalilSocketSimulator.cs 27 KB

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