tbf/TestBenchFramework/BenchControl/TestMethods/CombinedMeters/CombinedMetersSeq.cs

485 lines
20 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using TBF.BenchControl;
2014-07-31 15:04:09 +00:00
using TBF.Boxes;
using TBF.Resources;
2014-07-31 15:04:09 +00:00
using TBF.UiBridge;
namespace TBF.BenchControl.TestMethods.CombinedMeters
{
public class CombinedMetersSeq : Sequences.SequenceBase
{
private static readonly ILog log = LogManager.GetLogger(typeof(CombinedMetersSeq));
private static readonly ILog allResults = LogManager.GetLogger("AllResults");
private static readonly ILog summaryResults = LogManager.GetLogger("SummaryResults");
public IList<Event> Execute(Entities.Test test)
{
Elde.ControlBoardDev cBrd = StateMachine.ControlBoard;
/// Specific for combined meters
int[] lastWMPulses = new int[Program.WMsCount];
/// Operations running in more then one state
checkUiOp = new Operations.CheckUIOp(true);
IList<Event> e; /// Events from currently running operations
//--------------------------------
State.Create("CombinedMeters : Stopping diverter, gate, etc.")
.AddOperation(checkUiOp)
.AddOperation(cBrd.StopPreviousOp())
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.UiCmdStop)) goto stop;
}
while (!e.Contains(Event.PreviousStopped));
/// Fetch the test, the paths
bool success = StateMachine.GetPaths(test, out inPath, out benchPath, out outPath, out sensPath,
out transitionStart, out transitionStop);
if (!success) goto error;
/// Notes:
/// float timeHr = volumeLtr / (1000.0f * targetFlow);
/// float timeSec = 3600.0f * timeHr;
/// uint refPulses = (uint)(timeSec * (2000.0f * targetFlow / pOut.FlowMeter.NominalFlow));
int totalPulses = (int)(7200.0f * test.Volume / outPath.FlowMeter.NominalFlow); /// Nominal flow in [m3/h]
float ltrPerRefPulse = outPath.FlowMeter.NominalFlow / 7200.0f; /// [ltr/pulse]
///
/// Meters path is required for the following:
readRegisters1 = new Operations.ReadMoreRegistersOp(sensPath.RegisterReaders, ref WMPulses, ref WMRefPulses);
readRegisters2 = new Operations.ReadMoreRegistersOp(sensPath.RegisterReaders, ref WMPulses, ref WMRefPulses);
int repetitionNr = 1; /// First test: repetitionNr=1
//====================================
// Transition or SetRoute - Start
//====================================
if (transitionStart != null)
{
switch (Transition(transitionStart, "CombinedMeters : " + Strings.Test_start_sequence_i_n))
{
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
}
}
else
{
//--------------------------------
State.Create("CombinedMeters : Setting the route")
.AddOperation(checkUiOp)
.AddOperation(cBrd.SetValvesOp(GenericDevices.ValveBase.Merge(inPath.ValvesOpen, benchPath.ValvesOpen, outPath.ValvesOpen),
GenericDevices.ValveBase.Merge(inPath.ValvesClose, benchPath.ValvesClose, outPath.ValvesClose)))
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.UiCmdStop)) goto stop;
}
while (!e.Contains(Event.ValvesSet));
}
//====================================
loop:
/// Start the test, initialize test results
Bridge.OnTestSelected(this, new TestSelectedEventArgs(test, repetitionNr, inPath, benchPath, outPath, sensPath, (int)totalPulses));
Entities.TestResult tstRslt = new Entities.TestResult(test, repetitionNr, Entities.MetersKind.Combined);
if (!test.Emptying) /// Do not skip emptying if test.Emptying==true
{
//--------------------------------
State.Create("CombinedMeters : Measuring the mass of water in the tank")
.AddOperation(checkUiOp)
.AddOperation(outPath.Balance.ReadMassOp(ref mass))
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.Error)) goto error;
if (e.Contains(Event.UiCmdStop)) goto stop;
}
while (!e.Contains(Event.BalanceDone));
2014-07-31 15:04:09 +00:00
float estimatedEndMass = mass.Val + test.Volume;
if (estimatedEndMass < outPath.Balance.Capacity * Constants.TankFullFactor)
{
goto set_flow; /// Enough room in the tank -> skip emptying
}
}
// Empty the water tank
switch (EmptyTheTank(outPath.EmptyTankValve, outPath.Balance))
{
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
}
set_flow:
//------------------------------------------------
Bridge.OnActivity(this, Strings.Setting_the_flow);
//------------------------------------------------
State.Create("CombinedMeters : Starting the pump")
.AddOperation(checkUiOp)
.AddOperation((inPath != null && inPath.Pump is GenericDevices.IPumpFM) ? (inPath.Pump as GenericDevices.IPumpFM).TurnOn() : null)
.AddOperation(cBrd.SetValvesOp(inPath.Pump, null))
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.UiCmdStop)) goto stop;
}
while (!e.Contains(Event.ValvesSet));
//--------------------------------
State.Create("CombinedMeters : Setting the flow")
.AddOperation(checkUiOp)
.AddOperation(outPath.RegulValve.SetFlowOp(outPath.FlowMeter, test.Qfrom, test.Qto, measuredFlow, 600)) /// timeout = 10 min.
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.UiCmdStop)) goto stop;
if (e.Contains(Event.RegulValveTimeOut)) goto error;
}
while (!e.Contains(Event.FlowReached));
int time = StateMachine.CurrentTime;
2014-07-31 15:04:09 +00:00
float currentFlow = measuredFlow.Val;
/// Extract cameras from the current sensors path, add operations to the detection state
IList<IOperation> measureOperations = new List<IOperation>();
for (int i = 0; i < sensPath.RegisterReaders.Length; i++)
{
GenericDevices.ICameraRoi cameraRoi = sensPath.RegisterReaders[i] as GenericDevices.ICameraRoi;
if (cameraRoi != null) measureOperations.Add(cameraRoi.MeasureOp());
}
//------------------------------------------------
Bridge.OnActivity(this, Strings.Measuring_the_weight);
//------------------------------------------------
State.Create("CombinedMeters : Measuring the start mass")
.AddOperation(checkUiOp)
.AddOperations(measureOperations)
.AddOperation(outPath.Balance.ReadStableMassOp(ref stableMass, 5))
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.Error)) goto error;
if (e.Contains(Event.UiCmdStop)) goto stop;
}
while (!e.Contains(Event.BalanceDone));
tstRslt.TimeStart = DateTime.Now;
2014-07-31 15:04:09 +00:00
tstRslt.MassStartRaw = stableMass.Val;
mass.Val = stableMass.Val;
//------------------------------------------------
Bridge.OnActivity(this, Strings.Test_in_progress);
//------------------------------------------------
State.Create("CombinedMeters : Starting the test")
.AddOperation(checkUiOp)
.AddOperations(measureOperations)
.AddOperation(cBrd.StartMeasurementOp(outPath.FlowMeter, totalPulses, Elde.TestMethods.Diverter | Elde.TestMethods.Synchro, (float)test.TolerRed))
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.Error)) goto error;
if (e.Contains(Event.UiCmdStop)) goto stop;
}
while (!e.Contains(Event.MeasurementStarted));
///
/// Measurement loop - preparation
///
queryEnd1 = cBrd.QueryMeasurementEndOp();
queryEnd2 = cBrd.QueryMeasurementEndOp();
bool firstTime = true; /// Used to reset sums for averaging
ResetAveragedData();
/// Measurement loop - begin
while (true)
{
//--------------------------------
switch(ReadRegistersTempPressAmbient(measureOperations, true))
{
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
case Event.MeasurementCompleted: goto measurement_completed;
}
if (firstTime)
{
firstTime = false; /// Do the following only once
2014-07-31 15:04:09 +00:00
tstRslt.TempInStart = tempIn.Val;
tstRslt.TempOutStart = tempOut.Val;
tstRslt.TempDivStart = tempDiv.Val;
tstRslt.PressInStart = pressIn.Val;
tstRslt.PressOutStart = pressOut.Val;
}
2014-07-31 15:04:09 +00:00
tstRslt.TempInEnd = tempIn.Val;
tstRslt.TempOutEnd = tempOut.Val;
tstRslt.TempDivEnd = tempDiv.Val;
tstRslt.PressInEnd = pressIn.Val;
tstRslt.PressOutEnd = pressOut.Val;
tstRslt.MassEndRaw = mass.Val;
AccumulateAveragedData();
string logstr = string.Format("E={0} f={1} q={2}", cBrd.EtPulses(0), cBrd.ReferenceFreq,
cBrd.ReferenceFlow * outPath.FlowMeter.NominalFlow);
for (int i = 0; i < Program.WMsCount; i++)
{
logstr += string.Format(" M{0}=({1},{2})", i + 1, WMRefPulses[i], WMPulses[i]);
}
log.Info(logstr);
float progress = Formulas.TestProgress(cBrd.EtPulses(0), totalPulses, 10.0f, test.TstTime);
TestProgressEventArgs data = GetTestProgressData(test, tstRslt, cBrd.EtPulses(0), cBrd.TTime, progress);
data.WmVolume[2] = data.WmVolume[0] + data.WmVolume[1];
data.WmErrPct[2] = Formulas.ErrorFromVolumes(data.WmVolume[2], data.Volume);
Bridge.OnTestProgress(this, data);
}
/// Measurement loop - end
measurement_completed:
//------------------------------------------------
Bridge.OnActivity(this, Strings.Measuring_the_weight);
//------------------------------------------------
State.Create("CombinedMeters : Measuring the end mass")
.AddOperation(checkUiOp)
.AddOperation(outPath.Balance.ReadStableMassOp(ref stableMass, 5))
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.Error)) goto error;
if (e.Contains(Event.UiCmdStop)) goto stop;
}
while (!e.Contains(Event.BalanceDone));
///
2014-07-31 15:04:09 +00:00
tstRslt.MassEndRaw = stableMass.Val;
tstRslt.TimeEnd = DateTime.Now;
//------------------------------------------------
Bridge.OnActivity(this, Strings.Test_completed);
//------------------------------------------------
State.Create("CombinedMeters : Stopping the pump")
.AddOperation(checkUiOp)
.AddOperation((inPath != null && inPath.Pump is GenericDevices.IPumpFM) ? (inPath.Pump as GenericDevices.IPumpFM).TurnOff() : null)
.AddOperation(cBrd.SetValvesOp(null, inPath.Pump))
.AddOperation(cBrd.UpdateTankWeightOp())
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.UiCmdStop)) goto stop;
}
while (!e.Contains(Event.ValvesSet));
//--------------------------------
State.Create("CombinedMeters : Stopping diverter, gate, etc.")
.AddOperation(checkUiOp)
.AddOperation(cBrd.StopPreviousOp())
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.UiCmdStop)) goto stop;
}
while (!e.Contains(Event.PreviousStopped));
///
/// Populate TestRasult data entity with data
///
tstRslt.TimeEnd = DateTime.Now;
UpdateTestRsltWithAveragedData(tstRslt);
tstRslt.MassStart = Formulas.CorrectedValue(tstRslt.MassStartRaw, outPath.Balance.Corrections);
tstRslt.MassEnd = Formulas.CorrectedValue(tstRslt.MassEndRaw, outPath.Balance.Corrections);
tstRslt.MassDiff = tstRslt.MassEnd - tstRslt.MassStart;
tstRslt.DensityIn = Formulas.WaterDensityFromTemp(tstRslt.TempInAvrg); /// [kg/m3]
tstRslt.DensityOut = Formulas.WaterDensityFromTemp(tstRslt.TempOutAvrg); /// [kg/m3]
tstRslt.DensityDiv = Formulas.WaterDensityFromTemp(tstRslt.TempDivAvrg); /// [kg/m3]
tstRslt.Time = cBrd.TTime; /// [s] measurement time
tstRslt.FlowMass = 3600.0f * tstRslt.MassDiff / tstRslt.Time; /// [kg/h]
tstRslt.FlowVolume = 3600.0f * ltrPerRefPulse * cBrd.EtPulses(0) / tstRslt.Time; /// [l/h]
tstRslt.VolumeCTV = 1.00103f * 1000.0f * tstRslt.MassDiff / tstRslt.DensityOut; /// [l] 1000.0f is because density is in [kg/m3]
tstRslt.VolumeMaster = ltrPerRefPulse * cBrd.EtPulses(0); /// [l] volume from the master flow meter
tstRslt.PulsesMaster = cBrd.EtPulses(0); /// Pulses of the master flow meter (test total)
tstRslt.ConstMaster = ltrPerRefPulse * tstRslt.VolumeCTV / tstRslt.VolumeMaster;
/// Corrected master pulses per liter
if (tstRslt.VolumeCTV <= float.Epsilon)
{
tstRslt.ErrorMaster = 0.1f;
}
else
{
tstRslt.ErrorMaster = 100 * (tstRslt.VolumeMaster - tstRslt.VolumeCTV) / tstRslt.VolumeCTV;
}
tstRslt.TimeDivStart0 = 0;
tstRslt.TimeDivStart1 = 0;
tstRslt.TimeDivStart2 = 0;
tstRslt.TimeDivStart3 = 0;
tstRslt.TimeDivStart4 = 0;
tstRslt.TimeDivStart5 = 0;
tstRslt.TimeDivEnd0 = 0;
tstRslt.TimeDivEnd1 = 0;
tstRslt.TimeDivEnd2 = 0;
tstRslt.TimeDivEnd3 = 0;
tstRslt.TimeDivEnd4 = 0;
tstRslt.TimeDivEnd5 = 0;
for (int iCmbnd = 0; iCmbnd < Program.WMsCount / 2; iCmbnd++)
{
for (int i = 2 * iCmbnd; i < 2 * iCmbnd + 2; i++)
{
if (sensPath.RegisterReaders[i] == null || sensPath.RegisterReaders[i].PulsesPerLtr <= float.Epsilon)
{
tstRslt.Meters[i].VolumeMeter = 0;
}
else
{
tstRslt.Meters[i].VolumeMeter = Convert.ToSingle(WMPulses[i]) / sensPath.RegisterReaders[i].PulsesPerLtr;
}
tstRslt.Meters[i].SerialNr = "wm" + (i + 1).ToString();
tstRslt.Meters[i].VolumeStart = 0; /// liter
tstRslt.Meters[i].VolumeEnd = 0; /// liter
tstRslt.Meters[i].VolumeRef = tstRslt.VolumeCTV; /// liter
if (WMRefPulses[i] != 0)
{
tstRslt.Meters[i].VolumeMeter *= ((float)cBrd.EtPulses(0) / (float)WMRefPulses[i]);
}
tstRslt.Meters[i].PulsesMeter = WMPulses[i];
tstRslt.Meters[i].PulsesMaster = WMRefPulses[i];
tstRslt.Meters[i].Time = cBrd.TTime;
tstRslt.Meters[i].VolumeErrorPct = 0; /// Not used
}
tstRslt.CombinedMeters[iCmbnd].VolumeRef = tstRslt.VolumeCTV; /// liter
tstRslt.CombinedMeters[iCmbnd].VolumeMeter = tstRslt.Meters[2 * iCmbnd].VolumeMeter + tstRslt.Meters[2 * iCmbnd + 1].VolumeMeter;
tstRslt.CombinedMeters[iCmbnd].PulsesMaster = cBrd.EtPulses(0);
tstRslt.CombinedMeters[iCmbnd].Time = cBrd.TTime;
tstRslt.CombinedMeters[iCmbnd].VolumeErrorPct = Formulas.ErrorFromVolumes(tstRslt.CombinedMeters[iCmbnd].VolumeMeter, tstRslt.CombinedMeters[iCmbnd].VolumeRef);
tstRslt.CombinedMeters[iCmbnd].Passed = (tstRslt.ErrLimLo <= tstRslt.CombinedMeters[iCmbnd].VolumeErrorPct) && (tstRslt.CombinedMeters[iCmbnd].VolumeErrorPct <= tstRslt.ErrLimHi);
}
/// Add data - end
AddOrOverwriteResult(tstRslt);
Bridge.OnTestCompleted(this, new TestCompletedEventArgs(tstRslt));
/// Append the results to the CSV-file
allResults.Info(TestResult2CsvLine(tstRslt, cBrd.EtPulses(0)));
FluentCommon.SaveToDb(StateMachine.WtSession, tstRslt);
if (++repetitionNr <= test.Repeats)
{
goto loop;
}
///----------------------///
/// Quit this sequence ///
///----------------------///
/// Stop the pump
State.Create("CombinedMeters : Test(s) completed -> Closing the valves")
.AddOperation(checkUiOp)
.AddOperation(cBrd.SetValvesOp(null, inPath.Pump))
.AddOperation((inPath != null && inPath.Pump is GenericDevices.IPumpFM) ? (inPath.Pump as GenericDevices.IPumpFM).TurnOff() : null)
.EnterState();
do {
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.Error)) goto error;
}
while (!e.Contains(Event.ValvesSet) || ((inPath.Pump is GenericDevices.IPumpFM) && !e.Contains(Event.TurnPumpOnOffDone)));
/// Transition or SetRoute - End
if (transitionStop != null)
{
switch (Transition(transitionStop, "CombinedMeters : " + Strings.Test_stop_sequence_i_n))
{
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
}
}
else
{
State.Create("CombinedMeters : Test(s) completed -> Closing the valves")
.AddOperation(checkUiOp)
.AddOperation(cBrd.SetValvesOp(StateMachine.DefaultValvesOpen, StateMachine.DefaultValvesClose))
.EnterState();
do {
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.Error)) goto error;
}
while (!e.Contains(Event.ValvesSet));
}
/// Create the return value - a list with one event Event.Done - and return
IList<Event> retval = new List<Event>();
retval.Add(Event.Done);
return retval;
//====================================
stop:
State.Create("CombinedMeters : User selected STOP -> Closing the valves")
.AddOperation(checkUiOp)
.AddOperation((inPath != null && inPath.Pump is GenericDevices.IPumpFM) ? (inPath.Pump as GenericDevices.IPumpFM).TurnOff() : null)
.AddOperation(cBrd.SetValvesOp(StateMachine.DefaultValvesOpen, StateMachine.DefaultValvesClose))
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.Error)) goto error;
}
while (!e.Contains(Event.ValvesSet));
IList<Event> retval2 = new List<Event>();
retval2.Add(Event.UiCmdStop);
return retval2;
//====================================
error:
State.Create("CombinedMeters : ERROR -> Closing the valves")
.AddOperation(checkUiOp)
.AddOperation((inPath != null && inPath.Pump is GenericDevices.IPumpFM) ? (inPath.Pump as GenericDevices.IPumpFM).TurnOff() : null)
.AddOperation(cBrd.SetValvesOp(StateMachine.DefaultValvesOpen, StateMachine.DefaultValvesClose))
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.Error)) goto error;
}
while (!e.Contains(Event.ValvesSet));
IList<Event> retval3 = new List<Event>();
retval3.Add(Event.Error);
return retval3;
}
}
}