450 lines
16 KiB
C#
450 lines
16 KiB
C#
///
|
|
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using log4net;
|
|
using TBF.BenchControl.ControlBoard.Uni;
|
|
using TBF.BenchControl.GenericDevices;
|
|
using TBF.Boxes;
|
|
|
|
namespace TBF.BenchControl.Uni.RegValve
|
|
{
|
|
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);
|
|
}
|
|
|
|
/// Set by the constructor
|
|
readonly UniCB cBoard;
|
|
readonly RegValve regV;
|
|
readonly int regulValveNr;
|
|
readonly IFlowMeter flowMeter;
|
|
readonly double shrinkedTgtFlowLo;
|
|
readonly double shrinkedTgtFlowHi;
|
|
readonly double targetFlowAve;
|
|
readonly int timeout;
|
|
readonly bool leaveMeasurementRunning;
|
|
readonly bool reTryCommands;
|
|
|
|
readonly double nominalFlow;
|
|
readonly double maximalFlow;
|
|
|
|
///
|
|
/// Internal states of this operation
|
|
///
|
|
enum OpState
|
|
{
|
|
Idle = 0,
|
|
ValveMoveToPosition1, /// Wait 1 takt
|
|
ValveMoveToPosition2, /// This is actual move to position
|
|
ValveMoveToPosition3, /// Wait 1 takt
|
|
CheckStatePosition, /// Verify
|
|
SettingPosition,
|
|
ValveMoveToFlow1, /// Wait 1 takt
|
|
ValveMoveToFlow2, /// This is actual move to flow
|
|
ValveMoveToFlow3, /// Wait 1 takt
|
|
CheckStateFlow, /// Verify
|
|
SettingFlow,
|
|
FlowReached,
|
|
SendCommandAgain,
|
|
}
|
|
OpState opState;
|
|
|
|
double currentReqFlowLo;
|
|
double currentReqFlowHi;
|
|
double targetPositionLo;
|
|
double targetPositionHi;
|
|
|
|
int startTime;
|
|
int expireTime;
|
|
DoubleBox flowBox;
|
|
int flowWithinBoundsTime;
|
|
|
|
|
|
/// <summary>
|
|
/// Set required water flow. Conditionally leave the measurement running.
|
|
/// Events: FlowSet, FlowTimeOut
|
|
/// </summary>
|
|
/// <param name="cBoard">Control board device</param>
|
|
/// <param name="regulValve">Regulation valve component</param>
|
|
/// <param name="flowMeter">Flowmeter component</param>
|
|
/// <param name="targetFlowLo">Lower limit of the flow to be achieved in [m3/h]</param>
|
|
/// <param name="targetFlowHi">Upper limit of the flow to be achieved in [m3/h]</param>
|
|
/// <param name="pidCoef">PID coefficient (float)</param>
|
|
/// <param name="timeout">Timeout for the flow setting in [s]</param>
|
|
/// <param name="leaveMeasurementRunning">true = Leave the measurement running after op. stop</param>
|
|
/// <remarks>Only Elde.Valve flow are used, other flow on the lists are ignored</remarks>
|
|
public SetFlowOp(UniCB cBoard, IRegValve regValve, IFlowMeter flowMeter, double targetFlowLo, double targetFlowHi,
|
|
DoubleBox flowBox, bool reTryCmds, int timeout, bool leaveMeasurementRunning)
|
|
{
|
|
this.cBoard = cBoard;
|
|
if (this.cBoard == null) throw new ArgumentNullException("cBoard is null or not Uni");
|
|
|
|
this.regV = regValve as RegValve;
|
|
if (this.regV == null) throw new ArgumentNullException("regValve is null or not Uni");
|
|
|
|
this.flowMeter = flowMeter;
|
|
if (this.flowMeter == null) throw new ArgumentNullException("flowMeter");
|
|
|
|
nominalFlow = flowMeter.NominalFlow;
|
|
maximalFlow = nominalFlow * 1.25;
|
|
|
|
this.targetFlowAve = (targetFlowLo + targetFlowHi) / 2;
|
|
this.shrinkedTgtFlowLo = (0.7 * targetFlowLo) + (0.3 * this.targetFlowAve); /// Move the lower limit 15% of the range up
|
|
this.shrinkedTgtFlowHi = (0.7 * targetFlowHi) + (0.3 * this.targetFlowAve); /// Move the upper limit 15% of the range down
|
|
|
|
this.flowBox = flowBox;
|
|
if (this.flowBox == null) throw new ArgumentNullException("flowBox");
|
|
|
|
this.reTryCommands = reTryCmds;
|
|
|
|
this.timeout = timeout;
|
|
|
|
this.leaveMeasurementRunning = leaveMeasurementRunning;
|
|
|
|
log.Debug(this.ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set required water flow - Do not leave the measurement running.
|
|
/// Events: FlowSet
|
|
/// </summary>
|
|
public SetFlowOp(UniCB cBoard, IRegValve regValve, IFlowMeter flowMeter, double targetFlowLo, double targetFlowHi,
|
|
DoubleBox flowbox, bool reTryCmds, int timeout)
|
|
: this(cBoard, regValve, flowMeter, targetFlowLo, targetFlowHi, flowbox, reTryCmds, timeout, false)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set required water flow - No timeout.
|
|
/// Events: FlowSet
|
|
/// </summary>
|
|
public SetFlowOp(UniCB cBoard, IRegValve regValve, IFlowMeter flowMeter, double targetFlowLo, double targetFlowHi,
|
|
DoubleBox flowbox, bool reTryCmds)
|
|
: this(cBoard, regValve, flowMeter, targetFlowLo, targetFlowHi, flowbox, reTryCmds, int.MaxValue, false)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetch target position limits from the dictionary or return false
|
|
/// </summary>
|
|
/// <param name="avgReqFlow">Average of the flow targer range (input)</param>
|
|
/// <param name="positionLo">Target valve position low limit (output)</param>
|
|
/// <param name="positionHi">Target valve position high limit (output)</param>
|
|
/// <returns>true when positions for the target flow are stored in the memory, otherwise return false</returns>
|
|
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);
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
positionLo = 0;
|
|
positionHi = 0;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void StoreTargetPosition(double avgReqFlow, double actPosition)
|
|
{
|
|
if (!regV.Dict.ContainsKey(targetFlowAve))
|
|
{
|
|
regV.Dict.Add(new KeyValuePair<double, double>(avgReqFlow, actPosition));
|
|
}
|
|
return;
|
|
}
|
|
|
|
/// <summary>Start this operation</summary>
|
|
public void Start()
|
|
{
|
|
startTime = StateMachine.Time;
|
|
expireTime = StateMachine.Time + timeout;
|
|
if (expireTime < 0) expireTime = int.MaxValue;
|
|
|
|
double flowMtrFreq = flowMeter.ReadFrequency();
|
|
double flow = flowMeter.ReadFlow();
|
|
|
|
currentReqFlowLo = shrinkedTgtFlowLo; /// Default lower limit
|
|
currentReqFlowHi = shrinkedTgtFlowHi; /// Default upper limit
|
|
if (flow != 0)
|
|
{
|
|
flowBox.Val = flow;
|
|
if (flow >= targetFlowAve) currentReqFlowHi = targetFlowAve; /// Decrease upper limit
|
|
if (flow <= targetFlowAve) currentReqFlowLo = targetFlowAve; /// Increase lower limit
|
|
}
|
|
|
|
flowWithinBoundsTime = 0;
|
|
|
|
if (regV.regValveCfg.StoredPositionReuse && FetchTargetPosition(targetFlowAve, out targetPositionLo, out targetPositionHi))
|
|
{
|
|
log.InfoFormat("SetFlowOp:Start() rv#={0} flowMtr#={1} TARGET: flowLo={2} flowHi={3} FETCHED: posLo={4} posHi={5}",
|
|
regulValveNr, flowMeter.Idx1, currentReqFlowLo, currentReqFlowHi, targetPositionLo, targetPositionHi);
|
|
|
|
opState = OpState.ValveMoveToPosition1;
|
|
}
|
|
else
|
|
{
|
|
log.InfoFormat("SetFlowOp:Start() rv#={0} flowMtr#={1} TARGET: flowLo={2} flowHi={3}",
|
|
regulValveNr, flowMeter.Idx1, currentReqFlowLo, currentReqFlowHi);
|
|
|
|
opState = OpState.ValveMoveToFlow1;
|
|
}
|
|
|
|
/// Start flow measurement
|
|
regV.SendCommand(Command.Start, int.MaxValue, int.MaxValue, 0, 0); /// TestMethods=0, StopDevs=0
|
|
}
|
|
|
|
/// <summary>Run this operation</summary>
|
|
/// <returns>
|
|
/// Event.OpArgumentError
|
|
/// Event.Starting
|
|
/// Event.Busy
|
|
/// Event.FlowReached
|
|
/// Event.RegulValveTimeOut
|
|
/// </returns>
|
|
public Event Run()
|
|
{
|
|
double rvPosition = regV.Position;
|
|
double flowMtrFreq = flowMeter.ReadFrequency();
|
|
double flow = flowMeter.ReadFlow();
|
|
|
|
if (flow != 0)
|
|
{
|
|
flowBox.Val = flow;
|
|
}
|
|
|
|
log.InfoFormat("SetFlowOp:Run(T={0}) rv#={1} pos={2}% freq={3} flow={4} opState={5}",
|
|
StateMachine.Time - startTime, regulValveNr, rvPosition.ToString("F1"), flowMtrFreq, flow, opState);
|
|
|
|
if (StateMachine.Time > expireTime)
|
|
{
|
|
return Event.RegulValveTimeOut;
|
|
}
|
|
else if (opState == OpState.SendCommandAgain)
|
|
{
|
|
flowWithinBoundsTime = 0;
|
|
|
|
if (regV.regValveCfg.StoredPositionReuse && FetchTargetPosition(targetFlowAve, out targetPositionLo, out targetPositionHi))
|
|
{
|
|
log.InfoFormat("Run(): rv#={0} flowMtr#={1} TARGET: flowLo={2} flowHi={3} FETCHED: posLo={4} posHi={5}",
|
|
regulValveNr, flowMeter.Idx1, currentReqFlowLo, currentReqFlowHi, targetPositionLo, targetPositionHi);
|
|
|
|
opState = OpState.ValveMoveToPosition1;
|
|
}
|
|
else
|
|
{
|
|
log.InfoFormat("Run(): rv#={0} flowMtr#={1} TARGET: flowLo={2} flowHi={3}",
|
|
regulValveNr, flowMeter.Idx1, currentReqFlowLo, currentReqFlowHi);
|
|
|
|
opState = OpState.ValveMoveToFlow1;
|
|
}
|
|
|
|
/// Start flow measurement
|
|
regV.SendCommand(Command.Start, int.MaxValue, int.MaxValue, 0, 0); /// TestMethods=0, StopDevs=0
|
|
return Event.Starting;
|
|
}
|
|
else 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(targetPositionLo, targetPositionHi);
|
|
|
|
opState = OpState.SettingPosition;
|
|
return Event.Starting;
|
|
}
|
|
else if (opState == OpState.ValveMoveToPosition3)
|
|
{
|
|
if (reTryCommands)
|
|
{
|
|
opState = OpState.CheckStatePosition;
|
|
}
|
|
else
|
|
{
|
|
opState = OpState.SettingPosition;
|
|
}
|
|
return Event.Starting;
|
|
}
|
|
else if (opState == OpState.CheckStatePosition) /// Verify
|
|
{
|
|
if ((cBoard.State & (ulong)StatusP.TestWithRefFlowmtr) == 0)
|
|
{
|
|
opState = OpState.SendCommandAgain;
|
|
return Event.Starting;
|
|
}
|
|
else if (regV.RegValveState != RegValveState.DacValueRegul)
|
|
{
|
|
/// Repeat appropriate ValveMove(...) command
|
|
regV.MoveToPosition(targetPositionLo, targetPositionHi);
|
|
|
|
log.InfoFormat("SetFlowOp: ValveMove({0}, Position, {1}, {2}, {3})",
|
|
regulValveNr, targetPositionLo, targetPositionHi, regV.StableTime);
|
|
|
|
opState = OpState.ValveMoveToPosition3;
|
|
return Event.Starting;
|
|
}
|
|
else
|
|
{
|
|
opState = OpState.SettingPosition;
|
|
return Event.Starting;
|
|
}
|
|
}
|
|
else if (opState == OpState.SettingPosition)
|
|
{
|
|
/// Repeated untill position is reached
|
|
if ((targetPositionLo <= rvPosition) && (rvPosition <= targetPositionHi))
|
|
{
|
|
opState = OpState.ValveMoveToFlow1;
|
|
}
|
|
return Event.Starting;
|
|
}
|
|
else if (opState == OpState.ValveMoveToFlow1)
|
|
{
|
|
opState = OpState.ValveMoveToFlow2;
|
|
return Event.Starting;
|
|
}
|
|
else if (opState == OpState.ValveMoveToFlow2) /// Move to flow
|
|
{
|
|
///
|
|
/// Done once, issues an appropriate ValveMove(...) command
|
|
///
|
|
if ((currentReqFlowLo >= currentReqFlowHi) || (currentReqFlowLo > maximalFlow) || (currentReqFlowHi <= 0))
|
|
{
|
|
return Event.OpArgumentError;
|
|
}
|
|
|
|
log.InfoFormat("Run(): rv#={0} flowMtr#={1} TARGET: flowLo={2} flowHi={3}",
|
|
regulValveNr, flowMeter.Idx1, currentReqFlowLo, currentReqFlowHi);
|
|
|
|
double freqLo = 2000.0 * currentReqFlowLo / nominalFlow;
|
|
double freqHi = 2000.0 * currentReqFlowHi / nominalFlow;
|
|
regV.SetFlow(flowMeter.Idx1,freqLo, freqHi);
|
|
|
|
opState = OpState.ValveMoveToFlow3;
|
|
return Event.Starting;
|
|
}
|
|
else if (opState == OpState.ValveMoveToFlow3)
|
|
{
|
|
if (reTryCommands)
|
|
{
|
|
opState = OpState.CheckStateFlow;
|
|
}
|
|
else
|
|
{
|
|
opState = OpState.SettingFlow;
|
|
}
|
|
return Event.Starting;
|
|
}
|
|
else if (opState == OpState.CheckStateFlow) /// Verify the flow setting
|
|
{
|
|
if ( (cBoard.State & (ulong)StatusP.TestWithRefFlowmtr) == 0 ||
|
|
(regV.RegValveState & RegValveState.PwOrFreqRegul) != RegValveState.PwOrFreqRegul)
|
|
{
|
|
opState = OpState.SendCommandAgain;
|
|
return Event.Starting;
|
|
}
|
|
else if (regV.RegValveState != RegValveState.PwOrFreqRegul)
|
|
{
|
|
/// Done once, issues an appropriate ValveMove(...) command
|
|
double freqLo = 2000.0 * currentReqFlowLo / nominalFlow;
|
|
double freqHi = 2000.0 * currentReqFlowHi / nominalFlow;
|
|
regV.SetFlow(flowMeter.Idx1, freqLo, freqHi);
|
|
|
|
log.InfoFormat("SetFlowOp: ValveMove({0}, Frequency, {1}, {2}, {3})",
|
|
regulValveNr, freqLo, freqHi, regV.StableTime);
|
|
|
|
opState = OpState.ValveMoveToFlow3;
|
|
return Event.Starting;
|
|
}
|
|
else
|
|
{
|
|
opState = OpState.SettingFlow;
|
|
return Event.Busy;
|
|
}
|
|
}
|
|
else if (opState == OpState.SettingFlow)
|
|
{
|
|
/// Regulation valve is setting flow to the required value
|
|
|
|
if ((currentReqFlowLo <= flow) && (flow <= currentReqFlowHi))
|
|
{
|
|
/// Flow is within range
|
|
|
|
if (flowWithinBoundsTime++ >= regV.FlowStableSec)
|
|
{
|
|
/// Flow is within range for sufficiently long time
|
|
|
|
if (regV.regValveCfg.StoredPositionReuse)
|
|
{
|
|
double 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",
|
|
regulValveNr, 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",
|
|
regulValveNr, targetFlowAve, rvPosition.ToString("F1"));
|
|
}
|
|
}
|
|
|
|
log.InfoFormat("flow = {0} m3/h (lo={1}, hi={2}, REACHED)", flow, currentReqFlowLo, currentReqFlowHi);
|
|
|
|
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, flowWithinBoundsTime);
|
|
return Event.Busy;
|
|
}
|
|
}
|
|
else if (StateMachine.Time > expireTime)
|
|
{
|
|
return Event.RegulValveTimeOut;
|
|
}
|
|
else
|
|
{
|
|
log.InfoFormat("flow = {0} m3/h (lo={1}, hi={2})", flow, currentReqFlowLo, currentReqFlowHi);
|
|
flowWithinBoundsTime = 0;
|
|
return Event.Busy;
|
|
}
|
|
}
|
|
else /// opState == OpState.FlowReached
|
|
{
|
|
log.InfoFormat("flow = {0} m3/h (lo={1}, hi={2}, REACHED)", flow, currentReqFlowLo, currentReqFlowHi);
|
|
return Event.FlowReached;
|
|
}
|
|
}
|
|
|
|
/// <summary>Start this operation</summary>
|
|
public void Stop()
|
|
{
|
|
if (!leaveMeasurementRunning)
|
|
{
|
|
/// Stop measurement
|
|
regV.SendCommand(Command.Stop, 50000, 50000, 0, StopDevs.RegValveRegulation); /// TestMethods=0
|
|
}
|
|
|
|
opState = OpState.Idle;
|
|
}
|
|
}
|
|
}
|