RecipeToleranceChecker.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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].SetPoint != 0 && _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 && _toleranceObjects[i].SetPoint != 0)
  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. IsStable = true;
  63. }
  64. }
  65. }
  66. return RState.Running;
  67. }
  68. public void End()
  69. {
  70. IsStable = true;
  71. _stopwatch.Reset();
  72. }
  73. }
  74. public class ToleranceObject
  75. {
  76. public string Name { get; set; }
  77. public float SetPoint { get; set; }
  78. public float WarningScale { get; set; }
  79. public float AlarmScale { get; set; }
  80. public bool WarningFlag { get; set; }
  81. public bool AlarmFlag { get; set; }
  82. public int DelayTime { get; set; }
  83. public float WarningMax { get; set; }
  84. public float WarningMin { get; set; }
  85. public float AlarmMax { get; set; }
  86. public float AlarmMin { get; set; }
  87. public int AlarmCounter { get; set; }
  88. public int AlarmTriggerCounter { get; set; }
  89. public ToleranceObject(string name, float setPoint, float warningScale, float alarmScale, int delayTime, ToleranceMode toleranceMode, int alarmTriggerCounter=1)
  90. {
  91. Name = name;
  92. SetPoint = setPoint;
  93. DelayTime = delayTime;
  94. if (toleranceMode == ToleranceMode.Percent)
  95. {
  96. WarningScale = SetPoint * warningScale / 100;
  97. AlarmScale = SetPoint * alarmScale / 100;
  98. }
  99. else
  100. {
  101. WarningScale = warningScale;
  102. AlarmScale = alarmScale;
  103. }
  104. WarningMax = setPoint + WarningScale;
  105. WarningMin = setPoint - WarningScale;
  106. AlarmMax = setPoint + AlarmScale;
  107. AlarmMin = setPoint - AlarmScale;
  108. AlarmTriggerCounter = alarmTriggerCounter;
  109. }
  110. }
  111. }