SafetyToggleRoutine.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.Routine;
  4. using MECF.Framework.Common.Beckhoff.ModuleIO;
  5. using MECF.Framework.Common.Routine;
  6. using MECF.Framework.Common.TwinCat;
  7. using CyberX8_Core;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using MECF.Framework.Common.IOCore;
  15. namespace CyberX8_RT.Devices.Safety
  16. {
  17. public class SafetyArmResetRoutine : RoutineBase, IRoutine
  18. {
  19. private enum SafetyToggleStep
  20. {
  21. WriteFalse,
  22. WriteTrue,
  23. End
  24. }
  25. #region 内部变量
  26. /// <summary>
  27. /// do名称
  28. /// </summary>
  29. private string _doName = "";
  30. /// <summary>
  31. /// 写入数值
  32. /// </summary>
  33. private double _doWriteValue = 0;
  34. /// <summary>
  35. /// 设备
  36. /// </summary>
  37. private SafetyDevice _device;
  38. /// <summary>
  39. /// 超晨
  40. /// </summary>
  41. private int _timeout = 1000;
  42. #endregion
  43. /// <summary>
  44. /// 构造函数
  45. /// </summary>
  46. /// <param name="module"></param>
  47. public SafetyArmResetRoutine(string module) : base(module)
  48. {
  49. }
  50. /// <summary>
  51. /// 中止
  52. /// </summary>
  53. public void Abort()
  54. {
  55. Runner.Stop($"Manual Safty {_doName} Toggle");
  56. }
  57. /// <summary>
  58. /// 监控
  59. /// </summary>
  60. /// <returns></returns>
  61. public RState Monitor()
  62. {
  63. Runner.Run(SafetyToggleStep.WriteFalse, () => { return WriteDoValue(0); }, CheckDoValue, _timeout)
  64. .Run(SafetyToggleStep.WriteTrue, () => { return WriteDoValue(1); }, CheckDoValue, _timeout)
  65. .End(SafetyToggleStep.End, NullFun, _delay_1ms);
  66. return Runner.Status;
  67. }
  68. /// <summary>
  69. /// 写入do数值
  70. /// </summary>
  71. /// <param name="value"></param>
  72. /// <returns></returns>
  73. private bool WriteDoValue(double value)
  74. {
  75. string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{_doName}");
  76. _doWriteValue = value;
  77. return IOModuleManager.Instance.WriteIoValue(ioName, value);
  78. }
  79. /// <summary>
  80. /// 检验do的数值
  81. /// </summary>
  82. /// <returns></returns>
  83. private bool CheckDoValue()
  84. {
  85. PropertyInfo propertyInfo = _device.SafetyData.GetType().GetProperty(_doName);
  86. if (propertyInfo != null)
  87. {
  88. double doValue = (double)propertyInfo.GetValue(_device.SafetyData);
  89. return doValue==_doWriteValue;
  90. }
  91. else
  92. {
  93. LOG.WriteLog(eEvent.ERR_SAFETY, Module, $"{_doName} in not in property");
  94. return false;
  95. }
  96. }
  97. /// <summary>
  98. /// 启动
  99. /// </summary>
  100. /// <param name="objs"></param>
  101. /// <returns></returns>
  102. public RState Start(params object[] objs)
  103. {
  104. _device = DEVICE.GetDevice<SafetyDevice>(Module);
  105. _doName= objs[0].ToString();
  106. return Runner.Start(Module, $"{_doName} toggle");
  107. }
  108. }
  109. }