| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 | using Aitex.Core.Common.DeviceData;using Aitex.Core.RT.DataCenter;using Aitex.Core.RT.Device;using Aitex.Core.RT.Event;using Aitex.Core.RT.IOCore;using Aitex.Core.RT.OperationCenter;using Aitex.Core.RT.SCCore;using Aitex.Core.RT.Tolerance;using Aitex.Core.Util;using MECF.Framework.Common.DBCore;using SorterRT.Modules;using System;using System.Diagnostics;using System.Xml;using Aitex.Core.RT.Log;namespace VirgoRT.Devices.IODevices{    public class IoMfc1 : MfcBase1    {        public string Unit        {            get; set;        }        [Subscription(AITMfcDataPropertyName.Scale)]        public override double Scale        {            get            {                if (_scN2Scale == null || _scScaleFactor == null)                    return 0;                return _scN2Scale.IntValue * _scScaleFactor.DoubleValue;            }        }        [Subscription(AITMfcDataPropertyName.SetPoint)]        public override double SetPoint        {            get            {                if (_aoFlow != null)                {                    byte[] high = BitConverter.GetBytes(_aoFlow.Buffer[_aoFlow.Index]);                    byte[] low = BitConverter.GetBytes(_aoFlow.Buffer[_aoFlow.Index + 1]);                    float flow = BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);                    return flow * Scale / _ANALOG_TRANS_RANGE;                }                return 0;            }            set            {                if (_aoFlow != null)                {                    byte[] flow = BitConverter.GetBytes((float)(value * _ANALOG_TRANS_RANGE / Scale));                    _aoFlow.Buffer[_aoFlow.Index] = BitConverter.ToInt16(flow, 0);                    _aoFlow.Buffer[_aoFlow.Index + 1] = BitConverter.ToInt16(flow, 2);                }            }        }        [Subscription(AITMfcDataPropertyName.DefaultSetPoint)]        public double DefaultSetPoint        {            get            {                if (_scDefaultSetPoint != null)                    return _scDefaultSetPoint.IntValue;                return 0;            }        }        [Subscription(AITMfcDataPropertyName.FeedBack)]        public override double FeedBack        {            get            {                if (_aiFlow != null)                {                    byte[] high = BitConverter.GetBytes(_aiFlow.Buffer[_aiFlow.Index]);                    byte[] low = BitConverter.GetBytes(_aiFlow.Buffer[_aiFlow.Index + 1]);                    float flow = BitConverter.ToSingle(new[] { high[0], high[1], low[0], low[1] }, 0);                    return (_scRegulationFactor != null && _scRegulationFactor.IntValue > 0) ? flow * Scale / _ANALOG_TRANS_RANGE / _scRegulationFactor.IntValue                                                                                             : flow * Scale / _ANALOG_TRANS_RANGE;                }                return 0;            }        }        [Subscription(AITMfcDataPropertyName.IsOutOfTolerance)]        public override bool IsOutOfTolerance        {            get            {                return _alarmChecker.Result;            }        }        [Subscription(AITMfcDataPropertyName.IsEnableAlarm)]        public bool EnableAlarm        {            get            {                if (_scEnableAlarm != null)                    return _scEnableAlarm.BoolValue;                return false;            }        }        [Subscription(AITMfcDataPropertyName.AlarmRange)]        public double AlarmRange        {            get            {                if (_scAlarmRange != null)                    return _scAlarmRange.IntValue;                return 0;            }        }        [Subscription(AITMfcDataPropertyName.AlarmTime)]        public double AlarmTime        {            get            {                if (_scAlarmTime != null)                    return _scAlarmTime.IntValue;                return 0;            }        }        public double WarningRange        {            get            {                if (_scWarningRange != null)                    return _scWarningRange.IntValue;                return 0;            }        }        public double WarningTime        {            get            {                if (_scWarningTime != null)                    return _scWarningTime.IntValue;                return 0;            }        }        [Subscription(AITMfcDataPropertyName.PressureAlarm)]        public bool PressureAlarm        {            get { return _diPressureAlarm != null ? _diPressureAlarm.Value : true; }        }        [Subscription(AITMfcDataPropertyName.MfcAlarm)]        public bool MfcAlarm        {            get            {                return _bMfcAlarm;            }        }        [Subscription(AITMfcDataPropertyName.IsEnable)]        public bool Enable        {            get            {                if (_scEnable != null)                    return _scEnable.BoolValue;                return false;            }        }        [Subscription(AITMfcDataPropertyName.IsOffline)]        public bool IsOffline        {            get            {                if (_diOffline != null)                    return _diOffline.Value;                return false;            }        }        public override string DisplayName        {            get            {                if (_scGasName != null)                    return _scGasName.StringValue;                return Display;            }        }        private DeviceTimer rampTimer = new DeviceTimer();        private double rampTarget;        private double rampInitValue;        private int rampTime;        private bool _bMfcAlarm = false;        private ToleranceChecker _alarmChecker = new ToleranceChecker();        private ToleranceChecker _warningChecker = new ToleranceChecker();        private AIAccessor _aiFlow;        private AOAccessor _aoFlow;        private AOAccessor _aoRange;        private DIAccessor _diOffline;        private DIAccessor _diPressureAlarm;        private SCConfigItem _scGasName;        private SCConfigItem _scEnable;        private SCConfigItem _scN2Scale;        private SCConfigItem _scScaleFactor;        private SCConfigItem _scAlarmRange;        private SCConfigItem _scEnableAlarm;        private SCConfigItem _scAlarmTime;        private SCConfigItem _scDefaultSetPoint;        private SCConfigItem _scRegulationFactor;        private SCConfigItem _scWarningRange;        private SCConfigItem _scWarningTime;        public bool Recipetolerance;        private R_TRIG _trigOffline = new R_TRIG();        private R_TRIG _trigPressureAlarm = new R_TRIG();        private string _uniqueName;        private string GasFlowOutOfTolerance = "GasFlowOutOfTolerance";        private float _recipeAlarmRange;        private float _recipeWarningRange;        private int _recipeIgnoreTimeMS;        private ToleranceChecker _recipeAlarmChecker = new ToleranceChecker();        private ToleranceChecker _recipeWarningChecker = new ToleranceChecker();        private DeviceTimer _recipeIgnoreTimer = new DeviceTimer();        private object _lockerTolerance = new object();        private ushort _ANALOG_TRANS_RANGE = 8000;        public IoMfc1(string module, XmlElement node, string ioModule = "")        {            Unit = node.GetAttribute("unit");            base.Module = module;            base.Name = node.GetAttribute("id");            base.Display = node.GetAttribute("display");            base.DeviceID = node.GetAttribute("schematicId");            _aoRange = ParseAoNode("aoRange", node, ioModule);            _diOffline = ParseDiNode("diOffline", node, ioModule);            _aiFlow = ParseAiNode("aiFlow", node, ioModule);            _aoFlow = ParseAoNode("aoFlow", node, ioModule);            _diPressureAlarm = ParseDiNode("diPressureAlarm", node, ioModule);            _scGasName = SC.GetConfigItem($"{Module}.{Name}.GasName");            _scEnable = SC.GetConfigItem($"{Module}.{Name}.Enable");            _scN2Scale = SC.GetConfigItem($"{Module}.{Name}.MfcN2Scale");            _scScaleFactor = SC.GetConfigItem($"{Module}.{Name}.MfcScaleFactor");            _scAlarmRange = SC.GetConfigItem($"{Module}.{Name}.MfcAlarmRange");            _scEnableAlarm = SC.GetConfigItem($"{Module}.{Name}.MfcEnableAlarm");            _scAlarmTime = SC.GetConfigItem($"{Module}.{Name}.MfcAlarmTime");            _scDefaultSetPoint = SC.GetConfigItem($"{Module}.{Name}.DefaultSetPoint");            _scRegulationFactor = SC.GetConfigItem($"{Module}.{Name}.FlowRegulationFactor");            _scWarningRange = SC.GetConfigItem($"{Module}.{Name}.MfcWarningRange");            _scWarningTime = SC.GetConfigItem($"{Module}.{Name}.MfcWarningTime");            if (SC.ContainsItem("System.ANALOG_TRANS_RANGE"))            {                _ANALOG_TRANS_RANGE = Convert.ToUInt16(SC.GetValue<int>("System.ANALOG_TRANS_RANGE"));            }            Recipetolerance = SC.GetValue<bool>($"System.Recipetolerance");            _uniqueName = $"{Module}.{Name}";            if (_scGasName != null)                Display = _scGasName.StringValue;#if DEBUG            Debug.Assert(!string.IsNullOrWhiteSpace(_scGasName.StringValue));            Debug.Assert(null != _scN2Scale);            Debug.Assert(null != _aoFlow);            Debug.Assert(null != _aiFlow);#endif        }        public override bool Initialize()        {            EV.Subscribe(new EventItem("Event", GasFlowOutOfTolerance, "Gas Flow Out Of Tolerance", EventLevel.Alarm, EventType.HostNotification));            DATA.Subscribe($"{Module}.{Name}", () =>            {                AITMfcData data = new AITMfcData                {                    Type = "MFC",                    UniqueName = _uniqueName,                    DeviceName = Name,                    DeviceSchematicId = DeviceID,                    DisplayName = DisplayName,                    FeedBack = FeedBack,                    SetPoint = SetPoint,                    Scale = Scale,                    IsOffline = IsOffline,                };                return data;            }, SubscriptionAttribute.FLAG.IgnoreSaveDB);            OP.Subscribe($"{Module}.{Name}.{AITMfcOperation.Ramp}", (name, args) =>            {                double target = (double)args[0];                target = Math.Min(target, Scale);                target = Math.Max(target, 0);                Ramp(target, 0);                EV.PostInfoLog(Module, $"Ramp to {target}{Unit}");                return true;            });            DEVICE.Register($"{Module}.{Name}.{AITMfcOperation.Ramp}", (out string reason, int time, object[] param) =>            {                double target = Convert.ToDouble((string)param[0]);                target = Math.Min(target, Scale);                target = Math.Max(target, 0);                Ramp(target, time);                reason = $"{Display} ramp to {target}{Unit}";                return true;            });            //@AAA use recipe            DEVICE.Register($"{Module}.{Name}", (out string reason, int time, object[] param) =>            {                double target = Convert.ToDouble((string)param[0]);                target = Math.Min(target, Scale);                target = Math.Max(target, 0);                Ramp(target, time);                reason = $"{Display} ramp to {target}{Unit}";                return true;            });            OP.Subscribe($"{Module}.{Name}.SetRecipeTolerance", (out string reason, int time, object[] param) =>            {                reason = string.Empty;                lock (_lockerTolerance)                {                    _recipeIgnoreTimer.Stop();                    _recipeIgnoreTimeMS = Convert.ToInt32(param[0]) * 1000;                    _recipeWarningRange = Convert.ToSingle(param[1]);                    _recipeAlarmRange = Convert.ToSingle(param[2]);                    _recipeAlarmChecker.RST = true;                    _recipeWarningChecker.RST = true;                    _alarmChecker.RST = true;                    _warningChecker.RST = true;                    _recipeIgnoreTimer.Start(0);                }                return true;            });            return base.Initialize();        }        public override void Monitor()        {            if (Enable)            {                Ramping();                CheckTolerance();                if (_aoRange != null)                    _aoRange.Value = (short)Scale;                _trigOffline.CLK = IsOffline;                if (_trigOffline.Q)                {                    EV.PostAlarmLog(Module, string.Format("{0} is offline", DisplayName));                    _bMfcAlarm = true;                }                _trigPressureAlarm.CLK = PressureAlarm == false;                if (_trigPressureAlarm.Q)                    EV.PostAlarmLog(Module, $"{Name}, {DisplayName} Pressure Alarm");                if (PressureAlarm)                {                    _trigPressureAlarm.RST = true;                }            }        }        public override void Reset()        {            _bMfcAlarm = false;            _trigPressureAlarm.RST = true;            _trigOffline.RST = true;            _alarmChecker.RST = true;            _warningChecker.RST = true;            _recipeWarningChecker.RST = true;            _recipeAlarmChecker.RST = true;        }        public override void Terminate()        {            Ramp(DefaultSetPoint, 0);        }        public override void Ramp(int time)        {            Ramp(DefaultSetPoint, time);        }        public override void Ramp(double target, int time)        {            _recipeAlarmChecker.RST = true;            _recipeWarningChecker.RST = true;            _alarmChecker.RST = true;            _warningChecker.RST = true;            target = Math.Max(0, target);            target = Math.Min(Scale, target);            rampInitValue = SetPoint;    //ramp 初始值取当前设定值,而非实际读取值。零漂问题            rampTime = time;            rampTarget = target;            rampTimer.Start(rampTime);        }        public override void StopRamp()        {            Ramp(SetPoint, 0);        }        private void Ramping()        {            if (rampTimer.IsTimeout() || rampTime == 0)            {                SetPoint = rampTarget;            }            else            {                SetPoint = rampInitValue + (rampTarget - rampInitValue) * rampTimer.GetElapseTime() / rampTime;            }        }        private void CheckTolerance()        {            lock (_lockerTolerance)            {                if (!Enable)                    return;                if (Math.Abs(SetPoint) < 0.01)                    return;                double alarmRange = _scAlarmRange.IntValue;                double alarmTime = _scAlarmTime.IntValue;                ToleranceChecker alarmChecker = _alarmChecker;                if ((_recipeAlarmRange > 0) && (_recipeAlarmRange / 100.0 * SetPoint < _scAlarmRange.IntValue) && Recipetolerance)                {                    alarmRange = _recipeAlarmRange / 100.0 * SetPoint;                    alarmChecker = _recipeAlarmChecker;                    _alarmChecker.RST = true;                }                else                {                    if (!EnableAlarm || Math.Abs(alarmRange) < 0.01)                    {                        alarmChecker = null;                    }                    _recipeAlarmChecker.RST = true;                }                if (_recipeIgnoreTimer.GetElapseTime() > _recipeIgnoreTimeMS && alarmChecker != null)                {                    alarmChecker.Monitor(FeedBack, SetPoint - Math.Abs(alarmRange), SetPoint + Math.Abs(alarmRange), alarmTime);                    if (alarmChecker.Trig)                    {                        LOG.Write($"_recipeIgnoreTimer Elapse time : {_recipeIgnoreTimer.GetElapseTime()}ms , _recipeIgnoreTimeMS :{_recipeIgnoreTimeMS}ms");                        EV.PostAlarmLog(Module, $" flow={FeedBack}, setpoint={SetPoint}, out of tolerance({SetPoint - Math.Abs(alarmRange)},{SetPoint + Math.Abs(alarmRange)}) in {alarmTime:0} seconds");                        EV.Notify(GasFlowOutOfTolerance);                    }                }                double warningRange = _scWarningRange.IntValue;                double warningTime = _scWarningTime.IntValue;                ToleranceChecker warningChecker = _warningChecker;                if ((_recipeWarningRange > 0) && (_recipeWarningRange / 100.0 * SetPoint < _scWarningRange.IntValue) && Recipetolerance)                {                    warningRange = _recipeWarningRange / 100.0 * SetPoint;                    warningChecker = _recipeWarningChecker;                    _warningChecker.RST = true;                }                else                {                    if (!EnableAlarm || Math.Abs(warningRange) < 0.01)                    {                        warningChecker = null;                    }                    _recipeWarningChecker.RST = true;                }                if (_recipeIgnoreTimer.GetElapseTime() > _recipeIgnoreTimeMS && warningChecker != null)                {                    warningChecker.Monitor(FeedBack, SetPoint - Math.Abs(warningRange), SetPoint + Math.Abs(warningRange), warningTime);                    if (warningChecker.Trig)                    {                        EV.PostWarningLog(Module, $" flow={FeedBack}, setpoint={SetPoint}, out of tolerance({SetPoint - Math.Abs(warningRange)},{SetPoint + Math.Abs(warningRange)}) in {warningTime:0} seconds");                    }                }            }        }    }}
 |