tbf/TBF/Rig/Uni/FlowMetersInParallel/FlowMeter.cs

216 lines
8.2 KiB
C#

///
/// Copyright (c) 2021-2022 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using log4net;
using SchematicDrawing;
using SharedComponents;
using TBF.Boxes;
using TBF.Rig.ControlBoard.Uni;
using TBF.Rig.GenericDevices;
namespace TBF.Rig.Uni.FlowMetersInParallel
{
public class FlowMeter : ComponentBase, IFlowMeter, IDrawingItCmpntWithMeasuredVal
{
private static readonly ILog log = LogManager.GetLogger(typeof(FlowMeter));
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
readonly FlowMeterCfg flowMeterCfg;
UniCB uniCB;
IFlowMeterSingle flowMtr1;
IFlowMeterSingle flowMtr2;
IFlowMeterSingle flowMtr3;
double nominalFlow;
double nominalFreq;
double ltrPerPulse;
int flowMetersCount;
int flowMetersBitfield;
public int Idx1 { get { return flowMetersBitfield; } }
public double NominalFlow { get { return nominalFlow; } }
public double NominalFreq { get { return nominalFreq; } }
public double LtrPerPulse { get { return ltrPerPulse; } }
public SchematicDrawing.IDrawingItem DrawingItem { get { return flowMeterCfg as SchematicDrawing.IDrawingItem; } }
public string MsrdFormat { get { return flowMeterCfg.MsrdFormat; } }
public Common.Unit MsrdUnit { get { return flowMeterCfg.MsrdUnit; } }
public double MsrdValLimLo { get { return flowMeterCfg.MsrdValLimLo; } set { flowMeterCfg.MsrdValLimLo = value; } }
public double MsrdValLimHi { get { return flowMeterCfg.MsrdValLimHi; } set { flowMeterCfg.MsrdValLimHi = value; } }
public bool MsrmntAvailable
{
get
{
bool result = true;
if (flowMtr1 != null) result = result && flowMtr1.MsrmntAvailable;
if (flowMtr2 != null) result = result && flowMtr2.MsrmntAvailable;
if (flowMtr3 != null) result = result && flowMtr3.MsrmntAvailable;
return result;
}
}
public double MeasuredVal
{
get { return ReadFlow(); }
}
public string AltString { get { return "Invalid format"; } }
public FlowMeter() { }
public FlowMeter(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
flowMeterCfg = cfg as FlowMeterCfg;
}
public override void Initialize()
{
uniCB = TbfComponents.FindComponent(flowMeterCfg.ParentName) as UniCB;
if (uniCB == null) throw new Exception("Cannot find " + Name + " parent");
if (!string.IsNullOrEmpty(flowMeterCfg.Flowmeter1)) flowMtr1 = TbfComponents.FindComponent(flowMeterCfg.Flowmeter1) as IFlowMeterSingle;
if (!string.IsNullOrEmpty(flowMeterCfg.Flowmeter2)) flowMtr2 = TbfComponents.FindComponent(flowMeterCfg.Flowmeter2) as IFlowMeterSingle;
if (!string.IsNullOrEmpty(flowMeterCfg.Flowmeter3)) flowMtr3 = TbfComponents.FindComponent(flowMeterCfg.Flowmeter3) as IFlowMeterSingle;
nominalFlow = 0;
flowMetersCount = 0;
flowMetersBitfield = 0;
if (flowMtr1 != null)
{
nominalFlow += flowMtr1.NominalFlow;
nominalFreq += flowMtr1.NominalFreq;
flowMetersBitfield |= (4 << flowMtr1.Idx1);
flowMetersCount++;
}
if (flowMtr2 != null)
{
nominalFlow += flowMtr2.NominalFlow;
nominalFreq += flowMtr2.NominalFreq;
flowMetersBitfield |= (4 << flowMtr2.Idx1);
flowMetersCount++;
}
if (flowMtr3 != null)
{
nominalFlow += flowMtr3.NominalFlow;
nominalFreq += flowMtr3.NominalFreq;
flowMetersBitfield |= (4 << flowMtr3.Idx1);
flowMetersCount++;
}
if (flowMetersCount == 0) throw new Exception("Missing flowmeters");
MsrdValLimHi = nominalFlow;
nominalFreq = nominalFreq / flowMetersCount;
ltrPerPulse = nominalFlow / (3.6 * nominalFreq); /// Nominal freq. is sum of particular nominal frquencies
log.FatalFormat("{0} initialized: {1} nom.flow={2} m3/h nom.freq={3} Hz bitfield=0x{4:X2}",
Name, nominalFlow, nominalFreq, flowMetersBitfield);
}
public double ReadFlow()
{
double flowByNewFormulaSum = 0;
if (flowMtr1 != null)
{
flowByNewFormulaSum += flowMtr1.NominalFlow * uniCB.Data.ReferenceFreq[1] / flowMtr1.NominalFreq;
}
if (flowMtr2 != null)
{
flowByNewFormulaSum += flowMtr2.NominalFlow * uniCB.Data.ReferenceFreq[2] / flowMtr2.NominalFreq;
}
if (flowMtr3 != null)
{
flowByNewFormulaSum += flowMtr3.NominalFlow * uniCB.Data.ReferenceFreq[3] / flowMtr3.NominalFreq;
}
return flowByNewFormulaSum/*ReadFrequency() * nominalFlow / nominalFreq*/;
}
public double ReadFrequency()
{
return uniCB.RefFrequency;
}
/// <summary>
/// Events: FlowDone, Error
/// </summary>
/// <param name="flow">Reference to a variable for the flow in Bar</param>
/// <returns>ReadFlowOp instance reference casted to IOperaton</returns>
public IOperation ReadFlowOp(ref DoubleBox flow)
{
return new ReadFlowOp(this, ref flow);
}
/// <summary>
/// Events: flowDone, Error
/// </summary>
/// <param name="flow">Reference to a variable for the flow in Bar</param>
/// <param name="flowDone">Event returned when measurement done</param>
/// <returns>ReadFlowOp instance reference casted to IOperaton</returns>
public IOperation ReadFlowOp(ref DoubleBox flow, Event flowDone)
{
return new ReadFlowOp(this, ref flow, flowDone);
}
double flowRawSum;
double flowSum;
int timeSum;
int rng1;
int rng2;
int rng3;
public void StartStatistics(Config.Entities.Test test)
{
flowRawSum = 0;
flowSum = 0;
timeSum = 0;
if (flowMtr1 != null) rng1 = Math.Max(0, flowMtr1.GetRange(test.TempLimLo, test.TempLimHi));
if (flowMtr2 != null) rng2 = Math.Max(0, flowMtr2.GetRange(test.TempLimLo, test.TempLimHi));
if (flowMtr3 != null) rng3 = Math.Max(0, flowMtr3.GetRange(test.TempLimLo, test.TempLimHi));
}
public void UpdateStatistics(int timeDelta)
{
double tempFlow;
if (flowMtr1 != null)
{
flowRawSum += timeDelta * (tempFlow = flowMtr1.ReadFlow());
flowSum += timeDelta * flowMtr1.CorrectedFlow(tempFlow, rng1); ;
}
if (flowMtr2 != null)
{
flowRawSum += timeDelta * (tempFlow = flowMtr2.ReadFlow());
flowSum += timeDelta * flowMtr2.CorrectedFlow(tempFlow, rng2); ;
}
if (flowMtr3 != null)
{
flowRawSum += timeDelta * (tempFlow = flowMtr3.ReadFlow());
flowSum += timeDelta * flowMtr3.CorrectedFlow(tempFlow, rng3); ;
}
timeSum += timeDelta;
}
public void StopStatistics()
{
flowRawSum /= Convert.ToDouble(timeSum);
flowSum /= Convert.ToDouble(timeSum);
log.WarnFormat("StopStatistics() flowRawSum = {0:F3} m3/h flowSum = {1:F3} m3/h", flowRawSum, flowSum);
}
public double LtrPerPulseCorrected(double flow, float temperature)
{
double ltrPerPulseCorrected = (flowRawSum == 0) ? LtrPerPulse : LtrPerPulse * flowSum / flowRawSum;
log.WarnFormat("LtrPerPulseCorrected() flow = {0} m3/h flowRawSum = {1} m3/h flowSum = {2} m3/h temperature = {3} C ltrPerPulseCorrected = {4}", flow, flowRawSum, flowSum, temperature, ltrPerPulseCorrected);
return ltrPerPulseCorrected;
}
}
}