diff --git a/libraries/AP_GPS/AP_GPS.cpp b/libraries/AP_GPS/AP_GPS.cpp index 570cfae930..439ed0edc4 100644 --- a/libraries/AP_GPS/AP_GPS.cpp +++ b/libraries/AP_GPS/AP_GPS.cpp @@ -248,8 +248,32 @@ AP_GPS::update(void) update_instance(i); } - // update notify with gps status + // update notify with gps status. We always base this on the first GPS AP_Notify::flags.gps_status = state[0].status; + + // work out which GPS is the primary, and how many sensors we have + for (uint8_t i=0; i state[primary_instance].status) { + // we have a higher status lock, change GPS + primary_instance = i; + continue; + } + if (state[i].status == state[primary_instance].status && + state[i].num_sats >= state[primary_instance].num_sats + 2) { + // this GPS has at least 2 more satellites than the + // current primary, switch primary. Once we switch we will + // then tend to stick to the new GPS as primary. We don't + // want to switch too often as it will look like a + // position shift to the controllers. + primary_instance = i; + } + } } /* diff --git a/libraries/AP_GPS/AP_GPS.h b/libraries/AP_GPS/AP_GPS.h index 3c7c1bc936..c7f51229c4 100644 --- a/libraries/AP_GPS/AP_GPS.h +++ b/libraries/AP_GPS/AP_GPS.h @@ -113,13 +113,10 @@ public: // Accessor functions - // return number of active GPS sensors + // return number of active GPS sensors. Note that if the first GPS + // is not present but the 2nd is then we return 2 uint8_t num_sensors(void) const { - uint8_t count = 0; - for (uint8_t i=0; i