diff --git a/TBF/Rig/Uni/RegValve/ChangeRegValvePositionOp.cs b/TBF/Rig/Uni/RegValve/ChangeRegValvePositionOp.cs index 07efc054d..e4ff3520d 100644 --- a/TBF/Rig/Uni/RegValve/ChangeRegValvePositionOp.cs +++ b/TBF/Rig/Uni/RegValve/ChangeRegValvePositionOp.cs @@ -1,10 +1,9 @@ /// -/// Copyright (c) 2021-2023 Sensus Slovensko a.s. +/// Copyright (c) 2021 Sensus Metering Systems /// using System; using System.Collections.Generic; using log4net; -using TBF.Rig.ControlBoard.Uni; using TBF.Rig.GenericDevices; namespace TBF.Rig.Uni.RegValve @@ -14,12 +13,12 @@ namespace TBF.Rig.Uni.RegValve private static readonly ILog log = LogManager.GetLogger(typeof(ChangeRegValvePositionOp)); public override string ToString() { - return string.Format("ChangeRegValvePositionOp({0},{1}s)", regV.Name, timePulseSec.ToString("F2")); + return string.Format("ChangeRegValvePositionOp({0},{1}s)", regulValve.Name, timePulseSec.ToString("F2")); } /// Set by the constructor - readonly UniCB uniCB; - readonly RegValve regV; + readonly TBF.Rig.ControlBoard.Uni.UniCB controlBoard; + readonly RegValve regulValve; readonly int regulValveNr; readonly double timePulseSec; @@ -33,14 +32,14 @@ namespace TBF.Rig.Uni.RegValve /// Upper limit of the position to be achieved /// Timeout in sec. for setting the flow /// Only Elde.Valve flow are used, other flow on the lists are ignored - public ChangeRegValvePositionOp(UniCB uniCB, RegValve regV, double timePulseSec) + public ChangeRegValvePositionOp(TBF.Rig.ControlBoard.IControlBoard cb, RegValve rv, double timePulseSec) { - this.uniCB = uniCB; - if (this.uniCB == null) throw new ArgumentNullException("ctrlBoard"); + controlBoard = cb as TBF.Rig.ControlBoard.Uni.UniCB; + if (controlBoard == null) throw new ArgumentNullException("ctrlBoard"); - this.regV = regV as RegValve; - if (this.regV == null) throw new ArgumentNullException("regValve is null or not Uni"); - regulValveNr = this.regV.Idx1; + regulValve = rv as RegValve; + if (regulValve == null) throw new ArgumentNullException("regValve is null or not Elde"); + regulValveNr = this.regulValve.Idx1; this.timePulseSec = timePulseSec; @@ -50,7 +49,13 @@ namespace TBF.Rig.Uni.RegValve /// Start this operation public void Start() { - uniCB.RegVlvIncrMove(false, regulValveNr, timePulseSec); + //double positionPct = controlBoard.RValvePosition(regulValveNr); + //log.WarnFormat("RV#={0}, actPos={1}%", regulValveNr, positionPct.ToString("F1")); + + //controlBoard.ValveMove(regulValveNr, + // TBF.Rig.ControlBoard.Legacy.RegulValveMode.PulseWidth, + // new double[2] { timePulseSec, timePulseSec }, + // regulValve.StableTime); } /// Run this operation @@ -59,6 +64,9 @@ namespace TBF.Rig.Uni.RegValve /// public Event Run() { + //float positionPct = controlBoard.RValvePosition(regulValveNr); + //log.WarnFormat("RV#={0}, actPos={1}%", regulValveNr, positionPct.ToString("F1")); + return Event.PositionReached; } diff --git a/TBF/Rig/Uni/RegValve/Factory.cs b/TBF/Rig/Uni/RegValve/Factory.cs index 4c0c96e9c..0c0dafb98 100644 --- a/TBF/Rig/Uni/RegValve/Factory.cs +++ b/TBF/Rig/Uni/RegValve/Factory.cs @@ -1,5 +1,5 @@ /// -/// Copyright (c) 2021 Sensus Slovensko a.s. +/// Copyright (c) 2021 Sensus Metering Systems /// using System.Collections.Generic; using TBF.Rig.Generic; diff --git a/TBF/Rig/Uni/RegValve/Handlers.cs b/TBF/Rig/Uni/RegValve/Handlers.cs index b16a486f6..2ba144a37 100644 --- a/TBF/Rig/Uni/RegValve/Handlers.cs +++ b/TBF/Rig/Uni/RegValve/Handlers.cs @@ -1,5 +1,5 @@ /// -/// Copyright (c) 2021 Sensus Slovensko a.s. +/// Copyright (c) 2021 Sensus Metering Systems /// using System; using log4net; diff --git a/TBF/Rig/Uni/RegValve/SetFlowOp.cs b/TBF/Rig/Uni/RegValve/SetFlowOp.cs index 36cc84667..217acf5df 100644 --- a/TBF/Rig/Uni/RegValve/SetFlowOp.cs +++ b/TBF/Rig/Uni/RegValve/SetFlowOp.cs @@ -1,23 +1,24 @@ -using Config.Entities; -using log4net; -using SharedComponents; +/// +/// Copyright (c) 2021 Sensus Slovensko a.s. +/// using System; using System.Collections.Generic; -using System.Diagnostics; -using TBF.Boxes; +using log4net; +using Config.Entities; using TBF.Rig.ControlBoard.Uni; using TBF.Rig.GenericDevices; +using TBF.Boxes; namespace TBF.Rig.Uni.RegValve { - public class SetFlowOp : IOperation - { - private static readonly ILog log = LogManager.GetLogger(typeof(SetFlowOp)); + public class SetFlowOp : IOperation + { + private static readonly ILog log = LogManager.GetLogger(typeof(SetFlowOp)); + public override string ToString() + { + return string.Format("SetFlowOp({0}, Qfrom={1}, Qto={2})", regV.Name, shrinkedTgtFlowLo, shrinkedTgtFlowHi); + } - public override string ToString() - { - return string.Format("SetFlowOp({0}, Qfrom={1}, Qto={2})", regV.Name, shrinkedTgtFlowLo, shrinkedTgtFlowHi); - } public const double RqrdFlowRangeRatio = 0.5; @@ -35,54 +36,58 @@ namespace TBF.Rig.Uni.RegValve readonly bool leaveFlowControlRunning; readonly double maximalFlow; - + /// - /// Internal state of this operation - /// - enum OpState - { - Idle = 0, - ValveMoveToPosition1, - ValveMoveToPosition2, - ValveMoveToPosition3, - SettingPosition, - Wait4FlowMsrmntAndSendRVMove, - SettingFlow, - FlowReached, - SendCommandAgain, - } - OpState opState; + /// Internal state of this operation + /// + enum OpState + { + Idle = 0, + ValveMoveToPosition1, /// Wait 1 takt + ValveMoveToPosition2, /// This is actual move to position + ValveMoveToPosition3, /// Wait 1 takt + SettingPosition, + Wait4FlowMsrmntAndSendRVMove, /// Wait until the flow measurement starts, then issue reg. valve move command + SettingFlow, /// Measure the flow and verify whether it is within limits sufficiently long time + FlowReached, + SendCommandAgain, + } + OpState opState; double currentReqFlowLo; double currentReqFlowHi; - double targetPositionLo; - double targetPositionHi; + double targetPositionLo; + double targetPositionHi; + int startTime; int setFlowTime; int expireTime; - DoubleBox msrdFlow; + DoubleBox flowBox; int isFlowOkDuration; - /// + + /// /// Set required water flow. Conditionally leave the measurement running. - /// Events: FlowSet, FlowTimeOut - /// - public SetFlowOp( - UniCB uniCB, - IRegValve regValve, - IFlowMeter flowMeter, - double qFrom, - double qTo, - DoubleBox msrdFlow, - int timeout, - int delay, - bool leaveFlowControlRunning) + /// Events: FlowSet, FlowTimeOut + /// + /// Control board device + /// Regulation valve component + /// Flowmeter component + /// Lower limit of the flow to be achieved in [m3/h] + /// Upper limit of the flow to be achieved in [m3/h] + /// PID coefficient (float) + /// Timeout for the flow setting in [s] + /// Flow setting starts after this delay [s] + /// true = Leave the measurement running after op. stop + /// Only Elde.Valve flow are used, other flow on the lists are ignored + public SetFlowOp(UniCB uniCB, IRegValve regValve, IFlowMeter flowMeter, double qFrom, double qTo, DoubleBox flowBox, + int timeout, int delay, bool leaveFlowControlRunning) { this.uniCB = uniCB; - if (this.uniCB == null) throw new ArgumentNullException("Control board is null or not Uni"); + if (this.uniCB == null) throw new ArgumentNullException("cBoard is null or not Uni"); this.regV = regValve as RegValve; - if (this.regV == null) throw new ArgumentNullException(string.Format("{0} is not Uni.RegValve", regValve.Name)); + if (this.regV == null) throw new ArgumentNullException("regValve is null or not Uni"); this.flowMeter = flowMeter; if (this.flowMeter == null) throw new ArgumentNullException("flowMeter"); @@ -92,398 +97,249 @@ namespace TBF.Rig.Uni.RegValve double rqrdFlowLoBeforeCorr = qFrom - MeasurementCorrection.GetCorrection(qFrom, flowMeter.Corrections); double rqrdFlowHiBeforeCorr = qTo - MeasurementCorrection.GetCorrection(qTo, flowMeter.Corrections); targetFlowAve = (rqrdFlowLoBeforeCorr + rqrdFlowHiBeforeCorr) / 2; - shrinkedTgtFlowLo = (RqrdFlowRangeRatio * rqrdFlowLoBeforeCorr) - + ((1 - RqrdFlowRangeRatio) * targetFlowAve); - shrinkedTgtFlowHi = (RqrdFlowRangeRatio * rqrdFlowHiBeforeCorr) - + ((1 - RqrdFlowRangeRatio) * targetFlowAve); + shrinkedTgtFlowLo = (RqrdFlowRangeRatio * rqrdFlowLoBeforeCorr) + ((1 - RqrdFlowRangeRatio) * targetFlowAve); /// Move the lower limit 15% of the range up + shrinkedTgtFlowHi = (RqrdFlowRangeRatio * rqrdFlowHiBeforeCorr) + ((1 - RqrdFlowRangeRatio) * targetFlowAve); /// Move the upper limit 15% of the range down + + this.flowBox = flowBox; + if (this.flowBox == null) throw new ArgumentNullException("flowBox"); - this.msrdFlow = msrdFlow; this.timeout = timeout; this.delay = delay; + this.leaveFlowControlRunning = leaveFlowControlRunning; - log.Debug(this.ToString()); - - LiveLogDiag.Log1( - "{0} >> CTOR() >> qFrom={1} qTo={2} rqrdFlowLoBeforeCorr={3} rqrdFlowHiBeforeCorr={4} targetFlowAve={5} shrinkedTgtFlowLo={6} shrinkedTgtFlowHi={7} nominalFlow={8} nominalFreq={9} maximalFlow={10} delay={11} timeout={12} leaveFlowControlRunning={13}", - regV.Name, qFrom, qTo, rqrdFlowLoBeforeCorr, rqrdFlowHiBeforeCorr, targetFlowAve, - shrinkedTgtFlowLo, shrinkedTgtFlowHi, flowMeter.NominalFlow, flowMeter.NominalFreq, - maximalFlow, delay, timeout, leaveFlowControlRunning); + log.Debug(this.ToString()); } + /// + /// Set required water flow - Do not leave the measurement running. + /// Events: FlowSet + /// public SetFlowOp(UniCB uniCB, IRegValve regValve, IFlowMeter flowMeter, double qFrom, double qTo, DoubleBox flowbox, int timeout, int delay) : this(uniCB, regValve, flowMeter, qFrom, qTo, flowbox, timeout, delay, false) { } + /// + /// Set required water flow - Do not leave the measurement running. + /// Events: FlowSet + /// public SetFlowOp(UniCB uniCB, IRegValve regValve, IFlowMeter flowMeter, double qFrom, double qTo, DoubleBox flowbox, int timeout) : this(uniCB, regValve, flowMeter, qFrom, qTo, flowbox, timeout, 0, false) { } + /// + /// Set required water flow - No timeout. + /// Events: FlowSet + /// public SetFlowOp(UniCB uniCB, IRegValve regValve, IFlowMeter flowMeter, double qFrom, double qTo, DoubleBox flowbox) : this(uniCB, regValve, flowMeter, qFrom, qTo, flowbox, int.MaxValue, 0, false) { } - /// + /// /// Fetch target position limits from the dictionary or return false - /// + /// + /// Average of the flow targer range (input) + /// Target valve position low limit (output) + /// Target valve position high limit (output) + /// true when positions for the target flow are stored in the memory, otherwise return false bool FetchTargetPosition(double avgReqFlow, out double positionLo, out double positionHi) - { + { double targetPosition; - if (regV.Dict.TryGetValue(avgReqFlow, out targetPosition)) - { - positionLo = Math.Max(targetPosition * 0.95, 0.0); - positionHi = Math.Min(targetPosition * 1.05, 100.0); - - LiveLogDiag.Log1( - "{0} >> FetchTargetPosition() >> avgReqFlow={1} FOUND targetPosition={2} positionLo={3} positionHi={4}", - regV.Name, avgReqFlow, targetPosition, positionLo, positionHi); - - return true; - } - else - { - positionLo = 0; - positionHi = 0; - - LiveLogDiag.Log1( - "{0} >> FetchTargetPosition() >> avgReqFlow={1} NOT FOUND", - regV.Name, avgReqFlow); - - return false; - } - } + if (regV.Dict.TryGetValue(avgReqFlow, out targetPosition)) + { + positionLo = Math.Max(targetPosition * 0.95, 0.0); + positionHi = Math.Min(targetPosition * 1.05, 100.0); + return true; + } + else + { + positionLo = 0; + positionHi = 0; + return false; + } + } void StoreTargetPosition(double avgReqFlow, double actPosition) - { - if (!regV.Dict.ContainsKey(targetFlowAve)) - { + { + if (!regV.Dict.ContainsKey(targetFlowAve)) + { regV.Dict.Add(new KeyValuePair(avgReqFlow, actPosition)); + } + return; + } - LiveLogDiag.Log1( - "{0} >> StoreTargetPosition() >> STORED avgReqFlow={1} actPosition={2}", - regV.Name, avgReqFlow, actPosition); - } - else - { - LiveLogDiag.Log1( - "{0} >> StoreTargetPosition() >> SKIPPED because targetFlowAve={1} already exists in Dict", - regV.Name, targetFlowAve); - } - - return; - } - - /// Start this operation - public void Start() + /// + /// Start this operation + /// + public void Start() { + log.InfoFormat("SetFlowOp:Start() rv#={0} flowMtr#={1} TARGET: flowLo={2} flowHi={3}", + regV.Idx1, flowMeter.Idx1, currentReqFlowLo, currentReqFlowHi); + + /// Store the current time, etc. + startTime = StateMachine.Time; setFlowTime = StateMachine.Time + delay; expireTime = StateMachine.Time + timeout; if (expireTime < 0) expireTime = int.MaxValue; isFlowOkDuration = 0; - currentReqFlowLo = 0; - currentReqFlowHi = 0; - - log.InfoFormat( - "SetFlowOp:Start() rv#={0} flowMtr#={1} TARGET_SHRINKED: flowLo={2} flowHi={3}", - regV.Idx1, flowMeter.Idx1, shrinkedTgtFlowLo, shrinkedTgtFlowHi); - - LiveLogDiag.Log1( - "{0} >> Start() >> rv#={1} flowMtr#={2} setFlowTime={3} expireTime={4} delay={5} timeout={6} shrinkedTgtFlowLo={7} shrinkedTgtFlowHi={8} targetFlowAve={9} nominalFlow={10} nominalFreq={11}", - regV.Name, regV.Idx1, flowMeter.Idx1, setFlowTime, expireTime, delay, timeout, - shrinkedTgtFlowLo, shrinkedTgtFlowHi, targetFlowAve, flowMeter.NominalFlow, flowMeter.NominalFreq); /// Start the flow measurement - LiveLogDiag.Log1( - "{0} >> Start() >> calling uniCB.MeasureFlow(false, flowMeter.Idx1={1})", - regV.Name, flowMeter.Idx1); - uniCB.MeasureFlow(false, flowMeter.Idx1); - opState = OpState.Wait4FlowMsrmntAndSendRVMove; + opState = OpState.Wait4FlowMsrmntAndSendRVMove; + } - LiveLogDiag.Log1( - "{0} >> Start() >> opState={1}", - regV.Name, opState); - } - - /// + /// /// Run this operation /// - public Event Run() - { + /// + /// Event.OpArgumentError + /// Event.Starting + /// Event.Busy + /// Event.FlowReached + /// Event.RegulValveTimeOut + /// + public Event Run() + { log.DebugFormat("Op.Run() opState={0}", opState); - LiveLogDiag.Log1( - "{0} >> Run() >> time={1} opState={2} currentReqFlowLo={3} currentReqFlowHi={4} isFlowOkDuration={5}", - regV.Name, StateMachine.Time, opState, currentReqFlowLo, currentReqFlowHi, isFlowOkDuration); - - if (StateMachine.Time > expireTime) - { - LiveLogDiag.Log1( - "{0} >> Run() >> TIMEOUT: StateMachine.Time={1} > expireTime={2}", - regV.Name, StateMachine.Time, expireTime); - - return Event.RegulValveTimeOut; - } + if (StateMachine.Time > expireTime) return Event.RegulValveTimeOut; if (opState == OpState.Wait4FlowMsrmntAndSendRVMove) { - LiveLogDiag.Log1( - "{0} >> Run() >> Wait4FlowMsrmntAndSendRVMove: MsrmntAvailable={1}", - regV.Name, flowMeter.MsrmntAvailable); - - if (!flowMeter.MsrmntAvailable) - return Event.Starting; + if (!flowMeter.MsrmntAvailable) return Event.Starting; double flow1 = flowMeter.ReadFlow(); - LiveLogDiag.Log1( - "{0} >> Run() >> Wait4FlowMsrmntAndSendRVMove: initial measured flow1={1}", - regV.Name, flow1); - - /// Set the target flow/frequency range - currentReqFlowLo = shrinkedTgtFlowLo; - currentReqFlowHi = shrinkedTgtFlowHi; - - LiveLogDiag.Log1( - "{0} >> Run() >> target range assigned: currentReqFlowLo={1} currentReqFlowHi={2} maximalFlow={3}", - regV.Name, currentReqFlowLo, currentReqFlowHi, maximalFlow); - - //if (flow1 >= targetFlowAve) currentReqFlowHi = targetFlowAve; - //if (flow1 <= targetFlowAve) currentReqFlowLo = targetFlowAve; + /// Set the targer flow/frequency range + currentReqFlowLo = shrinkedTgtFlowLo; /// Default lower limit + currentReqFlowHi = shrinkedTgtFlowHi; /// Default upper limit + //if (flow1 >= targetFlowAve) currentReqFlowHi = targetFlowAve; /// Decrease upper limit + //if (flow1 <= targetFlowAve) currentReqFlowLo = targetFlowAve; /// Increase lower limit if ((currentReqFlowLo >= currentReqFlowHi) || (currentReqFlowLo > maximalFlow) || (currentReqFlowHi <= 0)) { - LiveLogDiag.Log1( - "{0} >> Run() >> OpArgumentError: currentReqFlowLo={1} currentReqFlowHi={2} maximalFlow={3}", - regV.Name, currentReqFlowLo, currentReqFlowHi, maximalFlow); - return Event.OpArgumentError; } if (StateMachine.Time > setFlowTime) { - double freqLo = flowMeter.NominalFreq * currentReqFlowLo / flowMeter.NominalFlow; - double freqHi = flowMeter.NominalFreq * currentReqFlowHi / flowMeter.NominalFlow; - - LiveLogDiag.Log1( - "{0} >> Run() >> calling uniCB.SetFlow(false, rv#={1}, freqLo={2}, freqHi={3}) from nominalFreq={4}, nominalFlow={5}, flowLo={6}, flowHi={7}", - regV.Name, regV.Idx1, freqLo, freqHi, flowMeter.NominalFreq, flowMeter.NominalFlow, currentReqFlowLo, currentReqFlowHi); - - uniCB.SetFlow(false, regV.Idx1, freqLo, freqHi); + uniCB.SetFlow(false, regV.Idx1, flowMeter.NominalFreq * currentReqFlowLo / flowMeter.NominalFlow, + flowMeter.NominalFreq * currentReqFlowHi / flowMeter.NominalFlow); log.InfoFormat("Run(): rv#={0} TARGET: flowLo={1} flowHi={2}", regV.Idx1, currentReqFlowLo, currentReqFlowHi); opState = OpState.SettingFlow; - - LiveLogDiag.Log1( - "{0} >> Run() >> opState changed to {1}", - regV.Name, opState); - } - else - { - LiveLogDiag.Log1( - "{0} >> Run() >> waiting for delay: StateMachine.Time={1}, setFlowTime={2}", - regV.Name, StateMachine.Time, setFlowTime); } return Event.Starting; } - if (opState == OpState.ValveMoveToPosition1) - { - LiveLogDiag.Log1("{0} >> Run() >> ValveMoveToPosition1 -> ValveMoveToPosition2", regV.Name); - opState = OpState.ValveMoveToPosition2; - return Event.Starting; - } - else if (opState == OpState.ValveMoveToPosition2) - { - LiveLogDiag.Log1( - "{0} >> Run() >> ValveMoveToPosition2: MoveToPosition(targetPositionLo={1}, targetPositionHi={2})", - regV.Name, targetPositionLo, targetPositionHi); - - regV.MoveToPosition(false, targetPositionLo, targetPositionHi); + if (opState == OpState.ValveMoveToPosition1) + { + opState = OpState.ValveMoveToPosition2; + return Event.Starting; + } + else if (opState == OpState.ValveMoveToPosition2) /// Move to position + { + /// Issues the appropriate ValveMove(...) command + regV.MoveToPosition(false, targetPositionLo, targetPositionHi); + opState = OpState.SettingPosition; + return Event.Starting; + } + else if (opState == OpState.ValveMoveToPosition3) + { opState = OpState.SettingPosition; - - LiveLogDiag.Log1("{0} >> Run() >> opState changed to {1}", regV.Name, opState); - return Event.Starting; - } - else if (opState == OpState.ValveMoveToPosition3) - { - LiveLogDiag.Log1("{0} >> Run() >> ValveMoveToPosition3 -> SettingPosition", regV.Name); - - opState = OpState.SettingPosition; - return Event.Starting; - } - else if (opState == OpState.SettingPosition) - { + return Event.Starting; + } + else if (opState == OpState.SettingPosition) + { + /// Repeated untill position is reached double rvPosition = regV.Position; - - LiveLogDiag.Log1( - "{0} >> Run() >> SettingPosition: rvPosition={1}, targetPositionLo={2}, targetPositionHi={3}", - regV.Name, rvPosition, targetPositionLo, targetPositionHi); - if ((targetPositionLo <= rvPosition) && (rvPosition <= targetPositionHi)) - { - opState = OpState.Wait4FlowMsrmntAndSendRVMove; - - LiveLogDiag.Log1( - "{0} >> Run() >> position reached, opState changed to {1}", - regV.Name, opState); - } - - return Event.Starting; - } - else if (opState == OpState.SettingFlow) - { + { + opState = OpState.Wait4FlowMsrmntAndSendRVMove; + } + return Event.Starting; + } + else if (opState == OpState.SettingFlow) + { + /// Regulation valve is setting flow to the required value + /// double frequency = flowMeter.ReadFrequency(); double flow = flowMeter.ReadFlow(); + if (flow != 0) flowBox.Val = flow; - LiveLogDiag.Log1( - "{0} >> Run() >> SettingFlow: frequency={1} flow={2} reqLo={3} reqHi={4}", - regV.Name, frequency, flow, currentReqFlowLo, currentReqFlowHi); - - if (msrdFlow != null && flow != 0) - { - msrdFlow.Val = flow; - - LiveLogDiag.Log1( - "{0} >> Run() >> SettingFlow: msrdFlow.Val updated to {1}", - regV.Name, msrdFlow.Val); - } - - if (currentReqFlowLo <= flow && flow <= currentReqFlowHi) - { + if ((currentReqFlowLo <= flow) && (flow <= currentReqFlowHi)) + { + /// Flow is within range isFlowOkDuration++; - LiveLogDiag.Log1( - "{0} >> Run() >> SettingFlow: flow IN RANGE, isFlowOkDuration={1}/{2}", - regV.Name, isFlowOkDuration, regV.FlowStableSec); - if (isFlowOkDuration >= regV.FlowStableSec) - { - if (regV.StoredPositionReuse) - { - double rvPosition = regV.Position; + { + /// Flow is within range for sufficiently long time + if (regV.regValveCfg.StoredPositionReuse) + { + double rvPosition = regV.Position; /// Read the current position double storedPosition; - - LiveLogDiag.Log1( - "{0} >> Run() >> StoredPositionReuse ON: rvPosition={1} targetFlowAve={2}", - regV.Name, rvPosition, targetFlowAve); - - if (regV.Dict.TryGetValue(targetFlowAve, out storedPosition)) - { + if (regV.Dict.TryGetValue(targetFlowAve, out storedPosition)) + { + /// update the stored valve position StoreTargetPosition(targetFlowAve, (rvPosition + storedPosition) / 2.0); + log.InfoFormat("Run(): rv#={0} reqFlow={1} pos={2}% stored={3} <--- Updating a stored position", + regV.Idx1, targetFlowAve, rvPosition.ToString("F1"), storedPosition); + } + else + { + /// store the valve position + StoreTargetPosition(targetFlowAve, rvPosition); + log.InfoFormat("Run(): rv#={0} reqFlow={1} pos={2}% <--- Storing a new position", + regV.Idx1, targetFlowAve, rvPosition.ToString("F1")); + } + } - log.InfoFormat( - "Run(): rv#={0} reqFlow={1} pos={2}% stored={3} <--- Updating a stored position", - regV.Idx1, targetFlowAve, rvPosition.ToString("F1"), storedPosition); + log.InfoFormat("flow = {0} m3/h (lo={1}, hi={2}, REACHED)", flow, currentReqFlowLo, currentReqFlowHi); - LiveLogDiag.Log1( - "{0} >> Run() >> updating stored position: old={1} newAvg={2}", - regV.Name, storedPosition, (rvPosition + storedPosition) / 2.0); - } - else - { - StoreTargetPosition(targetFlowAve, rvPosition); - - log.InfoFormat( - "Run(): rv#={0} reqFlow={1} pos={2}% <--- Storing a new position", - regV.Idx1, targetFlowAve, rvPosition.ToString("F1")); - - LiveLogDiag.Log1( - "{0} >> Run() >> storing new position: {1}", - regV.Name, rvPosition); - } - } - - log.InfoFormat("flow = {0} m3/h (lo={1}, hi={2}, REACHED)", flow, currentReqFlowLo, currentReqFlowHi); - - opState = OpState.FlowReached; - - LiveLogDiag.Log1( - "{0} >> Run() >> FLOW REACHED, opState changed to {1}", - regV.Name, opState); - - return Event.FlowReached; - } - else - { - log.InfoFormat("flow = {0} m3/h (lo={1}, hi={2}, FLOW_OK_TIMER={3}s)", flow, currentReqFlowLo, currentReqFlowHi, isFlowOkDuration); - - LiveLogDiag.Log1( - "{0} >> Run() >> flow in range but not stable long enough yet", - regV.Name); - - return Event.Busy; - } - } - else - { + opState = OpState.FlowReached; + return Event.FlowReached; + } + else + { + log.InfoFormat("flow = {0} m3/h (lo={1}, hi={2}, FLOW_OK_TIMER={3}s)", flow, currentReqFlowLo, currentReqFlowHi, isFlowOkDuration); + return Event.Busy; + } + } + else + { + /// Flow is out of range isFlowOkDuration = 0; - + log.InfoFormat("flow = {0} m3/h (lo={1}, hi={2})", flow, currentReqFlowLo, currentReqFlowHi); + return Event.Busy; + } + } + else /// opState == OpState.FlowReached + { + log.InfoFormat("flow = {0} m3/h (lo={1}, hi={2}, REACHED)", flowMeter.ReadFlow(), currentReqFlowLo, currentReqFlowHi); + return Event.FlowReached; + } + } - LiveLogDiag.Log1( - "{0} >> Run() >> SettingFlow: flow OUT OF RANGE, reset isFlowOkDuration to 0", - regV.Name); - - return Event.Busy; - } - } - else - { - double finalFlow = flowMeter.ReadFlow(); - - log.InfoFormat("flow = {0} m3/h (lo={1}, hi={2}, REACHED)", finalFlow, currentReqFlowLo, currentReqFlowHi); - - LiveLogDiag.Log1( - "{0} >> Run() >> FlowReached state: finalFlow={1} reqLo={2} reqHi={3}", - regV.Name, finalFlow, currentReqFlowLo, currentReqFlowHi); - - return Event.FlowReached; - } - } - - /// Stop this operation + /// Start this operation public void Stop() - { - LiveLogDiag.Log1( - "{0} >> Stop() >> leaveFlowControlRunning={1} opState(before)={2}", - regV.Name, leaveFlowControlRunning, opState); - + { if (!leaveFlowControlRunning) { - LiveLogDiag.Log1( - "{0} >> Stop() >> calling uniCB.StopFlowControl(false, regV.Idx1={1})", - regV.Name, regV.Idx1); - + /// Stop flow measurement uniCB.StopFlowControl(false, regV.Idx1); } opState = OpState.Idle; - - LiveLogDiag.Log1( - "{0} >> Stop() >> opState(after)={1}", - regV.Name, opState); } - - /// - /// Logging - /// For activation use compilation condition: LIVELOGDIAG_FlowMeter_cs - /// - static class LiveLogDiag - { - [Conditional("LIVELOGDIAG_RegValve_cs")] - public static void Log1(string format, params object[] args) - { - LiveLogCache.Instance.AddLog("RegValve.cs LOG>> " + string.Format(format, args)); - } - } - } -} \ No newline at end of file + } +} diff --git a/TBF/Rig/Uni/RegValve/SetRegValvePositionOp.cs b/TBF/Rig/Uni/RegValve/SetRegValvePositionOp.cs index c81e97844..fbc94082e 100644 --- a/TBF/Rig/Uni/RegValve/SetRegValvePositionOp.cs +++ b/TBF/Rig/Uni/RegValve/SetRegValvePositionOp.cs @@ -30,7 +30,7 @@ namespace TBF.Rig.Uni.RegValve OpState opState; /// Set by the constructor - readonly UniCB uniCB; + readonly UniCB cBoard; readonly RegValve regV; readonly int regValveNr; readonly double posLoPct; @@ -45,23 +45,24 @@ namespace TBF.Rig.Uni.RegValve /// Set required water flow. /// Events: FlowSet, FlowTimeOut /// - /// Control board device + /// Control board device /// Regulation valve component /// Lower limit of the position to be achieved /// Upper limit of the position to be achieved /// Timeout in sec. for setting the flow /// Only Elde.Valve flow are used, other flow on the lists are ignored - public SetRegValvePositionOp(UniCB uniCB, RegValve regV, double posLoPct, double posHiPct, int timeout) + public SetRegValvePositionOp(UniCB cBoard, RegValve regValve, double posLoPct, double posHiPct, int timeout) { - this.uniCB = uniCB; - if (this.uniCB == null) throw new ArgumentNullException("ctrlBoard"); + this.cBoard = cBoard; + if (this.cBoard == null) throw new ArgumentNullException("ctrlBoard"); - this.regV = regV as RegValve; - if (this.regV == null) throw new ArgumentNullException("regValve is null or not Uni"); + this.regV = regValve as RegValve; + if (this.regV == null) throw new ArgumentNullException("regValve is null or not Elde"); this.regValveNr = this.regV.Idx1; this.posLoPct = posLoPct; this.posHiPct = posHiPct; + this.timeout = timeout; log.Debug(this.ToString());