From 8feee5db4359bd1ccbed3f538703c1a8dd753b24 Mon Sep 17 00:00:00 2001 From: atef Date: Tue, 20 Jun 2023 15:34:04 +0200 Subject: [PATCH] Introduce ClampMin and ClampMax --- csharp/App/SaliMax/src/Ess/Controller.cs | 4 ++-- csharp/Lib/Utils/Utils.cs | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/csharp/App/SaliMax/src/Ess/Controller.cs b/csharp/App/SaliMax/src/Ess/Controller.cs index 1e7599f88..55007e035 100644 --- a/csharp/App/SaliMax/src/Ess/Controller.cs +++ b/csharp/App/SaliMax/src/Ess/Controller.cs @@ -85,8 +85,8 @@ public static class Controller // 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 }; + ? control with { PowerCorrection = clampedPowerDelta.ClampMax(-correction), LimitedBy = EssLimit.ChargeLimitedByMaxDcBusVoltage } + : control with { PowerCorrection = clampedPowerDelta.ClampMin(correction), LimitedBy = EssLimit.DischargeLimitedByMinDcBusVoltage }; } // private static Double AdjustMaxChargePower(StatusRecord s, Double powerDelta) diff --git a/csharp/Lib/Utils/Utils.cs b/csharp/Lib/Utils/Utils.cs index 50dddeab3..5fa0c4a12 100644 --- a/csharp/Lib/Utils/Utils.cs +++ b/csharp/Lib/Utils/Utils.cs @@ -124,7 +124,16 @@ public static class Utils return clamped; } + public static Double ClampMax(this Double value, Double maxValue) + { + return Math.Min(maxValue, value); + } + public static Double ClampMin(this Double value, Double minValue) + { + return Math.Max(minValue, value); + } + public static Decimal Clamp(this Decimal value, Decimal minValue, Decimal maxValue) { var clamped = Math.Min(maxValue, value);