Skip to content

Commit

Permalink
Sinexcel: calculate Energy from ActivePower (#229)
Browse files Browse the repository at this point in the history
Seems the original values from modbus are not reliable; tested on fems

Also: change DC_(DIS)CHARGE_ENERGY to LONG to avoid 'is not fitting in Integer range' exception
  • Loading branch information
sfeilmeier committed Apr 28, 2022
1 parent 3b1cb57 commit 08567a8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions io.openems.edge.batteryinverter.sinexcel/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Bundle-Version: 1.0.0.${tstamp}
io.openems.edge.bridge.modbus,\
io.openems.edge.common,\
io.openems.edge.ess.api,\
io.openems.edge.timedata.api,\

-testpath: \
${testpath},\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,10 @@ public enum ChannelId implements io.openems.edge.common.channel.ChannelId {
DC_CURRENT(Doc.of(OpenemsType.INTEGER) //
.accessMode(AccessMode.READ_ONLY)//
.unit(Unit.MILLIAMPERE)), //
DC_CHARGE_ENERGY(Doc.of(OpenemsType.INTEGER) //
DC_CHARGE_ENERGY(Doc.of(OpenemsType.LONG) //
.accessMode(AccessMode.READ_WRITE)//
.unit(Unit.WATT_HOURS)), //
DC_DISCHARGE_ENERGY(Doc.of(OpenemsType.INTEGER) //
DC_DISCHARGE_ENERGY(Doc.of(OpenemsType.LONG) //
.accessMode(AccessMode.READ_WRITE)//
.unit(Unit.WATT_HOURS)), //
TEMPERATURE_OF_DC_DC_HEAT_SINK(Doc.of(OpenemsType.INTEGER) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
import io.openems.edge.ess.power.api.Power;
import io.openems.edge.ess.power.api.Pwr;
import io.openems.edge.ess.power.api.Relationship;
import io.openems.edge.timedata.api.Timedata;
import io.openems.edge.timedata.api.TimedataProvider;
import io.openems.edge.timedata.api.utils.CalculateEnergyFromPower;

@Designate(ocd = Config.class, factory = true)
@Component(//
Expand All @@ -70,11 +73,20 @@
EdgeEventConstants.TOPIC_CYCLE_AFTER_PROCESS_IMAGE, //
EdgeEventConstants.TOPIC_CYCLE_BEFORE_PROCESS_IMAGE //
})
public class SinexcelImpl extends AbstractOpenemsModbusComponent implements Sinexcel, OffGridBatteryInverter,
ManagedSymmetricBatteryInverter, SymmetricBatteryInverter, ModbusComponent, OpenemsComponent, StartStoppable {
public class SinexcelImpl extends AbstractOpenemsModbusComponent
implements Sinexcel, OffGridBatteryInverter, ManagedSymmetricBatteryInverter, SymmetricBatteryInverter,
ModbusComponent, OpenemsComponent, TimedataProvider, StartStoppable {

private final Logger log = LoggerFactory.getLogger(SinexcelImpl.class);

@Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY, cardinality = ReferenceCardinality.OPTIONAL)
private volatile Timedata timedata = null;

private final CalculateEnergyFromPower calculateChargeEnergy = new CalculateEnergyFromPower(this,
SymmetricBatteryInverter.ChannelId.ACTIVE_CHARGE_ENERGY);
private final CalculateEnergyFromPower calculateDischargeEnergy = new CalculateEnergyFromPower(this,
SymmetricBatteryInverter.ChannelId.ACTIVE_DISCHARGE_ENERGY);

public static final int MAX_APPARENT_POWER = 30_000;
public static final int DEFAULT_UNIT_ID = 1;

Expand Down Expand Up @@ -149,6 +161,9 @@ public void run(Battery battery, int setActivePower, int setReactivePower) throw
// Set Battery Limits
this.setBatteryLimits(battery);

// Calculate the Energy values from ActivePower.
this.calculateEnergy();

// Prepare Context
var context = new Context(this, this.config, this.targetGridMode.get(), setActivePower, setReactivePower);

Expand Down Expand Up @@ -475,10 +490,7 @@ protected ModbusProtocol defineModbusProtocol() throws OpenemsException {
ElementToChannelConverter.SCALE_FACTOR_1), //
m(Sinexcel.ChannelId.COS_PHI, new SignedWordElement(125),
ElementToChannelConverter.SCALE_FACTOR_MINUS_2), //
m(SymmetricBatteryInverter.ChannelId.ACTIVE_DISCHARGE_ENERGY,
new UnsignedDoublewordElement(126), ElementToChannelConverter.SCALE_FACTOR_2), //
m(SymmetricBatteryInverter.ChannelId.ACTIVE_CHARGE_ENERGY, new UnsignedDoublewordElement(128),
ElementToChannelConverter.SCALE_FACTOR_2), //
new DummyRegisterElement(126, 129), //
m(Sinexcel.ChannelId.REACTIVE_ENERGY, new UnsignedDoublewordElement(130),
ElementToChannelConverter.SCALE_FACTOR_MINUS_1), //
m(Sinexcel.ChannelId.TEMPERATURE_OF_AC_HEAT_SINK, new SignedWordElement(132)), //
Expand Down Expand Up @@ -1107,4 +1119,30 @@ protected ModbusProtocol defineModbusProtocol() throws OpenemsException {
return value;
}, //
value -> value);

/**
* Calculate the Energy values from ActivePower.
*/
private void calculateEnergy() {
// Calculate Energy
var activePower = this.getActivePower().get();
if (activePower == null) {
// Not available
this.calculateChargeEnergy.update(null);
this.calculateDischargeEnergy.update(null);
} else if (activePower > 0) {
// Buy-From-Grid
this.calculateChargeEnergy.update(0);
this.calculateDischargeEnergy.update(activePower);
} else {
// Sell-To-Grid
this.calculateChargeEnergy.update(activePower * -1);
this.calculateDischargeEnergy.update(0);
}
}

@Override
public Timedata getTimedata() {
return this.timedata;
}
}

0 comments on commit 08567a8

Please sign in to comment.