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) ### GPS (When GPS support is compiled in)
#### View or change GPS state #### View GPS state
**Usage:** **Usage:**
- `gps` - `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>` - `gps <state>`
**Parameters:** **Parameters:**
@@ -1009,9 +1016,25 @@ region save
**Default:** `off` **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 #### Set this node's location based on the GPS coordinates
**Usage:** **Usage:**
- `gps setloc` - `gps setloc`
+52
View File
@@ -7,6 +7,7 @@ Since version 1.18 meshcore companion radio firmware has a CLI interface, which
- [Configuration](#configuration) - [Configuration](#configuration)
- [System](#system) - [System](#system)
- [Radio](#radio) - [Radio](#radio)
- [GPS](#gps)
- [BLE](#ble) - [BLE](#ble)
- [Wifi](#wifi) - [Wifi](#wifi)
- [UI](#ui) - [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) #### View or change this node's rx boosted gain mode (SX12xx and LR1110)
**Usage:** **Usage:**
- `get radio.rxgain` - `get radio.rxgain`
+42 -6
View File
@@ -37,13 +37,49 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors,
} }
if (location == nullptr) { if (location == nullptr) {
strcpy(reply, "gps provider not found"); strcpy(reply, "gps provider not found");
} else if (location->isEnabled()) { } else if (location->isGpsOn()) {
sprintf(reply, "on, %s, %s, %d sats, mode: %s", const char* activity = "active";
isGpsEnabled() ? "active" : "deactivated", if (!location->isEnabled()) {
location->isValid() ? "fix" : "no fix", if (location->hasHardwareEnableState() && !location->isHardwareEnabled()) {
location->satellitesCount(), mode); 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 { } 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; return true;
} }
+20 -1
View File
@@ -6,9 +6,22 @@
class LocationProvider { 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: protected:
bool _time_sync_needed = true; bool _time_sync_needed = true;
bool _gpsOn = false; bool _gpsOn = false;
LastFix _lastFix;
public: public:
virtual ~LocationProvider() = default; virtual ~LocationProvider() = default;
@@ -19,7 +32,9 @@ public:
virtual long getLongitude() = 0; virtual long getLongitude() = 0;
virtual long getAltitude() = 0; virtual long getAltitude() = 0;
virtual long satellitesCount() = 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 long getTimestamp() = 0;
virtual void sendSentence(const char * sentence); virtual void sendSentence(const char * sentence);
virtual bool waitFor(const char* prefix, uint32_t timeout_ms) { return false; } virtual bool waitFor(const char* prefix, uint32_t timeout_ms) { return false; }
@@ -28,6 +43,10 @@ public:
virtual bool keepEnabledAfterDetection() const { return false; } virtual bool keepEnabledAfterDetection() const { return false; }
virtual void setGpsPeriod(uint32_t seconds) { } virtual void setGpsPeriod(uint32_t seconds) { }
virtual uint32_t getGpsPeriod() const { return 0; } 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 hasHardwareEnableState() const { return false; }
virtual bool isHardwareEnabled() const { return true; } virtual bool isHardwareEnabled() const { return true; }
virtual bool isGpsOn() const { return _gpsOn; } virtual bool isGpsOn() const { return _gpsOn; }
@@ -32,10 +32,8 @@ class MicroNMEALocationProvider : public LocationProvider {
uint32_t _stop_calls = 0; uint32_t _stop_calls = 0;
unsigned long _last_uart_ms = 0; unsigned long _last_uart_ms = 0;
unsigned long _last_nmea_ms = 0; unsigned long _last_nmea_ms = 0;
unsigned long _last_fix_ms = 0;
bool _uart_seen = false; bool _uart_seen = false;
bool _nmea_seen = false; bool _nmea_seen = false;
bool _fix_seen = false;
static uint32_t ageMs(unsigned long timestamp) { static uint32_t ageMs(unsigned long timestamp) {
return (uint32_t)(millis() - timestamp); return (uint32_t)(millis() - timestamp);
@@ -64,6 +62,18 @@ class MicroNMEALocationProvider : public LocationProvider {
_nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL; _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() { void updateTime() {
if (!isValid()) time_valid = 0; if (!isValid()) time_valid = 0;
@@ -161,17 +171,25 @@ public:
LocationProvider::syncTime(); LocationProvider::syncTime();
} }
long getLatitude() override { return nmea.getLatitude(); } long getLatitude() override {
long getLongitude() override { return nmea.getLongitude(); } return nmea.isValid() ? nmea.getLatitude() : _lastFix.latitude;
}
long getLongitude() override {
return nmea.isValid() ? nmea.getLongitude() : _lastFix.longitude;
}
long getAltitude() override { long getAltitude() override {
if (!nmea.isValid()) return _lastFix.altitude;
long alt = 0; long alt = 0;
nmea.getAltitude(alt); nmea.getAltitude(alt);
return 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(); } bool isValid() override { return nmea.isValid(); }
long getTimestamp() override { long getTimestamp() override {
if (!nmea.isValid()) return _lastFix.timestamp;
DateTime dt(nmea.getYear(), nmea.getMonth(), nmea.getDay(), DateTime dt(nmea.getYear(), nmea.getMonth(), nmea.getDay(),
nmea.getHour(), nmea.getMinute(), nmea.getSecond()); nmea.getHour(), nmea.getMinute(), nmea.getSecond());
return dt.unixtime(); return dt.unixtime();
@@ -228,10 +246,9 @@ public:
_nmea_seen = true; _nmea_seen = true;
const char* messageId = nmea.getMessageID(); const char* messageId = nmea.getMessageID();
if (parsed && isValid() && if (parsed && nmea.isValid() &&
(strcmp(messageId, "GGA") == 0 || strcmp(messageId, "RMC") == 0)) { (strcmp(messageId, "GGA") == 0 || strcmp(messageId, "RMC") == 0)) {
_last_fix_ms = millis(); saveFix();
_fix_seen = true;
if (_acquiringPeriodically) finishPeriodicAcquisition(); if (_acquiringPeriodically) finishPeriodicAcquisition();
} }
} else { } else {
@@ -256,7 +273,7 @@ public:
char fix_age[11]; char fix_age[11];
formatAge(uart_age, sizeof(uart_age), _uart_seen, _last_uart_ms); formatAge(uart_age, sizeof(uart_age), _uart_seen, _last_uart_ms);
formatAge(nmea_age, sizeof(nmea_age), _nmea_seen, _last_nmea_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, 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", "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; unsigned long _acquisitionStarted = 0;
bool _acquiringPeriodically = false; 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: public:
RakLocationProvider(RakGpsDriver& driver, mesh::RTCClock* clock = nullptr) RakLocationProvider(RakGpsDriver& driver, mesh::RTCClock* clock = nullptr)
: _driver(driver), _nmea(driver.nmeaDriver(), clock) {} : _driver(driver), _nmea(driver.nmeaDriver(), clock) {}
@@ -21,27 +33,31 @@ public:
} }
long getLatitude() override { long getLatitude() override {
return _driver.usesRak12500() ? _driver.getLatitude() : _nmea.getLatitude(); return _driver.usesRak12500() ? _lastFix.latitude : _nmea.getLatitude();
} }
long getLongitude() override { long getLongitude() override {
return _driver.usesRak12500() ? _driver.getLongitude() : _nmea.getLongitude(); return _driver.usesRak12500() ? _lastFix.longitude : _nmea.getLongitude();
} }
long getAltitude() override { long getAltitude() override {
return _driver.usesRak12500() ? _driver.getAltitude() : _nmea.getAltitude(); return _driver.usesRak12500() ? _lastFix.altitude : _nmea.getAltitude();
} }
long satellitesCount() override { long satellitesCount() override {
return _driver.usesRak12500() ? _driver.getSatellites() : _nmea.satellitesCount(); return _driver.usesRak12500() ? _lastFix.satellites : _nmea.satellitesCount();
} }
bool isValid() override { bool isValid() override {
return _driver.usesRak12500() ? _driver.hasFix() : _nmea.isValid(); return _driver.usesRak12500() ? _driver.hasFix() : _nmea.isValid();
} }
LastFix getLastFix() const override {
return _driver.usesRak12500() ? _lastFix : _nmea.getLastFix();
}
long getTimestamp() override { long getTimestamp() override {
return _driver.usesRak12500() ? _driver.getTimestamp() : _nmea.getTimestamp(); return _driver.usesRak12500() ? _lastFix.timestamp : _nmea.getTimestamp();
} }
void sendSentence(const char* sentence) override { void sendSentence(const char* sentence) override {
@@ -104,18 +120,24 @@ public:
void loop() override { void loop() override {
if (_driver.usesRak12500()) { if (_driver.usesRak12500()) {
if (_gpsOn && _gpsPeriodSeconds > 0) { if (_gpsOn) {
if (!_acquiringPeriodically && if (_gpsPeriodSeconds > 0) {
(long)(millis() - _nextAcquisition) >= 0) { if (!_acquiringPeriodically &&
_driver.begin(); (long)(millis() - _nextAcquisition) >= 0) {
_acquiringPeriodically = true; _driver.begin();
_acquisitionStarted = millis(); _acquiringPeriodically = true;
_acquisitionStarted = millis();
}
} }
if (_acquiringPeriodically && isValid()) {
const bool hasCurrentFix = _driver.hasFix();
if (hasCurrentFix) saveRak12500Fix();
if (_gpsPeriodSeconds > 0 && _acquiringPeriodically && hasCurrentFix) {
_driver.stop(); _driver.stop();
_acquiringPeriodically = false; _acquiringPeriodically = false;
_nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL; _nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL;
} else if (_acquiringPeriodically && } else if (_gpsPeriodSeconds > 0 && _acquiringPeriodically &&
(uint32_t)(millis() - _acquisitionStarted) >= (uint32_t)(millis() - _acquisitionStarted) >=
GPS_PERIODIC_ACQUISITION_TIMEOUT_MS) { GPS_PERIODIC_ACQUISITION_TIMEOUT_MS) {
_driver.stop(); _driver.stop();