From 7635a8175e0923237f80b77d5b5d280f8590fb8f Mon Sep 17 00:00:00 2001 From: atef Date: Thu, 20 Apr 2023 13:19:33 +0200 Subject: [PATCH] Add EocReached and Total Current and correct naming --- .../src/Controller/AvgBatteriesStatus.cs | 5 +- csharp/App/SaliMax/src/Controller/Control.cs | 8 +-- .../App/SaliMax/src/Controller/Controller.cs | 64 +++++++++++-------- .../SaliMax/src/Controller/StatusRecord.cs | 2 +- 4 files changed, 46 insertions(+), 33 deletions(-) diff --git a/csharp/App/SaliMax/src/Controller/AvgBatteriesStatus.cs b/csharp/App/SaliMax/src/Controller/AvgBatteriesStatus.cs index 745b18187..151c0c5af 100644 --- a/csharp/App/SaliMax/src/Controller/AvgBatteriesStatus.cs +++ b/csharp/App/SaliMax/src/Controller/AvgBatteriesStatus.cs @@ -41,7 +41,10 @@ public static class AvgBatteriesStatus TemperatureState = stati.Any(b => b.TemperatureState == OperatingTemperature) // TODO: revisit when we have the overheated state ? OperatingTemperature : Cold, - + + TotalCurrent = stati.Average(b => b.TotalCurrent), + + EocReached = stati.All(b => b.EocReached), }; return new CombinedStatus diff --git a/csharp/App/SaliMax/src/Controller/Control.cs b/csharp/App/SaliMax/src/Controller/Control.cs index 08589edb5..7564a5f81 100644 --- a/csharp/App/SaliMax/src/Controller/Control.cs +++ b/csharp/App/SaliMax/src/Controller/Control.cs @@ -5,7 +5,7 @@ public static class Control public static Decimal ControlGridPower(this StatusRecord status, Decimal targetPower) { - return ControlPower(status.GridMeterStatus!.ActivePowerL123, targetPower, status.SalimaxConfig!.PConstant); + return ControlPower(status.GridMeterStatus!.Ac.ActivePower, targetPower, status.SalimaxConfig!.PConstant); } public static Decimal ControlInverterPower(this StatusRecord status, Decimal targetInverterPower) @@ -18,7 +18,7 @@ public static class Control public static Decimal ControlBatteryPower(this StatusRecord status, Decimal targetBatteryPower, UInt16 i = 0) //this will use the avg batteries { - return ControlPower(status.BatteriesStatus![i].Dc.Power, targetBatteryPower, status.SalimaxConfig!.PConstant); + return ControlPower(status.BatteriesStatus!.Combined.Dc.Power, targetBatteryPower, status.SalimaxConfig!.PConstant); } public static Decimal ControlLowBatterySoc(this StatusRecord status) @@ -29,14 +29,14 @@ public static class Control public static Decimal LowerLimit(params Decimal[] deltas) => deltas.Max(); public static Decimal UpperLimit(params Decimal[] deltas) => deltas.Min(); - private static Decimal HoldMinSocCurve(StatusRecord s, UInt16 i = 0) + private static Decimal HoldMinSocCurve(StatusRecord s) { // TODO: explain LowSOC curve var a = -2 * s.SalimaxConfig!.SelfDischargePower / s.SalimaxConfig.HoldSocZone; var b = -a * (s.SalimaxConfig.MinSoc + s.SalimaxConfig.HoldSocZone); - return s.BatteriesStatus![i].Soc * a + b; //this will use the avg batteries + return s.BatteriesStatus!.Combined.Soc * a + b; //this will use the avg batteries } private static Decimal ControlPower(Decimal measurement, Decimal target, Decimal p) diff --git a/csharp/App/SaliMax/src/Controller/Controller.cs b/csharp/App/SaliMax/src/Controller/Controller.cs index cadb47477..5638da544 100644 --- a/csharp/App/SaliMax/src/Controller/Controller.cs +++ b/csharp/App/SaliMax/src/Controller/Controller.cs @@ -5,6 +5,9 @@ using InnovEnergy.Lib.Devices.Trumpf.TruConvertAc; using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc; using InnovEnergy.Lib.Time.Unix; using InnovEnergy.Lib.Utils; + +using InnovEnergy.Lib.Devices.Trumpf.TruConvertAc.Enums; + using static InnovEnergy.App.SaliMax.SaliMaxRelays.RelayState; @@ -68,69 +71,69 @@ public static class Controller //Grid-Tied 400V/50 Hz { SaliMaxRelayStatus.K1: Open, SaliMaxRelayStatus.K2: Open, SaliMaxRelayStatus.K3: Open, - InverterStatus.AcDcActiveGridType: AcDcGridType.GridTied400V50Hz + InverterStatus.GridType: AcDcGridType.GridTied400V50Hz } => 9, { SaliMaxRelayStatus.K1: Closed, SaliMaxRelayStatus.K2: Open, SaliMaxRelayStatus.K3: Open, - InverterStatus.AcDcActiveGridType: AcDcGridType.GridTied400V50Hz + InverterStatus.GridType: AcDcGridType.GridTied400V50Hz } => 10, { SaliMaxRelayStatus.K1: Open, SaliMaxRelayStatus.K2: Closed, SaliMaxRelayStatus.K3: Open, - InverterStatus.AcDcActiveGridType: AcDcGridType.GridTied400V50Hz + InverterStatus.GridType: AcDcGridType.GridTied400V50Hz } => 11, { SaliMaxRelayStatus.K1: Closed, SaliMaxRelayStatus.K2: Closed, SaliMaxRelayStatus.K3: Open, - InverterStatus.AcDcActiveGridType: AcDcGridType.GridTied400V50Hz + InverterStatus.GridType: AcDcGridType.GridTied400V50Hz } => 12, { SaliMaxRelayStatus.K1: Open, SaliMaxRelayStatus.K2: Open, SaliMaxRelayStatus.K3: Closed, - InverterStatus.AcDcActiveGridType: AcDcGridType.GridTied400V50Hz + InverterStatus.GridType: AcDcGridType.GridTied400V50Hz } => 13, { SaliMaxRelayStatus.K1: Closed, SaliMaxRelayStatus.K2: Open, SaliMaxRelayStatus.K3: Closed, - InverterStatus.AcDcActiveGridType: AcDcGridType.GridTied400V50Hz + InverterStatus.GridType: AcDcGridType.GridTied400V50Hz } => 14, { SaliMaxRelayStatus.K1: Open, SaliMaxRelayStatus.K2: Closed, SaliMaxRelayStatus.K3: Closed, - InverterStatus.AcDcActiveGridType: AcDcGridType.GridTied400V50Hz + InverterStatus.GridType: AcDcGridType.GridTied400V50Hz } => 15, { SaliMaxRelayStatus.K1: Closed, SaliMaxRelayStatus.K2: Closed, SaliMaxRelayStatus.K3: Closed, - InverterStatus.AcDcActiveGridType: AcDcGridType.GridTied400V50Hz + InverterStatus.GridType: AcDcGridType.GridTied400V50Hz } => 16, //Island 400V / 50Hz { SaliMaxRelayStatus.K1: Open, SaliMaxRelayStatus.K2: Open, SaliMaxRelayStatus.K3: Open, - InverterStatus.AcDcActiveGridType: AcDcGridType.Island400V50Hz + InverterStatus.GridType: AcDcGridType.Island400V50Hz } => 17, { SaliMaxRelayStatus.K1: Closed, SaliMaxRelayStatus.K2: Open, SaliMaxRelayStatus.K3: Open, - InverterStatus.AcDcActiveGridType: AcDcGridType.Island400V50Hz + InverterStatus.GridType: AcDcGridType.Island400V50Hz } => 18, { SaliMaxRelayStatus.K1: Open, SaliMaxRelayStatus.K2: Closed, SaliMaxRelayStatus.K3: Open, - InverterStatus.AcDcActiveGridType: AcDcGridType.Island400V50Hz + InverterStatus.GridType: AcDcGridType.Island400V50Hz } => 19, { SaliMaxRelayStatus.K1: Closed, SaliMaxRelayStatus.K2: Closed, SaliMaxRelayStatus.K3: Open, - InverterStatus.AcDcActiveGridType: AcDcGridType.Island400V50Hz + InverterStatus.GridType: AcDcGridType.Island400V50Hz } => 20, { SaliMaxRelayStatus.K1: Open, SaliMaxRelayStatus.K2: Open, SaliMaxRelayStatus.K3: Closed, //this is wrong - InverterStatus.AcDcActiveGridType: AcDcGridType.Island400V50Hz + InverterStatus.GridType: AcDcGridType.Island400V50Hz } => 21, { SaliMaxRelayStatus.K1: Closed, SaliMaxRelayStatus.K2: Open, SaliMaxRelayStatus.K3: Closed, - InverterStatus.AcDcActiveGridType: AcDcGridType.Island400V50Hz + InverterStatus.GridType: AcDcGridType.Island400V50Hz } => 22, { SaliMaxRelayStatus.K1: Open, SaliMaxRelayStatus.K2: Closed, SaliMaxRelayStatus.K3: Closed, - InverterStatus.AcDcActiveGridType: AcDcGridType.Island400V50Hz + InverterStatus.GridType: AcDcGridType.Island400V50Hz } => 23, { SaliMaxRelayStatus.K1: Closed, SaliMaxRelayStatus.K2: Closed, SaliMaxRelayStatus.K3: Closed, - InverterStatus.AcDcActiveGridType: AcDcGridType.Island400V50Hz + InverterStatus.GridType: AcDcGridType.Island400V50Hz } => 24, @@ -149,7 +152,7 @@ public static class Controller var lastEocTime = GetLastEocTime(statusRecord); var timeSinceLastEoc = UnixTime.Now - lastEocTime; - _numberOfInverters = statusRecord.InverterStatus!.NumberOfConnectedSlaves; + _numberOfInverters = (UInt16)statusRecord.InverterStatus!.NumberOfConnectedSlaves ; _mustChargeFlag = timeSinceLastEoc > MaxTimeWithoutEoc; var noGridMeter = statusRecord.GridMeterStatus == null; @@ -263,7 +266,7 @@ public static class Controller goal = "Calibration Charge"; delta = statusRecord.ControlInverterPower(statusRecord.SalimaxConfig.MaxInverterPower); } - else if (statusRecord.AvgBatteriesStatus!.Soc < statusRecord.SalimaxConfig.MinSoc) // TODO + else if (statusRecord.BatteriesStatus!.Combined.Soc < statusRecord.SalimaxConfig.MinSoc) // TODO { goal = $"reach min SOC (Min soc: {statusRecord.SalimaxConfig.MinSoc})"; delta = statusRecord.ControlInverterPower(statusRecord.SalimaxConfig @@ -285,7 +288,7 @@ public static class Controller delta = inverterAc2DcLimitPower; } - var batteryChargingLimitPower = statusRecord.ControlBatteryPower(statusRecord.BatteriesStatus[0]!.MaxChargingPower); + var batteryChargingLimitPower = statusRecord.ControlBatteryPower(statusRecord.BatteriesStatus!.Combined.MaxChargingPower); if (delta > batteryChargingLimitPower) { @@ -304,12 +307,12 @@ public static class Controller } var batteryDischargingLimitPower = - statusRecord.ControlBatteryPower(statusRecord.BatteriesStatus[0]!.MaxDischargingPower); // TODO change to avg battery + statusRecord.ControlBatteryPower(statusRecord.BatteriesStatus.Combined.MaxDischargingPower); // TODO change to avg battery if (delta < batteryDischargingLimitPower) { limitReason = - $"limited by max battery discharging power: {statusRecord.BatteriesStatus[0]!.MaxDischargingPower}";// TODO change to avg battery + $"limited by max battery discharging power: {statusRecord.BatteriesStatus.Combined.MaxDischargingPower}";// TODO change to avg battery delta = batteryDischargingLimitPower; } @@ -448,14 +451,21 @@ public static class Controller } private static UnixTime GetLastEocTime(StatusRecord statusRecord) - { - if (statusRecord.BatteriesStatus[0]!.Soc >= 100 && statusRecord.BatteriesStatus[1]!.Soc >= 100 ) + { if (statusRecord.BatteriesStatus != null) { - Console.WriteLine("battery has reached EOC"); - File.AppendAllTextAsync(Config.LogSalimaxLog, String.Join(Environment.NewLine, UnixTime.Now + "battery has reached EOC")); - return UnixTime.Now; + if (statusRecord.BatteriesStatus!.Combined.EocReached) + { + Console.WriteLine("battery has reached EOC"); + File.AppendAllTextAsync(Config.LogSalimaxLog, + String.Join(Environment.NewLine, + UnixTime.Now + "battery has reached EOC")); + return UnixTime.Now; + } + } + else + { + Console.WriteLine("No battery Detected"); } - return statusRecord.SalimaxConfig.LastEoc; } diff --git a/csharp/App/SaliMax/src/Controller/StatusRecord.cs b/csharp/App/SaliMax/src/Controller/StatusRecord.cs index 9071f5976..feebca76a 100644 --- a/csharp/App/SaliMax/src/Controller/StatusRecord.cs +++ b/csharp/App/SaliMax/src/Controller/StatusRecord.cs @@ -17,7 +17,7 @@ public record StatusRecord public EmuMeterStatus? GridMeterStatus { get; init; } public SaliMaxRelayStatus? SaliMaxRelayStatus { get; init; } - public AmptStatus? AmptStatus { get; init; } + public AmptCommunicationUnitStatus? AmptStatus { get; init; } public EmuMeterStatus? AcInToAcOutMeterStatus { get; init; } public SalimaxConfig SalimaxConfig { get; init; } = null!; } \ No newline at end of file