Skip to content

0.8.0805 #10

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

Draft
wants to merge 2 commits into
base: development03
Choose a base branch
from
Draft
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
101 changes: 69 additions & 32 deletions src/hm/Communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Communication : public CommQueue<> {
testMode = mHeu.getTestModeEnabled();
q->iv->mGotFragment = false;
q->iv->mGotLastMsg = false;
mFirstTry = mFirstTry ? false : ( ( (IV_HM == q->iv->ivGen) || (IV_MI == q->iv->ivGen) ) && (q->iv->isAvailable()) || (millis() < 120000) );
//mFirstTry = mFirstTry ? false : ( ( (IV_HM == q->iv->ivGen) || (IV_MI == q->iv->ivGen) ) && (q->iv->isAvailable() || millis() < 120000) );
if(NULL == q->iv->radio)
cmdDone(true); // can't communicate while radio is not defined!
mState = States::START;
Expand Down Expand Up @@ -95,52 +95,53 @@ class Communication : public CommQueue<> {

case States::WAIT:
if(millis() > mWaitTimeout_min) {
if(!q->iv->mGotFragment) { // nothing received yet?
if(q->iv->mGotFragment) { // nothing received yet?
if(q->iv->mGotLastMsg) {
//mState = States::CHECK_FRAMES;
mWaitTimeout = mWaitTimeout_min;
}
} else if(mFirstTry) {
DPRINT_IVID(DBG_INFO, q->iv->id);
DBGPRINTLN(F("second try"));
DBGPRINT(String(millis() - mWaitTimeout_min + timeout_min));
DBGPRINTLN(F("ms - second try"));
mFirstTry = false;
if(!testMode)
q->iv->radioStatistics.retransmits++; // got nothing
mState = States::START;
break;
}

}
if(millis() < mWaitTimeout)
return;
mState = States::CHECK_FRAMES;
break;

case States::CHECK_FRAMES: {
if(!q->iv->radio->get()) { // radio buffer empty
cmdDone();
if(!q->iv->radio->get() || ((q->iv->mGotFragment) && (0 == q->attempts))) { // radio buffer empty or nothing received yet
//cmdDone();
DPRINT_IVID(DBG_INFO, q->iv->id);
DBGPRINT(F("request timeout: "));
DBGPRINT(String(millis() - mWaitTimeout + timeout));
DBGPRINTLN(F("ms"));

if(!q->iv->mGotFragment) {
if(!testMode)
/*if(!testMode)
q->iv->radioStatistics.rxFailNoAnser++; // got nothing
mHeu.setGotNothing(q->iv);
mHeu.setGotNothing(q->iv);*/
if((IV_HMS == q->iv->ivGen) || (IV_HMT == q->iv->ivGen)) {
q->iv->radio->switchFrequency(q->iv, HOY_BOOT_FREQ_KHZ, (q->iv->config->frequency*FREQ_STEP_KHZ + HOY_BASE_FREQ_KHZ));
mWaitTimeout = millis() + 1000;
}
} else {
}/* else {
if(!testMode)
q->iv->radioStatistics.rxFail++;
}
mState = States::RESET;
}*/
closeRequest(q->iv,false);
//mState = States::RESET;
break;
}

mFirstTry = false; // for correct reset
//mFirstTry = false; // for correct reset
States nextState = States::RESET;
while(!q->iv->radio->mBufCtrl.empty()) {
packet_t *p = &q->iv->radio->mBufCtrl.front();
Expand Down Expand Up @@ -187,11 +188,12 @@ class Communication : public CommQueue<> {
yield();
}
if(0 == q->attempts) {
if(!testMode)
/*if(!testMode)
q->iv->radioStatistics.rxFail++; // got no complete payload
mHeu.setGotFragment(q->iv);
cmdDone(true);
mState = States::RESET;
mState = States::RESET;*/
closeRequest(q->iv,false);
} else
mState = nextState;

Expand Down Expand Up @@ -233,14 +235,15 @@ class Communication : public CommQueue<> {
return;
}

mHeu.setGotAll(q->iv);
//mHeu.setGotAll(q->iv);
compilePayload(q, testMode);

if(NULL != mCbPayload)
(mCbPayload)(q->cmd, q->iv);

cmdDone(true); // remove done request
mState = States::RESET; // everything ok, next request
//cmdDone(true); // remove done request
//mState = States::RESET; // everything ok, next request
closeRequest(q->iv);
break;
}
});
Expand Down Expand Up @@ -301,7 +304,7 @@ class Communication : public CommQueue<> {
record_t<> *rec = q->iv->getRecordStruct(RealTimeRunData_Debug); // choose the record structure
rec->ts = q->ts;
miStsConsolidate(q, ((p->packet[0] == 0x88) ? 1 : 2), rec, p->packet[10], p->packet[12], p->packet[9], p->packet[11]);
mHeu.setGotFragment(q->iv);
//mHeu.setGotFragment(q->iv);
}
}

Expand Down Expand Up @@ -421,15 +424,45 @@ class Communication : public CommQueue<> {
mWaitTimeout = millis() + SINGLEFR_TIMEOUT; // timeout
mState = States::WAIT;
} else {
mHeu.setGotFragment(q->iv);
q->iv->radioStatistics.rxFail++; // got no complete payload
//mHeu.setGotFragment(q->iv);
//q->iv->radioStatistics.rxFail++; // got no complete payload
add(q, true);
cmdDone(true);
mState = States::RESET;
//cmdDone(true);
closeRequest(q->iv, false);
//mState = States::RESET;
}
}

private:

inline void closeRequest(Inverter<> *iv, bool succeeded = true) {
bool testMode = false;
if(succeeded) {
iv->radioStatistics.rxSuccess++;
mHeu.setGotAll(iv);
cmdDone(true);
} else if(iv->mGotFragment) {
mHeu.setGotFragment(iv);
iv->radioStatistics.rxFail++; // got no complete payload
cmdDone(true);
} else {
if(!testMode)
iv->radioStatistics.rxFailNoAnser++; // got nothing
mHeu.setGotNothing(iv);
cmdDone();
}
mState = States::RESET;
iv->mGotFragment = false;
iv->mGotLastMsg = false;
if(( ( (IV_HM == iv->ivGen) || (IV_MI == iv->ivGen) ) && (iv->isAvailable() || millis() < 120000) )) {
mFirstTry = true; // for correct reset
iv->miMultiParts = 0;
} else
mFirstTry = false;
}



inline void miHwDecode(packet_t *p, const queue_s *q) {
record_t<> *rec = q->iv->getRecordStruct(InverterDevInform_All); // choose the record structure
rec->ts = q->ts;
Expand Down Expand Up @@ -541,13 +574,14 @@ class Communication : public CommQueue<> {
q->iv->miMultiParts++;
}
if(q->iv->miMultiParts > 5) {
cmdDone(true);
closeRequest(q->iv,true);
/*cmdDone(true);
mState = States::RESET;
q->iv->radioStatistics.rxSuccess++;
mHeu.setGotAll(q->iv);
q->iv->miMultiParts = 0;
q->iv->miMultiParts = 0;*/
} else {
mHeu.setGotFragment(q->iv);
//mHeu.setGotFragment(q->iv);
mState = States::WAIT;
}

Expand All @@ -566,7 +600,7 @@ class Communication : public CommQueue<> {
inline void miDataDecode(packet_t *p, const queue_s *q) {
record_t<> *rec = q->iv->getRecordStruct(RealTimeRunData_Debug); // choose the parser
rec->ts = q->ts;
q->iv->radioStatistics.rxSuccess++;
//q->iv->radioStatistics.rxSuccess++;
mState = States::RESET;

uint8_t datachan = ( p->packet[0] == (MI_REQ_CH1 + ALL_FRAMES) || p->packet[0] == (MI_REQ_4CH + ALL_FRAMES) ) ? CH1 :
Expand Down Expand Up @@ -608,15 +642,15 @@ class Communication : public CommQueue<> {
//mPayload[iv->id].retransmits = 0; // reserve retransmissions for each response
//mPayload[iv->id].complete = false;
miNextRequest((p->packet[0] - ALL_FRAMES + 1), q);
mHeu.setGotFragment(q->iv);
//mHeu.setGotFragment(q->iv);
} else {
miComplete(q->iv);
}
} 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);
//use also miMultiParts here for better statistics?
mHeu.setGotFragment(q->iv);
//mHeu.setGotFragment(q->iv);
} else { // first data msg for 1ch, 2nd for 2ch
miComplete(q->iv);
}
Expand All @@ -632,7 +666,8 @@ class Communication : public CommQueue<> {

//if(q->attempts) {
q->iv->radio->sendCmdPacket(q->iv, cmd, 0x00, true);
q->iv->radioStatistics.retransmits++;
//q->iv->radioStatistics.retransmits++;
q->iv->radioStatistics.rxSuccess++;
mWaitTimeout = millis() + MI_TIMEOUT;
chgCmd(cmd);
mState = States::WAIT;
Expand Down Expand Up @@ -744,12 +779,14 @@ class Communication : public CommQueue<> {
// update status state-machine,
if (ac_pow)
iv->isProducing();
mHeu.setGotAll(iv);
cmdDone(true);
closeRequest(iv,true);

//mHeu.setGotAll(iv);
//cmdDone(true);
if(NULL != mCbPayload)
(mCbPayload)(RealTimeRunData_Debug, iv);

mState = States::RESET; // everything ok, next request
//mState = States::RESET; // everything ok, next request
}

private:
Expand Down
6 changes: 3 additions & 3 deletions src/hm/hmRadio.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class HmRadio : public Radio {
mNrf24.begin(mSpi, ce, cs);
mNrf24.setRetries(3, 15); // 3*250us + 250us and 15 loops -> 15ms

mNrf24.openReadingPipe(1, reinterpret_cast<uint8_t*>(&DTU_RADIO_ID));
mNrf24.setChannel(mRfChLst[mRxChIdx]);
mNrf24.startListening();
mNrf24.setDataRate(RF24_250KBPS);
Expand All @@ -76,7 +77,6 @@ class HmRadio : public Radio {
mNrf24.enableDynamicPayloads();
mNrf24.setCRCLength(RF24_CRC_16);
mNrf24.setAddressWidth(5);
mNrf24.openReadingPipe(1, reinterpret_cast<uint8_t*>(&DTU_RADIO_ID));

// enable all receiving interrupts
mNrf24.maskIRQ(false, false, false);
Expand All @@ -95,10 +95,11 @@ class HmRadio : public Radio {
void loop(void) {
if (!mIrqRcvd)
return; // nothing to do
mIrqRcvd = false;
bool tx_ok, tx_fail, rx_ready;
mNrf24.whatHappened(tx_ok, tx_fail, rx_ready); // resets the IRQ pin to HIGH
mNrf24.flush_tx(); // empty TX FIFO
if(tx_ok || tx_fail) // do not reset in case of rx triggering
mIrqRcvd = false;

// start listening
//mNrf24.setChannel(23);
Expand Down Expand Up @@ -322,7 +323,6 @@ class HmRadio : public Radio {
uint8_t mRfChLst[RF_CHANNELS] = {03, 23, 40, 61, 75}; // channel List:2403, 2423, 2440, 2461, 2475MHz
uint8_t mTxChIdx = 0;
uint8_t mRxChIdx = 0;
bool mGotLastMsg = false;
uint32_t mMillis;

SPIClass* mSpi;
Expand Down