Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 23 additions & 30 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,51 +28,43 @@ jobs:
retention-days: 7
path: "ResultsAndTiming/*.chm"
Build:
runs-on:
group: OpenTAP-SpokeVPC
labels: [Linux, X64]
container: ghcr.io/opentap/oci-images/build-dotnet:latest
runs-on: windows-latest
env:
KS8500_USER_TOKEN: ${{ secrets.KS8500_USER_TOKEN }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

fetch-depth: 0
- name: Fix tags
if: startsWith(github.ref, 'refs/tags/v')
run: git fetch -f origin ${{ github.ref }}:${{ github.ref }} # Fixes an issue with actions/checkout@v4. See https://github.com/actions/checkout/issues/290

- name: Setup OpenTAP
uses: opentap/setup-opentap@main

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.x


- name: Build
run: dotnet build -c Release

- name: Create Package
run: |
dotnet restore
dotnet publish -c Release
mv bin/Release/*.TapPackage .

cd bin/Release
./tap package create ../../package.xml -v

- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: tap_package
retention-days: 7
path: "*.TapPackage"
path: "bin/Release/*.TapPackage"

Package-Test:
runs-on:
group: OpenTAP-SpokeVPC
labels: [Linux, X64]
container: ghcr.io/opentap/oci-images/build-dotnet:latest
runs-on: ubuntu-latest
needs:
- Build
steps:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x
- name: Setup OpenTAP
uses: opentap/setup-opentap@main

Expand All @@ -85,17 +77,18 @@ jobs:
- name: Package
run: |
cp Demonstration.*.TapPackage DemonstrationTest.TapPackage
tap package install DemonstrationTest.TapPackage --force
tap package install DemonstrationTest.TapPackage
tap package test Demonstration -v

TestPlan-Test:
runs-on:
group: OpenTAP-SpokeVPC
labels: [Linux, X64]
container: ghcr.io/opentap/oci-images/build-dotnet:latest
runs-on: ubuntu-latest
needs:
- Build
steps:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x
- name: Setup OpenTAP
uses: opentap/setup-opentap@main

Expand All @@ -108,7 +101,7 @@ jobs:
- name: Package
run: |
cp Demonstration.*.TapPackage DemonstrationTest.TapPackage
tap package install DemonstrationTest.TapPackage --force
tap package install DemonstrationTest.TapPackage
cd /opt/tap
tap run Packages/Demonstration/DataGenForResultsViewer.TapPlan
tap run Packages/Demonstration/DataGenForTimingAnalysis.TapPlan --settings "../../Packages/Demonstration/Tests/Test Bench Profile"
Expand Down
2 changes: 1 addition & 1 deletion .gitversion
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# This is the version number that will be used. Prerelease numbers are calculated by
# counting git commits since the last change in this value.
version = 9.0.7
version = 9.1.0

# A version is determined to be a "beta" prerelease if it originates from the default branch
# The default branch is the first branch that matches the following regular expession.
Expand Down
42 changes: 42 additions & 0 deletions Battery/BatteryDut.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace OpenTap.Plugins.Demo.Battery
{
[Display("Battery", "This DUT represents the battery itself.")]
public class BatteryDut : Dut
{
#region Settings

[Display("Capacity", "A larger cell size will result in faster charging and discharging.")]
[Unit("Ah")]
public double Capacity { get; set; } = 0.3;

[Display("Base Voltage", "The battery voltage when discharged.")]
[Unit("V")]
public double BaseVoltage { get; set; } = 3.0;

[Display("Charged Voltage", "The battery voltage when charged.")]
[Unit("V")]
public double ChargedVoltage { get; set; } = 4.2;

[Display("Initial Charge")]
[Unit("Ah")]
public double InitialCharge { get; set; } = 0.01;

#endregion

internal BatteryModel Model { get; private set; }
public BatteryDut()
{
Name = "Bat";
Rules.Add(() => Capacity >= 0, "Capacity must be greater than 0", nameof(Capacity));
Rules.Add(() => BaseVoltage >= 0, "Base Voltage must be greater than 0", nameof(BaseVoltage));
Rules.Add(() => ChargedVoltage >= BaseVoltage, "Charted Voltage must be greater than 0", nameof(ChargedVoltage));
Model = new BatteryModel();
}

public override void Open()
{
Model = new BatteryModel(initialCharge_Ah: InitialCharge, capacity_Ah: Capacity, chargedVoltage: ChargedVoltage, baseVoltage: BaseVoltage);
base.Open();
}
}
}
109 changes: 109 additions & 0 deletions Battery/BatteryModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;

namespace OpenTap.Plugins.Demo.Battery
{
/// <summary>
/// This is a relatively inaccurate physical model of a Lithium-ion-like battery.
/// </summary>
class BatteryModel
{
// --- Physical constants ---
private const double Rgas = 8.314; // J/mol·K
private const double Tref = 298.15; // 25°C in Kelvin

// --- Simulation
private double baseVoltage = 3.0;
private double chargedVoltage = 4.2;

// --- Nominal parameters ---
private readonly double nominalCapacity; // Ah
private readonly double R25; // Internal resistance (Ohm) at 25°C
private readonly double beta; // Temperature coefficient for resistance
private readonly double Ea; // Activation energy (J/mol)
private readonly double selfDischarge25; // Self-discharge rate at 25°C (fraction/min)

// --- State variables ---
public double Charge_Ah { get; private set; } // Current stored charge
public double Voltage_V { get; private set; } // Terminal voltage
public double Current_A { get; private set; } // Charging (+) or discharging (-)
public double Voc { get; private set; } // Voltage open-circuit
public double SOC { get; private set; } // State of Charge (0–1)
public double CoulombicEfficiency { get; private set; }

public BatteryModel(
double capacity_Ah = 3.0,
double initialCharge_Ah = 1.5,
double internalResistance25 = 0.05,
double temperatureCoeff = 0.04,
double activationEnergy = 35000.0,
double selfDischargeRate25 = 0.0001,
double baseVoltage = 3.0,
double chargedVoltage = 4.2)
{
nominalCapacity = capacity_Ah;
Charge_Ah = initialCharge_Ah;
R25 = internalResistance25;
beta = temperatureCoeff;
Ea = activationEnergy;
selfDischarge25 = selfDischargeRate25;
this.baseVoltage = baseVoltage;
this.chargedVoltage = chargedVoltage;

double capacity = nominalCapacity * 1;
SOC = Charge_Ah / capacity;
SOC = Math.Min(Math.Max(SOC, 0.0), 1.0);
Voc = baseVoltage + (chargedVoltage - baseVoltage) * SOC + 0.05 * Math.Sin(5 * SOC);
}

/// <summary>
/// Update the battery state given an applied terminal voltage, temperature, and timestep.
/// </summary>
/// <param name="appliedVoltage">Applied terminal voltage (V)</param>
/// <param name="dt_min">Timestep (minutes)</param>
/// <param name="temperature_C">Cell temperature (°C)</param>
/// <param name="current_limit">Current limited by the generator. </param>
public void Update(double appliedVoltage, double dt_min, double temperature_C, double current_limit)
{
double T_K = temperature_C + 273.15;

// external resistance
double R_external = 0.02;

// --- Temperature-dependent parameters ---
double R_internal = R25 * Math.Exp(beta * (25 - temperature_C));
double capacity = nominalCapacity * (1 - 0.002 * Math.Abs(temperature_C - 25));
double k_self = selfDischarge25 * Math.Exp(0.05 * (temperature_C - 25));

// --- Compute open-circuit voltage based on SOC ---
SOC = Charge_Ah / capacity;
SOC = Math.Min(Math.Max(SOC, 0.0), 1.0);
Voc = baseVoltage + (chargedVoltage - baseVoltage) * SOC + 0.05 * Math.Sin(5 * SOC);

// --- Solve current from voltage equation ---
// I is clamped by +/- current_limit.
// appliedVoltage = Voc - I * R_internal
Current_A = Math.Max(Math.Min((Voc- appliedVoltage) / (R_internal + R_external), current_limit), -current_limit);

// --- Temperature-dependent efficiency (Arrhenius relation) ---
CoulombicEfficiency = Math.Exp(-Ea / Rgas * (1.0 / T_K - 1.0 / Tref));
CoulombicEfficiency = Math.Min(Math.Max(CoulombicEfficiency, 0.7), 1.0);

// --- Effective current (charging losses) ---
double effectiveCurrent = Current_A;
if (Current_A > 0) // Charging
effectiveCurrent *= CoulombicEfficiency;

// --- Update charge (Ah) ---
Charge_Ah += -effectiveCurrent * dt_min / 60.0;

// --- Apply self-discharge ---
Charge_Ah -= Charge_Ah * k_self * dt_min;

// --- Clamp charge ---
Charge_Ah = Math.Min(Math.Max(Charge_Ah, 0.0), capacity);

// --- Update terminal voltage ---
Voltage_V = appliedVoltage;
}
}
}
38 changes: 27 additions & 11 deletions Battery/ChargeStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
// you find useful, provided that you agree that Keysight Technologies has no
// warranty, obligations or liability for any sample application files.
using System;
using OpenTap;

using System.Diagnostics; // Use Platform infrastructure/core components (log,TestStep definition, etc)

namespace OpenTap.Plugins.Demo.Battery
Expand All @@ -21,40 +19,46 @@ public class ChargeStep : SamplingStepBase
[Unit("V")]
public double Voltage { get; set; }

[Display("Target Voltage Margin", Group: "Cell", Order: -1)]
[Display("Target Voltage", Group: "Power Supply", Order: -1)]
[Unit("V")]
public double TargetCellVoltageMargin { get; set; }
public double TargetVoltage { get; set; }

[Display("Charge Time", Group: "Output", Order: 0)]
[Unit("s")]
[Output]
public double ChargeTime { get; private set; }


#endregion

public ChargeStep()
{
Voltage = 4.2;
Current = 10;
TargetCellVoltageMargin = 0.1;
Rules.Add(() => (Voltage >= 0) && (Voltage <= 10), "Voltage must be >= 0 and <= 10", "Voltage");
Rules.Add(() => (Current >= 0) && (Current <= 20), "Current must be >= 0 and <= 20", "Current");
Rules.Add(() => (TargetCellVoltageMargin >= 0) && (TargetCellVoltageMargin <= 1), "TargetCellVoltageMargin must be >= 0 and <= 1", "TargetCellVoltageMargin");
TargetVoltage = 4.1;
Rules.Add(() => (Voltage >= 0) && (Voltage <= 10), "Voltage must be >= 0 and <= 10", nameof(Voltage));
Rules.Add(() => (Current >= 0) && (Current <= 20), "Current must be >= 0 and <= 20", nameof(Current));
Rules.Add(() => TargetVoltage < Voltage, "Target voltage must be less than the voltage", nameof(TargetVoltage));

}

public double accumulatedCharge;
public override void Run()
{
accumulatedCharge = 0;
var sw = Stopwatch.StartNew();
PowerAnalyzer.Setup(Voltage, Current);
PowerAnalyzer.EnableOutput();
Log.Info("Charging at: " + Current + "A" + " Target Voltage: " + Voltage + "V");
base.Run(); // Most of the work is being done here, with callbacks to this class.
PowerAnalyzer.DisableOutput();
ChargeTime = sw.Elapsed.TotalSeconds;
UpgradeVerdict(Verdict.Pass);
}

protected override void WhileSampling()
{
while(Math.Abs(PowerAnalyzer.MeasureVoltage() - Voltage) > TargetCellVoltageMargin)
while(Dut.Model.Voc < TargetVoltage)
{
TapThread.Sleep(50);
}
Expand All @@ -66,9 +70,21 @@ public class ChargeResult
[Display("Sample Number")]
public int SampleNo { get; set; }
[Display("Voltage")]

[Unit("V")]
public double Voltage { get; set; }

[Display("Current")]
[Unit("A")]
public double Current { get; set; }

[Display("Power")]
[Unit("W")]
public double Power { get; set; }

[Display("Acc. Charge")]
[Unit("J")]
public double AccumulatedCharge { get; set; }
}

protected override void OnSample(double voltage, double current, int sampleNo)
Expand All @@ -78,8 +94,8 @@ protected override void OnSample(double voltage, double current, int sampleNo)
barVoltage.LowerLimit = 2; //Cell voltage defined in PowerAnalyzer
barVoltage.UpperLimit = 4.7;
Log.Info("Voltage: " + barVoltage.GetBar(voltage));

Results.Publish(new ChargeResult { SampleNo = sampleNo, Voltage = Math.Truncate(voltage * 100) / 100, Current = Math.Truncate(current * 100) / 100});
accumulatedCharge += voltage * current * MeasurementInterval;
Results.Publish(new ChargeResult { SampleNo = sampleNo, Voltage = Math.Round(voltage, 2), Current = Math.Round(current,2), Power = voltage * current, AccumulatedCharge = accumulatedCharge});
}

}
Expand Down
Loading