2017-10-17 10:43:26 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// Copyright (c) 2017 Sensus Metering Systems
|
|
|
|
|
|
///
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using log4net;
|
|
|
|
|
|
using TBF.Boxes;
|
|
|
|
|
|
|
|
|
|
|
|
namespace TBF.BenchControl.Elde
|
|
|
|
|
|
{
|
|
|
|
|
|
public class ReadDiverterTransitionOp : IOperation
|
|
|
|
|
|
{
|
|
|
|
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(ReadDiverterTransitionOp));
|
2018-06-11 11:40:35 +00:00
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Format("ReadDiverterTransitionOp(.,{0},{1},{2},...)", div.Name, runsCountWhenSendingCmd, runsCountWhenReadingDiv);
|
|
|
|
|
|
}
|
2017-10-17 10:43:26 +00:00
|
|
|
|
|
2018-06-08 12:52:34 +00:00
|
|
|
|
const int SamplesCount = 240; /// Should be less then or equal to 256
|
|
|
|
|
|
float[] samples;
|
|
|
|
|
|
|
2017-10-17 10:43:26 +00:00
|
|
|
|
/// Set by the constructor
|
|
|
|
|
|
readonly ControlBoardDev controlBoard;
|
2018-06-08 12:52:34 +00:00
|
|
|
|
readonly GenericDevices.IDiverter div;
|
2019-09-04 10:28:30 +00:00
|
|
|
|
readonly bool toTank;
|
2017-10-17 10:43:26 +00:00
|
|
|
|
readonly int runsCountWhenSendingCmd;
|
|
|
|
|
|
readonly int runsCountWhenReadingDiv;
|
2018-06-08 12:52:34 +00:00
|
|
|
|
readonly Sequences.Statistics plotter;
|
|
|
|
|
|
readonly int batchNr;
|
|
|
|
|
|
readonly string testName;
|
|
|
|
|
|
readonly int repetition;
|
2017-10-17 10:43:26 +00:00
|
|
|
|
|
|
|
|
|
|
int counter;
|
|
|
|
|
|
bool cmdSent = false;
|
|
|
|
|
|
bool divRead = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2019-09-04 10:28:30 +00:00
|
|
|
|
public ReadDiverterTransitionOp(ControlBoardDev controlBoard, GenericDevices.IDiverter div, int runsCountWhenSendingCmd, int runsCountWhenReadingDiv, bool toTank,
|
|
|
|
|
|
Sequences.Statistics plotter, int batchNr, string testName, int repetition)
|
2017-10-17 10:43:26 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (controlBoard == null) throw new ArgumentNullException("controlBoardDev");
|
2019-09-04 10:28:30 +00:00
|
|
|
|
if (div == null) throw new ArgumentNullException("div");
|
2017-10-17 10:43:26 +00:00
|
|
|
|
if (runsCountWhenSendingCmd < 0) throw new ArgumentNullException("runsCountWhenSendingCmd");
|
|
|
|
|
|
if (runsCountWhenReadingDiv <= runsCountWhenSendingCmd) throw new ArgumentNullException("runsCountWhenReadingDiv");
|
|
|
|
|
|
|
|
|
|
|
|
this.controlBoard = controlBoard;
|
2018-06-08 12:52:34 +00:00
|
|
|
|
this.div = div;
|
2017-10-17 10:43:26 +00:00
|
|
|
|
this.runsCountWhenSendingCmd = runsCountWhenSendingCmd;
|
|
|
|
|
|
this.runsCountWhenReadingDiv = runsCountWhenReadingDiv;
|
2019-09-04 10:28:30 +00:00
|
|
|
|
this.toTank = toTank;
|
2018-06-08 12:52:34 +00:00
|
|
|
|
this.plotter = plotter;
|
|
|
|
|
|
this.batchNr = batchNr;
|
|
|
|
|
|
this.testName = testName;
|
|
|
|
|
|
this.repetition = repetition;
|
|
|
|
|
|
|
|
|
|
|
|
samples = new float[SamplesCount];
|
2017-10-17 10:43:26 +00:00
|
|
|
|
|
|
|
|
|
|
log.Debug(this.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Start this operation</summary>
|
|
|
|
|
|
public void Start()
|
|
|
|
|
|
{
|
|
|
|
|
|
counter = 0;
|
2018-06-08 12:52:34 +00:00
|
|
|
|
|
2018-06-11 11:40:35 +00:00
|
|
|
|
if (runsCountWhenSendingCmd == 0 && div.DiverterNr > 0)
|
2017-10-17 10:43:26 +00:00
|
|
|
|
{
|
2018-06-11 11:40:35 +00:00
|
|
|
|
SendCommand();
|
|
|
|
|
|
log.DebugFormat("Start() ... Sending a command to read diverter {0}", div.Name);
|
2018-06-08 12:52:34 +00:00
|
|
|
|
}
|
2018-06-11 11:40:35 +00:00
|
|
|
|
|
|
|
|
|
|
if (plotter != null)
|
2018-06-08 12:52:34 +00:00
|
|
|
|
{
|
|
|
|
|
|
plotter.Start(batchNr, testName, repetition);
|
2018-06-11 11:40:35 +00:00
|
|
|
|
log.DebugFormat("Start() ... plotter.Start({0}, {1}, {2})", batchNr, testName, repetition);
|
2017-10-17 10:43:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Run this operation</summary>
|
|
|
|
|
|
/// <returns>
|
|
|
|
|
|
/// Event.FlowInDone or Event.FlowOutDone
|
|
|
|
|
|
/// </returns>
|
|
|
|
|
|
public Event Run()
|
|
|
|
|
|
{
|
|
|
|
|
|
counter++;
|
|
|
|
|
|
|
2018-06-11 11:40:35 +00:00
|
|
|
|
if (counter == runsCountWhenSendingCmd && div.DiverterNr > 0)
|
2017-10-17 10:43:26 +00:00
|
|
|
|
{
|
2018-06-11 11:40:35 +00:00
|
|
|
|
SendCommand();
|
|
|
|
|
|
log.DebugFormat("Run() ... Sending a command to read diverter {0}", div.Name);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (counter == runsCountWhenReadingDiv && div.DiverterNr > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (plotter == null)
|
2018-06-08 12:52:34 +00:00
|
|
|
|
{
|
2018-06-11 11:40:35 +00:00
|
|
|
|
ReadDiverterTransitionTimes();
|
2019-09-04 10:28:30 +00:00
|
|
|
|
log.DebugFormat("Run() ... Reading diverter {0} transition times: {1}s", div.Name, (toTank ? div.SwitchTimeStart : div.SwitchTimeEnd).Val);
|
2018-06-08 12:52:34 +00:00
|
|
|
|
}
|
2018-06-11 11:40:35 +00:00
|
|
|
|
else
|
2018-06-08 12:52:34 +00:00
|
|
|
|
{
|
2018-06-11 11:40:35 +00:00
|
|
|
|
log.DebugFormat("Run() ... Reading diverter {0} samples", div.Name);
|
|
|
|
|
|
for (int i = 0; i < SamplesCount; i++)
|
2018-06-08 12:52:34 +00:00
|
|
|
|
{
|
2018-06-11 11:40:35 +00:00
|
|
|
|
float oneSample = controlBoard.DivSamples(i, div.DiverterNr - 1);
|
|
|
|
|
|
samples[i] = oneSample;
|
|
|
|
|
|
plotter.Update(oneSample);
|
2018-06-08 12:52:34 +00:00
|
|
|
|
}
|
2018-06-11 11:40:35 +00:00
|
|
|
|
plotter.Stop();
|
|
|
|
|
|
|
2019-09-04 10:28:30 +00:00
|
|
|
|
log.DebugFormat("Run() ... Calculating diverter {0} transition times: {1}s", div.Name, (toTank ? div.SwitchTimeStart : div.SwitchTimeEnd).Val);
|
2018-06-11 11:40:35 +00:00
|
|
|
|
CalculateDiverterTransitionTimes();
|
2018-06-08 12:52:34 +00:00
|
|
|
|
}
|
2017-10-17 10:43:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Event.None;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Stop this operation</summary>
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
|
{
|
2018-06-11 11:40:35 +00:00
|
|
|
|
log.DebugFormat("Stop()");
|
2018-06-08 12:52:34 +00:00
|
|
|
|
}
|
2017-10-17 10:43:26 +00:00
|
|
|
|
|
|
|
|
|
|
void SendCommand()
|
|
|
|
|
|
{
|
2019-10-24 10:57:37 +00:00
|
|
|
|
controlBoard.SendCommand(Command.ReadDivTransition, 0, 0, 0, 0, StopDevs.None);
|
2017-10-17 10:43:26 +00:00
|
|
|
|
cmdSent = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-08 12:52:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Diverter transition time is read from the control board
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>true (always)</returns>
|
|
|
|
|
|
bool ReadDiverterTransitionTimes()
|
|
|
|
|
|
{
|
2019-09-04 10:28:30 +00:00
|
|
|
|
(toTank ? div.SwitchTimeStart : div.SwitchTimeEnd).Val = (float)controlBoard.DivTime(2) - (float)controlBoard.DivTime(1);
|
2018-06-08 12:52:34 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Diverter transition time is calculated from ADC samples and threshold levels
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>true when transition time calculated OK</returns>
|
|
|
|
|
|
bool CalculateDiverterTransitionTimes()
|
2017-10-17 10:43:26 +00:00
|
|
|
|
{
|
2018-06-08 12:52:34 +00:00
|
|
|
|
bool startedRising = false;
|
|
|
|
|
|
bool startedFalling = false;
|
|
|
|
|
|
bool stopped = false;
|
|
|
|
|
|
|
|
|
|
|
|
int transitionStartIdx = 0;
|
|
|
|
|
|
int transitionEndIdx = 0;
|
|
|
|
|
|
|
|
|
|
|
|
float level = samples[0];
|
|
|
|
|
|
for (int i = 1; i < SamplesCount; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!startedRising && !startedFalling)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (level <= div.ThresholdLo && samples[i] > div.ThresholdLo)
|
|
|
|
|
|
{
|
|
|
|
|
|
transitionStartIdx = i;
|
|
|
|
|
|
startedRising = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (level >= div.ThresholdHi && samples[i] < div.ThresholdHi)
|
|
|
|
|
|
{
|
|
|
|
|
|
transitionStartIdx = i;
|
|
|
|
|
|
startedFalling = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (startedRising && !stopped && level < div.ThresholdHi && samples[i] >= div.ThresholdHi)
|
|
|
|
|
|
{
|
|
|
|
|
|
transitionEndIdx = i;
|
|
|
|
|
|
stopped = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (startedFalling && !stopped && level > div.ThresholdLo && samples[i] <= div.ThresholdLo)
|
|
|
|
|
|
{
|
|
|
|
|
|
transitionEndIdx = i;
|
|
|
|
|
|
stopped = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((startedRising || startedFalling) && stopped)
|
|
|
|
|
|
{
|
2019-09-04 10:28:30 +00:00
|
|
|
|
(toTank ? div.SwitchTimeStart : div.SwitchTimeEnd).Val = 0.002f * (float)(transitionEndIdx - transitionStartIdx); /// 2 ms per sample
|
2018-06-08 12:52:34 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2017-10-17 10:43:26 +00:00
|
|
|
|
}
|
2018-06-08 12:52:34 +00:00
|
|
|
|
}
|
2017-10-17 10:43:26 +00:00
|
|
|
|
}
|