From f2243b78ae8467e271491cbd9109592afa759714 Mon Sep 17 00:00:00 2001 From: Scott Powell Date: Sat, 24 May 2025 21:24:44 +1000 Subject: [PATCH] * added Radio::loop() virtual function * RadioLibWrapper: new isChannelActive() based on current RSSI being above noise_floor + THRESHOLD --- src/Dispatcher.cpp | 2 ++ src/Dispatcher.h | 5 ++++ src/helpers/CustomLLCC68Wrapper.h | 3 +++ src/helpers/CustomLR1110Wrapper.h | 5 ++++ src/helpers/CustomSTM32WLxWrapper.h | 3 +++ src/helpers/CustomSX1262Wrapper.h | 3 +++ src/helpers/CustomSX1268Wrapper.h | 3 +++ src/helpers/CustomSX1276Wrapper.h | 3 +++ src/helpers/RadioLibWrappers.cpp | 36 +++++++++++++++++++++-------- src/helpers/RadioLibWrappers.h | 6 +++++ 10 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/Dispatcher.cpp b/src/Dispatcher.cpp index 3d5b04fc..6412b6a9 100644 --- a/src/Dispatcher.cpp +++ b/src/Dispatcher.cpp @@ -36,6 +36,8 @@ uint32_t Dispatcher::getCADFailMaxDuration() const { } void Dispatcher::loop() { + _radio->loop(); + // check for radio 'stuck' in mode other than Rx bool is_recv = _radio->isInRecvMode(); if (is_recv != prev_isrecv_mode) { diff --git a/src/Dispatcher.h b/src/Dispatcher.h index d03c9f73..7a48067d 100644 --- a/src/Dispatcher.h +++ b/src/Dispatcher.h @@ -56,6 +56,11 @@ public: */ virtual void onSendFinished() = 0; + /** + * \brief do any processing needed on each loop cycle + */ + virtual void loop() { } + virtual bool isInRecvMode() const = 0; /** diff --git a/src/helpers/CustomLLCC68Wrapper.h b/src/helpers/CustomLLCC68Wrapper.h index d547880f..f7dd7a9f 100644 --- a/src/helpers/CustomLLCC68Wrapper.h +++ b/src/helpers/CustomLLCC68Wrapper.h @@ -9,6 +9,9 @@ public: bool isReceivingPacket() override { return ((CustomLLCC68 *)_radio)->isReceiving(); } + float getCurrentRSSI() override { + return ((CustomLLCC68 *)_radio)->getRSSI(false); + } float getLastRSSI() const override { return ((CustomLLCC68 *)_radio)->getRSSI(); } float getLastSNR() const override { return ((CustomLLCC68 *)_radio)->getSNR(); } diff --git a/src/helpers/CustomLR1110Wrapper.h b/src/helpers/CustomLR1110Wrapper.h index c02052a1..7e2ffa2d 100644 --- a/src/helpers/CustomLR1110Wrapper.h +++ b/src/helpers/CustomLR1110Wrapper.h @@ -9,6 +9,11 @@ public: bool isReceivingPacket() override { return ((CustomLR1110 *)_radio)->isReceiving(); } + float getCurrentRSSI() override { + float rssi = -110; + ((CustomLR1110 *)_radio)->getRssiInst(&rssi); + return rssi; + } void onSendFinished() override { RadioLibWrapper::onSendFinished(); diff --git a/src/helpers/CustomSTM32WLxWrapper.h b/src/helpers/CustomSTM32WLxWrapper.h index 491d648e..9e2d0441 100644 --- a/src/helpers/CustomSTM32WLxWrapper.h +++ b/src/helpers/CustomSTM32WLxWrapper.h @@ -10,6 +10,9 @@ public: bool isReceivingPacket() override { return ((CustomSTM32WLx *)_radio)->isReceiving(); } + float getCurrentRSSI() override { + return ((CustomSTM32WLx *)_radio)->getRSSI(false); + } float getLastRSSI() const override { return ((CustomSTM32WLx *)_radio)->getRSSI(); } float getLastSNR() const override { return ((CustomSTM32WLx *)_radio)->getSNR(); } diff --git a/src/helpers/CustomSX1262Wrapper.h b/src/helpers/CustomSX1262Wrapper.h index 3aee2966..119f6dce 100644 --- a/src/helpers/CustomSX1262Wrapper.h +++ b/src/helpers/CustomSX1262Wrapper.h @@ -9,6 +9,9 @@ public: bool isReceivingPacket() override { return ((CustomSX1262 *)_radio)->isReceiving(); } + float getCurrentRSSI() override { + return ((CustomSX1262 *)_radio)->getRSSI(false); + } float getLastRSSI() const override { return ((CustomSX1262 *)_radio)->getRSSI(); } float getLastSNR() const override { return ((CustomSX1262 *)_radio)->getSNR(); } diff --git a/src/helpers/CustomSX1268Wrapper.h b/src/helpers/CustomSX1268Wrapper.h index 5ee8e6a2..5d7106b4 100644 --- a/src/helpers/CustomSX1268Wrapper.h +++ b/src/helpers/CustomSX1268Wrapper.h @@ -9,6 +9,9 @@ public: bool isReceivingPacket() override { return ((CustomSX1268 *)_radio)->isReceiving(); } + float getCurrentRSSI() override { + return ((CustomSX1268 *)_radio)->getRSSI(false); + } float getLastRSSI() const override { return ((CustomSX1268 *)_radio)->getRSSI(); } float getLastSNR() const override { return ((CustomSX1268 *)_radio)->getSNR(); } diff --git a/src/helpers/CustomSX1276Wrapper.h b/src/helpers/CustomSX1276Wrapper.h index 26e925a9..28257990 100644 --- a/src/helpers/CustomSX1276Wrapper.h +++ b/src/helpers/CustomSX1276Wrapper.h @@ -9,6 +9,9 @@ public: bool isReceivingPacket() override { return ((CustomSX1276 *)_radio)->isReceiving(); } + float getCurrentRSSI() override { + return ((CustomSX1276 *)_radio)->getRSSI(false); + } float getLastRSSI() const override { return ((CustomSX1276 *)_radio)->getRSSI(); } float getLastSNR() const override { return ((CustomSX1276 *)_radio)->getSNR(); } diff --git a/src/helpers/RadioLibWrappers.cpp b/src/helpers/RadioLibWrappers.cpp index 358c5f42..d52d81ff 100644 --- a/src/helpers/RadioLibWrappers.cpp +++ b/src/helpers/RadioLibWrappers.cpp @@ -8,6 +8,12 @@ #define STATE_TX_DONE 4 #define STATE_INT_READY 16 +#ifndef INTERFERENCE_THRESHOLD_DB + #define INTERFERENCE_THRESHOLD_DB 14 +#endif + +#define NUM_NOISE_FLOOR_SAMPLES 64 + static volatile uint8_t state = STATE_IDLE; // this function is called when a complete packet @@ -28,6 +34,12 @@ void RadioLibWrapper::begin() { if (_board->getStartupReason() == BD_STARTUP_RX_PACKET) { // received a LoRa packet (while in deep sleep) setFlag(); // LoRa packet is already received } + + _noise_floor = -140; + + // start average out some samples + _num_floor_samples = 0; + _floor_sample_sum = 0; } void RadioLibWrapper::idle() { @@ -35,6 +47,20 @@ void RadioLibWrapper::idle() { state = STATE_IDLE; // need another startReceive() } +void RadioLibWrapper::loop() { + if (state == STATE_RX && _num_floor_samples < NUM_NOISE_FLOOR_SAMPLES) { + if (!isReceivingPacket()) { + _num_floor_samples++; + _floor_sample_sum += getCurrentRSSI(); + } + } else if (_floor_sample_sum != 0) { + _noise_floor = _floor_sample_sum / NUM_NOISE_FLOOR_SAMPLES; + _floor_sample_sum = 0; + + MESH_DEBUG_PRINTLN("RadioLibWrapper: noise_floor = %d", (int)_noise_floor); + } +} + void RadioLibWrapper::startRecv() { int err = _radio->startReceive(); if (err == RADIOLIB_ERR_NONE) { @@ -109,15 +135,7 @@ void RadioLibWrapper::onSendFinished() { } bool RadioLibWrapper::isChannelActive() { - idle(); // put sx126x into standby - // do some basic CAD (blocks for ~12780 micros (on SF 10)!) - bool activity = _radio->scanChannel() == RADIOLIB_LORA_DETECTED; - if (activity) { - startRecv(); - } else { - idle(); - } - return activity; + return getCurrentRSSI() > _noise_floor + INTERFERENCE_THRESHOLD_DB; } float RadioLibWrapper::getLastRSSI() const { diff --git a/src/helpers/RadioLibWrappers.h b/src/helpers/RadioLibWrappers.h index 3e8b9fea..211e0187 100644 --- a/src/helpers/RadioLibWrappers.h +++ b/src/helpers/RadioLibWrappers.h @@ -8,11 +8,15 @@ protected: PhysicalLayer* _radio; mesh::MainBoard* _board; uint32_t n_recv, n_sent; + int16_t _noise_floor; + uint16_t _num_floor_samples; + int32_t _floor_sample_sum; void idle(); void startRecv(); float packetScoreInt(float snr, int sf, int packet_len); virtual bool isReceivingPacket() =0; + virtual float getCurrentRSSI() =0; public: RadioLibWrapper(PhysicalLayer& radio, mesh::MainBoard& board) : _radio(&radio), _board(&board) { n_recv = n_sent = 0; } @@ -32,6 +36,8 @@ public: return isChannelActive(); } + void loop() override; + uint32_t getPacketsRecv() const { return n_recv; } uint32_t getPacketsSent() const { return n_sent; } void resetStats() { n_recv = n_sent = 0; }