feat(gps): duty-cycle the receiver, for nodes that run GPS only for the clock

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5 committed 2026-08-06 10:05:26 +02:00
1 parent 4d96ff9e2f
commit 5486cc5439
6 files changed
+169 -5

No files matched your search

+12 -1
View File
@@ -955,13 +955,24 @@ region save
- `gps <state>`
**Parameters:**
- `state`: `on`|`off`
- `state`: `on`|`off`|`duty`
**Default:** `off`
**Note:** Output format:
- `off` when the GPS hardware is disabled
- `on, {active|deactivated}, {fix|no fix}, {sat count} sats` when the GPS hardware is enabled
- `duty, {awake|asleep}, {fix|no fix}, {sat count} sats` in duty-cycle mode
`gps duty` powers the receiver only around a clock sync and cuts it in between.
It is meant for a node that runs GPS purely to keep its (volatile) RTC set — a
mast or solar repeater — where a receiver drawing tens of mA continuously
dwarfs the rest of the power budget. Clock accuracy is unchanged: the sync
cadence matches the periodic re-sync that `gps on` already does. The position
still comes from prefs (see `gps advert`), so duty mode does not affect what a
repeater advertises. Build-time knobs: `GPS_DUTY_SYNC_INTERVAL_SECS` (1800),
`GPS_DUTY_MAX_AWAKE_SECS` (300, the cap on one attempt) and
`GPS_DUTY_RETRY_SECS` (3600, backoff after a window with no fix).
---
+2 -1
View File
@@ -168,7 +168,8 @@ public:
#if ENV_INCLUDE_GPS == 1
void applyGpsPrefs() {
sensors.setSettingValue("gps", _prefs.gps_enabled ? "1" : "0");
// 0 off / 1 on / 2 duty cycle (see EnvironmentSensorManager::gpsDutyLoop)
sensors.setSettingValue("gps", _prefs.gps_enabled == 2 ? "2" : (_prefs.gps_enabled ? "1" : "0"));
if (_prefs.gps_interval > 0) {
char interval_str[12]; // Max: 24 hours = 86400 seconds (5 digits + null)
sprintf(interval_str, "%u", _prefs.gps_interval);
+4 -1
View File
@@ -316,7 +316,10 @@ protected:
#if ENV_INCLUDE_GPS == 1
void applyGpsPrefs() {
sensors.setSettingValue("gps", _prefs.gps_enabled?"1":"0");
// 0 off / 1 on / 2 duty cycle -- pass the mode through, not a boolean, or
// a duty-cycled node comes back from a reboot with GPS powered forever.
const char* mode = _prefs.gps_enabled == 2 ? "2" : (_prefs.gps_enabled ? "1" : "0");
sensors.setSettingValue("gps", mode);
}
#endif
+20 -2
View File
@@ -117,7 +117,7 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
_prefs->powersaving_enabled = constrain(_prefs->powersaving_enabled, 0, 1);
_prefs->gps_enabled = constrain(_prefs->gps_enabled, 0, 1);
_prefs->gps_enabled = constrain(_prefs->gps_enabled, 0, 2); // 0 off, 1 on, 2 duty cycle
_prefs->advert_loc_policy = constrain(_prefs->advert_loc_policy, 0, 2);
// sanitise settings
@@ -469,6 +469,17 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
} else {
strcpy(reply, "gps toggle not found");
}
} else if (memcmp(command, "gps duty", 8) == 0) {
// Power GPS only around a clock sync. For a node that runs GPS purely for
// the (volatile) RTC this is the same service at a fraction of the power;
// position keeps coming from prefs, see `gps advert`.
if (_sensors->setSettingValue("gps", "2")) {
_prefs->gps_enabled = 2;
savePrefs();
strcpy(reply, "ok");
} else {
strcpy(reply, "gps toggle not found");
}
} else if (memcmp(command, "gps sync", 8) == 0) {
LocationProvider * l = _sensors->getLocationProvider();
if (l != NULL) {
@@ -519,7 +530,14 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
bool fix = l->isValid(); // has fix ?
int sats = l->satellitesCount();
bool active = !strcmp(_sensors->getSettingByKey("gps"), "1");
if (enabled) {
if (_prefs->gps_enabled == 2) {
// Duty cycle: the EN pin is off most of the time BY DESIGN, so
// reporting plain "off" here would read as a dead GPS. Say which.
sprintf(reply, "duty, %s, %s, %d sats",
enabled?"awake":"asleep",
fix?"fix":"no fix",
sats);
} else if (enabled) {
sprintf(reply, "on, %s, %s, %d sats",
active?"active":"deactivated",
fix?"fix":"no fix",
@@ -658,6 +658,57 @@ bool EnvironmentSensorManager::begin() {
return true;
}
// Answers "what is really on this bus?" on a live node, and flags the case that
// actually bites: an address the build's sensor table claims, with nothing
// answering on it (or something else answering, as SHT41 did on INA226's
// default 0x44). Marks each hit with the table name when one matches.
int EnvironmentSensorManager::scanI2C(char* dest, size_t max_len) {
bool found[128] = {};
scanI2CBus(TELEM_WIRE, found);
int n = 0;
size_t used = 0;
dest[0] = 0;
for (uint8_t addr = 0x08; addr < 0x78; addr++) {
if (!found[addr]) continue;
const char* name = NULL;
for (size_t i = 0; i < SENSOR_TABLE_SIZE; i++) {
if (SENSOR_TABLE[i].address == addr) { name = SENSOR_TABLE[i].name; break; }
}
char one[32];
int len = snprintf(one, sizeof(one), "%s0x%02X%s%s", n ? " " : "", addr,
name ? "=" : "", name ? name : "");
if (len > 0 && used + (size_t)len + 1 < max_len) {
memcpy(dest + used, one, (size_t)len + 1);
used += (size_t)len;
}
n++;
}
if (n == 0) {
used = (size_t)snprintf(dest, max_len, "no I2C device answered");
}
// Addresses the build's sensor table expects but nothing answered on. Bare
// hex, deduplicated: three entries share 0x76 and the names alone overran the
// 150 B mesh reply, truncating the part worth reading.
bool listed[128] = {};
bool first = true;
for (size_t i = 0; i < SENSOR_TABLE_SIZE; i++) {
uint8_t addr = SENSOR_TABLE[i].address;
if (found[addr] || listed[addr]) continue;
listed[addr] = true;
char one[16];
int len = snprintf(one, sizeof(one), first ? "; expected-empty %02X" : " %02X", addr);
if (len > 0 && used + (size_t)len + 1 < max_len) {
memcpy(dest + used, one, (size_t)len + 1);
used += (size_t)len;
first = false;
}
}
return n;
}
// ============================================================
// querySensors() — GPS stays on channel 1; each active sensor
// gets the next available channel in the order it was
@@ -704,6 +755,10 @@ const char* EnvironmentSensorManager::getSettingValue(int i) const {
int settings = 0;
#if ENV_INCLUDE_GPS
if (gps_detected && i == settings++) {
// In duty mode report the mode, not the momentary power state: asleep
// between syncs is not the same thing as `gps off`, and something that
// round-trips this value must not silently downgrade the mode.
if (gps_duty) return "2";
return gps_active ? "1" : "0";
}
#endif
@@ -714,8 +769,20 @@ bool EnvironmentSensorManager::setSettingValue(const char* name, const char* val
#if ENV_INCLUDE_GPS
if (gps_detected && strcmp(name, "gps") == 0) {
if (strcmp(value, "0") == 0) {
gps_duty = false;
stop_gps();
} else if (strcmp(value, "2") == 0) {
// Duty cycle: power up now (the clock is volatile, so the first sync is
// wanted immediately at boot) and let gpsDutyLoop() cut power once the
// RTC is set. The awake deadline MUST be armed here as well as in the
// loop -- left at 0 it is already in the past, and the first loop pass
// would cut GPS before it ever saw a satellite and back off for an hour.
gps_duty = true;
gps_wake_at = millis();
gps_awake_until = millis() + (uint32_t)GPS_DUTY_MAX_AWAKE_SECS * 1000;
start_gps();
} else {
gps_duty = false;
start_gps();
}
return true;
@@ -869,6 +936,40 @@ void EnvironmentSensorManager::start_gps() {
#endif
}
// Powers the receiver only around a clock sync (see gps_duty in the header).
//
// The cadence is owned here, not by MicroNMEALocationProvider's 30-minute
// re-arm, for one concrete reason: while the receiver is off its loop() is not
// called, so nothing feeds the parser -- and MicroNMEA keeps reporting the LAST
// fix as valid. A provider-driven re-arm would therefore sync the RTC to a
// half-hour-old timestamp. Hence syncTime() (which clears the parser) on every
// wake, and our own timer for when the next wake is due.
void EnvironmentSensorManager::gpsDutyLoop() {
if (!gps_duty) return;
// int32_t deltas, so the 49-day millis() wrap does not park GPS on (or off)
// forever -- these repeaters run for months between reboots.
if (!gps_active) {
if ((int32_t)(millis() - gps_wake_at) >= 0) {
start_gps();
_location->syncTime(); // clears the parser + arms the sync request
gps_awake_until = millis() + (uint32_t)GPS_DUTY_MAX_AWAKE_SECS * 1000;
MESH_DEBUG_PRINTLN("gps duty: awake, waiting for fix");
}
return;
}
if (!_location->waitingTimeSync()) { // the RTC got set -> nothing left to stay on for
stop_gps();
gps_wake_at = millis() + (uint32_t)GPS_DUTY_SYNC_INTERVAL_SECS * 1000;
MESH_DEBUG_PRINTLN("gps duty: synced, off for %d s", (int)GPS_DUTY_SYNC_INTERVAL_SECS);
} else if ((int32_t)(millis() - gps_awake_until) >= 0) {
stop_gps();
gps_wake_at = millis() + (uint32_t)GPS_DUTY_RETRY_SECS * 1000;
MESH_DEBUG_PRINTLN("gps duty: no fix in window, retry in %d s", (int)GPS_DUTY_RETRY_SECS);
}
}
void EnvironmentSensorManager::stop_gps() {
gps_active = false;
#ifdef RAK_WISBLOCK_GPS
@@ -893,6 +994,7 @@ void EnvironmentSensorManager::loop() {
if (gps_active) {
_location->loop();
}
gpsDutyLoop();
if (millis() > next_gps_update) {
if(gps_active){
@@ -4,6 +4,26 @@
#include <helpers/SensorManager.h>
#include <helpers/sensors/LocationProvider.h>
// GPS duty cycle (`gps duty`, gps_enabled == 2). A repeater that runs GPS only
// for the clock does not need a continuous fix: the receiver is powered just
// long enough to sync the RTC, then cut. On a solar node this is the single
// biggest lever there is -- an L76K draws 25-35 mA, an order of magnitude more
// than the whole radio + MCU budget it is being spent next to.
#ifndef GPS_DUTY_SYNC_INTERVAL_SECS
// Same cadence as MicroNMEALocationProvider::TIME_SYNC_INTERVAL, so clock
// accuracy is unchanged from `gps on` -- only the power in between differs.
#define GPS_DUTY_SYNC_INTERVAL_SECS 1800
#endif
#ifndef GPS_DUTY_MAX_AWAKE_SECS
// Cap on one sync attempt: a blocked sky must not turn duty cycle back into
// "always on". A cold fix is tens of seconds, so this is deliberately loose.
#define GPS_DUTY_MAX_AWAKE_SECS 300
#endif
#ifndef GPS_DUTY_RETRY_SECS
// Backoff after a window that produced no fix (antenna, snow, indoors).
#define GPS_DUTY_RETRY_SECS 3600
#endif
class EnvironmentSensorManager : public SensorManager {
protected:
static const int MAX_ACTIVE_SENSORS = 16;
@@ -24,9 +44,17 @@ protected:
uint32_t gps_update_interval_sec = 1;
#if ENV_INCLUDE_GPS
// Duty-cycle state. `gps_duty` is the mode; `gps_active` stays the truth
// about the receiver being powered, so everything that already reads it
// (telemetry, settings) keeps working unchanged.
bool gps_duty = false;
uint32_t gps_wake_at = 0; // millis of the next sync attempt
uint32_t gps_awake_until = 0; // millis the current attempt gives up at
LocationProvider* _location;
void start_gps();
void stop_gps();
void gpsDutyLoop();
void initBasicGPS();
#ifdef RAK_BOARD
void rakGPSInit();
@@ -50,4 +78,5 @@ public:
const char* getSettingName(int i) const override;
const char* getSettingValue(int i) const override;
bool setSettingValue(const char* name, const char* value) override;
int scanI2C(char* dest, size_t max_len) override;
};