tbf/TBF/BenchControl/Elde/StartMeasurementOp.cs

205 lines
7.2 KiB
C#

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using log4net;
namespace TBF.BenchControl.Elde
{
public class StartMeasurementOp : IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(StartMeasurementOp));
public override string ToString() { return string.Format("StartMeasurementOp(ref={0},pulses={1})", flowMeterNr, totalRefPulses); }
/// Set by the constructor
readonly ControlBoardDev controlBoard;
readonly Elde.RegulValve.RegulValve regulValve;
readonly int regulValveNr;
readonly int flowMeterNr;
readonly float nominalFlow;
readonly float reqFlowLo;
readonly float reqFlowHi;
readonly float reqFlowAve;
readonly float pidCoef;
readonly int totalRefPulses; /// Number of reference pulses for a complete test
readonly int massRefPulses; /// Number of reference pulses for mass collection
readonly TestMethods testMethod;
readonly float filterConstant;
readonly int diverterNr;
bool started = false;
///
/// Internal states of this operation
///
enum OpState
{
StartingTest,
ResendCommand,
TestStarted,
ValveMoveToFlow,
OpCompleted,
}
OpState opState;
/// <summary>
/// Starts the measurement.
/// Events: MeasurementStarted
/// </summary>
/// <param name="controlBoard">Control board component reference</param>
/// <param name="outputPath">Specifies used components</param>
/// <param name="method">Test method</param>
/// <param name="totalRefPulses">Test duration in number of reference flowmeter pulses</param>
/// <param name="filterConstant">Specifies filter for watermeter pulses</param>
public StartMeasurementOp(ControlBoardDev controlBoard, OutputPath outputPath, TestMethods method,
float requiredFlowLo, float requiredFlowHi,
int refPulses, float filterConstant)
: this(controlBoard, outputPath, method, requiredFlowLo, requiredFlowHi, refPulses, refPulses, filterConstant)
{
}
/// <summary>
/// Starts the measurement.
/// Events: MeasurementStarted
/// </summary>
/// <param name="controlBoard">Control board component reference</param>
/// <param name="outputPath">Specifies used components</param>
/// <param name="method">Test method</param>
/// <param name="refPulses">Test duration in number of reference flowmeter pulses</param>
/// <param name="filterConstant">Specifies filter for watermeter pulses</param>
public StartMeasurementOp(ControlBoardDev controlBoard, OutputPath outputPath, TestMethods method,
float requiredFlowLo, float requiredFlowHi,
int totalRefPulses, int massRefPulses, float filterConstant)
{
if (controlBoard == null) throw new ArgumentNullException("controlBoardDev");
this.controlBoard = controlBoard;
if (outputPath.FlowMeter == null) throw new ArgumentNullException("Flow meter is missing");
flowMeterNr = outputPath.FlowMeter.Idx1;
if (outputPath.RegulValve is Elde.RegulValve.RegulValve)
{
regulValve = outputPath.RegulValve as Elde.RegulValve.RegulValve;
regulValveNr = this.regulValve.Idx1;
}
else if (!(outputPath.RegulValve is Dummy.RegulValve.RegulValve))
{
throw new ArgumentNullException("Regulation valve is not compatible with Elde Control Board");
}
if (outputPath.Diverter is Elde.Diverter.Diverter)
{
diverterNr = (outputPath.Diverter as Elde.Diverter.Diverter).DiverterNr;
}
nominalFlow = outputPath.FlowMeter.NominalFlow;
this.reqFlowLo = requiredFlowLo;
this.reqFlowHi = requiredFlowHi;
this.reqFlowAve = (reqFlowLo + reqFlowHi) / 2.0f;
this.pidCoef = outputPath.PidCoef;
this.totalRefPulses = totalRefPulses;
this.massRefPulses = massRefPulses;
this.testMethod = method;
this.filterConstant = filterConstant;
log.Debug(this.ToString());
}
void SendStartCommand()
{
controlBoard.SyncRoute();
#if TURA_IPERL || TURA_SPECIAL || BADGER_VELKA_TRAT || SLM_50
int flowMeterIdxAndDiv = flowMeterNr + 16 * diverterNr;
#elif BADGER_STREDNA_TRAT
int flowMeterIdxAndDiv = flowMeterNr + 8 * (diverterNr - 1);
#else
int flowMeterIdxAndDiv = flowMeterNr;
#endif
log.InfoFormat("Start() : SendCommand(Cmd.Start, {0}, route, {1}, {1}, {2}, {3}, {4}, 0, Stop.None)",
flowMeterIdxAndDiv, totalRefPulses, testMethod, filterConstant, (int)pidCoef);
controlBoard.SendCommand(Command.Start, flowMeterIdxAndDiv, controlBoard.Route,
totalRefPulses, massRefPulses, testMethod,
filterConstant, (int)pidCoef, 0, StopDevs.None);
}
/// <summary>Start this operation</summary>
public void Start()
{
SendStartCommand();
opState = OpState.StartingTest;
}
/// <summary>Run this operation</summary>
/// <returns>
/// Event.FlowInDone or Event.FlowOutDone
/// </returns>
public Event Run()
{
if (opState == OpState.StartingTest)
{
StatusP statusP = controlBoard.StatusP;
log.DebugFormat("statusP = {0} {1} {2} {3}",
(((ulong)statusP >> 48) & 0xFFFF).ToString("X4"),
(((ulong)statusP >> 32) & 0xFFFF).ToString("X4"),
(((ulong)statusP >> 16) & 0xFFFF).ToString("X4"),
((ulong)statusP & 0xFFFF).ToString("X4"));
if ((statusP & StatusP.TestInProgress) != 0)
{
log.Info("Test started");
opState = OpState.TestStarted;
}
else
{
//opState = OpState.ResendCommand;
}
return Event.None;
}
else if (opState == OpState.ResendCommand)
{
SendStartCommand();
opState = OpState.StartingTest;
return Event.None;
}
else if (opState == OpState.TestStarted)
{
///
/// Done once, issues an appropriate ValveMove(...) command
///
if (reqFlowLo >= reqFlowHi || reqFlowLo > nominalFlow || reqFlowHi <= 0)
{
return Event.OpArgumentError;
}
log.InfoFormat("Run(): rv#={0} flowMtr#={1} TARGET: flowLo={2} flowHi={3}",
regulValveNr, flowMeterNr, reqFlowLo, reqFlowHi);
float freqLo = 2000.0f * reqFlowLo / nominalFlow;
float freqHi = 2000.0f * reqFlowHi / nominalFlow;
controlBoard.ValveMove(regulValveNr, RegulValveMode.TargetFrequency,
new float[2] { freqLo, freqHi },
regulValve.StableTime);
opState = OpState.OpCompleted;
return Event.None;
}
else if (opState == OpState.OpCompleted)
{
return Event.MeasurementStarted;
}
return Event.None;
}
/// <summary>Stop this operation</summary>
public void Stop()
{
}
}
}