tbf/TestBenchFramework/BenchControl/TestMethods/RoiDetection/RoiDetectionSeq.cs

246 lines
8.9 KiB
C#

///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using Config.Entities;
using TBF.Resources;
using TBF.UiBridge;
using System.IO;
namespace TBF.BenchControl.TestMethods.RoiDetection
{
public class RoiDetectionSeq : Sequences.SequenceBase
{
private static readonly ILog log = LogManager.GetLogger(typeof(RoiDetectionSeq));
public IList<Event> Execute(Test test, GenericDevices.ICameraDisplay display, bool outerLoopMode, int outerLoopCounter)
{
Elde.ControlBoardDev cBrd = StateMachine.ControlBoard;
IList<Event> e = new List<Event>(); /// Events from currently running operations
Event retVal = Event.Done;
/// Operations
checkUiOp = new Operations.CheckUIOp(true);
processDataLoggingOp = null;
/// LtrPerRefPulse and totalPulses are only to make time estimates
LtrPerRefPulse = outPath.FlowMeter.LtrPerPulse; /// [ltr/pulse], nominal flow in [m3/h]
/// Notes:
/// float timeHr = volumeLtr / (1000.0f * targetFlow);
/// float timeSec = 3600.0f * timeHr;
/// int refPulses = (int)(timeSec * (2000.0f * targetFlow / pOut.FlowMeter.NominalFlow));
int totalPulses = (int)(test.Volume / LtrPerRefPulse + 0.5f);
int repetitionNr = outerLoopMode ? outerLoopCounter : 1; /// First test: repetitionNr=1
//====================================
// Transition or SetRoute - Start
//====================================
switch (Transition(transitionBefore, TransitionContext.BeforeTest))
{
case Event.Error: { retVal = Event.Error; goto stopTest; }
case Event.UiCmdStop: { retVal = Event.UiCmdStop; goto stopTest; }
}
//====================
// Start the test
//====================
Bridge.OnTestSelected(this, new TestSelectedEventArgs(test, repetitionNr, inPath, benchPath, outPath, sensPath, totalPulses));
string testName = Results.Utils.GetTestName(test.Name, test.Repeats, repetitionNr);
TestStartTime = DateTime.Now;
TestProgressEventArgs.SetEstimatedTimes(new int[] { 1, 1, 1, 30, 0, Convert.ToInt32(test.TstTime) + 15, 0, 0 });
Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr, Config.Entities.Progress.JustStarted));
//-------------------------------------------------
Bridge.OnActivity(this, Strings.Starting_the_pump);
//-------------------------------------------------
///
if (inPath.Pump is GenericDevices.IPumpFM) (inPath.Pump as GenericDevices.IPumpFM).TurnOn(test.PumpPower);
State.Create("RoiDetection : Starting the pump")
.AddOperation(checkUiOp)
.AddOperation(StateMachine.ControlBoard.UpdateTankWeightOp())
.AddOperation(inPath.Pump != null ? cBrd.SetValvesOp(inPath.Pump, null) : null)
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr, Config.Entities.Progress.FlowSetting));
if (TestAndLogUiCmdStop(test, e)) { retVal = Event.UiCmdStop; goto stopTest; }
if (e.Contains(Event.Error)) { retVal = Event.Error; goto stopTest; }
}
while (e.Contains(Event.ValvesBusy));
//------------------------------------------------
Bridge.OnActivity(this, Strings.Setting_the_flow);
//------------------------------------------------
State.Create("RoiDetection : Setting the flow")
.AddOperation(checkUiOp)
.AddOperation(outPath.RegulValve.SetFlowOp(outPath.FlowMeter, test.Qfrom, test.Qto, outPath.PidCoef, RefFlow, 600)) /// timeout = 10 min.
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr, Config.Entities.Progress.FlowSetting));
if (e.Contains(Event.OpArgumentError)) { retVal = Event.OpArgumentError; goto stopTest; }
if (TestAndLogUiCmdStop(test,e)) { retVal = Event.UiCmdStop; goto stopTest; }
if (e.Contains(Event.RegulValveTimeOut))
{
Bridge.OnError(this, Strings.Timeout);
retVal = Event.Done;
goto stopTest;
}
if (e.Contains(Event.Next)) goto flow_set;
}
while (!e.Contains(Event.FlowReached));
flow_set:
int time = StateMachine.Time;
double currentFlow = RefFlow.Val;
string[] imageNames = new string[sensPath.RegisterReaders.Length];
for (int i = 0; i < sensPath.RegisterReaders.Length; i++)
{
GenericDevices.IRoi roi = sensPath.RegisterReaders[i] as GenericDevices.IRoi;
if (roi != null)
{
roi.ClearRoi();
}
else
{
imageNames[i] = null;
}
}
detection_loop:
//---------------------------------------------------------
Bridge.OnActivity(this, Strings.ROI_Detection_in_Progress);
//---------------------------------------------------------
State detection = State.Create("RoiDetection : Detection of ROI-s")
.AddOperation(checkUiOp);
for (int i = 0; i < sensPath.RegisterReaders.Length; i++)
{
GenericDevices.IRoi roi = sensPath.RegisterReaders[i] as GenericDevices.IRoi;
if (roi != null && !roi.Detected)
{
detection.AddOperation(roi.RoiDetectionOp());
imageNames[i] = roi.ImageFileNamePrefix + "detected.jpg";
}
}
detection.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr, Config.Entities.Progress.Test));
if (e.Contains(Event.Error))
{
retVal = Event.Error;
goto stopTest;
}
if (TestAndLogUiCmdStop(test,e))
{
retVal = Event.UiCmdStop;
goto stopTest;
}
}
while (e.Contains(Event.CameraBusy));
//-----------------------------------------------------------------------------------------------------
if (e.Contains(Event.RoiDetectionPreviousFailed)) Bridge.OnActivity(this, Strings.Camera_error);
else if (e.Contains(Event.RoiDetectionFailed)) Bridge.OnActivity(this, Strings.Detection_failed);
else Bridge.OnActivity(this, Strings.Detection_completed);
//-----------------------------------------------------------------------------------------------------
string title = Strings.Select_images_and_press_Retry_to_retry_RoI_detection;
State.Create("RoiDetection : Select images and press Retry to retry RoI detection")
.AddOperation(checkUiOp)
.AddOperation((display == null) ? null : display.ShowStaticImageOp(imageNames, title))
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (TestAndLogUiCmdStop(test, e))
{
retVal = Event.UiCmdStop;
goto stopTest;
}
}
while (e.Contains(Event.ModelessFormIsOpen));
if (e.Contains(Event.Retry))
{
for (int i = 0; i < display.SelectedImages.Length; i++)
{
GenericDevices.IRoi roi = sensPath.RegisterReaders[i] as GenericDevices.IRoi;
if (roi != null && display.SelectedImages[i])
{
roi.ClearRoi();
}
}
goto detection_loop;
}
else if (e.Contains(Event.Abort))
{
retVal = Event.UiCmdStop;
goto stopTest;
}
//---------------------------------------------------
Bridge.OnActivity(this, Strings.Detection_completed);
//---------------------------------------------------
Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr, Config.Entities.Progress.TransitionAfter));
stopTest:
///----------------------///
/// Quit this sequence ///
///----------------------///
State.Create("RoiDetection : Stopping diverter, gate, etc.")
.AddOperation(checkUiOp)
.AddOperation(cBrd.StopPreviousOp())
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (TestAndLogUiCmdStop(test, e)) { if (retVal == Event.Done) retVal = Event.UiCmdStop; break; }
}
while (!e.Contains(Event.PreviousStopped));
/// Transition sequence at the end of test
/// Prevent overwriting 'retVal' in case it was set to non-default value earlier
switch (Transition(transitionAfter, (retVal == Event.Done) ? TransitionContext.AfterTest : TransitionContext.Stop))
{
case Event.Error: { if (retVal == Event.Done) retVal = Event.Error; break; }
case Event.UiCmdStop: { if (retVal == Event.Done) retVal = Event.UiCmdStop; break; }
}
Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr,
(retVal == Event.Done) ? Config.Entities.Progress.Completed : Config.Entities.Progress.Aborted));
/// Create a list with one item 'retVal' (default is Event.Done) and return it
IList<Event> retList = new List<Event>(1);
retList.Add(retVal);
return retList;
}
}
}