95 lines
2.7 KiB
C#
95 lines
2.7 KiB
C#
///
|
|
/// Copyright (c) 2013-2015 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using log4net;
|
|
using TBF.Boxes;
|
|
|
|
namespace TBF.BenchControl.MettlerToledo.Standard
|
|
{
|
|
/// <summary>
|
|
/// Operation to read more mass values and average them to obtain a more accurate result.
|
|
/// </summary>
|
|
public class ReadStableMassOp : IOperation
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(ReadStableMassOp));
|
|
public override string ToString() { return string.Format("ReadStableMassOp(.,{0},.)", totalReadingsCount); }
|
|
|
|
/// Operation specific
|
|
float[] massReadings;
|
|
int currentReadingsCount;
|
|
|
|
/// Set by the constructor
|
|
BalanceDev balanceDev;
|
|
int totalReadingsCount;
|
|
FloatBox result;
|
|
|
|
/// <summary>
|
|
/// Events: BalanceDone, Error
|
|
/// </summary>
|
|
/// <param name="balanceDev">Balance device instance</param>
|
|
/// <param name="result">Reference to the measured mass in kg</param>
|
|
/// <param name="readingsCount">Required mass readings count (>= 3)</param>
|
|
public ReadStableMassOp(BalanceDev balanceDev, ref FloatBox result, int readingsCount)
|
|
{
|
|
if (balanceDev == null) throw new ArgumentNullException("balanceDev");
|
|
this.balanceDev = balanceDev;
|
|
|
|
if (readingsCount < 3) throw new ArgumentOutOfRangeException("requiredReadingsCount");
|
|
this.totalReadingsCount = readingsCount;
|
|
|
|
this.result = result;
|
|
|
|
log.Debug(this.ToString());
|
|
}
|
|
|
|
/// <summary>Start this operation</summary>
|
|
public void Start()
|
|
{
|
|
currentReadingsCount = 0;
|
|
massReadings = new float[totalReadingsCount];
|
|
balanceDev.GetStableMassMeasurement();
|
|
}
|
|
|
|
/// <summary>Run this operation</summary>
|
|
/// <returns>
|
|
/// Event.None
|
|
/// Event.BalanceDone
|
|
/// Event.Error . . . . . Current.Tick == null
|
|
/// </returns>
|
|
public Event Run()
|
|
{
|
|
if (currentReadingsCount >= totalReadingsCount) return Event.BalanceDone; /// Mass has already been calculated
|
|
|
|
if (balanceDev.MsrmntState == MsrmntState.Busy) return Event.None;
|
|
|
|
if (balanceDev.MsrmntState == MsrmntState.Valid)
|
|
{
|
|
massReadings[currentReadingsCount++] = balanceDev.Mass;
|
|
|
|
if (currentReadingsCount == totalReadingsCount)
|
|
{
|
|
/// calculate the average
|
|
float calcMass = 0;
|
|
Array.Sort(massReadings);
|
|
for (int i = 1; i < totalReadingsCount - 1; i++) calcMass += massReadings[i];
|
|
calcMass = (calcMass / (totalReadingsCount - 2));
|
|
|
|
log.InfoFormat("ReadStableMassOp.Run() ... valid mass={0} ... returning Event.BalanceDone", calcMass);
|
|
|
|
result.Val = calcMass;
|
|
return Event.BalanceDone; /// Mass has just been calculated
|
|
}
|
|
}
|
|
|
|
balanceDev.GetStableMassMeasurement();
|
|
return Event.None;
|
|
}
|
|
|
|
/// <summary>Stop this operation</summary>
|
|
public void Stop()
|
|
{
|
|
}
|
|
}
|
|
}
|