Skip to content

Commit

Permalink
Tested and fixed E2E with bms simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
ieb committed Dec 21, 2024
1 parent 231c887 commit 71ed8ac
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ pcb/*.bak
data/*
src/version.h
node_modules
*.gcode
*.gbr
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ updated from build or via the admin ui over http.
* [x] Build and test new boards.
* [x] Update BMS app to use streaming http api
* [x] Add gitsha1 to firmware, press h to see on serial
* [x] Bench tested E2E with BMS simulator
* [ ] Test BLE UI on ChromeOS.
* [ ] Backport BMS seasmart stream handlder to main ui
* [ ] Log BMS data to flash
Expand Down
16 changes: 10 additions & 6 deletions lib/jdbbms/jdb_bms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,17 @@ void JdbBMS::update() {
unsigned long now = millis();
if ( (now - lastSend) > 1000) {
lastSend = now;
if ( reg > maxReqRegs) {
reg = REQ_REGSTART;
if ( requestReg05 == 0) {
requestRegister(0x05);
requestReg05 = 15;
} else {
if ( reg > maxReqRegs) {
reg = REQ_REGSTART;
}
requestRegister(reg);
requestReg05--;
reg++;
}
requestRegister(reg);
reg++;
}
}

Expand Down Expand Up @@ -150,8 +156,6 @@ int JdbBMS::processFrame(int from) {
} else if (responseReg == 0x05) {
copyReg05(&buffer[from+4], dataLength);
register05Length = dataLength;
maxReqRegs = 4; // only require 0x05 once.
reg = 3;
}
return endPacketPos+1;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/jdbbms/jdb_bms.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class JdbBMS {
unsigned long lastRegO5;

uint8_t reg = 3;
uint8_t maxReqRegs = 5;
uint8_t requestReg05 = 0;
uint8_t maxReqRegs = 4;
bool debug = false;

void requestRegister(uint8_t regNo);
Expand Down
12 changes: 4 additions & 8 deletions simulator/bmssim.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Got <Buffer dd 5a 00 02 56 78 ff 30 77>

let rstate = 0;
let factoryMode = false;
let inBufferLen = 0;
serialPort.on('data', function (data) {
console.log("Got ",data);
data.forEach((val) => {
Expand Down Expand Up @@ -183,14 +184,9 @@ function createReg04() {
return new Uint8Array(buffer);
}
function createReg05() {
const b = new Uint8Array(6);
b[0] = 5;
b[1] = 'M';
b[2] = 'Y';
b[3] = 'B';
b[4] = 'M';
b[5] = 'S';
return b;
const hw = Buffer.from(' fw:1.0 sw:2.3');
hw[0] = hw.length-1;
return new Uint8Array(hw);
}

function createRegAA() {
Expand Down
40 changes: 14 additions & 26 deletions ui/lifepo4/bmsseasmartreader.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class NMEA2000JBDMessageDecoder {
if (registerLength === 0) {
return {};
}
console.log("Register length == ", registerLength);
const nNTC = canMessage.data.getUint8(27);
const endNTC = REG_NTC_READINGS_U8 + 2 * nNTC;
/*
Expand Down Expand Up @@ -94,11 +93,11 @@ class NMEA2000JBDMessageDecoder {
protectionStatus: this.get2ByteUInt(canMessage.data, REG_ERRORS_U16),
balanceActive: this.getBalanceStatus(canMessage.data),
currentErrors: this.getCurrentErrors(canMessage.data),
bmsSWVersion: this.get1ByteUDouble(canMessage, REG_SOFTWARE_VERSION_U8, 0.1),
bmsSWVersion: this.get1ByteUDouble(canMessage.data, REG_SOFTWARE_VERSION_U8, 0.1),
FETStatus: this.getFETStatus(canMessage.data),
numberOfCells: this.get1ByteUInt(canMessage, REG_NUMBER_OF_CELLS_U8),
numberOfCells: this.get1ByteUInt(canMessage.data, REG_NUMBER_OF_CELLS_U8),
tempSensorCount: nNTC,
humidity: this.get1ByteUInt(canMessage, endNTC),
humidity: this.get1ByteUInt(canMessage.data, endNTC),
alarmStatus: this.get2ByteUInt(canMessage.data, endNTC + 1),
fullChargeCapacity: this.get2ByteUDouble(canMessage.data, endNTC + 3, 0.01),
remainingChargeCapacity: this.get2ByteUDouble(canMessage.data, endNTC + 5, 0.01),
Expand Down Expand Up @@ -143,9 +142,9 @@ class NMEA2000JBDMessageDecoder {
getCellMv(dataView) {
const nCells = dataView.getUint8(4) / 2;
const cellMv = [];
for (let i = 0; i < nCells / 2; i++) {
for (let i = 0; i < nCells; i++) {
cellMv[i] = dataView.getUint16(5 + i * 2, true);
if ( cellMv[i] === 0xffff) {
if (cellMv[i] === 0xffff) {
return undefined;
}
}
Expand All @@ -154,7 +153,7 @@ class NMEA2000JBDMessageDecoder {

// eslint-disable-next-line class-methods-use-this
decodeDate(dateU16) {
if ( dateU16 == 0xffff) {
if (dateU16 === 0xffff) {
return undefined;
}
// eslint-disable-next-line no-bitwise
Expand All @@ -168,7 +167,7 @@ class NMEA2000JBDMessageDecoder {

getBalanceStatus(dataView) {
let status = dataView.getUint16(REG_BAT0_15_STATUS_U16, true);
if (status == 0xffff) {
if (status === 0xffff) {
return undefined;
}
const ncells = dataView.getUint8(REG_NUMBER_OF_CELLS_U8);
Expand Down Expand Up @@ -197,7 +196,7 @@ class NMEA2000JBDMessageDecoder {

getCurrentErrors(dataView) {
const status = dataView.getUint16(REG_ERRORS_U16, true);
if ( status === 0xffff) {
if (status === 0xffff) {
return undefined;
}
const currentErrors = {
Expand Down Expand Up @@ -302,17 +301,6 @@ class NMEA2000JBDMessageDecoder {
}


// eslint-disable-next-line class-methods-use-this
get2ByteUDouble(dataView, byteOffset, factor) {
if (dataView.byteLength < byteOffset + 2) {
return undefined;
}
if (dataView.getUint8(byteOffset) === 0xff
&& dataView.getUint8(byteOffset + 1) === 0xff) {
return undefined;
}
return factor * dataView.getUint16(byteOffset, true);
}

// eslint-disable-next-line class-methods-use-this
get2ByteDouble(dataView, byteOffset, factor) {
Expand Down Expand Up @@ -445,7 +433,7 @@ class SeaSmartParser extends EventEmitter {
* statusUpdate the BLE status updates.
*
*
*/
*/
class ChunkedSeaSmartStream extends EventEmitter {
constructor(url, seasmartParser) {
super();
Expand All @@ -461,7 +449,7 @@ class ChunkedSeaSmartStream extends EventEmitter {
* started goes from false to true, but is never reset.
* once started, running is set to true, and then false, never reset.
* end state is started == true and running == false.
*
*
*/
start() {
if (!this.started) {
Expand Down Expand Up @@ -614,7 +602,7 @@ class JDBBMSReaderSeasmart extends EventEmitter {
this.messagedDecoded++;
that.emitEvent('n2kdecoded', decodedMessage);
} catch (e) {
log.error('Failed to process decoded message', e);
console.error('Failed to process decoded message', e);
}
});
setInterval(() => {
Expand All @@ -630,7 +618,7 @@ class JDBBMSReaderSeasmart extends EventEmitter {

async connectBMS(url) {
if (!this.stream) {
console.log("Starting connection to ", url);
console.log('Starting connection to ', url);
this.streamCount++;
this.stream = new ChunkedSeaSmartStream(url, this.parser);
const that = this;
Expand All @@ -639,14 +627,14 @@ class JDBBMSReaderSeasmart extends EventEmitter {
});
this.stream.start();
} else {
console.log("Already connected");
console.log('Already connected');
}
}

async disconnectBMS() {
this.url = undefined;
await this.stream.stop();
console.log("Stopped");
console.log('Stopped');
this.stream = undefined;
}
}
Expand Down
63 changes: 51 additions & 12 deletions ui/lifepo4/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class TimeSeries {

storedSize() {
const size = JSON.stringify(this.history).length;
console.log("Stored size is ",size);
console.log('Stored size is ', size);
}

// event emitter
Expand Down Expand Up @@ -340,7 +340,8 @@ class VoltagesGraph {
});
} else {
for (let i = 0; i < history.ts.length; i++) {
if (!Number.isNaN(history.voltageV.mean[i]) && history.voltageV.mean[i] !== 0) {
if (Number.isFinite(history.voltageV.mean[i])
&& history.voltageV.mean[i] !== 0) {
voltageV = history.voltageV.mean[i];
}
if (history.ts[i] >= startTs && history.ts[i] <= endTs) {
Expand All @@ -352,6 +353,12 @@ class VoltagesGraph {
}
}

data.forEach((d) => {
if (!Number.isFinite(d.voltageV)) {
console.log('Error in graph data', d);
}
});



const x = d3.scaleTime(
Expand Down Expand Up @@ -441,7 +448,7 @@ class CurrentGraph {
});
} else {
for (let i = 0; i < history.ts.length; i++) {
if (!Number.isNaN(history.currentA.mean[i]) && history.currentA.mean[i] !== 0) {
if (Number.isFinite(history.currentA.mean[i]) && history.currentA.mean[i] !== 0) {
currentA = history.currentA.mean[i];
}
if (history.ts[i] >= startTs && history.ts[i] <= endTs) {
Expand All @@ -452,6 +459,11 @@ class CurrentGraph {
}
}
}
data.forEach((d) => {
if (!Number.isFinite(d.currentA)) {
console.log('Error in graph data', d);
}
});



Expand Down Expand Up @@ -546,7 +558,7 @@ class StateOfChargeGraph {
});
} else {
for (let i = 0; i < history.ts.length; i++) {
if (!Number.isNaN(history.soc.mean[i]) && history.soc.mean[i] !== 0) {
if (Number.isFinite(history.soc.mean[i]) && history.soc.mean[i] !== 0) {
soc = 100 * history.soc.mean[i];
}
if (history.ts[i] >= startTs && history.ts[i] <= endTs) {
Expand All @@ -558,6 +570,11 @@ class StateOfChargeGraph {
}
}

data.forEach((d) => {
if (!Number.isFinite(d.soc)) {
console.log('Error in graph data', d);
}
});

console.debug('StateOfCharge ', data);

Expand Down Expand Up @@ -647,7 +664,7 @@ class ChargeRemainingGraph {
});
} else {
for (let i = 0; i < history.ts.length; i++) {
if (!Number.isNaN(history.chargeAh.mean[i]) && history.chargeAh.mean[i] !== 0) {
if (Number.isFinite(history.chargeAh.mean[i]) && history.chargeAh.mean[i] !== 0) {
chargeAh = history.chargeAh.mean[i];
}
if (history.ts[i] >= startTs && history.ts[i] <= endTs) {
Expand All @@ -659,6 +676,11 @@ class ChargeRemainingGraph {
}
}

data.forEach((d) => {
if (!Number.isFinite(d.chargeAh)) {
console.log('Error in graph data', d);
}
});

console.debug('ChrgeRemaining ', data);

Expand Down Expand Up @@ -758,16 +780,16 @@ class CellVoltagesGraph {
}
} else {
for (let i = 0; i < history.ts.length; i++) {
if (!Number.isNaN(history.cell0V.mean[i]) && history.cell0V.mean[i]) {
if (Number.isFinite(history.cell0V.mean[i]) && history.cell0V.mean[i]) {
cellV[0] = history.cell0V.mean[i];
}
if (!Number.isNaN(history.cell1V.mean[i]) && history.cell1V.mean[i]) {
if (Number.isFinite(history.cell1V.mean[i]) && history.cell1V.mean[i]) {
cellV[1] = history.cell1V.mean[i];
}
if (!Number.isNaN(history.cell2V.mean[i]) && history.cell2V.mean[i]) {
if (Number.isFinite(history.cell2V.mean[i]) && history.cell2V.mean[i]) {
cellV[2] = history.cell2V.mean[i];
}
if (!Number.isNaN(history.cell3V.mean[i]) && history.cell3V.mean[i]) {
if (Number.isFinite(history.cell3V.mean[i]) && history.cell3V.mean[i]) {
cellV[3] = history.cell3V.mean[i];
}
if (history.ts[i] >= startTs && history.ts[i] <= endTs) {
Expand All @@ -778,6 +800,15 @@ class CellVoltagesGraph {
}
}


data.forEach((cell) => {
cell.forEach((d) => {
if (!Number.isFinite(d.v)) {
console.log('Error in graph data', d);
}
});
});

console.debug('Cell Voltage Data ', data);


Expand Down Expand Up @@ -896,13 +927,13 @@ class TemperatureGraph {
}
} else {
for (let i = 0; i < history.ts.length; i++) {
if (!Number.isNaN(history.boardTempC.mean[i]) && history.boardTempC.mean[i]) {
if (Number.isFinite(history.boardTempC.mean[i]) && history.boardTempC.mean[i]) {
temps[0] = history.boardTempC.mean[i];
}
if (!Number.isNaN(history.cell0C.mean[i]) && history.cell0C.mean[i]) {
if (Number.isFinite(history.cell0C.mean[i]) && history.cell0C.mean[i]) {
temps[1] = history.cell0C.mean[i];
}
if (!Number.isNaN(history.cell1C.mean[i]) && history.cell1C.mean[i]) {
if (Number.isFinite(history.cell1C.mean[i]) && history.cell1C.mean[i]) {
temps[2] = history.cell1C.mean[i];
}
if (history.ts[i] >= startTs && history.ts[i] <= endTs) {
Expand All @@ -913,6 +944,14 @@ class TemperatureGraph {
}
}

data.forEach((cell) => {
cell.forEach((d) => {
if (!Number.isFinite(d.t)) {
console.log('Error in graph data', d);
}
});
});


console.debug('Temperatures ', data);

Expand Down
2 changes: 1 addition & 1 deletion ui/v2/src/n2k/messages_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class PGN127508_DCBatteryStatus extends CANMessage {
count: 1,
message: 'DCBatteryStatus',
instance: this.getByte(message, 0),
batteryVoltage: this.get2ByteUDouble(message, 1, 0.01),
batteryVoltage: this.get2ByteDouble(message, 1, 0.01),
batteryCurrent: this.get2ByteDouble(message, 3, 0.1),
batteryTemperature: this.get2ByteUDouble(message, 5, 0.01),
sid: this.getByte(message, 7),
Expand Down

0 comments on commit 71ed8ac

Please sign in to comment.