using Aitex.Core.Util;
namespace Aitex.Core.RT.Tolerance
{
///
/// 误差检查器。
///
public class ToleranceChecker
{
#region Variables
private readonly DeviceTimer _timer = new();
private readonly R_TRIG _trigger = new();
private bool _started;
#endregion
#region Properties
///
/// 返回是否检测到误差超限异常。
///
public bool Trig => _trigger.Q;
///
/// 返回被检测值是否已超出误差范围。
///
public bool Result => _trigger.M;
///
/// 复位定时器和误差超限触发器。
///
public bool RST
{
set
{
_started = false;
_trigger.RST = value;
}
}
#endregion
#region Constructors
///
/// 构造误差检查器对象实例。
///
public ToleranceChecker()
{
_timer.Start(0.0);
}
///
/// 构造误差检查器对象实例。
///
/// 误差超限持续时长。
public ToleranceChecker(double durationSec)
{
_timer.Start(durationSec * 1000.0);
}
#endregion
#region Methods
///
/// 检查器复位。
///
/// 误差超限持续时长。
public void Reset(double durationSec)
{
_timer.Start(durationSec * 1000.0);
RST = true;
}
///
/// 后台扫描函数,判断被监测值是否在指定的时间内持续超出范围。
///
/// 被监测值。
/// 范围下限。
/// 范围上限。
/// 持续时长,单位秒。
public void Monitor(double value, double min, double max, double durationSec)
{
if (!_started || (value >= min && value <= max))
{
_started = true;
_timer.Start(durationSec * 1000.0);
}
_trigger.CLK = _timer.IsTimeout();
}
#endregion
}
}