Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/gps/GPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ void GPS::up()
// We've finished a GPS search cycle (lock or timeout). Enter a low power state, potentially.
void GPS::down()
{
if (hasValidLocation)
if (scheduling.hasValidFixSinceSearchStarted())
scheduling.informGotLock();
else
scheduling.informSearchFailed();
Expand Down Expand Up @@ -1545,6 +1545,7 @@ int32_t GPS::runOnce()
// 2. Got a lock for the first time, or 3. Got a lock after turning back on
bool gotLoc = lookForLocation();
if (gotLoc) {
scheduling.informValidFix();
#if GPS_DEBUG
if (!hasValidLocation) { // declare that we have location ASAP
LOG_DEBUG("hasValidLocation RISING EDGE");
Expand All @@ -1567,7 +1568,7 @@ int32_t GPS::runOnce()
}

bool tooLong = scheduling.searchedTooLong();
if (tooLong && !gotLoc) {
if (tooLong && !scheduling.hasValidFixSinceSearchStarted()) {
LOG_WARN("Can't publish valid location: no GPS lock in time");
// we didn't get a location during this ack window, therefore declare loss of lock
if (hasValidLocation) {
Expand Down
13 changes: 13 additions & 0 deletions src/gps/GPSUpdateScheduling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ uint32_t gpsHardsleepThresholdMs(uint32_t predictedSearchSecs)
void GPSUpdateScheduling::informSearching()
{
searching = true;
validFixReceived = false;
searchStartedMs = Time::getMillis();
}

void GPSUpdateScheduling::informValidFix()
{
if (searching)
validFixReceived = true;
}

// Mark the time when searching for GPS is complete,
// then update the predicted lock-time
void GPSUpdateScheduling::informGotLock()
Expand Down Expand Up @@ -64,6 +71,7 @@ void GPSUpdateScheduling::informSearchFailed()
void GPSUpdateScheduling::reset()
{
searching = false;
validFixReceived = false;
searchStartedMs = 0;
searchEndedMs = 0;
searchCount = 0;
Expand Down Expand Up @@ -152,6 +160,11 @@ bool GPSUpdateScheduling::searchedTooLong()
return false;
}

bool GPSUpdateScheduling::hasValidFixSinceSearchStarted() const
{
return searching && validFixReceived;
}

// Updates the predicted time-to-get-lock, by exponentially smoothing the latest observation
void GPSUpdateScheduling::updateLockTimePrediction()
{
Expand Down
5 changes: 4 additions & 1 deletion src/gps/GPSUpdateScheduling.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ class GPSUpdateScheduling
public:
// Marks the time of these events, for calculation use
void informSearching();
void informValidFix();
void informGotLock(); // Predicted lock-time is recalculated here
void informSearchFailed(); // Search ended without a fix; prediction is left untouched

void reset(); // Reset the prediction - after GPS::disable() / GPS::enable()
bool isUpdateDue(); // Is it time to begin searching for a GPS position?
bool searchedTooLong(); // Have we been searching for too long?
bool hasValidFixSinceSearchStarted() const;

uint32_t msUntilNextSearch(); // How long until we need to begin searching for a GPS? Info provided to GPS hardware for sleep
uint32_t elapsedSearchMs(); // How long have we been searching so far?
Expand All @@ -26,11 +28,12 @@ class GPSUpdateScheduling
private:
void updateLockTimePrediction(); // Called from informGotLock
bool searching = false; // Set by the inform*() calls; never inferred from stamp ordering
bool validFixReceived = false;
uint32_t searchStartedMs = 0;
uint32_t searchEndedMs = 0;
uint32_t searchCount = 0;
uint32_t predictedMsToGetLock = 0;
uint32_t consecutiveFailures = 0; // Count of search cycles that ended without a fix; reset on lock

const float weighting = 0.2; // Controls exponential smoothing of lock-times prediction. 20% weighting of "latest lock-time".
};
};
Loading