diff --git a/docs/cli_commands.md b/docs/cli_commands.md index eb8c6b20..d7b8d5d0 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -1044,6 +1044,14 @@ region save --- +#### View GPS time to first fix statistics +**Usage:** +- `gps fix_stats` + +**Output:** `ttf:/ ok: miss:`: last and maximum time between a start of the GPS and its first valid fix, then the last count of consecutive starts with a fix (`ok`) or without one (`miss`). + +--- + #### View GPS diagnostics **Usage:** - `gps diag` diff --git a/docs/companion_cli.md b/docs/companion_cli.md index 24843269..0b3a27df 100644 --- a/docs/companion_cli.md +++ b/docs/companion_cli.md @@ -165,6 +165,14 @@ Radio parameters are common to all roles and handled by `CommonRadioPrefs`. --- +#### View GPS time to first fix statistics +**Usage:** +- `gps fix_stats` + +**Output:** `ttf:/ ok: miss:`: last and maximum time between a start of the GPS and its first valid fix, then the last count of consecutive starts with a fix (`ok`) or without one (`miss`). + +--- + #### View GPS diagnostics **Usage:** - `gps diag` diff --git a/src/helpers/CommonGpsPrefs.cpp b/src/helpers/CommonGpsPrefs.cpp index e08878a6..c4936d4f 100644 --- a/src/helpers/CommonGpsPrefs.cpp +++ b/src/helpers/CommonGpsPrefs.cpp @@ -25,6 +25,17 @@ bool CommonGpsPrefs::apply(SensorManager& sensors) const { return true; } +// single unit, rounded down: "45s", "12m", "3h" +static void formatDuration(char* buf, size_t size, uint32_t seconds) { + if (seconds < 60) { + snprintf(buf, size, "%lus", (unsigned long)seconds); + } else if (seconds < 3600) { + snprintf(buf, size, "%lum", (unsigned long)(seconds / 60)); + } else { + snprintf(buf, size, "%luh", (unsigned long)(seconds / 3600)); + } +} + bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, char* reply) { if (strcmp(command, "gps") == 0) { LocationProvider* location = sensors.getLocationProvider(); @@ -50,8 +61,9 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, } char fix[32]; if (location->hasValidFix()) { - snprintf(fix, sizeof(fix), "%ld sats %lus ago", - location->satellitesCount(), (unsigned long)location->getLastFixAge()); + char age[12]; + formatDuration(age, sizeof(age), location->getLastFixAge()); + snprintf(fix, sizeof(fix), "%ld sats %s ago", location->satellitesCount(), age); } else { strcpy(fix, "no fix"); } @@ -134,6 +146,22 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, } return true; } + if (strcmp(command, "gps fix_stats") == 0) { + LocationProvider* location = sensors.getLocationProvider(); + if (location == nullptr) { + strcpy(reply, "gps provider not found"); + } else { + const LocationProvider::FixStats s = location->getFixStats(); + char last[12] = "-", max[12] = "-"; + if (s.maxTtffMs > 0) { + formatDuration(last, sizeof(last), (s.lastTtffMs + 500) / 1000); + formatDuration(max, sizeof(max), (s.maxTtffMs + 500) / 1000); + } + snprintf(reply, 160, "ttf:%s/%s ok:%lu miss:%lu", last, max, + (unsigned long)s.consecutiveFixes, (unsigned long)s.consecutiveMisses); + } + return true; + } if (strcmp(command, "gps diag") == 0) { LocationProvider* location = sensors.getLocationProvider(); if (location == nullptr) { diff --git a/src/helpers/sensors/LocationProvider.h b/src/helpers/sensors/LocationProvider.h index 5bb6ec6a..3d7abd7a 100644 --- a/src/helpers/sensors/LocationProvider.h +++ b/src/helpers/sensors/LocationProvider.h @@ -18,10 +18,29 @@ public: unsigned long acquiredAt = 0; // millis() when the fix was stored }; + struct FixStats { + uint32_t lastTtffMs = 0; //time to first fix in ms + uint32_t maxTtffMs = 0; + uint32_t consecutiveFixes = 0; + uint32_t consecutiveMisses = 0; + }; + protected: bool _time_sync_needed = true; bool _gpsOn = false; LastFix _lastFix; + FixStats _fixStats; + + void fixStatsFix(uint32_t ttffMs) { + _fixStats.lastTtffMs = ttffMs; + if (ttffMs > _fixStats.maxTtffMs) _fixStats.maxTtffMs = ttffMs; + _fixStats.consecutiveFixes++; + _fixStats.consecutiveMisses = 0; + } + void fixStatsMiss() { + _fixStats.consecutiveMisses++; + _fixStats.consecutiveFixes = 0; + } public: virtual ~LocationProvider() = default; @@ -35,6 +54,7 @@ public: 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 FixStats getFixStats() const { return _fixStats; } virtual long getTimestamp() = 0; virtual void sendSentence(const char * sentence); virtual bool waitFor(const char* prefix, uint32_t timeout_ms) { return false; } diff --git a/src/helpers/sensors/MicroNMEALocationProvider.h b/src/helpers/sensors/MicroNMEALocationProvider.h index b07f486b..3c2adc6e 100644 --- a/src/helpers/sensors/MicroNMEALocationProvider.h +++ b/src/helpers/sensors/MicroNMEALocationProvider.h @@ -68,21 +68,30 @@ class MicroNMEALocationProvider : public LocationProvider { } void finishPeriodicAcquisition() { + if (_firstFixInWindow == 0) fixStatsMiss(); _driver->stop(); _acquiringPeriodically = false; _nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL; } - void saveFix() { + bool saveFix() { + // only store a fix when the snapshot is coherent (sats > 0). + // Returns true if the fix was stored. + if (nmea.getNumSatellites() == 0) return false; + LastFix fix; fix.latitude = nmea.getLatitude(); fix.longitude = nmea.getLongitude(); - nmea.getAltitude(fix.altitude); + // altitude comes from GGA only: keep the previous one if not available + if (!nmea.getAltitude(fix.altitude)) { + fix.altitude = _lastFix.valid ? _lastFix.altitude : 0; + } fix.satellites = nmea.getNumSatellites(); fix.timestamp = getTimestamp(); fix.acquiredAt = millis(); fix.valid = true; _lastFix = fix; + return true; } void updateTime() { @@ -132,7 +141,12 @@ public: void begin() override { _begin_calls++; + const bool wasActive = _driver->isActive(); _driver->begin(); + if (!wasActive) { + _acquisitionStarted = millis(); + _firstFixInWindow = 0; + } if (_gpsPeriodSeconds > 0 && _driver->isActive()) { nmea.clear(); time_valid = 0; @@ -183,20 +197,22 @@ public: LocationProvider::syncTime(); } + // Position, altitude and satellite count describe the last saved fix + // Before the first saved fix, fall back to the live parser state. long getLatitude() override { - return nmea.isValid() ? nmea.getLatitude() : _lastFix.latitude; + return _lastFix.valid ? _lastFix.latitude : nmea.getLatitude(); } long getLongitude() override { - return nmea.isValid() ? nmea.getLongitude() : _lastFix.longitude; + return _lastFix.valid ? _lastFix.longitude : nmea.getLongitude(); } long getAltitude() override { - if (!nmea.isValid()) return _lastFix.altitude; + if (_lastFix.valid) return _lastFix.altitude; long alt = 0; nmea.getAltitude(alt); return alt; } long satellitesCount() override { - return nmea.isValid() ? nmea.getNumSatellites() : _lastFix.satellites; + return _lastFix.valid ? _lastFix.satellites : nmea.getNumSatellites(); } bool isValid() override { return nmea.isValid(); } @@ -260,9 +276,12 @@ public: const char* messageId = nmea.getMessageID(); if (parsed && nmea.isValid() && (strcmp(messageId, "GGA") == 0 || strcmp(messageId, "RMC") == 0)) { - saveFix(); + if (!saveFix()) continue; // incoherent snapshot + if (_firstFixInWindow == 0) { + _firstFixInWindow = millis(); + fixStatsFix((uint32_t)(_firstFixInWindow - _acquisitionStarted)); + } if (_acquiringPeriodically) { - if (_firstFixInWindow == 0) _firstFixInWindow = millis(); if (nmea.getNumSatellites() >= GPS_MIN_SATS_FOR_FIX || (uint32_t)(millis() - _firstFixInWindow) >= GPS_FIX_SETTLE_MS) { finishPeriodicAcquisition(); diff --git a/src/helpers/sensors/RakLocationProvider.h b/src/helpers/sensors/RakLocationProvider.h index d2c755e6..7deddf43 100644 --- a/src/helpers/sensors/RakLocationProvider.h +++ b/src/helpers/sensors/RakLocationProvider.h @@ -57,6 +57,10 @@ public: return _useI2c ? _driver.hasFix() : _nmea.isValid(); } + FixStats getFixStats() const override { + return _useI2c ? _fixStats : _nmea.getFixStats(); + } + LastFix getLastFix() const override { return _useI2c ? _lastFix : _nmea.getLastFix(); } @@ -146,7 +150,13 @@ public: } const bool hasCurrentFix = _driver.hasFix(); - if (hasCurrentFix) saveRak12500Fix(); + if (hasCurrentFix) { + // first fix since the start + if (!_lastFix.valid || (long)(_lastFix.acquiredAt - _acquisitionStarted) < 0) { + fixStatsFix((uint32_t)(millis() - _acquisitionStarted)); + } + saveRak12500Fix(); + } if (_gpsPeriodSeconds > 0 && _acquiringPeriodically && hasCurrentFix) { _driver.stop(); @@ -155,6 +165,7 @@ public: } else if (_gpsPeriodSeconds > 0 && _acquiringPeriodically && _lastFix.valid && (uint32_t)(millis() - _acquisitionStarted) >= GPS_PERIODIC_ACQUISITION_TIMEOUT_MS) { + fixStatsMiss(); _driver.stop(); _acquiringPeriodically = false; _nextAcquisition = millis() + _gpsPeriodSeconds * 1000UL;