diff --git a/src/lib/events/enums.json b/src/lib/events/enums.json index e53211e86e6..d2e97ec0781 100644 --- a/src/lib/events/enums.json +++ b/src/lib/events/enums.json @@ -403,6 +403,40 @@ } } }, + "gnss_selection_reason_t": { + "type": "uint8_t", + "description": "Why the sensors module selected a GNSS receiver, the values of vehicle_gnss.selection_reason", + "entries": { + "0": { + "name": "preferred", + "description": "Preferred receiver" + }, + "1": { + "name": "only", + "description": "Only receiver" + }, + "2": { + "name": "ranked", + "description": "No receiver ranks higher" + }, + "3": { + "name": "timeout", + "description": "Previous receiver stopped publishing" + }, + "4": { + "name": "unhealthy", + "description": "Previous receiver failed its checks or was late" + }, + "5": { + "name": "requirements", + "description": "Meets the accuracy requirements, the previous receiver didn't" + }, + "6": { + "name": "rtk_fixed", + "description": "RTK fixed, the previous receiver wasn't" + } + } + }, "arming_state_t": { "type": "uint8_t", "description": "State of the main arming state machine", diff --git a/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.cpp b/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.cpp index b884be5fb28..bf0e13cb8a3 100644 --- a/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.cpp +++ b/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.cpp @@ -274,8 +274,13 @@ void VehicleGPSPosition::Run() gnss_output.timestamp = hrt_absolute_time(); _vehicle_gnss_pub.publish(gnss_output); + if ((_published_instance >= 0) && (gnss_output.selection_count != _published_selection_count)) { + reportSwitch(_published_instance, selected, gnss_output.selection_reason); + } + _selected_device_id = gnss_output.receiver.device_id; _published_instance = selected; + _published_selection_count = gnss_output.selection_count; } PublishStatus(); @@ -529,6 +534,25 @@ void VehicleGPSPosition::PublishStatus() _sensors_status_gnss_pub.publish(status); } +void VehicleGPSPosition::reportSwitch(int previous, int selected, uint8_t reason) const +{ + // A failed receiver is a warning; a return to the preferred or a more accurate receiver is not + const bool failure = (reason == vehicle_gnss_s::SELECTION_TIMEOUT) || (reason == vehicle_gnss_s::SELECTION_UNHEALTHY); + + /* EVENT + * @description + * The estimator uses the new receiver from now on. + * + * + * Set a preferred receiver with SENS_GNSS_PRIME. + * + */ + events::send(events::ID("gnss_receiver_switched"), + {failure ? events::Log::Warning : events::Log::Info, events::LogInternal::Info}, + "Switched from GNSS {3} to GNSS {1}: {2}", static_cast(selected), + static_cast(reason), static_cast(previous)); +} + matrix::Vector3f VehicleGPSPosition::antennaOffset(int instance) const { const GpsParamSlot *slot = findParamSlot(_latest_sample[instance].device_id, instance); diff --git a/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.hpp b/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.hpp index ce19ffe1abd..2a462bb8a39 100644 --- a/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.hpp +++ b/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.hpp @@ -83,6 +83,9 @@ private: void UpdateVehicleState(); void PublishStatus(); + // One event per change of the receiver that vehicle_gnss carries + void reportSwitch(int previous, int selected, uint8_t reason) const; + // define max number of GPS receivers supported static constexpr int GPS_MAX_RECEIVERS = 2; static_assert(GPS_MAX_RECEIVERS == GnssSelector::GNSS_MAX_RECEIVERS, @@ -179,6 +182,7 @@ private: sensor_gnss_s _latest_sample[GPS_MAX_RECEIVERS] {}; ///< timestamp_sample corrected, timestamp 0 until it publishes uint32_t _selected_device_id{0}; int8_t _published_instance{-1}; ///< receiver of the last vehicle_gnss sample, -1 before the first one + uint8_t _published_selection_count{0}; int8_t _preferred_instance{-1}; uint8_t _first_publication[GPS_MAX_RECEIVERS] {}; ///< 1 for the first receiver to publish, 2 for the next, 0 before uint8_t _receivers_published{0};