gps: cache the last fix in LocationProvider (isValid vs hasValidFix), fix and status commands

This commit is contained in:
Florent committed 2026-11-08 08:47:34 -04:00
1 parent dea38936d7
commit e58be9ab11
6 files changed
+208 -33

No files matched your search

+33 -4
View File
@@ -999,9 +999,16 @@ region save
---
### GPS (When GPS support is compiled in)
#### View or change GPS state
#### View GPS state
**Usage:**
- `gps`
**Note:** Reports the requested state, receiver activity, last-fix status (last fix gives number of sats and how long ago was the fix), and mode. In periodic mode, `waiting` means the receiver is off between acquisitions; the last fix remains available. `off(hw)` means a hardware GPS switch is off.
---
#### Change GPS state
**Usage:**
- `gps <state>`
**Parameters:**
@@ -1009,9 +1016,25 @@ region save
**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
---
#### View or change the GPS acquisition period
**Usage:**
- `gps period`
- `gps period <seconds>`
**Parameters:**
- `seconds`: `0` for continuous tracking, or `1`-`86400` for periodic acquisition
**Note:** A periodic acquisition stops after receiving a valid fix, then resumes after the configured period.
---
#### View the last GPS fix
**Usage:**
- `gps fix`
**Note:** Returns the last valid latitude, longitude, altitude, and elapsed time since the fix. Returns `no fix` until a valid fix is received.
---
@@ -1021,6 +1044,12 @@ region save
---
#### View GPS diagnostics
**Usage:**
- `gps diag`
---
#### Set this node's location based on the GPS coordinates
**Usage:**
- `gps setloc`
+52
View File
@@ -7,6 +7,7 @@ Since version 1.18 meshcore companion radio firmware has a CLI interface, which
- [Configuration](#configuration)
- [System](#system)
- [Radio](#radio)
- [GPS](#gps)
- [BLE](#ble)
- [Wifi](#wifi)
- [UI](#ui)
@@ -119,6 +120,57 @@ Radio parameters are common to all roles and handled by `CommonRadioPrefs`.
---
### GPS
#### View GPS state
**Usage:**
- `gps`
**Note:** Reports the requested state, receiver activity, last-fix status (last fix gives number of sats and how long ago was the fix), and mode. In periodic mode, `waiting` means the receiver is off between acquisitions; the last fix remains available. `off(hw)` means a hardware GPS switch is off.
---
#### Change GPS state
**Usage:**
- `gps <state>`
**Parameters:**
- `state`: `on`|`off`
---
#### View or change the GPS acquisition period
**Usage:**
- `gps period`
- `gps period <seconds>`
**Parameters:**
- `seconds`: `0` for continuous tracking, or `1`-`86400` for periodic acquisition
**Note:** A periodic acquisition stops after receiving a valid fix, then resumes after the configured period.
---
#### View the last GPS fix
**Usage:**
- `gps fix`
**Note:** Returns the last valid latitude, longitude, altitude, and elapsed time since the fix. Returns `no fix` until a valid fix is received.
---
#### Sync this node's clock with GPS time
**Usage:**
- `gps sync`
---
#### View GPS diagnostics
**Usage:**
- `gps diag`
---
#### View or change this node's rx boosted gain mode (SX12xx and LR1110)
**Usage:**
- `get radio.rxgain`
+42 -6
View File
@@ -37,13 +37,49 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors,
}
if (location == nullptr) {
strcpy(reply, "gps provider not found");
} else if (location->isEnabled()) {
sprintf(reply, "on, %s, %s, %d sats, mode: %s",
isGpsEnabled() ? "active" : "deactivated",
location->isValid() ? "fix" : "no fix",
location->satellitesCount(), mode);
} else if (location->isGpsOn()) {
const char* activity = "active";
if (!location->isEnabled()) {
if (location->hasHardwareEnableState() && !location->isHardwareEnabled()) {
activity = "off(hw)";
} else if (getGpsPeriod() > 0) {
activity = "waiting";
} else {
activity = "inactive";
}
}
char fix[32];
if (location->hasValidFix()) {
snprintf(fix, sizeof(fix), "%ld sats %lus ago",
location->satellitesCount(), (unsigned long)location->getLastFixAge());
} else {
strcpy(fix, "no fix");
}
snprintf(reply, 160, "on, %s, %s, mode: %s", activity, fix, mode);
}
return true;
}
if (strcmp(command, "gps fix") == 0) {
LocationProvider* location = sensors.getLocationProvider();
if (location == nullptr) {
strcpy(reply, "gps provider not found");
} else if (!location->hasValidFix()) {
strcpy(reply, "no fix");
} else {
snprintf(reply, 160, "off, mode: %s", mode);
const long latitude = location->getLatitude();
const long longitude = location->getLongitude();
const uint32_t fixAge = location->getLastFixAge();
char age[24];
if (fixAge == UINT32_MAX) {
strcpy(age, "age unavailable");
} else {
snprintf(age, sizeof(age), "%lus ago", (unsigned long)fixAge);
}
snprintf(reply, 160, "%s%ld.%06ld, %s%ld.%06ld, %ldm, %s",
latitude < 0 ? "-" : "", labs(latitude) / 1000000L,
labs(latitude) % 1000000L,
longitude < 0 ? "-" : "", labs(longitude) / 1000000L,
labs(longitude) % 1000000L, location->getAltitude() / 1000L, age);
}
return true;
}
+20 -1
View File
@@ -6,9 +6,22 @@
class LocationProvider {
public:
// Last valid fix, always filled in one go so readers get a consistent snapshot.
struct LastFix {
bool valid = false;
long latitude = 0;
long longitude = 0;
long altitude = 0;
long satellites = 0;
long timestamp = 0;
unsigned long acquiredAt = 0; // millis() when the fix was stored
};
protected:
bool _time_sync_needed = true;
bool _gpsOn = false;
LastFix _lastFix;
public:
virtual ~LocationProvider() = default;
@@ -19,7 +32,9 @@ public:
virtual long getLongitude() = 0;
virtual long getAltitude() = 0;
virtual long satellitesCount() = 0;
virtual bool isValid() = 0;
virtual bool isValid() = 0; // current fix is valid
virtual bool hasValidFix() { return getLastFix().valid || isValid(); } // a valid fix was obtained at some point
virtual LastFix getLastFix() const { return _lastFix; } // snapshot of the last valid fix
virtual long getTimestamp() = 0;
virtual void sendSentence(const char * sentence);
virtual bool waitFor(const char* prefix, uint32_t timeout_ms) { return false; }
@@ -28,6 +43,10 @@ public:
virtual bool keepEnabledAfterDetection() const { return false; }
virtual void setGpsPeriod(uint32_t seconds) { }
virtual uint32_t getGpsPeriod() const { return 0; }
virtual uint32_t getLastFixAge() const { // in seconds, UINT32_MAX if never
const LastFix f = getLastFix();
return f.valid ? (uint32_t)(millis() - f.acquiredAt) / 1000UL : UINT32_MAX;
}
virtual bool hasHardwareEnableState() const { return false; }
virtual bool isHardwareEnabled() const { return true; }
virtual bool isGpsOn() const { return _gpsOn; }
@@ -32,10 +32,8 @@ class MicroNMEALocationProvider : public LocationProvider {
uint32_t _stop_calls = 0;
unsigned long _last_uart_ms = 0;
unsigned long _last_nmea_ms = 0;
unsigned long _last_fix_ms = 0;
bool _uart_seen = false;
bool _nmea_seen = false;
bool _fix_seen = false;
static uint32_t ageMs(unsigned long timestamp) {
return (uint32_t)(millis() - timestamp);
@@ -64,6 +62,18 @@ class MicroNMEALocationProvider : public LocationProvider {
_nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL;
}
void saveFix() {
LastFix fix;
fix.latitude = nmea.getLatitude();
fix.longitude = nmea.getLongitude();
nmea.getAltitude(fix.altitude);
fix.satellites = nmea.getNumSatellites();
fix.timestamp = getTimestamp();
fix.acquiredAt = millis();
fix.valid = true;
_lastFix = fix;
}
void updateTime() {
if (!isValid()) time_valid = 0;
@@ -161,17 +171,25 @@ public:
LocationProvider::syncTime();
}
long getLatitude() override { return nmea.getLatitude(); }
long getLongitude() override { return nmea.getLongitude(); }
long getLatitude() override {
return nmea.isValid() ? nmea.getLatitude() : _lastFix.latitude;
}
long getLongitude() override {
return nmea.isValid() ? nmea.getLongitude() : _lastFix.longitude;
}
long getAltitude() override {
if (!nmea.isValid()) return _lastFix.altitude;
long alt = 0;
nmea.getAltitude(alt);
return alt;
}
long satellitesCount() override { return nmea.getNumSatellites(); }
long satellitesCount() override {
return nmea.isValid() ? nmea.getNumSatellites() : _lastFix.satellites;
}
bool isValid() override { return nmea.isValid(); }
long getTimestamp() override {
if (!nmea.isValid()) return _lastFix.timestamp;
DateTime dt(nmea.getYear(), nmea.getMonth(), nmea.getDay(),
nmea.getHour(), nmea.getMinute(), nmea.getSecond());
return dt.unixtime();
@@ -228,10 +246,9 @@ public:
_nmea_seen = true;
const char* messageId = nmea.getMessageID();
if (parsed && isValid() &&
if (parsed && nmea.isValid() &&
(strcmp(messageId, "GGA") == 0 || strcmp(messageId, "RMC") == 0)) {
_last_fix_ms = millis();
_fix_seen = true;
saveFix();
if (_acquiringPeriodically) finishPeriodicAcquisition();
}
} else {
@@ -256,7 +273,7 @@ public:
char fix_age[11];
formatAge(uart_age, sizeof(uart_age), _uart_seen, _last_uart_ms);
formatAge(nmea_age, sizeof(nmea_age), _nmea_seen, _last_nmea_ms);
formatAge(fix_age, sizeof(fix_age), _fix_seen, _last_fix_ms);
formatAge(fix_age, sizeof(fix_age), _lastFix.valid, _lastFix.acquiredAt);
snprintf(out, out_size,
"en:%u ub:%lu ua:%s ok:%lu bad:%lu na:%s sat:%ld fix:%u fa:%s bc:%lu sc:%lu",
+35 -13
View File
@@ -11,6 +11,18 @@ class RakLocationProvider : public LocationProvider {
unsigned long _acquisitionStarted = 0;
bool _acquiringPeriodically = false;
void saveRak12500Fix() {
LastFix fix;
fix.latitude = _driver.getLatitude();
fix.longitude = _driver.getLongitude();
fix.altitude = _driver.getAltitude();
fix.satellites = _driver.getSatellites();
fix.timestamp = _driver.getTimestamp();
fix.acquiredAt = millis();
fix.valid = true;
_lastFix = fix;
}
public:
RakLocationProvider(RakGpsDriver& driver, mesh::RTCClock* clock = nullptr)
: _driver(driver), _nmea(driver.nmeaDriver(), clock) {}
@@ -21,27 +33,31 @@ public:
}
long getLatitude() override {
return _driver.usesRak12500() ? _driver.getLatitude() : _nmea.getLatitude();
return _driver.usesRak12500() ? _lastFix.latitude : _nmea.getLatitude();
}
long getLongitude() override {
return _driver.usesRak12500() ? _driver.getLongitude() : _nmea.getLongitude();
return _driver.usesRak12500() ? _lastFix.longitude : _nmea.getLongitude();
}
long getAltitude() override {
return _driver.usesRak12500() ? _driver.getAltitude() : _nmea.getAltitude();
return _driver.usesRak12500() ? _lastFix.altitude : _nmea.getAltitude();
}
long satellitesCount() override {
return _driver.usesRak12500() ? _driver.getSatellites() : _nmea.satellitesCount();
return _driver.usesRak12500() ? _lastFix.satellites : _nmea.satellitesCount();
}
bool isValid() override {
return _driver.usesRak12500() ? _driver.hasFix() : _nmea.isValid();
}
LastFix getLastFix() const override {
return _driver.usesRak12500() ? _lastFix : _nmea.getLastFix();
}
long getTimestamp() override {
return _driver.usesRak12500() ? _driver.getTimestamp() : _nmea.getTimestamp();
return _driver.usesRak12500() ? _lastFix.timestamp : _nmea.getTimestamp();
}
void sendSentence(const char* sentence) override {
@@ -104,18 +120,24 @@ public:
void loop() override {
if (_driver.usesRak12500()) {
if (_gpsOn && _gpsPeriodSeconds > 0) {
if (!_acquiringPeriodically &&
(long)(millis() - _nextAcquisition) >= 0) {
_driver.begin();
_acquiringPeriodically = true;
_acquisitionStarted = millis();
if (_gpsOn) {
if (_gpsPeriodSeconds > 0) {
if (!_acquiringPeriodically &&
(long)(millis() - _nextAcquisition) >= 0) {
_driver.begin();
_acquiringPeriodically = true;
_acquisitionStarted = millis();
}
}
if (_acquiringPeriodically && isValid()) {
const bool hasCurrentFix = _driver.hasFix();
if (hasCurrentFix) saveRak12500Fix();
if (_gpsPeriodSeconds > 0 && _acquiringPeriodically && hasCurrentFix) {
_driver.stop();
_acquiringPeriodically = false;
_nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL;
} else if (_acquiringPeriodically &&
} else if (_gpsPeriodSeconds > 0 && _acquiringPeriodically &&
(uint32_t)(millis() - _acquisitionStarted) >=
GPS_PERIODIC_ACQUISITION_TIMEOUT_MS) {
_driver.stop();