throw exception if min is bigger than max in Clamp

This commit is contained in:
atef 2023-06-20 15:27:53 +02:00
parent 3e2e3e7190
commit 789780791f
2 changed files with 11 additions and 3 deletions

View File

@ -63,7 +63,7 @@ public static class Controller
return control; // current loop cannot happen return control; // current loop cannot happen
var nominalPower = acDcs.Average(d => d.Status.Nominal.Power); var nominalPower = acDcs.Average(d => d.Status.Nominal.Power);
var maxStep = nominalPower / 25; var maxStep = nominalPower / 25; //TODO magic number to config
var clampedPowerDelta = powerDelta.Clamp(-maxStep, maxStep); var clampedPowerDelta = powerDelta.Clamp(-maxStep, maxStep);
@ -80,8 +80,10 @@ public static class Controller
if (powerDifference < maxStep) if (powerDifference < maxStep)
return control with { PowerCorrection = clampedPowerDelta }; return control with { PowerCorrection = clampedPowerDelta };
var correction = powerDifference / 4; var correction = powerDifference / 4; //TODO magic number to config
// find out if we reach the lower or upper Dc limit by comparing the current Dc voltage to the reference voltage
return s.AcDc.Dc.Voltage > s.Config.ReferenceDcBusVoltage return s.AcDc.Dc.Voltage > s.Config.ReferenceDcBusVoltage
? control with { PowerCorrection = clampedPowerDelta.Clamp(-maxStep, -correction), LimitedBy = EssLimit.ChargeLimitedByMaxDcBusVoltage } ? control with { PowerCorrection = clampedPowerDelta.Clamp(-maxStep, -correction), LimitedBy = EssLimit.ChargeLimitedByMaxDcBusVoltage }
: control with { PowerCorrection = clampedPowerDelta.Clamp(correction, maxStep), LimitedBy = EssLimit.DischargeLimitedByMinDcBusVoltage }; : control with { PowerCorrection = clampedPowerDelta.Clamp(correction, maxStep), LimitedBy = EssLimit.DischargeLimitedByMinDcBusVoltage };

View File

@ -104,6 +104,9 @@ public static class Utils
public static Int32 Clamp(this Int32 value, Int32 minValue, Int32 maxValue) public static Int32 Clamp(this Int32 value, Int32 minValue, Int32 maxValue)
{ {
if (minValue > maxValue)
throw new ArgumentException();
var clamped = Math.Min(maxValue, value); var clamped = Math.Min(maxValue, value);
clamped = Math.Max(minValue, clamped); clamped = Math.Max(minValue, clamped);
@ -112,6 +115,9 @@ public static class Utils
public static Double Clamp(this Double value, Double minValue, Double maxValue) public static Double Clamp(this Double value, Double minValue, Double maxValue)
{ {
if (minValue > maxValue)
throw new ArgumentException();
var clamped = Math.Min(maxValue, value); var clamped = Math.Min(maxValue, value);
clamped = Math.Max(minValue, clamped); clamped = Math.Max(minValue, clamped);