Skip to content

Commit

Permalink
fix(core): Avoid flickering between charge/discharge due to PID filter
Browse files Browse the repository at this point in the history
Before this change the PID filter implementation eventually caused the behavior to switch from charging to discharing, eventhough the system wanted to (a) charge with a lower value than before (b) set active power to 0. This was due to the fact, that the PID filter overshoots its target.
By detecting if the system wants charge/discharge/no current flow the battery, it now dynamically adjusts the limits of the battery so it does not overshoot in the unwanted area. This reduces stress on the battery. It also improves the controller that are using this filter (e.g. self consumption optimization, peak load shaving). Sometimes they showed behavior where they wildly flicker between charging/discharging.
  • Loading branch information
Felix Remmel committed Jan 8, 2025
1 parent 5214d22 commit b2f818c
Show file tree
Hide file tree
Showing 3 changed files with 272 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.openems.edge.ess.api;

import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.common.function.ThrowingBiConsumer;
import io.openems.edge.ess.power.api.Phase;
import io.openems.edge.ess.power.api.Pwr;

public final class ActivePowerConstraintWithPid implements ThrowingBiConsumer<ManagedSymmetricEss, Integer, OpenemsNamedException> {

@Override
public void accept(ManagedSymmetricEss ess, Integer value) throws OpenemsNamedException {
if (value != null) {
var power = ess.getPower();
var pidFilter = power.getPidFilter();

// configure PID filter
var minPower = power.getMinPower(ess, Phase.ALL, Pwr.ACTIVE);
var maxPower = power.getMaxPower(ess, Phase.ALL, Pwr.ACTIVE);
if (maxPower < minPower) {
maxPower = minPower; // avoid rounding error
}

int currentActivePower = ess.getActivePower().orElse(0);

if (value <= 0 && currentActivePower <= 0 && minPower < 0 && maxPower > 0) {
// Prevent PID filter to overshoot and flicker from charging to discharging
pidFilter.setLimits(minPower, 0);
} else if (value >= 0 && currentActivePower >= 0 && minPower < 0 && maxPower > 0) {
pidFilter.setLimits(0, maxPower);
} else {
// changing between charging/discharging is intended behavior, so we allow it.
pidFilter.setLimits(minPower, maxPower);
}

var pidOutput = pidFilter.applyPidFilter(currentActivePower, value);

ess.setActivePowerEquals(pidOutput);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,7 @@ public enum ChannelId implements io.openems.edge.common.channel.ChannelId {
.unit(Unit.WATT) //
.accessMode(AccessMode.WRITE_ONLY) //
.onChannelSetNextWrite(
new PowerConstraint("SetActivePowerEqualsWithPid", Phase.ALL, Pwr.ACTIVE, Relationship.EQUALS) {
@Override
public void accept(ManagedSymmetricEss ess, Integer value) throws OpenemsNamedException {
if (value != null) {
var power = ess.getPower();
var pidFilter = power.getPidFilter();

// configure PID filter
var minPower = power.getMinPower(ess, Phase.ALL, Pwr.ACTIVE);
var maxPower = power.getMaxPower(ess, Phase.ALL, Pwr.ACTIVE);
if (maxPower < minPower) {
maxPower = minPower; // avoid rounding error
}
pidFilter.setLimits(minPower, maxPower);

int currentActivePower = ess.getActivePower().orElse(0);
var pidOutput = pidFilter.applyPidFilter(currentActivePower, value);

ess.setActivePowerEquals(pidOutput);
}
}
})),
new ActivePowerConstraintWithPid())),
/**
* Sets a fixed Reactive Power.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package io.openems.edge.ess.api;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.edge.common.channel.IntegerWriteChannel;
import io.openems.edge.ess.power.api.Phase;
import io.openems.edge.ess.power.api.Pwr;
import io.openems.edge.ess.test.DummyManagedSymmetricEss;
import io.openems.edge.ess.test.DummyPower;

public class ActivePowerConstraintWithPidTest {

@Test
public void testSwitchBetweenChargeDischarge() throws OpenemsNamedException {
var constraint = new ActivePowerConstraintWithPid();
var ess = new DummyManagedSymmetricEss("bla");
ess.setPower(new DummyPower(0.3, 0.3, 0.1));
var actualPower = this.setAndVerifyPower(constraint, ess, 0, 10_000, 3_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 4_800);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 6_480);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 7_548);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 8_345);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 8_868);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_235);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_481);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_648);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_762);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_839);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_891);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_926);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_950);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_966);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_977);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_984);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_989);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_993);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_995);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_997);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_998);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_998);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_999);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9_999);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, 4000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, 400);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -2960);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -5096);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -6690);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -7737);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -8471);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -8961);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9297);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9523);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9677);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9781);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9852);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9900);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9932);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9954);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9969);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9979);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9986);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9990);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9993);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9996);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9997);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9998);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9999);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9999);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -9999);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, -10_000, -10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, -3_999);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, -400);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 2960);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 5096);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 6690);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 7737);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 8471);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 8961);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9297);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9523);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9677);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9781);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9852);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9900);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9932);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9954);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9969);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9979);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9985);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9990);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9993);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9996);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9997);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9998);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9999);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9999);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 9999);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 10_000, 10_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 7_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 5_200);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 3_520);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 2_452);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 1655);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 1131);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 765);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 519);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 352);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 238);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 161);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 109);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 74);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 50);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 34);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 23);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 16);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 11);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 7);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 5);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 3);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 2);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 2);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 1);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 1);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
}

@Test
public void testBetweenChargeNoCharge() throws OpenemsNamedException {
var constraint = new ActivePowerConstraintWithPid();
var ess = new DummyManagedSymmetricEss("bla");
ess.setPower(new DummyPower(0.3, 0.3, 0.1));
var actualPower = this.setAndVerifyPower(constraint, ess, 100_000, 100_000, 100_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
}

@Test
public void testBetweenDischargeNoCharge() throws OpenemsNamedException {
var constraint = new ActivePowerConstraintWithPid();
var ess = new DummyManagedSymmetricEss("bla");
ess.setPower(new DummyPower(0.3, 0.3, 0.1));
var actualPower = this.setAndVerifyPower(constraint, ess, -100_000, -100_000, -100_000);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, 0);
}

@Test
public void testMaxLowerThanMin() throws OpenemsNamedException {
var constraint = new ActivePowerConstraintWithPid();
var ess = new DummyManagedSymmetricEss("bla");
int minPower = 5;
ess.setPower(new DummyPower(0.3, 0.3, 0.1) {
@Override
public int getMinPower(ManagedSymmetricEss ess, Phase phase, Pwr pwr) {
return minPower;
}
@Override

Check warning on line 209 in io.openems.edge.ess.api/test/io/openems/edge/ess/api/ActivePowerConstraintWithPidTest.java

View workflow job for this annotation

GitHub Actions / build-java

'METHOD_DEF' should be separated from previous line.
public int getMaxPower(ManagedSymmetricEss ess, Phase phase, Pwr pwr) {
return minPower - 1;
}
});
var actualPower = this.setAndVerifyPower(constraint, ess, 0, -100_000, minPower);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, minPower);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, minPower, minPower);
actualPower = this.setAndVerifyPower(constraint, ess, actualPower, 0, minPower);
}

private int setAndVerifyPower(ActivePowerConstraintWithPid constraint, DummyManagedSymmetricEss ess,
int currentActivePower, int targetActivePower, int expectedActivePower) throws OpenemsNamedException {
ess._setActivePower(currentActivePower);
ess.getActivePowerChannel().nextProcessImage();
IntegerWriteChannel activePowerChannel = (IntegerWriteChannel) ess.getSetActivePowerEqualsChannel();
constraint.accept(ess, targetActivePower);
int actualPower = activePowerChannel.getNextWriteValue().get().intValue();
assertEquals(expectedActivePower, actualPower);
return actualPower;
}

}

0 comments on commit b2f818c

Please sign in to comment.