diff --git a/csharp/App/SaliMax/src/Ess/Controller.cs b/csharp/App/SaliMax/src/Ess/Controller.cs index 89a5071ce..1e7599f88 100644 --- a/csharp/App/SaliMax/src/Ess/Controller.cs +++ b/csharp/App/SaliMax/src/Ess/Controller.cs @@ -63,7 +63,7 @@ public static class Controller return control; // current loop cannot happen 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); @@ -80,8 +80,10 @@ public static class Controller if (powerDifference < maxStep) 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 ? control with { PowerCorrection = clampedPowerDelta.Clamp(-maxStep, -correction), LimitedBy = EssLimit.ChargeLimitedByMaxDcBusVoltage } : control with { PowerCorrection = clampedPowerDelta.Clamp(correction, maxStep), LimitedBy = EssLimit.DischargeLimitedByMinDcBusVoltage }; diff --git a/csharp/Lib/Utils/Utils.cs b/csharp/Lib/Utils/Utils.cs index c98953484..50dddeab3 100644 --- a/csharp/Lib/Utils/Utils.cs +++ b/csharp/Lib/Utils/Utils.cs @@ -104,6 +104,9 @@ public static class Utils public static Int32 Clamp(this Int32 value, Int32 minValue, Int32 maxValue) { + if (minValue > maxValue) + throw new ArgumentException(); + var clamped = Math.Min(maxValue, value); clamped = Math.Max(minValue, clamped); @@ -112,9 +115,12 @@ public static class Utils public static Double Clamp(this Double value, Double minValue, Double maxValue) { + if (minValue > maxValue) + throw new ArgumentException(); + var clamped = Math.Min(maxValue, value); clamped = Math.Max(minValue, clamped); - + return clamped; }