(1) Diverter start/stop transition graph plotted, (2) Diverter ToTank / ToSink buttons, ver. 3.1.1616

This commit is contained in:
Milan Hanajik
2021-03-28 08:38:30 +02:00
parent 0fc8bb4ea0
commit baf7f84343
14 changed files with 414 additions and 112 deletions
+99 -7
View File
@@ -1,21 +1,38 @@
///
/// Copyright (c) 2021 Sensus Slovensko a.s.
///
using System;
using log4net;
using TBF.Rig.Sequences;
using TBF.Rig.Uni.Diverter;
namespace TBF.Rig.ControlBoard.Uni
{
public class DivTransitionData
{
private static readonly ILog log = LogManager.GetLogger(typeof(DivTransitionData));
/// Used in FrameFitsData(), etc.
public const int MessageLen = 255;
public const byte MessageCode = (byte)'K';
/// Associated diverter and plotter
public static Diverter Diverter;
public static bool ToTank;
public static Statistics Plotter;
public static int DiverterAssociationValidUntil;
/// Diverter transition data
const int samplesCount = 245;
int DiverterTime_ms;
byte[] CurveRaw;
public byte[] Samples;
public int Resolution_ms;
public int TransitionTime_ms;
const int SamplesCount = 247;
DivTransitionData()
{
CurveRaw = new byte[samplesCount];
Samples = new byte[SamplesCount];
Resolution_ms = Const.DivTimeResolution;
}
/// <summary>
@@ -47,13 +64,88 @@ namespace TBF.Rig.ControlBoard.Uni
/// <param name="offset">Offset of the message</param>
public static DivTransitionData GetData(byte[] rcvdData, int offset)
{
log.DebugFormat("DivTransitionDataGetData() ... Diverter = {0}, DiverterAssociationValidUntil-StateMachine.Time = {1}s left",
(Diverter != null) ? Diverter.Name : "none", DiverterAssociationValidUntil - StateMachine.Time);
var data = new DivTransitionData();
data.DiverterTime_ms = Const.DivTimeResolution * rcvdData[offset + 4];
for (int i = 0; i < samplesCount; i++)
for (int i = 0; i < SamplesCount; i++)
{
data.CurveRaw[i] = rcvdData[offset + 6 + i];
data.Samples[i] = rcvdData[offset + 6 + i];
}
if (Diverter != null && StateMachine.Time <= DiverterAssociationValidUntil)
{
data.TransitionTime_ms = Const.DivTimeResolution * CalculateDivTransitionTime(data.Samples, Diverter.ThresholdLo, Diverter.ThresholdHi);
log.DebugFormat("{0} transition time = {1} ms", Diverter.Name, data.TransitionTime_ms);
if (Plotter != null)
{
int rangeLo = Math.Min(Diverter.AdcValueToSink, Diverter.AdcValueToTank);
int rangeHi = Math.Max(Diverter.AdcValueToSink, Diverter.AdcValueToTank);
if (rangeLo != rangeHi)
{
float denominator = Convert.ToSingle(rangeHi - rangeLo);
for (int i = 0; i < SamplesCount; i++)
{
float y = Convert.ToSingle(data.Samples[i] - rangeLo) / denominator;
Plotter.Update(y);
}
}
Plotter.Stop();
}
}
return data;
}
/// <summary>
/// Analyze diverter transition data and calculate transition time
/// </summary>
/// <param name="samples">Raw data samples</param>
/// <param name="thresholdLo">Threshod level Low</param>
/// <param name="thresholdHi">Threshold level High</param>
/// <returns>Transition time in sasmple periods</returns>
static int CalculateDivTransitionTime(byte[] samples, int thresholdLo, int thresholdHi)
{
bool startedRising = false;
bool startedFalling = false;
bool stopped = false;
int transitionStartIdx = 0;
int transitionEndIdx = 0;
int level = samples[0];
for (int i = 1; i < SamplesCount; i++)
{
if (!startedRising && !startedFalling)
{
if (level <= thresholdLo && samples[i] > thresholdLo)
{
transitionStartIdx = i;
startedRising = true;
}
else if (level >= thresholdHi && samples[i] < thresholdHi)
{
transitionStartIdx = i;
startedFalling = true;
}
}
else if (startedRising && !stopped && level < thresholdHi && samples[i] >= thresholdHi)
{
transitionEndIdx = i;
stopped = true;
break;
}
else if (startedFalling && !stopped && level > thresholdLo && samples[i] <= thresholdLo)
{
transitionEndIdx = i;
stopped = true;
break;
}
}
return transitionEndIdx - transitionStartIdx;
}
}
}