GalilAxis.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Aitex.Core.RT.Device;
  2. using MECF.Framework.Common.Beckhoff.IOAxis;
  3. using MECF.Framework.Common.CommonData.PUF;
  4. using MECF.Framework.Common.IOCore;
  5. using MECF.Framework.Common.TwinCat;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace CyberX8_RT.Devices.Galil
  13. {
  14. public class GalilAxis : BaseDevice, IDevice
  15. {
  16. #region 常量
  17. private const string MOTOR_POSITION = "MotorPosition";
  18. private const string POSITION_ERROR = "PositionError";
  19. #endregion
  20. #region 内部变量
  21. /// <summary>
  22. /// 变量是否初始化字典
  23. /// </summary>
  24. private Dictionary<string, bool> _variableInitializeDic = new Dictionary<string, bool>();
  25. /// <summary>
  26. /// 运动数据对象
  27. /// </summary>
  28. protected CommandMotionData _commandMotionData = new CommandMotionData();
  29. #endregion
  30. #region 属性
  31. /// <summary>
  32. /// 运动数据对象
  33. /// </summary>
  34. public CommandMotionData MotionData { get { return _commandMotionData; } }
  35. #endregion
  36. /// <summary>
  37. /// 构造函数
  38. /// </summary>
  39. /// <param name="moduleName"></param>
  40. /// <param name="name"></param>
  41. public GalilAxis(string moduleName, string name) : base(moduleName, name, name, name)
  42. {
  43. }
  44. #region public 公开方法
  45. /// <summary>
  46. /// 初始化
  47. /// </summary>
  48. /// <returns></returns>
  49. public bool Initialize()
  50. {
  51. SubscribeValueAction();
  52. return true;
  53. }
  54. /// <summary>
  55. /// 订阅变量数值发生变化
  56. /// </summary>
  57. protected void SubscribeValueAction()
  58. {
  59. BeckhoffIoSubscribeUpdateVariable(MOTOR_POSITION);
  60. BeckhoffIoSubscribeUpdateVariable(POSITION_ERROR);
  61. }
  62. /// <summary>
  63. /// 订阅IO变量
  64. /// </summary>
  65. /// <param name="variable"></param>
  66. private void BeckhoffIoSubscribeUpdateVariable(string variable)
  67. {
  68. _variableInitializeDic[variable] = false;
  69. IOModuleManager.Instance.SubscribeModuleVariable($"{Module}.{Name}", variable, UpdateVariableValue);
  70. }
  71. /// <summary>
  72. /// 更新变量数值
  73. /// </summary>
  74. /// <param name="variable"></param>
  75. /// <param name="value"></param>
  76. protected void UpdateVariableValue(string variable, object value)
  77. {
  78. if (value == null)
  79. {
  80. return;
  81. }
  82. if (_variableInitializeDic.ContainsKey(variable) && !_variableInitializeDic[variable])
  83. {
  84. _variableInitializeDic[variable] = true;
  85. }
  86. UpdateMotionData(variable, value);
  87. }
  88. /// <summary>
  89. /// 更新运动数据
  90. /// </summary>
  91. /// <param name="variable"></param>
  92. /// <param name="value"></param>
  93. private void UpdateMotionData(string variable, object value)
  94. {
  95. if (!MotionData.IsDataInitialized)
  96. {
  97. MotionData.IsDataInitialized = true;
  98. }
  99. PropertyInfo property = MotionData.GetType().GetProperty(variable);
  100. if (property != null)
  101. {
  102. property.SetValue(MotionData, value);
  103. }
  104. }
  105. #endregion
  106. /// <summary>
  107. /// 监控
  108. /// </summary>
  109. public void Monitor()
  110. {
  111. }
  112. public void Reset()
  113. {
  114. }
  115. /// 停止
  116. /// </summary>
  117. public void Terminate()
  118. {
  119. }
  120. }
  121. }