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 b477aca
Show file tree
Hide file tree
Showing 3 changed files with 328 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
Loading

0 comments on commit b477aca

Please sign in to comment.