Stable mass measurement rewritten, it can use immediate measurement, DB change, ver.2.5.265.
This commit is contained in:
@@ -23,6 +23,8 @@ namespace TBF.BenchControl.MettlerToledo.Standard
|
||||
/// Set by the constructor
|
||||
BalanceDev balanceDev;
|
||||
int totalReadingsCount;
|
||||
float maxSpread; /// Maximum spread of measurements in kg (otherwise the measurement continues)
|
||||
bool method; /// false = slow (precise), true = fast (immediate)
|
||||
FloatBox result;
|
||||
|
||||
/// <summary>
|
||||
@@ -31,13 +33,26 @@ namespace TBF.BenchControl.MettlerToledo.Standard
|
||||
/// <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)
|
||||
/// <param name="maxSpread">Maximum spread of measurements in kg (otherwise the measurement continues)</param>
|
||||
/// <param name="method">Method: false = slow (precise), true = fast (immediate)</param>
|
||||
public ReadStableMassOp(BalanceDev balanceDev, ref FloatBox result, int readingsCount, float maxSpread, bool method)
|
||||
{
|
||||
if (balanceDev == null) throw new ArgumentNullException("balanceDev");
|
||||
this.balanceDev = balanceDev;
|
||||
|
||||
if (readingsCount < 3) throw new ArgumentOutOfRangeException("requiredReadingsCount");
|
||||
this.totalReadingsCount = readingsCount;
|
||||
if (readingsCount == 0)
|
||||
this.totalReadingsCount = 5;
|
||||
else if (readingsCount < 3)
|
||||
this.totalReadingsCount = 3;
|
||||
else
|
||||
this.totalReadingsCount = readingsCount;
|
||||
|
||||
if (maxSpread == 0)
|
||||
this.maxSpread = balanceDev.Capacity / 20000;
|
||||
else
|
||||
this.maxSpread = maxSpread;
|
||||
|
||||
this.method = method;
|
||||
|
||||
this.result = result;
|
||||
|
||||
@@ -49,7 +64,11 @@ namespace TBF.BenchControl.MettlerToledo.Standard
|
||||
{
|
||||
currentReadingsCount = 0;
|
||||
massReadings = new float[totalReadingsCount];
|
||||
balanceDev.GetStableMassMeasurement();
|
||||
|
||||
if (method)
|
||||
balanceDev.GetImmediateMassMeasurement();
|
||||
else
|
||||
balanceDev.GetStableMassMeasurement();
|
||||
}
|
||||
|
||||
/// <summary>Run this operation</summary>
|
||||
@@ -70,21 +89,58 @@ namespace TBF.BenchControl.MettlerToledo.Standard
|
||||
|
||||
if (currentReadingsCount == totalReadingsCount)
|
||||
{
|
||||
/// calculate the average
|
||||
float calcMass = 0;
|
||||
#if OLD_WAY_OF_MASS_CALCULATION
|
||||
/// Exclude min. and max. value, calculate average
|
||||
Array.Sort(massReadings);
|
||||
for (int i = 1; i < totalReadingsCount - 1; i++) calcMass += massReadings[i];
|
||||
float calcMass = 0;
|
||||
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
|
||||
}
|
||||
#else
|
||||
float minimum = float.MaxValue;
|
||||
float maximum = float.MinValue;
|
||||
foreach (float val in massReadings)
|
||||
{
|
||||
if (val < minimum) minimum = val;
|
||||
if (val > maximum) maximum = val;
|
||||
}
|
||||
|
||||
if ((maximum - minimum) <= maxSpread)
|
||||
{
|
||||
float calcMass = 0;
|
||||
for (int i = 0; i < totalReadingsCount; i++)
|
||||
{
|
||||
calcMass += massReadings[i];
|
||||
}
|
||||
calcMass = (calcMass / totalReadingsCount);
|
||||
log.InfoFormat("ReadStableMassOp.Run() ... valid mass={0} ... returning Event.BalanceDone", calcMass);
|
||||
result.Val = calcMass;
|
||||
return Event.BalanceDone; /// Mass has just been calculated
|
||||
}
|
||||
else
|
||||
{
|
||||
/// Shift data and decrement 'currentReadingsCount'
|
||||
for (int i = 0; i < totalReadingsCount - 1; i++)
|
||||
{
|
||||
massReadings[i] = massReadings[i + 1];
|
||||
}
|
||||
currentReadingsCount--;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
balanceDev.GetStableMassMeasurement();
|
||||
return Event.None;
|
||||
if (method)
|
||||
balanceDev.GetImmediateMassMeasurement();
|
||||
else
|
||||
balanceDev.GetStableMassMeasurement();
|
||||
|
||||
return Event.None;
|
||||
}
|
||||
|
||||
/// <summary>Stop this operation</summary>
|
||||
|
||||
Reference in New Issue
Block a user