Skip to content

Commit

Permalink
0.8.28
Browse files Browse the repository at this point in the history
* add get loss rate @rejoe2
* improve communication @rejoe2
  • Loading branch information
lumapu committed Dec 23, 2023
1 parent b3ceada commit 5a52677
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## 0.8.28 - 2023-12-23
* fix bug heuristic
* add version information to clipboard once 'copy' was clicked
* add get loss rate @rejoe2
* improve communication @rejoe2

## 0.8.27 - 2023-12-18
* fix set power limit #1276
Expand Down
4 changes: 2 additions & 2 deletions src/hm/CommQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ class CommQueue {
mQueue[mRdPtr].attempts--;
}

void incrAttempt(void) {
mQueue[mRdPtr].attempts++;
void incrAttempt(uint8_t attempts = 1) {
mQueue[mRdPtr].attempts += attempts;
}

void inc(uint8_t *ptr) {
Expand Down
65 changes: 56 additions & 9 deletions src/hm/Communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#define MI_TIMEOUT 250 // timeout for MI type requests
#define FRSTMSG_TIMEOUT 150 // how long to wait for first msg to be received
#define DEFAULT_TIMEOUT 500 // timeout for regular requests
#define SINGLEFR_TIMEOUT 65 // timeout for single frame requests
#define SINGLEFR_TIMEOUT 100 // timeout for single frame requests
#define MAX_BUFFER 250

typedef std::function<void(uint8_t, Inverter<> *)> payloadListenerType;
Expand Down Expand Up @@ -59,7 +59,7 @@ class Communication : public CommQueue<> {
mLastEmptyQueueMillis = millis();
mPrintSequenceDuration = true;

uint16_t timeout = (q->iv->ivGen == IV_MI) ? MI_TIMEOUT : ((q->iv->mGotFragment && q->iv->mGotLastMsg) || mIsRetransmit) ? SINGLEFR_TIMEOUT : DEFAULT_TIMEOUT;
uint16_t timeout = (q->iv->ivGen == IV_MI) ? MI_TIMEOUT : (((q->iv->mGotFragment && q->iv->mGotLastMsg) || mIsRetransmit) ? SINGLEFR_TIMEOUT : ((q->cmd != AlarmData) ? DEFAULT_TIMEOUT : (1.5 * DEFAULT_TIMEOUT)));
uint16_t timeout_min = (q->iv->ivGen == IV_MI) ? MI_TIMEOUT : ((q->iv->mGotFragment || mIsRetransmit)) ? SINGLEFR_TIMEOUT : FRSTMSG_TIMEOUT;

/*if(mDebugState != mState) {
Expand All @@ -85,6 +85,8 @@ class Communication : public CommQueue<> {
mIsRetransmit = false;
if(NULL == q->iv->radio)
cmdDone(false); // can't communicate while radio is not defined!
q->iv->mCmd = q->cmd;
q->iv->mIsSingleframeReq = false;
mState = States::START;
break;

Expand Down Expand Up @@ -159,15 +161,17 @@ class Communication : public CommQueue<> {
closeRequest(q, false);
break;
}
mIsRetransmit = false;
mFirstTry = false; // for correct reset
if((IV_MI != q->iv->ivGen) || (0 == q->attempts))
mIsRetransmit = false;

while(!q->iv->radio->mBufCtrl.empty()) {
packet_t *p = &q->iv->radio->mBufCtrl.front();
printRxInfo(q, p);

if(validateIvSerial(&p->packet[1], q->iv)) {
q->iv->radioStatistics.frmCnt++;
q->iv->mDtuRxCnt++;

if (p->packet[0] == (TX_REQ_INFO + ALL_FRAMES)) { // response from get information command
if(parseFrame(p))
Expand Down Expand Up @@ -197,15 +201,25 @@ class Communication : public CommQueue<> {
if(q->iv->ivGen != IV_MI) {
mState = States::CHECK_PACKAGE;
} else {
bool fastNext = true;
if(q->iv->miMultiParts < 6) {
mState = States::WAIT;
if((millis() > mWaitTimeout && mIsRetransmit) || !mIsRetransmit) {
miRepeatRequest(q);
return;
}
} else {
mHeu.evalTxChQuality(q->iv, true, (4 - q->attempts), q->iv->curFrmCnt);
if(((q->cmd == 0x39) && (q->iv->type == INV_TYPE_4CH))
|| ((q->cmd == MI_REQ_CH2) && (q->iv->type == INV_TYPE_2CH))
|| ((q->cmd == MI_REQ_CH1) && (q->iv->type == INV_TYPE_1CH))) {
miComplete(q->iv);
fastNext = false;
}
closeRequest(q, true);
if(fastNext)
miNextRequest(q->iv->type == INV_TYPE_4CH ? MI_REQ_4CH : MI_REQ_CH1, q);
else
closeRequest(q, true);
}

}
Expand Down Expand Up @@ -247,6 +261,8 @@ class Communication : public CommQueue<> {
DBGPRINT(String(q->attempts));
DBGPRINTLN(F(" attempts left)"));
}
if (!mIsRetransmit)
q->iv->mIsSingleframeReq = true;
sendRetransmit(q, (framnr-1));
mIsRetransmit = true;
mlastTO_min = timeout_min;
Expand Down Expand Up @@ -290,7 +306,9 @@ class Communication : public CommQueue<> {
else
ah::dumpBuf(p->packet, p->len);
} else {
DBGPRINT(F("| "));
DBGPRINT(F("| 0x"));
DHEX(p->packet[0]);
DBGPRINT(F(" "));
DBGHEXLN(p->packet[9]);
}
}
Expand Down Expand Up @@ -331,8 +349,11 @@ class Communication : public CommQueue<> {
return false; // CRC8 is wrong, frame invalid
}

if((*frameId & ALL_FRAMES) == ALL_FRAMES)
if((*frameId & ALL_FRAMES) == ALL_FRAMES) {
mMaxFrameId = (*frameId & 0x7f);
if(mMaxFrameId > 8) // large payloads, e.g. AlarmData
incrAttempt(mMaxFrameId - 6);
}

frame_t *f = &mLocalBuf[(*frameId & 0x7f) - 1];
memcpy(f->buf, &p->packet[10], p->len-11);
Expand Down Expand Up @@ -373,7 +394,7 @@ class Communication : public CommQueue<> {
accepted = false;

DPRINT_IVID(DBG_INFO, q->iv->id);
DBGPRINT(F(" has "));
DBGPRINT(F("has "));
if(!accepted) DBGPRINT(F("not "));
DBGPRINT(F("accepted power limit set point "));
DBGPRINT(String(q->iv->powerLimit[0]));
Expand Down Expand Up @@ -446,8 +467,14 @@ class Communication : public CommQueue<> {

record_t<> *rec = q->iv->getRecordStruct(q->cmd);
if(NULL == rec) {
DPRINTLN(DBG_ERROR, F("record is NULL!"));
closeRequest(q, false);
if(GetLossRate == q->cmd) {
q->iv->parseGetLossRate(mPayload, len);
//closeRequest(q, true); //@lumapu: Activating would crash most esp's!
return;
} else {
DPRINTLN(DBG_ERROR, F("record is NULL!"));
closeRequest(q, false);
}
return;
}
if((rec->pyldLen != len) && (0 != rec->pyldLen)) {
Expand Down Expand Up @@ -688,6 +715,7 @@ class Communication : public CommQueue<> {
miStsConsolidate(q, datachan, rec, p->packet[23], p->packet[24]);

if (p->packet[0] < (0x39 + ALL_FRAMES) ) {
mHeu.evalTxChQuality(q->iv, true, (4 - q->attempts), 1);
miNextRequest((p->packet[0] - ALL_FRAMES + 1), q);
} else {
q->iv->miMultiParts = 7; // indicate we are ready
Expand All @@ -696,6 +724,7 @@ class Communication : public CommQueue<> {
} else if((p->packet[0] == (MI_REQ_CH1 + ALL_FRAMES)) && (q->iv->type == INV_TYPE_2CH)) {
//addImportant(q->iv, MI_REQ_CH2);
miNextRequest(MI_REQ_CH2, q);
mHeu.evalTxChQuality(q->iv, true, (4 - q->attempts), q->iv->curFrmCnt);
//use also miMultiParts here for better statistics?
//mHeu.setGotFragment(q->iv);
} else { // first data msg for 1ch, 2nd for 2ch
Expand Down Expand Up @@ -732,6 +761,24 @@ class Communication : public CommQueue<> {
//mState = States::WAIT;
}

void miRepeatRequest(const queue_s *q) {
setAttempt(); // if function is called, we got something, and we necessarily need more transmissions for MI types...
if(*mSerialDebug) {
DPRINT_IVID(DBG_WARN, q->iv->id);
DBGPRINT(F("resend request ("));
DBGPRINT(String(q->attempts));
DBGPRINT(F(" attempts left): 0x"));
DBGHEXLN(q->cmd);
}

q->iv->radio->sendCmdPacket(q->iv, q->cmd, 0x00, true);

mWaitTimeout = millis() + MI_TIMEOUT;
mWaitTimeout_min = mWaitTimeout;
//mState = States::WAIT;
mIsRetransmit = false;
}

void miStsConsolidate(const queue_s *q, uint8_t stschan, record_t<> *rec, uint8_t uState, uint8_t uEnum, uint8_t lState = 0, uint8_t lEnum = 0) {
//uint8_t status = (p->packet[11] << 8) + p->packet[12];
uint16_t statusMi = 3; // regular status for MI, change to 1 later?
Expand Down
2 changes: 2 additions & 0 deletions src/hm/hmDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ const byteAssign_t AlarmDataAssignment[] = {
#define HMALARMDATA_PAYLOAD_LEN 0 // 0: means check is off
#define ALARM_LOG_ENTRY_SIZE 12

#define HMGETLOSSRATE_PAYLOAD_LEN 4
#define AHOY_GET_LOSS_INTERVAL 10

//-------------------------------------
// HM300, HM350, HM400
Expand Down
42 changes: 42 additions & 0 deletions src/hm/hmInverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,20 @@ class Inverter {
bool mGotFragment; // shows if inverter has sent at least one fragment
uint8_t curFrmCnt; // count received frames in current loop
bool mGotLastMsg; // shows if inverter has already finished transmission cycle
uint8_t mCmd; // holds the command to send
bool mIsSingleframeReq; // indicates this is a missing single frame request
Radio *radio; // pointer to associated radio class
statistics_t radioStatistics; // information about transmitted, failed, ... packets
HeuristicInv heuristics; // heuristic information / logic
uint8_t curCmtFreq; // current used CMT frequency, used to check if freq. was changed during runtime
bool commEnabled; // 'pause night communication' sets this field to false

uint16_t mIvRxCnt; // last iv rx frames (from GetLossRate)
uint16_t mIvTxCnt; // last iv tx frames (from GetLossRate)
uint16_t mDtuRxCnt; // cur dtu rx frames (since last GetLossRate)
uint16_t mDtuTxCnt; // cur dtu tx frames (since last getLoassRate)
uint8_t mGetLossInterval; // request iv every AHOY_GET_LOSS_INTERVAL RealTimeRunData_Debu

static uint32_t *timestamp; // system timestamp
static cfgInst_t *generalConfig; // general inverter configuration from setup

Expand All @@ -159,8 +167,14 @@ class Inverter {
rssi = -127;
miMultiParts = 0;
mGotLastMsg = false;
mCmd = InitDataState;
mIsSingleframeReq = false;
radio = NULL;
commEnabled = true;
mIvRxCnt = 0;
mIvTxCnt = 0;
mDtuRxCnt = 0;
mDtuTxCnt = 0;

memset(&radioStatistics, 0, sizeof(statistics_t));
memset(heuristics.txRfQuality, -6, 5);
Expand All @@ -174,6 +188,7 @@ class Inverter {
cb(devControlCmd, true);
mDevControlRequest = false;
} else if (IV_MI != ivGen) {
mGetLossInterval++;
if((alarmLastId != alarmMesIndex) && (alarmMesIndex != 0))
cb(AlarmData, false); // get last alarms
else if(0 == getFwVersion())
Expand All @@ -185,8 +200,12 @@ class Inverter {
else if(InitDataState != devControlCmd) {
cb(devControlCmd, false); // custom command which was received by API
devControlCmd = InitDataState;
mGetLossInterval = 1;
} else if((0 == mGridLen) && generalConfig->readGrid) { // read grid profile
cb(GridOnProFilePara, false);
} else if (mGetLossInterval > AHOY_GET_LOSS_INTERVAL) { // get loss rate
mGetLossInterval = 1;
cb(GetLossRate, false);
} else
cb(RealTimeRunData_Debug, false); // get live data
} else {
Expand Down Expand Up @@ -574,6 +593,29 @@ class Inverter {
memset(mLastYD, 0, sizeof(float) * 6);
}

bool parseGetLossRate(uint8_t pyld[], uint8_t len) {
if (len == HMGETLOSSRATE_PAYLOAD_LEN) {
uint16_t rxCnt = (pyld[0] << 8) + pyld[1];
uint16_t txCnt = (pyld[2] << 8) + pyld[3];

if (mIvRxCnt || mIvTxCnt) { // there was successful GetLossRate in the past
DPRINT_IVID(DBG_INFO, id);
DBGPRINTLN("Inv loss: " +
String (mDtuTxCnt - (rxCnt - mIvRxCnt)) + " of " +
String (mDtuTxCnt) + ", DTU loss: " +
String (txCnt - mIvTxCnt - mDtuRxCnt) + " of " +
String (txCnt - mIvTxCnt));
}

mIvRxCnt = rxCnt;
mIvTxCnt = txCnt;
mDtuRxCnt = 0; // start new interval
mDtuTxCnt = 0; // start new interval
return true;
}
return false;
}

uint16_t parseAlarmLog(uint8_t id, uint8_t pyld[], uint8_t len) {
uint8_t startOff = 2 + id * ALARM_LOG_ENTRY_SIZE;
if((startOff + ALARM_LOG_ENTRY_SIZE) > len)
Expand Down
17 changes: 13 additions & 4 deletions src/hm/hmRadio.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ class HmRadio : public Radio {
if(NULL == mLastIv) // prevent reading on NULL object!
return;

uint32_t startMicros = micros();
uint32_t loopMillis = millis();
while ((millis() - loopMillis) < 400) {
uint32_t startMicros = micros();
uint32_t loopMillis = millis();
uint32_t outerLoopTimeout = (mLastIv->mIsSingleframeReq) ? 100 : ((mLastIv->mCmd != AlarmData) ? 400 : 600);

while ((millis() - loopMillis) < outerLoopTimeout) {
while ((micros() - startMicros) < 5110) { // listen (4088us or?) 5110us to each channel
if (mIrqRcvd) {
mIrqRcvd = false;
Expand Down Expand Up @@ -304,8 +306,14 @@ class HmRadio : public Radio {
ah::dumpBuf(mTxBuf, len, 1, 4);
else
ah::dumpBuf(mTxBuf, len);
} else
} else {
DBGPRINT(F("0x"));
DHEX(mTxBuf[0]);
DBGPRINT(F(" 0x"));
DHEX(mTxBuf[10]);
DBGPRINT(F(" "));
DBGHEXLN(mTxBuf[9]);
}
}

mNrf24->stopListening();
Expand All @@ -315,6 +323,7 @@ class HmRadio : public Radio {
mMillis = millis();

mLastIv = iv;
iv->mDtuTxCnt++;
}

uint64_t getIvId(Inverter<> *iv) {
Expand Down
8 changes: 7 additions & 1 deletion src/hms/hmsRadio.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,13 @@ class CmtRadio : public Radio {
ah::dumpBuf(mTxBuf, len, 1, 4);
else
ah::dumpBuf(mTxBuf, len);
} else
} else {
DBGPRINT(F("0x"));
DHEX(mTxBuf[0]);
DBGPRINT(F(" 0x"));
DHEX(mTxBuf[10]);
DBGHEXLN(mTxBuf[9]);
}
}

uint8_t status = mCmt.tx(mTxBuf, len);
Expand All @@ -107,6 +112,7 @@ class CmtRadio : public Radio {
if(CMT_ERR_RX_IN_FIFO == status)
mIrqRcvd = true;
}
iv->mDtuTxCnt++;
}

uint64_t getIvId(Inverter<> *iv) {
Expand Down
5 changes: 5 additions & 0 deletions src/publisher/pubMqttIvData.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class PubMqttIvData {

void stateSend() {
record_t<> *rec = mIv->getRecordStruct(mCmd);
if(rec == NULL) {
if (mCmd != GetLossRate)
DPRINT(DBG_WARN, "unknown record to publish!");
return;
}
uint32_t lastTs = mIv->getLastTs(rec);
bool pubData = (lastTs > 0);
if (mCmd == RealTimeRunData_Debug)
Expand Down
2 changes: 2 additions & 0 deletions src/web/html/serial.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
exeOnce = false;
setTimeOffset();
}
version = obj.version;
build = obj.build;
}

function setTimeOffset() {
Expand Down

0 comments on commit 5a52677

Please sign in to comment.