GalilSocketSimulator.cs 25 KB

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