gps: fix_stats command

This commit is contained in:
Florent committed 2026-11-08 08:47:34 -04:00
1 parent 5303703750
commit 47959b8960
6 files changed
+105 -11

No files matched your search

+8
View File
@@ -1044,6 +1044,14 @@ region save
---
#### View GPS time to first fix statistics
**Usage:**
- `gps fix_stats`
**Output:** `ttf:<last>/<max> ok:<n> miss:<n>`: 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`
+8
View File
@@ -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:<last>/<max> ok:<n> miss:<n>`: 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`
+30 -2
View File
@@ -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) {
+20
View File
@@ -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; }
@@ -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();
+12 -1
View File
@@ -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;