* added Radio::loop() virtual function

* RadioLibWrapper:  new isChannelActive() based on current RSSI being above noise_floor + THRESHOLD
This commit is contained in:
Scott Powell
2025-05-24 21:24:44 +10:00
parent 2f8d9cf96a
commit f2243b78ae
10 changed files with 60 additions and 9 deletions

View File

@@ -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) {

View File

@@ -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;
/**

View File

@@ -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(); }

View File

@@ -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();

View File

@@ -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(); }

View File

@@ -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(); }

View File

@@ -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(); }

View File

@@ -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(); }

View File

@@ -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 {

View File

@@ -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; }