RX power saving using rx duty cycle

This commit is contained in:
Jarosław Domański committed 2026-08-14 16:11:21 +02:00
1 parent d929643524
commit 285cf10a7a
17 files changed
+1436 -40

No files matched your search

+36 -1
View File
@@ -2,6 +2,7 @@
#include <Arduino.h> // needed for PlatformIO
#include <Mesh.h>
#include "helpers/radiolib/RXPowerSaving.h"
#define CMD_APP_START 1
#define CMD_SEND_TXT_MSG 2
@@ -265,6 +266,33 @@ bool MyMesh::getCADEnabled() const {
return false; // hardware CAD before TX (disabled by default, until configurable)
}
static void applyCompanionRxPowerSaving(uint8_t sf, float bw) {
#ifdef WRAPPER_CLASS
RxPowerSavingControl* control = &radio_driver;
// setRxPowerSaving() rejects out-of-range periods without touching the
// wrapper's state, so on any failure we must explicitly stand the duty cycle
// down. Otherwise it would stay armed with the *previous* SF/BW timings -
// e.g. SF5/BW500 yields rx=655us (below the 1ms minimum), and the radio would
// keep sleeping in windows sized for SF11, missing every preamble.
uint32_t rx_us = 0;
uint32_t sleep_us = 0;
bool ok = calcRxPowerSavingLevel(RX_POWERSAVING_BALANCED_LEVEL, sf, bw,
RX_POWERSAVING_PROFILE_PREAMBLE, &rx_us, &sleep_us) &&
control->setRxPowerSaving(true, rx_us, sleep_us);
if (!ok) {
control->setRxPowerSaving(false, RX_POWERSAVING_DEFAULT_RX_US,
RX_POWERSAVING_DEFAULT_SLEEP_US);
}
MESH_DEBUG_PRINTLN("RX Power Saving: companion level=5,preamble=16,rx=%lu,sleep=%lu,%s",
(unsigned long)rx_us, (unsigned long)sleep_us,
ok ? "accepted" : "unavailable - continuous RX");
#else
(void)sf;
(void)bw;
#endif
}
int MyMesh::calcRxDelay(float score, uint32_t air_time) const {
if (_prefs.rx_delay_base <= 0.0f) return 0;
return (int)((pow(_prefs.rx_delay_base, 0.85f - score) - 1.0) * air_time);
@@ -980,6 +1008,7 @@ void MyMesh::begin(bool has_display) {
board.setLoRaFemPaGainEnabled(_prefs.radio_fem_txgain);
MESH_DEBUG_PRINTLN("RX Boosted Gain Mode: %s",
radio_driver.getRxBoostedGainMode() ? "Enabled" : "Disabled");
applyCompanionRxPowerSaving(_prefs.sf, _prefs.bw);
}
const char *MyMesh::getNodeName() {
@@ -1403,6 +1432,7 @@ void MyMesh::handleCmdFrame(size_t len) {
savePrefs();
radio_driver.setParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
applyCompanionRxPowerSaving(_prefs.sf, _prefs.bw);
MESH_DEBUG_PRINTLN("OK: CMD_SET_RADIO_PARAMS: f=%d, bw=%d, sf=%d, cr=%d", freq, bw, (uint32_t)sf,
(uint32_t)cr);
@@ -2263,5 +2293,10 @@ bool MyMesh::advert() {
// To check if there is pending work
bool MyMesh::hasPendingWork() const {
return _mgr->getOutboundTotal() > 0 || dirty_contacts_expiry != 0;
bool calibration_active = false;
#ifdef WRAPPER_CLASS
const RxPowerSavingControl* rxps_control = &radio_driver;
calibration_active = rxps_control->isRxPowerSavingCalibrationActive();
#endif
return _mgr->getOutboundTotal() > 0 || dirty_contacts_expiry != 0 || calibration_active;
}
+29 -1
View File
@@ -1,5 +1,28 @@
#include "MyMesh.h"
#include <algorithm>
#include "helpers/radiolib/RXPowerSaving.h"
static RxPowerSavingControl* getRxPowerSavingControl() {
#ifdef WRAPPER_CLASS
return &radio_driver;
#else
return nullptr;
#endif
}
static void applyRxPowerSavingConfig(NodePrefs& prefs, uint8_t sf, float bw) {
normalizeRxPowerSavingConfig(&prefs.rxps, sf, bw);
RxPowerSavingControl* control = getRxPowerSavingControl();
bool ok = control != nullptr
? control->setRxPowerSaving(
prefs.rxps.enabled != 0, prefs.rxps.rx_us, prefs.rxps.sleep_us)
: prefs.rxps.enabled == 0;
MESH_DEBUG_PRINTLN("RX Power Saving: desired=%s, rx=%lu, sleep=%lu, %s",
prefs.rxps.enabled ? "on" : "off",
(unsigned long)prefs.rxps.rx_us,
(unsigned long)prefs.rxps.sleep_us,
ok ? "accepted" : "unsupported");
}
/* ------------------------------ Config -------------------------------- */
@@ -862,7 +885,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
mesh::RTCClock &rtc, mesh::MeshTables &tables)
: mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables),
region_map(key_store), temp_map(key_store),
_cli(board, rtc, sensors, region_map, acl, &_prefs, this),
_cli(board, rtc, sensors, region_map, acl, &_prefs, this, getRxPowerSavingControl()),
telemetry(MAX_PACKET_PAYLOAD - 4),
discover_limiter(4, 120), // max 4 every 2 minutes
anon_limiter(4, 180) // max 4 every 3 minutes
@@ -983,6 +1006,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
radio_driver.getRxBoostedGainMode() ? "Enabled" : "Disabled");
board.setLoRaFemLnaEnabled(_prefs.radio_fem_rxgain);
board.setLoRaFemPaGainEnabled(_prefs.radio_fem_txgain);
applyRxPowerSavingConfig(_prefs, _prefs.sf, _prefs.bw);
updateAdvertTimer();
updateFloodAdvertTimer();
@@ -1308,12 +1332,14 @@ void MyMesh::loop() {
if (set_radio_at && millisHasNowPassed(set_radio_at)) { // apply pending (temporary) radio params
set_radio_at = 0; // clear timer
radio_driver.setParams(pending_freq, pending_bw, pending_sf, pending_cr);
applyRxPowerSavingConfig(_prefs, pending_sf, pending_bw);
MESH_DEBUG_PRINTLN("Temp radio params");
}
if (revert_radio_at && millisHasNowPassed(revert_radio_at)) { // revert radio params to orig
revert_radio_at = 0; // clear timer
radio_driver.setParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
applyRxPowerSavingConfig(_prefs, _prefs.sf, _prefs.bw);
MESH_DEBUG_PRINTLN("Radio params restored");
}
@@ -1334,5 +1360,7 @@ bool MyMesh::hasPendingWork() const {
#if defined(WITH_BRIDGE)
if (bridge.isRunning()) return true; // bridge needs WiFi radio, can't sleep
#endif
const RxPowerSavingControl* control = getRxPowerSavingControl();
if (control != nullptr && control->isRxPowerSavingCalibrationActive()) return true;
return _mgr->getOutboundTotal() > 0;
}