SafetyArmResetRoutine.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 SafetyToggleRoutine : RoutineBase, IRoutine
  18. {
  19. private enum SafetyToggleStep
  20. {
  21. WriteFalse,
  22. WriteTrue,
  23. LastWriteFalse,
  24. End
  25. }
  26. #region 内部变量
  27. /// <summary>
  28. /// do名称
  29. /// </summary>
  30. private string _doName = "";
  31. /// <summary>
  32. /// do写入数值
  33. /// </summary>
  34. private bool _doWriteValue = false;
  35. /// <summary>
  36. /// 设备
  37. /// </summary>
  38. private SafetyDevice _device;
  39. /// <summary>
  40. /// 超晨
  41. /// </summary>
  42. private int _timeout = 1000;
  43. #endregion
  44. /// <summary>
  45. /// 构造函数
  46. /// </summary>
  47. /// <param name="module"></param>
  48. public SafetyToggleRoutine(string module) : base(module)
  49. {
  50. }
  51. /// <summary>
  52. /// 中止
  53. /// </summary>
  54. public void Abort()
  55. {
  56. Runner.Stop($"Manual Safty {_doName} Toggle");
  57. }
  58. /// <summary>
  59. /// 监控
  60. /// </summary>
  61. /// <returns></returns>
  62. public RState Monitor()
  63. {
  64. Runner.Run(SafetyToggleStep.WriteFalse, () => { return WriteDoValue(false); }, CheckDoValue, _timeout)
  65. .Run(SafetyToggleStep.WriteTrue, () => { return WriteDoValue(true); }, CheckDoValue, _timeout)
  66. .Run(SafetyToggleStep.LastWriteFalse, () => { return WriteDoValue(false); }, CheckDoValue, _timeout)
  67. .End(SafetyToggleStep.End, NullFun, _delay_1ms);
  68. return Runner.Status;
  69. }
  70. /// <summary>
  71. /// 写入do数值
  72. /// </summary>
  73. /// <param name="value"></param>
  74. /// <returns></returns>
  75. private bool WriteDoValue(bool value)
  76. {
  77. string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{_doName}");
  78. _doWriteValue=value;
  79. return IOModuleManager.Instance.WriteIoValue(ioName, value);
  80. }
  81. /// <summary>
  82. /// 检验do的数值
  83. /// </summary>
  84. /// <returns></returns>
  85. private bool CheckDoValue()
  86. {
  87. PropertyInfo propertyInfo = _device.SafetyData.GetType().GetProperty(_doName);
  88. if (propertyInfo != null)
  89. {
  90. bool doValue = (bool)propertyInfo.GetValue(_device.SafetyData);
  91. return doValue==_doWriteValue;
  92. }
  93. else
  94. {
  95. LOG.WriteLog(eEvent.ERR_SAFETY, Module, $"{_doName} in not in property");
  96. return false;
  97. }
  98. }
  99. /// <summary>
  100. /// 启动
  101. /// </summary>
  102. /// <param name="objs"></param>
  103. /// <returns></returns>
  104. public RState Start(params object[] objs)
  105. {
  106. _device = DEVICE.GetDevice<SafetyDevice>(Module);
  107. _doName= objs[0].ToString();
  108. return Runner.Start(Module, $"{_doName} toggle");
  109. }
  110. }
  111. }