Skip to content

Commit

Permalink
PC200 überarbeitet
Browse files Browse the repository at this point in the history
  • Loading branch information
shining-man committed May 28, 2024
1 parent 095578d commit 340899d
Showing 1 changed file with 50 additions and 66 deletions.
116 changes: 50 additions & 66 deletions src/devices/GobelBms_PC200.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,63 +137,45 @@ bool GobelBmsPC200_readBmsData(Stream *port, uint8_t devNr, void (*callback)(uin
u8_mCountOfPacks = devData->u8_NumberOfDevices;
uint8_t response[GOBELBMS_MAX_ANSWER_LEN];

uint8_t u8_lGobelAdr = 0;
uint8_t u8_lGobelAdrBmsData = 0;
if (u8_mCountOfPacks > 1)
{
u8_lGobelAdr = 1;
u8_mCountOfPacks++;
}

#ifdef GOBELPC200_DEBUG
uint8_t u8_lGobelAdr = devData->u8_deviceNr;
uint8_t u8_lGobelAdrBmsData = devData->u8_BmsDataAdr;
if(u8_mCountOfPacks > 1) u8_lGobelAdr += 1;

#ifdef GOBELPC200_DEBUG
BSC_LOGI(TAG, "GobelBms_readBmsData() devNr=%i, firstAdr=%i, CountOfPacks=%i, Packs=%i", devNr, u8_lGobelAdr, u8_mCountOfPacks, devData->u8_NumberOfDevices);
#endif
#endif

for (; u8_lGobelAdr < u8_mCountOfPacks; u8_lGobelAdr++)
#ifdef GOBELPC200_DEBUG
BSC_LOGI(TAG, "read data from pack %i", u8_lGobelAdr);
#endif
getDataFromBms(u8_lGobelAdr, 0x42);
if (recvAnswer(response))
{
#ifdef GOBELPC200_DEBUG
BSC_LOGI(TAG, "read data from pack %i", u8_lGobelAdr);
#endif
getDataFromBms(u8_lGobelAdr, 0x42);
if (recvAnswer(response))
{
parseMessage(response, u8_lGobelAdrBmsData);
parseMessage(response, u8_lGobelAdrBmsData);

// mqtt
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + u8_mDevNr + u8_lGobelAdrBmsData, MQTT_TOPIC2_TOTAL_VOLTAGE, -1, getBmsTotalVoltage(BT_DEVICES_COUNT + u8_mDevNr + u8_lGobelAdrBmsData));
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + u8_mDevNr + u8_lGobelAdrBmsData, MQTT_TOPIC2_TOTAL_CURRENT, -1, getBmsTotalCurrent(BT_DEVICES_COUNT + u8_mDevNr + u8_lGobelAdrBmsData));
}
else
{
ret = false;
}

if (ret == true)
// mqtt
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + u8_lGobelAdrBmsData, MQTT_TOPIC2_TOTAL_VOLTAGE, -1, getBmsTotalVoltage(BT_DEVICES_COUNT + u8_lGobelAdrBmsData));
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + u8_lGobelAdrBmsData, MQTT_TOPIC2_TOTAL_CURRENT, -1, getBmsTotalCurrent(BT_DEVICES_COUNT + u8_lGobelAdrBmsData));
}
else ret = false;

if (ret == true)
{
vTaskDelay(pdMS_TO_TICKS(25));
getDataFromBms(u8_lGobelAdr, 0x44); // Alarms
if (recvAnswer(response))
{
getDataFromBms(u8_lGobelAdr, 0x44); // Alarms

if (recvAnswer(response))
{
parseMessage_Alarms(response, u8_lGobelAdrBmsData);
}
else
{
ret = false;
}

if (ret == true)
setBmsLastDataMillis(BT_DEVICES_COUNT + u8_mDevNr + u8_lGobelAdrBmsData, millis());
parseMessage_Alarms(response, u8_lGobelAdrBmsData);
}

u8_lGobelAdrBmsData++;
vTaskDelay(pdMS_TO_TICKS(25));
else ret = false;
}

if (devNr >= 2)
callbackSetTxRxEn(u8_mDevNr, serialRxTx_RxTxDisable);
if(devNr >= 2) callbackSetTxRxEn(u8_mDevNr, serialRxTx_RxTxDisable);
vTaskDelay(pdMS_TO_TICKS(25));
return ret;
}


static void getDataFromBms(uint8_t address, uint8_t function)
{
uint16_t lenid = lCrc(2);
Expand Down Expand Up @@ -236,6 +218,7 @@ static void getDataFromBms(uint8_t address, uint8_t function)
callbackSetTxRxEn(u8_mDevNr, serialRxTx_RxEn);
}


static bool recvAnswer(uint8_t *p_lRecvBytes)
{
uint8_t SMrecvState, u8_lRecvByte, u8_lRecvBytesCnt, u8_lRecvDataLen, u8_CyclesWithoutData;
Expand Down Expand Up @@ -313,6 +296,7 @@ static bool recvAnswer(uint8_t *p_lRecvBytes)
return true;
}


static void parseMessage(uint8_t *t_message, uint8_t address)
{
// lambda get16bitFromMsg(i)
Expand All @@ -323,7 +307,7 @@ static void parseMessage(uint8_t *t_message, uint8_t address)
};

#ifdef GOBELPC200_DEBUG
BSC_LOGI(TAG, "parseMessage: serialDev=%i", u8_mDevNr + address);
BSC_LOGI(TAG, "parseMessage: serialDev=%i", address);
#endif

uint8_t u8_lNumOfCells = 0;
Expand Down Expand Up @@ -394,7 +378,7 @@ Byte | Data
for (uint8_t i = 0; i < u8_lNumOfCells; i++)
{
u16_lZellVoltage = get16bitFromMsg(u8_lMsgoffset);
setBmsCellVoltage(BT_DEVICES_COUNT + u8_mDevNr + address, i, (float)(u16_lZellVoltage));
setBmsCellVoltage(BT_DEVICES_COUNT + address, i, (float)(u16_lZellVoltage));
u8_lMsgoffset += 2;

u16_lCellSum += u16_lZellVoltage;
Expand All @@ -415,12 +399,12 @@ Byte | Data
u16_lZellDifferenceVoltage = u16_lCellHigh - u16_lCellLow;
}

setBmsMaxCellVoltage(BT_DEVICES_COUNT + u8_mDevNr + address, u16_lCellHigh);
setBmsMinCellVoltage(BT_DEVICES_COUNT + u8_mDevNr + address, u16_lCellLow);
setBmsMaxVoltageCellNumber(BT_DEVICES_COUNT + u8_mDevNr + address, u8_lZellNumberMaxVoltage);
setBmsMinVoltageCellNumber(BT_DEVICES_COUNT + u8_mDevNr + address, u8_lZellNumberMinVoltage);
setBmsAvgVoltage(BT_DEVICES_COUNT + u8_mDevNr + address, (float)(u16_lCellSum / u8_lNumOfCells));
setBmsMaxCellDifferenceVoltage(BT_DEVICES_COUNT + u8_mDevNr + address, (float)(u16_lZellDifferenceVoltage));
setBmsMaxCellVoltage(BT_DEVICES_COUNT + address, u16_lCellHigh);
setBmsMinCellVoltage(BT_DEVICES_COUNT + address, u16_lCellLow);
setBmsMaxVoltageCellNumber(BT_DEVICES_COUNT + address, u8_lZellNumberMaxVoltage);
setBmsMinVoltageCellNumber(BT_DEVICES_COUNT + address, u8_lZellNumberMinVoltage);
setBmsAvgVoltage(BT_DEVICES_COUNT + address, (float)(u16_lCellSum / u8_lNumOfCells));
setBmsMaxCellDifferenceVoltage(BT_DEVICES_COUNT + address, (float)(u16_lZellDifferenceVoltage));

// 41 | 30 36(temperature number N,06H,has 6 temperatures
uint8_t u8_lCntTempSensors = convertAsciiHexToByte(t_message, u8_lMsgoffset++);
Expand All @@ -440,19 +424,19 @@ Byte | Data
fl_lBmsTemps[2] = (float)(get16bitFromMsg(u8_lMsgoffset) - 0xAAB) * 0.1;
u8_lMsgoffset += 2;
if (i < 3)
setBmsTempature(BT_DEVICES_COUNT + u8_mDevNr + address, i, fl_lBmsTemps[2]);
setBmsTempature(BT_DEVICES_COUNT + address, i, fl_lBmsTemps[2]);
else if (i >= 3 && i < 5)
fl_lBmsTemps[i - 3] = fl_lBmsTemps[2];
}

// 54 | 30 30 30 30(PACK current,0000H,unit:10mA,range: -327.68A-+327.67A)
float f_lTotalCurrent = (float)((int16_t)get16bitFromMsg(u8_lMsgoffset)) * 0.01f;
setBmsTotalCurrent(BT_DEVICES_COUNT + u8_mDevNr + address, f_lTotalCurrent);
setBmsTotalCurrent(BT_DEVICES_COUNT + address, f_lTotalCurrent);
u8_lMsgoffset += 2;

// 56 | 44 31 35 35(PACK total voltage,D155H , that’s 53.589V)
float f_lTotalVoltage = (float)get16bitFromMsg(u8_lMsgoffset) * 0.001f;
setBmsTotalVoltage(BT_DEVICES_COUNT + u8_mDevNr + address, f_lTotalVoltage);
setBmsTotalVoltage(BT_DEVICES_COUNT + address, f_lTotalVoltage);
u8_lMsgoffset += 2;

// 58 | 31 32 38 45(PACK remain capacity,128EH, that’s 47.50AH)
Expand All @@ -464,7 +448,7 @@ Byte | Data
uint16_t u16_lFullCapacity = get16bitFromMsg(u8_lMsgoffset) / 100;
u8_lMsgoffset += 2;

setBmsChargePercentage(BT_DEVICES_COUNT + u8_mDevNr + address, (u16_lBalanceCapacity / u16_lFullCapacity) / 100);
setBmsChargePercentage(BT_DEVICES_COUNT + address, (u16_lBalanceCapacity / u16_lFullCapacity) / 100);

// 63 | 30 30 30 30(cycle times,0000H)
uint16_t u16_lCycle = get16bitFromMsg(u8_lMsgoffset);
Expand All @@ -475,21 +459,21 @@ Byte | Data
if (mDevData->bo_sendMqttMsg)
{
// Nachrichten senden
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + u8_mDevNr + address, MQTT_TOPIC2_TEMPERATURE, 3, fl_lBmsTemps[0]);
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + u8_mDevNr + address, MQTT_TOPIC2_TEMPERATURE, 4, fl_lBmsTemps[1]);
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + u8_mDevNr + address, MQTT_TOPIC2_TEMPERATURE, 5, fl_lBmsTemps[2]);
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + address, MQTT_TOPIC2_TEMPERATURE, 3, fl_lBmsTemps[0]);
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + address, MQTT_TOPIC2_TEMPERATURE, 4, fl_lBmsTemps[1]);
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + address, MQTT_TOPIC2_TEMPERATURE, 5, fl_lBmsTemps[2]);

mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + u8_mDevNr + address, MQTT_TOPIC2_BALANCE_CAPACITY, -1, u16_lBalanceCapacity);
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + u8_mDevNr + address, MQTT_TOPIC2_FULL_CAPACITY, -1, u16_lFullCapacity);
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + u8_mDevNr + address, MQTT_TOPIC2_CYCLE, -1, u16_lCycle);
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + address, MQTT_TOPIC2_BALANCE_CAPACITY, -1, u16_lBalanceCapacity);
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + address, MQTT_TOPIC2_FULL_CAPACITY, -1, u16_lFullCapacity);
mqttPublish(MQTT_TOPIC_BMS_BT, BT_DEVICES_COUNT + address, MQTT_TOPIC2_CYCLE, -1, u16_lCycle);

}
}

static void parseMessage_Alarms(uint8_t *t_message, uint8_t address)
{
#ifdef GOBELPC200_DEBUG
BSC_LOGI(TAG, "parseMessage: serialDev=%i", u8_mDevNr + address);
BSC_LOGI(TAG, "parseMessage: serialDev=%i", address);
#endif
/*
Expand Down Expand Up @@ -704,7 +688,7 @@ Char A.25 Warn state2 explanation
if (warnstate2 & (1<<3))
u32_alarm |= BMS_ERR_STATUS_DSG_UTP;

setBmsErrors(BT_DEVICES_COUNT + u8_mDevNr + address, u32_alarm);
setBmsErrors(BT_DEVICES_COUNT + address, u32_alarm);
}

uint8_t convertAsciiHexToByte(char a, char b)
Expand Down

0 comments on commit 340899d

Please sign in to comment.