Default action and stopping the FM-controlled pump

moved to SequenceBase.Transition(sequence, context).
Sequences cleaned-up.
This commit is contained in:
Milan Hanajik
2014-08-14 09:06:01 +02:00
parent e128784bb8
commit 596667a67e
7 changed files with 185 additions and 256 deletions
+11
View File
@@ -21,6 +21,17 @@
Warning = 0x02, /// Some settings in the user control are suspicious, can be set after user confirmation
}
/// <summary>
/// Passed as an argument to function executing transition sequences
/// </summary>
public enum TransitionContext
{
PurgeBegin,
PurgeEnd,
TestStart,
TestEnd,
}
/// <summary>
/// StateMachine events (issued by operations, IOperation derived classes)
/// </summary>
@@ -130,7 +130,7 @@ namespace TBF.BenchControl.Sequences
fill_the_bench:
/// Purge - Begin
switch (Transition(purgeBegin, Strings.Purging_i_n))
switch (Transition(purgeBegin, TransitionContext.PurgeBegin))
{
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
@@ -340,7 +340,7 @@ namespace TBF.BenchControl.Sequences
}
/// Purge - End
switch (Transition(purgeEnd, Strings.Emptying_i_n))
switch (Transition(purgeEnd, TransitionContext.PurgeEnd))
{
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
@@ -312,92 +312,159 @@ namespace TBF.BenchControl.Sequences
/// </summary>
/// <param name="transitionSequence">TransitionSequence entity</param>
/// <returns>Event.Done, Event.Error or Event.UiCmdStop</returns>
protected Event Transition(Entities.TransitionSequence transitionSequence, string message)
protected Event Transition(Entities.TransitionSequence transitionSequence, TransitionContext context)
{
if (transitionSequence == null) return Event.None;
IList<Entities.TransitionStep> transitionSteps = StateMachine.WtSession
.CreateQuery("FROM TransitionStep WHERE TransitionSequence = :tsId ORDER BY ItemNr")
.SetParameter("tsId", transitionSequence.Id)
.List<Entities.TransitionStep>();
IList<Event> e;
int stepsCount = transitionSteps.Count;
foreach (var step in transitionSteps)
string message;
switch (context)
{
//------------------------------------------------
Bridge.OnActivity(this, string.Format(message, transitionSequence.Name, step.ItemNr + 1, stepsCount));
//------------------------------------------------
case TransitionContext.PurgeBegin: message = Strings.Purging_i_n; break;
case TransitionContext.PurgeEnd: message = Strings.Emptying_i_n; break;
case TransitionContext.TestStart: message = Strings.Test_start_sequence_i_n; break;
case TransitionContext.TestEnd: message = Strings.Test_stop_sequence_i_n; break;
default: message = "Transition"; break;
}
/// Get new regulation valve positions and calculate the number of valves to change
float[] allRegvPositions = Utils.RegulValvesPositions(step);
///
IList<IOperation> rvPosOperations = new List<IOperation>();
for (int i = 0; i < allRegvPositions.Length; i++)
{
if (allRegvPositions[i] >= 0) /// Negative value means no position change
{
rvPosOperations.Add(RegulValves[i].SetRegulValvePositionOp(
Math.Max(0, allRegvPositions[i] - 0.05f), Math.Min(100.0f, allRegvPositions[i] + 0.05f), 60));
}
}
int changingRVsCount = rvPosOperations.Count; /// Number of RV-s with changing position in this step
/// Duration must be at least the number of regulation valves to change seconds
int duration = Math.Max(step.Duration, changingRVsCount);
///
/// The first state updates all (regular) valves and the 1st regulation valve to be changed
/// Max. one regulation valve may be controlled in each sub-step (in each state)
///
State oneStep = State.Create(string.Format("SequenceBase.Transition() : Step {0}.1/{1}", step.ItemNr + 1, stepsCount))
.AddOperation(checkUiOp)
.AddOperation(StateMachine.ControlBoard.SetValvesOp(Utils.ValvesOpen(step), Utils.ValvesClose(step)))
.AddOperation(new TimerOp((changingRVsCount <= 1) ? step.Duration : 1));
if (changingRVsCount > 0)
{
oneStep.AddOperation(rvPosOperations[0]);
}
oneStep.EnterState();
do {
e = StateMachine.WaitRunDevsRunOps();
log.InfoFormat("RV1={0}% RV2={1}% RV3={2}% RV4={3}% RV5={4}%", StateMachine.ControlBoard.RValvePosition(1),
StateMachine.ControlBoard.RValvePosition(2), StateMachine.ControlBoard.RValvePosition(3),
StateMachine.ControlBoard.RValvePosition(4), StateMachine.ControlBoard.RValvePosition(5));
if (e.Contains(Event.Error)) return Event.Error;
if (e.Contains(Event.RegulValveTimeOut)) return Event.Error;
if (e.Contains(Event.UiCmdStop)) return Event.UiCmdStop;
if (transitionSequence == null)
{
///
/// Default action when no transition sequence is selected
///
if (context == TransitionContext.TestStart)
{
State.Create("SequenceBase : Transition : TestStart - Default action")
.AddOperation(checkUiOp).AddOperation(StateMachine.ControlBoard
.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)) return Event.UiCmdStop;
}
while (!e.Contains(Event.ValvesSet));
}
while (!e.Contains(Event.TimerExpired));
else if (context == TransitionContext.TestEnd)
{
State.Create("SequenceBase : Transition : TestEnd - Default action")
.AddOperation(checkUiOp).AddOperation(StateMachine.ControlBoard
.SetValvesOp(StateMachine.DefaultValvesOpen, StateMachine.DefaultValvesClose))
.EnterState();
do {
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.UiCmdStop)) return Event.UiCmdStop;
}
while (!e.Contains(Event.ValvesSet));
}
}
else
{
///
/// Execute an existing transition sequence
///
IList<Entities.TransitionStep> transitionSteps = StateMachine.WtSession
.CreateQuery("FROM TransitionStep WHERE TransitionSequence = :tsId ORDER BY ItemNr")
.SetParameter("tsId", transitionSequence.Id)
.List<Entities.TransitionStep>();
///
/// Additional extra states the 2nd and all subsequent regulation valves to be changed
/// Max. one regulation valve may be controlled in each sub-step (in each state)
///
for (int j = 1; j < changingRVsCount; j++)
{
/// The first state updating all plain valves and the 1st regulation valve
/// Max. one regulation valve may change in each step
State oneStepContinued = State.Create(string.Format("SequenceBase.Transition() : Step {0}.{2}/{1}", step.ItemNr + 1, stepsCount, j+1))
.AddOperation(checkUiOp)
.AddOperation(new TimerOp((j == changingRVsCount - 1) ? (step.Duration - changingRVsCount + 1) : 1));
for (int k = 0; k <= j; k++) { oneStepContinued.AddOperation(rvPosOperations[k]); }
oneStepContinued.EnterState();
int stepsCount = transitionSteps.Count;
foreach (var step in transitionSteps)
{
//------------------------------------------------
Bridge.OnActivity(this, string.Format(message, transitionSequence.Name, step.ItemNr + 1, stepsCount));
//------------------------------------------------
do
{
e = StateMachine.WaitRunDevsRunOps();
log.InfoFormat("RV1={0}% RV2={1}% RV3={2}% RV4={3}% RV5={4}%", StateMachine.ControlBoard.RValvePosition(1),
StateMachine.ControlBoard.RValvePosition(2), StateMachine.ControlBoard.RValvePosition(3),
StateMachine.ControlBoard.RValvePosition(4), StateMachine.ControlBoard.RValvePosition(5));
if (e.Contains(Event.Error)) return Event.Error;
if (e.Contains(Event.RegulValveTimeOut)) return Event.Error;
if (e.Contains(Event.UiCmdStop)) return Event.UiCmdStop;
}
while (!e.Contains(Event.TimerExpired));
}
/// Get new regulation valve positions and calculate the number of valves to change
float[] allRegvPositions = Utils.RegulValvesPositions(step);
///
IList<IOperation> rvPosOperations = new List<IOperation>();
for (int i = 0; i < allRegvPositions.Length; i++)
{
if (allRegvPositions[i] >= 0) /// Negative value means no position change
{
rvPosOperations.Add(RegulValves[i].SetRegulValvePositionOp(
Math.Max(0, allRegvPositions[i] - 0.05f), Math.Min(100.0f, allRegvPositions[i] + 0.05f), 60));
}
}
int changingRVsCount = rvPosOperations.Count; /// Number of RV-s with changing position in this step
/// Duration must be at least the number of regulation valves to change seconds
int duration = Math.Max(step.Duration, changingRVsCount);
///
/// The first state updates all (regular) valves and the 1st regulation valve to be changed
/// Max. one regulation valve may be controlled in each sub-step (in each state)
///
State oneStep = State.Create(string.Format("SequenceBase.Transition() : Step {0}.1/{1}", step.ItemNr + 1, stepsCount))
.AddOperation(checkUiOp)
.AddOperation(StateMachine.ControlBoard.SetValvesOp(Utils.ValvesOpen(step), Utils.ValvesClose(step)))
.AddOperation(new TimerOp((changingRVsCount <= 1) ? step.Duration : 1));
if (changingRVsCount > 0)
{
oneStep.AddOperation(rvPosOperations[0]);
}
oneStep.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
log.InfoFormat("RV1={0}% RV2={1}% RV3={2}% RV4={3}% RV5={4}%", StateMachine.ControlBoard.RValvePosition(1),
StateMachine.ControlBoard.RValvePosition(2), StateMachine.ControlBoard.RValvePosition(3),
StateMachine.ControlBoard.RValvePosition(4), StateMachine.ControlBoard.RValvePosition(5));
if (e.Contains(Event.Error)) return Event.Error;
if (e.Contains(Event.RegulValveTimeOut)) return Event.Error;
if (e.Contains(Event.UiCmdStop)) return Event.UiCmdStop;
}
while (!e.Contains(Event.TimerExpired));
///
/// Additional extra states the 2nd and all subsequent regulation valves to be changed
/// Max. one regulation valve may be controlled in each sub-step (in each state)
///
for (int j = 1; j < changingRVsCount; j++)
{
/// The first state updating all plain valves and the 1st regulation valve
/// Max. one regulation valve may change in each step
State oneStepContinued = State.Create(string.Format("SequenceBase.Transition() : Step {0}.{2}/{1}", step.ItemNr + 1, stepsCount, j + 1))
.AddOperation(checkUiOp)
.AddOperation(new TimerOp((j == changingRVsCount - 1) ? (step.Duration - changingRVsCount + 1) : 1));
for (int k = 0; k <= j; k++) { oneStepContinued.AddOperation(rvPosOperations[k]); }
oneStepContinued.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
log.InfoFormat("RV1={0}% RV2={1}% RV3={2}% RV4={3}% RV5={4}%", StateMachine.ControlBoard.RValvePosition(1),
StateMachine.ControlBoard.RValvePosition(2), StateMachine.ControlBoard.RValvePosition(3),
StateMachine.ControlBoard.RValvePosition(4), StateMachine.ControlBoard.RValvePosition(5));
if (e.Contains(Event.Error)) return Event.Error;
if (e.Contains(Event.RegulValveTimeOut)) return Event.Error;
if (e.Contains(Event.UiCmdStop)) return Event.UiCmdStop;
}
while (!e.Contains(Event.TimerExpired));
}
}
}
if (context == TransitionContext.TestEnd)
{
///
/// Stop the pump at the end of test
///
StateMachine.ControlBoard.SetFMFreq(0, 0); /// Stop FM controlled pumps
State.Create("SequenceBase : Test(s) completed -> Stopping the pump")
.AddOperation(checkUiOp)
.AddOperation((inPath.Pump is GenericDevices.IPumpFM) ? (inPath.Pump as GenericDevices.IPumpFM).TurnOff() : null)
.AddOperation(StateMachine.ControlBoard.SetValvesOp(null, inPath.Pump))
.AddOperation(StateMachine.ControlBoard.UpdateTankWeightOp())
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.Error)) return Event.Error;
}
while (!e.Contains(Event.ValvesSet) || ((inPath.Pump is GenericDevices.IPumpFM) && !e.Contains(Event.TurnPumpOnOffDone)));
}
return Event.Done;
@@ -43,28 +43,10 @@ namespace TBF.BenchControl.TestMethods.CameraRoiDetection
//====================================
// Transition or SetRoute - Start
//====================================
if (transitionStart != null)
switch (Transition(transitionStart, TransitionContext.TestStart))
{
switch (Transition(transitionStart, "RoiDetection : " + Strings.Test_start_sequence_i_n))
{
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
}
}
else
{
//--------------------------------
State.Create("RoiDetection : 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));
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
}
//------------------------------------------------
@@ -170,25 +152,10 @@ namespace TBF.BenchControl.TestMethods.CameraRoiDetection
while (!e.Contains(Event.ValvesSet) || ((inPath.Pump is GenericDevices.IPumpFM) && !e.Contains(Event.TurnPumpOnOffDone)));
/// Transition or SetRoute - End
if (transitionStop != null)
switch (Transition(transitionStop, TransitionContext.TestEnd))
{
switch (Transition(transitionStop, "RoiDetection : " + Strings.Test_stop_sequence_i_n))
{
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
}
}
else
{
State.Create("RoiDetection : Detection 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));
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
}
/// Create the return value: a list with one event Event.Done
@@ -55,28 +55,10 @@ namespace TBF.BenchControl.TestMethods.CombinedMeters
//====================================
// Transition or SetRoute - Start
//====================================
if (transitionStart != null)
switch (Transition(transitionStart, TransitionContext.TestStart))
{
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));
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
}
//====================================
@@ -424,25 +406,10 @@ namespace TBF.BenchControl.TestMethods.CombinedMeters
while (!e.Contains(Event.ValvesSet) || ((inPath.Pump is GenericDevices.IPumpFM) && !e.Contains(Event.TurnPumpOnOffDone)));
/// Transition or SetRoute - End
if (transitionStop != null)
switch (Transition(transitionStop, TransitionContext.TestEnd))
{
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));
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
}
/// Create the return value - a list with one event Event.Done - and return
@@ -56,28 +56,10 @@ namespace TBF.BenchControl.TestMethods.CombinedWithDetection
//====================================
// Transition or SetRoute - Start
//====================================
if (transitionStart != null)
switch (Transition(transitionStart, TransitionContext.TestStart))
{
switch (Transition(transitionStart, "CombinedWithDetection : " + Strings.Test_start_sequence_i_n))
{
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
}
}
else
{
//--------------------------------
State.Create("CombinedWithDetection : 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));
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
}
//====================================
@@ -642,25 +624,10 @@ namespace TBF.BenchControl.TestMethods.CombinedWithDetection
while (!e.Contains(Event.ValvesSet) || ((inPath.Pump is GenericDevices.IPumpFM) && !e.Contains(Event.TurnPumpOnOffDone)));
/// Transition or SetRoute - End
if (transitionStop != null)
switch (Transition(transitionStop, TransitionContext.TestEnd))
{
switch (Transition(transitionStop, "CombinedWithDetection : " + Strings.Test_stop_sequence_i_n))
{
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
}
}
else
{
State.Create("CombinedWithDetection : 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));
case Event.Error: goto error;
case Event.UiCmdStop: goto stop;
}
/// Create the return value - a list with one event Event.Done - and return
@@ -36,28 +36,11 @@ namespace TBF.BenchControl.TestMethods.FlyingStartCollectionMethod
//====================================
// Transition or SetRoute - Start
//====================================
if (transitionStart != null)
switch (Transition(transitionStart, TransitionContext.TestStart))
{
switch (Transition(transitionStart, "FlyingStartCollectionMethod : " + Strings.Test_start_sequence_i_n))
{
case Event.Error: goto error;
case Event.UiCmdStop: goto stopTestAndRunningWater;
}
}
else
{
//--------------------------------
State.Create("FlyingStartCollectionMethod : 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 stopTestAndRunningWater;
}
while (!e.Contains(Event.ValvesSet));
case Event.Error: goto error;
case Event.UiCmdStop: goto stopTestAndRunningWater;
default: break;
}
//====================================
@@ -374,47 +357,14 @@ namespace TBF.BenchControl.TestMethods.FlyingStartCollectionMethod
}
while (!e.Contains(Event.PreviousStopped));
stopTestAndRunningWater:
///--------------------------------
/// Transition or SetRoute - End
if (transitionStop != null)
{
switch (Transition(transitionStop, "FlyingStartCollectionMethod : " + Strings.Test_stop_sequence_i_n))
{
case Event.Error: goto error;
case Event.UiCmdStop: goto stopTestAndRunningWater;
}
}
else
{
State.Create("FlyingStartCollectionMethod : 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));
}
///--------------------------------
/// Stop the pump
cBrd.SetFMFreq(0, 0); /// Stop FM controlled pumps
State.Create("FlyingStartCollectionMethod : Test(s) completed -> Stopping the pump")
.AddOperation(checkUiOp)
.AddOperation((inPath.Pump is GenericDevices.IPumpFM) ? (inPath.Pump as GenericDevices.IPumpFM).TurnOff() : null)
.AddOperation(cBrd.SetValvesOp(null, inPath.Pump))
.AddOperation(cBrd.UpdateTankWeightOp())
.EnterState();
do
/// Transition sequence at the end of test
switch (Transition(transitionStop, TransitionContext.TestEnd))
{
e = StateMachine.WaitRunDevsRunOps();
if (e.Contains(Event.Error)) goto error;
case Event.Error: goto error;
case Event.UiCmdStop: goto stopTestAndRunningWater;
}
while (!e.Contains(Event.ValvesSet) || ((inPath.Pump is GenericDevices.IPumpFM) && !e.Contains(Event.TurnPumpOnOffDone)));
/// Create the return value - a list with one event Event.Done - and return
IList<Event> retval = new List<Event>();