RecipeToleranceChecker.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Aitex.Core.RT.Log;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using Venus_Core;
  5. namespace Aitex.Core.RT.Tolerance
  6. {
  7. public class RecipeToleranceChecker
  8. {
  9. Stopwatch _stopwatch;
  10. List<ToleranceObject> _toleranceObjects;
  11. string _Module;
  12. public bool IsStable = true;
  13. private bool _IsStableType;
  14. public RecipeToleranceChecker(string module)
  15. {
  16. _stopwatch = new Stopwatch();
  17. _Module = module;
  18. }
  19. public void Start(List<ToleranceObject> toleranceObjects, bool isStableType)
  20. {
  21. _stopwatch.Restart();
  22. _toleranceObjects = toleranceObjects;
  23. IsStable = false;
  24. _IsStableType = isStableType;
  25. }
  26. public RState Monitor(params double[] feedBacks)
  27. {
  28. if (_toleranceObjects == null)
  29. {
  30. return RState.Running;
  31. }
  32. for (int i = 0; i < _toleranceObjects.Count; i++)
  33. {
  34. if (_stopwatch.ElapsedMilliseconds > _toleranceObjects[i].DelayTime || (_IsStableType && _stopwatch.ElapsedMilliseconds > 500))
  35. {
  36. if (_IsStableType)
  37. {
  38. _toleranceObjects[i].AlarmFlag = false;
  39. _toleranceObjects[i].WarningFlag = false;
  40. }
  41. if ((feedBacks[i] < _toleranceObjects[i].AlarmMin || feedBacks[i] > _toleranceObjects[i].AlarmMax) && _toleranceObjects[i].WarningFlag == true)
  42. {
  43. //_toleranceObjects[i].AlarmFlag = true;
  44. _toleranceObjects[i].AlarmCounter += 1;
  45. if (_IsStableType == false && _toleranceObjects[i].AlarmCounter == _toleranceObjects[i].AlarmTriggerCounter)
  46. {
  47. LOG.Write(eEvent.ERR_PM_TOLERANCE, _Module, $"{_toleranceObjects[i].Name}:Current Value:{feedBacks[i]},Over Range {_toleranceObjects[i].AlarmMin}⁓{_toleranceObjects[i].AlarmMax}");
  48. return RState.Failed;
  49. }
  50. }
  51. else if ((feedBacks[i] < _toleranceObjects[i].WarningMin || feedBacks[i] > _toleranceObjects[i].WarningMax) && _toleranceObjects[i].WarningFlag == false)
  52. {
  53. IsStable = false;
  54. _toleranceObjects[i].WarningFlag = true;
  55. if (_IsStableType == false)
  56. {
  57. LOG.Write(eEvent.WARN_PM_TOLERANCE, _Module, $"{_toleranceObjects[i].Name}:Current Value:{feedBacks[i]},Over Range {_toleranceObjects[i].WarningMin}⁓{_toleranceObjects[i].WarningMax}");
  58. }
  59. }
  60. else
  61. {
  62. _toleranceObjects[i].AlarmCounter = 0;
  63. IsStable = true;
  64. }
  65. }
  66. }
  67. return RState.Running;
  68. }
  69. public void End()
  70. {
  71. IsStable = true;
  72. _stopwatch.Reset();
  73. }
  74. }
  75. public class ToleranceObject
  76. {
  77. public string Name { get; set; }
  78. public float SetPoint { get; set; }
  79. public float WarningScale { get; set; }
  80. public float AlarmScale { get; set; }
  81. public bool WarningFlag { get; set; }
  82. public bool AlarmFlag { get; set; }
  83. public int DelayTime { get; set; }
  84. public float WarningMax { get; set; }
  85. public float WarningMin { get; set; }
  86. public float AlarmMax { get; set; }
  87. public float AlarmMin { get; set; }
  88. public int AlarmCounter { get; set; }
  89. public int AlarmTriggerCounter { get; set; }
  90. public ToleranceObject(string name, float setPoint, float warningScale, float alarmScale, int delayTime, ToleranceMode toleranceMode, int alarmTriggerCounter = 1)
  91. {
  92. Name = name;
  93. SetPoint = setPoint;
  94. DelayTime = delayTime;
  95. if (toleranceMode == ToleranceMode.Percent)
  96. {
  97. WarningScale = SetPoint * warningScale / 100;
  98. AlarmScale = SetPoint * alarmScale / 100;
  99. }
  100. else
  101. {
  102. WarningScale = warningScale;
  103. AlarmScale = alarmScale;
  104. }
  105. WarningMax = setPoint + WarningScale;
  106. WarningMin = setPoint - WarningScale;
  107. AlarmMax = setPoint + AlarmScale;
  108. AlarmMin = setPoint - AlarmScale;
  109. AlarmTriggerCounter = alarmTriggerCounter;
  110. }
  111. }
  112. }