Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility with old BYD Battery-Box Commercial C130 hardware #1353

Merged
merged 1 commit into from
Jan 5, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.common.types.OpenemsType;
import io.openems.edge.battery.api.Battery;
import io.openems.edge.battery.bydcommercial.enums.BatteryWorkState;
import io.openems.edge.battery.bydcommercial.enums.PowerCircuitControl;
import io.openems.edge.battery.bydcommercial.statemachine.StateMachine.State;
import io.openems.edge.common.channel.Doc;
import io.openems.edge.common.channel.StateChannel;
Expand Down Expand Up @@ -124,7 +126,7 @@ public enum ChannelId implements io.openems.edge.common.channel.ChannelId {
BATTERY_WORK_STATE(Doc.of(BatteryWorkState.values())), //

// IntegerReadChannels
POWER_CIRCUIT_CONTROL(Doc.of(PowerCircuitControl.values()) //
POWER_CIRCUIT_CONTROL(Doc.of(PowerCircuitControl.values()) //
.accessMode(AccessMode.READ_WRITE)), //
CLUSTER_1_VOLTAGE(Doc.of(OpenemsType.INTEGER) //
.unit(Unit.MILLIVOLT)), //
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.openems.edge.battery.bydcommercial;

import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.ComponentContext;
Expand Down Expand Up @@ -30,6 +31,7 @@
import io.openems.edge.bridge.modbus.api.BridgeModbus;
import io.openems.edge.bridge.modbus.api.ElementToChannelConverter;
import io.openems.edge.bridge.modbus.api.ModbusProtocol;
import io.openems.edge.bridge.modbus.api.ModbusUtils;
import io.openems.edge.bridge.modbus.api.element.BitsWordElement;
import io.openems.edge.bridge.modbus.api.element.SignedWordElement;
import io.openems.edge.bridge.modbus.api.element.UnsignedWordElement;
Expand All @@ -55,11 +57,12 @@ public class BatteryBoxC130Impl extends AbstractOpenemsModbusComponent
implements BatteryBoxC130, Battery, OpenemsComponent, EventHandler, ModbusSlave, StartStoppable {

private static final float CAPACITY_PER_MODULE = 6.9f;

private static final int MIN_ALLOWED_VOLTAGE_PER_MODULE = 34;

private static final int MAX_ALLOWED_VOLTAGE_PER_MODULE = 42;

private static final int OLD_VERSION_DEFAULT_CHARGE_MAX_VOLTAGE = 820;
private static final int OLD_VERSION_DEFAULT_DISCHARGE_MIN_VOLTAGE = 638;

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

@Reference
Expand Down Expand Up @@ -162,7 +165,7 @@ protected ModbusProtocol defineModbusProtocol() throws OpenemsException {
m(BatteryBoxC130.ChannelId.POWER_CIRCUIT_CONTROL, new UnsignedWordElement(0x2010)) //
), //
new FC3ReadRegistersTask(0x2100, Priority.HIGH, //
m(new UnsignedWordElement(0x2100)) //
m(new UnsignedWordElement(0x2100).onUpdateCallback(this.onRegister0x2100Update)) //
.m(BatteryBoxC130.ChannelId.CLUSTER_1_VOLTAGE, ElementToChannelConverter.SCALE_FACTOR_2) // [mV]
.m(Battery.ChannelId.VOLTAGE, ElementToChannelConverter.SCALE_FACTOR_MINUS_1) // [V]
.build(), //
Expand Down Expand Up @@ -200,9 +203,8 @@ protected ModbusProtocol defineModbusProtocol() throws OpenemsException {
ElementToChannelConverter.DIRECT_1_TO_1) //
.m(Battery.ChannelId.MIN_CELL_TEMPERATURE,
ElementToChannelConverter.SCALE_FACTOR_MINUS_1) //
.build(), //
m(BatteryBoxC130.ChannelId.MODULE_QTY, new UnsignedWordElement(0x210D)), //
m(BatteryBoxC130.ChannelId.TOTAL_VOLTAGE_OF_SINGLE_MODULE, new UnsignedWordElement(0x210E))), //
.build()), //

new FC3ReadRegistersTask(0x211D, Priority.HIGH, //
m(new BitsWordElement(0x211D, this) //
.bit(1, BatteryBoxC130.ChannelId.NEED_CHARGE)) //
Expand Down Expand Up @@ -302,10 +304,6 @@ protected ModbusProtocol defineModbusProtocol() throws OpenemsException {
m(Battery.ChannelId.CHARGE_MAX_CURRENT, new SignedWordElement(0x216C), //
ElementToChannelConverter.SCALE_FACTOR_MINUS_1), //
m(Battery.ChannelId.DISCHARGE_MAX_CURRENT, new SignedWordElement(0x216D), //
ElementToChannelConverter.SCALE_FACTOR_MINUS_1), //
m(Battery.ChannelId.CHARGE_MAX_VOLTAGE, new UnsignedWordElement(0x216E), //
ElementToChannelConverter.SCALE_FACTOR_MINUS_1), //
m(Battery.ChannelId.DISCHARGE_MIN_VOLTAGE, new UnsignedWordElement(0x216F), //
ElementToChannelConverter.SCALE_FACTOR_MINUS_1) //
), //

Expand Down Expand Up @@ -664,4 +662,61 @@ public StartStop getStartStopTarget() {
return StartStop.UNDEFINED; // can never happen
}

/*
* Handle incompatibility with old hardware protocol.
*
* 'onRegister0x2100Update()' callback is called when register 0x2100 is read.
*/

private boolean isModbusProtocolInitialized = false;
private final Consumer<Integer> onRegister0x2100Update = (value) -> {
if (value == null) {
// ignore invalid values; modbus bridge has no connection yet
return;
}
if (BatteryBoxC130Impl.this.isModbusProtocolInitialized) {
// execute only once
return;
}
BatteryBoxC130Impl.this.isModbusProtocolInitialized = true;

// Try to read MODULE_QTY Register
try {
ModbusUtils.readELementOnce(this.getModbusProtocol(), new UnsignedWordElement(0x210D), false)
.thenAccept(moduleQtyValue -> {
if (moduleQtyValue != null) {
// Register is available -> add Registers for current hardware to protocol
try {
this.getModbusProtocol().addTasks(//
new FC3ReadRegistersTask(0x210D, Priority.LOW, //
m(BatteryBoxC130.ChannelId.MODULE_QTY, new UnsignedWordElement(0x210D)), //
m(BatteryBoxC130.ChannelId.TOTAL_VOLTAGE_OF_SINGLE_MODULE,
new UnsignedWordElement(0x210E))), //
new FC3ReadRegistersTask(0x216E, Priority.LOW, //
m(Battery.ChannelId.CHARGE_MAX_VOLTAGE, new UnsignedWordElement(0x216E), //
ElementToChannelConverter.SCALE_FACTOR_MINUS_1), //
m(Battery.ChannelId.DISCHARGE_MIN_VOLTAGE,
new UnsignedWordElement(0x216F), //
ElementToChannelConverter.SCALE_FACTOR_MINUS_1) //
));
} catch (OpenemsException e) {
BatteryBoxC130Impl.this.logError(BatteryBoxC130Impl.this.log,
"Unable to add registers for detected hardware version: " + e.getMessage());
e.printStackTrace();
} //
} else {
BatteryBoxC130Impl.this.logInfo(BatteryBoxC130Impl.this.log,
"Detected old hardware version. Registers are not available. Setting default values.");

this._setChargeMaxVoltage(OLD_VERSION_DEFAULT_CHARGE_MAX_VOLTAGE);
this._setDischargeMinVoltage(OLD_VERSION_DEFAULT_DISCHARGE_MIN_VOLTAGE);
}
});
} catch (OpenemsException e) {
BatteryBoxC130Impl.this.logError(BatteryBoxC130Impl.this.log,
"Unable to detect hardware version: " + e.getMessage());
e.printStackTrace();
}
};

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.openems.edge.battery.bydcommercial;
package io.openems.edge.battery.bydcommercial.enums;

import io.openems.common.types.OptionsEnum;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.openems.edge.battery.bydcommercial;
package io.openems.edge.battery.bydcommercial.enums;

import io.openems.common.types.OptionsEnum;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.time.Instant;

import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.edge.battery.bydcommercial.PowerCircuitControl;
import io.openems.edge.battery.bydcommercial.enums.PowerCircuitControl;
import io.openems.edge.battery.bydcommercial.statemachine.StateMachine.State;
import io.openems.edge.common.statemachine.StateHandler;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.time.Instant;

import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.edge.battery.bydcommercial.PowerCircuitControl;
import io.openems.edge.battery.bydcommercial.enums.PowerCircuitControl;
import io.openems.edge.battery.bydcommercial.statemachine.StateMachine.State;
import io.openems.edge.battery.bydcommercial.utils.Constants;
import io.openems.edge.common.statemachine.StateHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.time.Instant;

import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.edge.battery.bydcommercial.PowerCircuitControl;
import io.openems.edge.battery.bydcommercial.enums.PowerCircuitControl;
import io.openems.edge.battery.bydcommercial.statemachine.StateMachine.State;
import io.openems.edge.battery.bydcommercial.utils.Constants;
import io.openems.edge.common.statemachine.StateHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.openems.edge.battery.bydcommercial.statemachine;

import io.openems.edge.battery.bydcommercial.PowerCircuitControl;
import io.openems.edge.battery.bydcommercial.enums.PowerCircuitControl;
import io.openems.edge.battery.bydcommercial.statemachine.StateMachine.State;
import io.openems.edge.common.startstop.StartStop;
import io.openems.edge.common.statemachine.StateHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.openems.edge.bridge.modbus.api;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -115,7 +113,7 @@ protected boolean activate(ComponentContext context, String id, String alias, bo
this.unitId = unitId;
BridgeModbus modbus = this.modbus.get();
if (this.isEnabled() && modbus != null) {
modbus.addProtocol(this.id(), this.getModbusProtocol(this.unitId));
modbus.addProtocol(this.id(), this.getModbusProtocol());
}
return false;
}
Expand Down Expand Up @@ -171,7 +169,14 @@ public BridgeModbus getBridgeModbus() {
return modbus.get();
}

private ModbusProtocol getModbusProtocol(int unitId) throws OpenemsException {
/**
* Gets the {@link ModbusProtocol}. Creates it via
* {@link #defineModbusProtocol()} if it does not yet exist.
*
* @return the {@link ModbusProtocol}
* @throws OpenemsException on error
*/
protected ModbusProtocol getModbusProtocol() throws OpenemsException {
ModbusProtocol protocol = this.protocol;
if (protocol != null) {
return protocol;
Expand Down Expand Up @@ -336,26 +341,4 @@ public enum BitConverter {
DIRECT_1_TO_1, INVERT
}

/**
* Converts upper/lower bytes to Short.
*
* @param value the int value
* @param upperBytes 1 = upper two bytes, 0 = lower two bytes
* @return the Short
*/
public static Short convert(int value, int upperBytes) {
ByteBuffer b = ByteBuffer.allocate(4);
b.order(ByteOrder.LITTLE_ENDIAN);
b.putInt(value);

byte byte0 = b.get(upperBytes * 2);
byte byte1 = b.get(upperBytes * 2 + 1);

ByteBuffer shortBuf = ByteBuffer.allocate(2);
shortBuf.order(ByteOrder.LITTLE_ENDIAN);
shortBuf.put(0, byte0);
shortBuf.put(1, byte1);

return shortBuf.getShort();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package io.openems.edge.bridge.modbus.api;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.concurrent.CompletableFuture;

import io.openems.common.exceptions.OpenemsException;
import io.openems.edge.bridge.modbus.api.element.AbstractModbusElement;
import io.openems.edge.bridge.modbus.api.task.FC3ReadRegistersTask;
import io.openems.edge.bridge.modbus.api.task.Task;
import io.openems.edge.common.taskmanager.Priority;

public class ModbusUtils {

/**
* Reads given Element once from Modbus.
*
* @param <T> the Type of the element
* @param modbusProtocol the {@link ModbusProtocol}, that is linked with a
* {@link BridgeModbus}
* @param element the {@link AbstractModbusElement}
* @param tryAgainOnError if true, tries to read till it receives a value; if
* false, stops after first try and possibly return null
* @return a future value, e.g. a Integer or null (if tryAgainOnError is false)
* @throws OpenemsException on error with the {@link ModbusProtocol} object
*/
public static <T> CompletableFuture<T> readELementOnce(ModbusProtocol modbusProtocol,
AbstractModbusElement<T> element, boolean tryAgainOnError) throws OpenemsException {
// Prepare result
final CompletableFuture<T> result = new CompletableFuture<T>();

// Activate task
final Task task = new FC3ReadRegistersTask(element.getStartAddress(), Priority.HIGH, element);
modbusProtocol.addTask(task);

// Register listener for element
element.onUpdateCallback(value -> {
if (value == null) {
if (tryAgainOnError) {
return;
} else {
result.complete(null);
}
}
// do not try again
modbusProtocol.removeTask(task);
result.complete(value);
});

return result;
}

/**
* Converts upper/lower bytes to Short.
*
* @param value the int value
* @param upperBytes 1 = upper two bytes, 0 = lower two bytes
* @return the Short
*/
public static Short convert(int value, int upperBytes) {
ByteBuffer b = ByteBuffer.allocate(4);
b.order(ByteOrder.LITTLE_ENDIAN);
b.putInt(value);

byte byte0 = b.get(upperBytes * 2);
byte byte1 = b.get(upperBytes * 2 + 1);

ByteBuffer shortBuf = ByteBuffer.allocate(2);
shortBuf.order(ByteOrder.LITTLE_ENDIAN);
shortBuf.put(0, byte0);
shortBuf.put(1, byte1);

return shortBuf.getShort();
}
}
Loading