| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672 | 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.Log;using Aitex.Core.RT.OperationCenter;using Aitex.Core.RT.SCCore;using Aitex.Core.RT.Tolerance;using Aitex.Core.Util;using MECF.Framework.Common.Event;using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.MFCs;using System;using System.Diagnostics;using System.Xml;namespace FurnaceRT.Equipments.PMs.Devices{    public class IoMFC : BaseDevice, IDevice, IMfc    {        public string Unit        {            get; set;        }        public double Scale        {            get            {                if (_scN2Scale == null || _scScaleFactor == null)                    return 0;                return _scN2Scale.DoubleValue * _scScaleFactor.DoubleValue;            }        }        private double _setpoint;        public double SetPoint        {            get            {                return _setpoint;            }            set            {                _setpoint = value;                if (_aoFlow != null)                {                    if (_isFloatAioType)                    {                        if (SC.ContainsItem($"{Module}.MFC.{Name}.DelayTime") && SC.GetValue<double>($"{Module}.MFC.{Name}.DelayTime") > 0)                            _aoFlow.SetPulseValue((float)value, TimeSpan.FromSeconds(SC.GetValue<double>($"{Module}.MFC.{Name}.DelayTime")));                        else                            _aoFlow.FloatValue = (float)value;                    }                    else                        _aoFlow.Value = (short)value;                }            }        }        public double DefaultSetPoint        {            get            {                if (_scDefaultSetPoint != null)                    return _scDefaultSetPoint.DoubleValue;                return 0;            }        }        public double FeedBack        {            get            {                if (_aiFlow != null)                {                    return _isFloatAioType ? _aiFlow.FloatValue : _aiFlow.Value;                    //var phy = _aiFlow == null ? 0 : (_isFloatAioType ? _aiFlow.FloatValue : _aiFlow.Value);                    //return Converter.Phy2Logic(phy, 0, Scale, 0, _phyScale);                    //return _maxScale != 0 ? aiValue * Scale / _maxScale : aiValue;                }                return 0;            }        }        public bool EnableAlarm        {            get            {                if (_scEnableAlarm != null)                    return _scEnableAlarm.BoolValue;                return false;            }        }        public bool IsStable        {            get            {                if (!_stableDelayTimer.IsIdle() && _stableDelayTimer.GetElapseTime() >= _stableDelayTime * 1000)                {                    if (_stableJudgmentTimer.IsRunning)                    {                        if (FeedBack < SetPoint * (100 - _stableMinValue) / 100 ||                            FeedBack > SetPoint * (100 + _stableMaxValue) / 100)                        {                            _stableJudgmentTimer.Restart();                        }                        if (_stableJudgmentTimer.ElapsedMilliseconds >= _stableJudgmentTime * 1000)                        {                            return true;                        }                    }                    else                    {                        _stableJudgmentTimer.Restart();                    }                }                return false;            }        }        public AITMfcData DeviceData        {            get            {                AITMfcData data = new AITMfcData()                {                    UniqueName = $"{Module}.{Name}",                    Type = "MFC",                    Module = Module,                    DeviceName = Name,                    DeviceSchematicId = DeviceID,                    DisplayName = DisplayName,                    FeedBack = FeedBack < 0 ? 0 : FeedBack,                    SetPoint = SetPoint,                    Ramping = _ramping,                    AlarmWatchTable = AlarmWatchTable,                    Scale = Scale,                    //IsWarning = !AlarmToleranceWarning.IsAcknowledged,                    //IsError = !AlarmToleranceAlarm.IsAcknowledged,                    Unit = Unit,                    VirtualSetPoint = (float)VirtualSetPoint,                    VirtualFeedBack = (float)VirtualFeedBack,                    VirtualAlarmWatchTable = VirtualAlarmWatchTable,                    VirtualRamping = VirtualRamping,                    IsInstalled = IsMFCInstalled                };                return data;            }        }        public string DisplayName        {            get            {                if (_scGasName != null)                    return _scGasName.StringValue;                return Display;            }        }        public string AlarmWatchTable        {            get;            set;        }        public double VirtualSetPoint;        public double VirtualFeedBack;        public string VirtualAlarmWatchTable;        public float VirtualRamping;        private DeviceTimer _rampTimer = new DeviceTimer();        private double _rampTarget;        private double _rampInitValue;        private int _rampTime;        private float _ramping;        private double _phyScale;        private ToleranceChecker _toleranceCheckerWarning = new ToleranceChecker();        private ToleranceChecker _toleranceCheckerAlarm = new ToleranceChecker();        private AIAccessor _aiFlow;        private AOAccessor _aoFlow;        private AOAccessor _aoRamp;        private bool _isErrorStatus;        private bool _isMFCInstalled;        private int _mfcIndex;        public bool IsMFCInstalled        {            get { return _isMFCInstalled; }        }        public int MFCIndex        {            get { return _mfcIndex; }        }        protected SCConfigItem _scGasName;        protected SCConfigItem _scEnable;        private SCConfigItem _scN2Scale;        private SCConfigItem _scScaleFactor;        private SCConfigItem _scEnableAlarm;        private SCConfigItem _scDefaultSetPoint;        private SCConfigItem _scRegulationFactor;        private SCConfigItem _scSetLastPoint;        private SCConfigItem _scSetLastRamping;        //tolerance check        private float _alarmJudgmentRange;        private float _warningJudgmentRange;        private float _alarmJudgmentTime;        private float _warningJudgmentTime;        private float _toleranceJudgmentDelayTime;        private DeviceTimer _toleranceJudgmentDelayTimer = new DeviceTimer();        //stable check        private DeviceTimer _stableDelayTimer = new DeviceTimer();        private float _stableDelayTime;        private Stopwatch _stableJudgmentTimer = new Stopwatch();        private float _stableJudgmentTime = 1;        private float _stableMinValue;        private float _stableMaxValue;        public AlarmEventItem AlarmToleranceWarning { get; set; }        public AlarmEventItem AlarmToleranceAlarm { get; set; }        private bool _isFloatAioType = false;        private bool _isWait;        private float _waitHigh;        private float _waitLow;        private string _writeLog = "";        public IoMFC(string module, XmlElement node, string ioModule = "")        {            Unit = node.GetAttribute("unit");            base.Module = string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module");            base.Name = node.GetAttribute("id");            base.Display = node.GetAttribute("display");            base.DeviceID = node.GetAttribute("schematicId");            _aiFlow = ParseAiNode("aiFlow", node, ioModule);            _aoFlow = ParseAoNode("aoFlow", node, ioModule);            _aoRamp = ParseAoNode("aoRamp", node, ioModule);            _isFloatAioType = !string.IsNullOrEmpty(node.GetAttribute("aioType")) && (node.GetAttribute("aioType") == "float");            string scBasePath = node.GetAttribute("scBasePath");            if (string.IsNullOrEmpty(scBasePath))                scBasePath = $"{Module}.MFC.{Name}";            else            {                scBasePath = scBasePath.Replace("{module}", Module);            }            _scGasName = SC.GetConfigItem($"{scBasePath}.{Name}.GasName");            _scEnable = SC.GetConfigItem($"{scBasePath}.{Name}.Enable");            _isMFCInstalled = SC.GetConfigItem($"{scBasePath}.{Name}.IsMFCInstalled").BoolValue;            _scN2Scale = ParseScNode("scN2Scale", node, ioModule, $"{scBasePath}.{Name}.N2Scale");            _scScaleFactor = ParseScNode("scScaleFactor", node, ioModule, $"{scBasePath}.{Name}.ScaleFactor");            _scEnableAlarm = ParseScNode("scEnableAlarm", node, ioModule, $"{scBasePath}.{Name}.EnableAlarm");            _scDefaultSetPoint = ParseScNode("scDefaultSetPoint", node, ioModule, $"{scBasePath}.{Name}.DefaultSetPoint");            _scRegulationFactor = ParseScNode("scFlowRegulationFactor", node, ioModule, $"{scBasePath}.{Name}.RegulationFactor");            if (SC.ContainsItem($"{scBasePath}.{Name}.FlowUnit"))                Unit = SC.GetStringValue($"{scBasePath}.{Name}.FlowUnit");            if (SC.ContainsItem($"{scBasePath}.{Name}.SetLastPoint"))                _scSetLastPoint = SC.GetConfigItem($"{scBasePath}.{Name}.SetLastPoint");            if (SC.ContainsItem($"{scBasePath}.{Name}.SetLastRamping"))                _scSetLastRamping = SC.GetConfigItem($"{scBasePath}.{Name}.SetLastRamping");        }        public bool Initialize()        {            DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);            DATA.Subscribe($"{Module}.{Name}.Feedback", () => FeedBack);            DATA.Subscribe($"{Module}.{Name}.SetPoint", () => SetPoint);            //  DATA.Subscribe($"{Module}.{Name}.ForceOpen", () => _doOpen.Value);            //   DATA.Subscribe($"{Module}.{Name}.ForceClose", () => _doClose.Value);            OP.Subscribe($"{Module}.{Name}.Ramp", (out string reason, int time, object[] param) =>            {                double target = Convert.ToDouble(param[0].ToString());                if (target < 0 || target > Scale)                {                    reason = $"set {Display} value {target} out of range [0, {Scale}] {Unit}";                    return false;                }                Ramp(target, time);                if (time > 0)                {                    reason = $"{Display} ramp to {target} {Unit} in {time} seconds";                }                else                {                    reason = $"{Display} ramp to {target} {Unit}";                }                return true;            });            OP.Subscribe($"{Module}.{Name}.SetParameters", SetParameters);            OP.Subscribe($"{Module}.{Name}.SetMfcValue", SetMfcValue);            OP.Subscribe($"{Module}.{Name}.SetMfcVirtualValue", SetMfcVirtualValue);            //_phyScale = SC.GetValue<double>($"{Module}.MFC.{Name}.PhyScale");            //if (_scSetLastPoint != null)            //{            //    SetPoint = _scSetLastPoint.DoubleValue;            //}            //if (_scSetLastRamping != null)            //{            //    _aoRamp.FloatValue = (float)_scSetLastRamping.DoubleValue;            //}            return true;        }        public void Monitor()        {            if (!string.IsNullOrEmpty(_writeLog))            {                LOG.Write(_writeLog);                _writeLog = "";            }            //MonitorRamping();            //MonitorTolerance();        }        public bool ResetWarningChecker()        {            _toleranceCheckerWarning.Reset(_warningJudgmentTime);            return true;        }        public bool ResetAlarmChecker()        {            _toleranceCheckerAlarm.Reset(_alarmJudgmentTime);            return true;        }        public void Reset()        {            _toleranceCheckerWarning.Reset(_warningJudgmentTime);            _toleranceCheckerAlarm.Reset(_alarmJudgmentTime);            //AlarmToleranceWarning.Reset();            //AlarmToleranceAlarm.Reset();        }        public void Terminate()        {            Ramp(DefaultSetPoint, 0);        }        private bool SetParameters(out string reason, int time, object[] param)        {            reason = string.Empty;            var paras = param[0].ToString().Split(';');//flow;rampTime            _isWait = false;            float setpoint = 0.0f;            if (paras[0].ToString() == "Continue")            {                EV.PostInfoLog(Module, $"Set {DisplayName} flow to Continue");                return true;            }            else            {                if (System.Text.RegularExpressions.Regex.Match(paras[0].ToString(), @"[a-zA-Z]").Success)                {                    var table = paras[0].ToString().Split(':')[0];                    if (SC.ContainsItem($"{Module}.RecipeEditParameter.FlowSetting.{Name}.{table}"))                        setpoint = (float)SC.GetValue<double>($"{Module}.RecipeEditParameter.FlowSetting.{Name}.{table}");                }                else                {                    float.TryParse(paras[0].ToString(), out setpoint);                }            }            float ramp = 0;            if (paras.Length > 1)                float.TryParse(paras[1].ToString(), out ramp);            if (paras.Length >= 8)            {                var setUnit = paras[2].ToString();                var ramprateUnit = paras[3].ToString();                bool.TryParse(paras[4].ToString(), out _isWait);                float.TryParse(paras[5].ToString(), out float waitHigh);                float.TryParse(paras[6].ToString(), out float waitLow);                var waitUnit = paras[7].ToString();                if (waitUnit.ToLower() == "%sv")                {                    _waitHigh = setpoint * waitHigh;                    _waitLow = setpoint * waitLow;                }                else if (waitUnit.ToLower() == "%fs")                {                    _waitHigh = (float)_scN2Scale.DoubleValue * waitHigh;                    _waitLow = (float)_scN2Scale.DoubleValue * waitLow;                }                else                {                    _waitHigh = waitHigh;                    _waitLow = waitLow;                }                _stableJudgmentTimer.Stop();                if (ramprateUnit.ToLower() == "time")                {                    if (ramp > 0)                        ramp = (float)(setpoint - FeedBack) / ramp;                }                else if (ramprateUnit.ToLower() == "%")                {                    ramp = (float)_scN2Scale.DoubleValue * ramp;                }            }            SetPoint = setpoint;            _aoRamp.FloatValue = ramp;            //LOG.Write($"{Name} setpoint={setpoint}, ramp={ramp} wait={_isWait} waitHigh={_waitHigh} waitLow={_waitLow}");            _writeLog = $"{Name} setpoint={setpoint}, ramp={ramp} wait={_isWait} waitHigh={_waitHigh} waitLow={_waitLow}";            return true;        }        public bool SetMfcValue(out string reason, int time, object[] param)        {            reason = string.Empty;            float setpoint = 0.0f;            var paras = param[0].ToString().Split(';'); // setpoint;ramping;alarmWatchTable            if (System.Text.RegularExpressions.Regex.Match(paras[0].ToString(), @"[a-zA-Z]").Success)            {                var table = paras[0].ToString().Split(':')[0];                if (SC.ContainsItem($"{Module}.RecipeEditParameter.FlowSetting.{Name}.{table}"))                    setpoint = (float)SC.GetValue<double>($"{Module}.RecipeEditParameter.FlowSetting.{Name}.{table}");            }            else            {                float.TryParse(paras[0].ToString(), out setpoint);            }            float ramp = 0;            if (paras.Length > 1)                float.TryParse(paras[1].ToString(), out ramp);            if (paras.Length > 2)            {            }            SetPoint = setpoint;            _ramping = ramp;            _aoRamp.FloatValue = ramp;            LOG.Write($"{Name} setpoint={setpoint}, ramp={ramp}");            return true;        }        public bool SetMfcSetPoint(out string reason, int time, object[] param)        {            reason = string.Empty;            float setpoint = 0.0f;            var paras = param[0].ToString().Split(';'); // setpoint;ramping;alarmWatchTable            if (System.Text.RegularExpressions.Regex.Match(paras[0].ToString(), @"[a-zA-Z]").Success)            {                var table = paras[0].ToString().Split(':')[0];                if (SC.ContainsItem($"{Module}.RecipeEditParameter.FlowSetting.{Name}.{table}"))                    setpoint = (float)SC.GetValue<double>($"{Module}.RecipeEditParameter.FlowSetting.{Name}.{table}");            }            else            {                float.TryParse(paras[0].ToString(), out setpoint);            }            float ramp = 0;            if (paras.Length > 1)                float.TryParse(paras[1].ToString(), out ramp);            _setpoint = setpoint;            _ramping = ramp;            _aoRamp.FloatValue = ramp;            LOG.Write($"{Name} setpoint={setpoint}, ramp={ramp}");            return true;        }        public bool SetMfcVirtualValue(out string reason, int time, object[] param)        {            reason = string.Empty;            float setpoint = 0.0f;            var paras = param[0].ToString().Split(';'); // setpoint;ramping;alarmWatchTable            if (System.Text.RegularExpressions.Regex.Match(paras[0].ToString(), @"[a-zA-Z]").Success)            {                var table = paras[0].ToString().Split(':')[0];                if (SC.ContainsItem($"{Module}.RecipeEditParameter.FlowSetting.{Name}.{table}"))                    setpoint = (float)SC.GetValue<double>($"{Module}.RecipeEditParameter.FlowSetting.{Name}.{table}");            }            else            {                float.TryParse(paras[0].ToString(), out setpoint);            }            VirtualSetPoint = setpoint;            VirtualFeedBack = setpoint;            if (paras.Length > 1)            {                float.TryParse(paras[1].ToString(), out _ramping);                VirtualRamping = _ramping;            }            return true;        }        public bool Ramp(double flowSetPoint, int time, out string reason)        {            if (HasAlarm)            {                reason = $"{DisplayName} in error status, can not flow";                return false;            }            if (flowSetPoint < 0 || flowSetPoint > Scale)            {                reason = $"{DisplayName} range is [0, {Scale}], can not flow {flowSetPoint}";                return false;            }            if (time > 0)            {                EV.PostInfoLog(Module, $"Set {DisplayName} flow to {flowSetPoint} {Unit} in {time / 1000:F0} seconds");            }            else            {                EV.PostInfoLog(Module, $"Set {DisplayName} flow to {flowSetPoint} {Unit}");            }            Ramp(flowSetPoint, time);            reason = string.Empty;            return true;        }        public void Ramp(int time)        {            Ramp(DefaultSetPoint, time);        }        public void Ramp(double target, int time)        {            target = Math.Max(0, target);            target = Math.Min(Scale, target);            _rampInitValue = FeedBack;    //ramp 初始值取当前设定值,而非实际读取值.零漂问题            _rampTime = time;            _rampTarget = target;            _rampTimer.Start(time);        }        public void StopRamp()        {            Ramp(SetPoint, 0);        }        private void MonitorRamping()        {            if (!_rampTimer.IsIdle())            {                if (_rampTimer.IsTimeout() || _rampTime == 0)                {                    _rampTimer.Stop();                    SetPoint = _rampTarget;                }                else                {                    SetPoint = _rampInitValue + (_rampTarget - _rampInitValue) * _rampTimer.GetElapseTime() / _rampTime;                }            }        }        private void MonitorTolerance()        {            if (!EnableAlarm || SetPoint < 0.01 ||                _toleranceJudgmentDelayTimer.IsIdle() ||                _toleranceJudgmentDelayTimer.GetElapseTime() < _toleranceJudgmentDelayTime * 1000)            {                _toleranceCheckerWarning.RST = true;                _toleranceCheckerAlarm.RST = true;                return;            }            if (_warningJudgmentRange != 0 && _warningJudgmentTime > 0)            {                _toleranceCheckerWarning.Monitor(FeedBack, (SetPoint - Math.Abs(_warningJudgmentRange)), (SetPoint + Math.Abs(_warningJudgmentRange)), _warningJudgmentTime);                if (_toleranceCheckerWarning.Trig)                {                    //AlarmToleranceWarning.Description = $"{Display} flow out of range {_warningJudgmentRange} {Unit} in {_warningJudgmentTime:F0} seconds";                    AlarmToleranceWarning.Set($"{Display} flow out of range {_warningJudgmentRange} {Unit} in {_warningJudgmentTime:F0} seconds");                }            }            if (_alarmJudgmentRange != 0 && _alarmJudgmentTime > 0)            {                _toleranceCheckerAlarm.Monitor(FeedBack, (SetPoint - Math.Abs(_alarmJudgmentRange)), (SetPoint + Math.Abs(_alarmJudgmentRange)), _alarmJudgmentTime);                if (_toleranceCheckerAlarm.Trig)                {                    //AlarmToleranceAlarm.Description = $"{Display} flow out of range {_alarmJudgmentRange} {Unit} in {_alarmJudgmentTime:F0} seconds";                    AlarmToleranceAlarm.Set($"{Display} flow out of range {_alarmJudgmentRange} {Unit} in {_alarmJudgmentTime:F0} seconds");                }            }        }        public bool CheckWaitCondition(out string reason)        {            reason = "";            if (!_isWait || _waitHigh == 0 || _waitLow == 0)                return true;            if (_stableJudgmentTimer.IsRunning)            {                if (FeedBack < SetPoint - _waitLow ||                    FeedBack > SetPoint + _waitHigh)                {                    _stableJudgmentTimer.Restart();                }                if (_stableJudgmentTimer.ElapsedMilliseconds >= _stableJudgmentTime * 1000)                {                    return true;                }            }            else            {                _stableJudgmentTimer.Restart();            }            reason = $"{Name} feedback={FeedBack}, wait limit is ({SetPoint - _waitLow}, {SetPoint + _waitHigh})";            return false;        }    }}
 |