diff --git a/msg/SensorsStatusGnss.msg b/msg/SensorsStatusGnss.msg index aec9f652d97..040b82d5273 100644 --- a/msg/SensorsStatusGnss.msg +++ b/msg/SensorsStatusGnss.msg @@ -6,7 +6,10 @@ uint64 timestamp # [us] Time since system start uint32 device_id_selected # [-] Receiver that vehicle_gnss carries +uint8 MAX_RECEIVERS = 4 # Length of the per-receiver arrays + uint32[4] device_ids # [-] One entry per receiver, 0 where unused +int8[4] order # [-] Fixed position of the receiver: 0 for the preferred one, otherwise for the first to publish, then in order of first publication. Changes only with the preference, -1 until the receiver publishes bool[4] healthy # Publishing, and has passed its checks for long enough uint16[4] failed_checks # [-] Bitmask of the enabled checks that failed, VehicleGnss CHECK_* bits bool[4] strict # The pre-flight thresholds apply rather than the relaxed in-flight ones diff --git a/src/lib/gnss/SensorGpsSelector.hpp b/src/lib/gnss/SensorGpsSelector.hpp deleted file mode 100644 index 440603aa070..00000000000 --- a/src/lib/gnss/SensorGpsSelector.hpp +++ /dev/null @@ -1,130 +0,0 @@ -/**************************************************************************** - * - * Copyright (c) 2026 PX4 Development Team. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name PX4 nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -#pragma once - -#include -#include -#include -#include -#include - -// Resolves SENS_GNSS_PRIME to the sensor_gnss uORB instance of the primary receiver: -// 0 and 1 select the uORB instance directly, 2-127 select a DroneCAN receiver by -// node ID (matched via device_id). Used by the GPS_RAW_INT/GPS2_RAW MAVLink streams -// so the reported receiver does not depend on uORB instance ordering, which is a -// boot-order race for CAN receivers, and shared with the vehicle_gnss -// selection. -class SensorGpsSelector -{ -public: - // SENS_GNSS_PRIME values 2-127 designate a DroneCAN receiver by node ID - static bool is_node_id(int32_t gnss_prime) { return (gnss_prime >= 2) && (gnss_prime <= 127); } - - // true if the SENS_GNSS_PRIME node ID designates the receiver with this device_id - static bool node_id_matches(int32_t gnss_prime, uint32_t device_id) - { - if (is_node_id(gnss_prime)) { - device::Device::DeviceId id{}; - id.devid = device_id; - - return (id.devid_s.bus_type == device::Device::DeviceBusType_UAVCAN) - && (id.devid_s.address == gnss_prime); - } - - return false; - } - - SensorGpsSelector() - { - read_param(); - } - - // uORB instance of the primary receiver (0 or 1) - uint8_t primary_instance() - { - if (_parameter_update_sub.updated()) { - parameter_update_s parameter_update; - _parameter_update_sub.copy(¶meter_update); - read_param(); - } - - if ((_gnss_prime == 0) || (_gnss_prime == 1)) { - return _gnss_prime; - } - - if (is_node_id(_gnss_prime)) { - // keep looking until the receiver has published - if (_node_id_instance < 0) { - _node_id_instance = find_instance_by_node_id(_gnss_prime); - } - - if (_node_id_instance >= 0) { - return _node_id_instance; - } - } - - // -1 (auto) or not resolvable - return 0; - } - -private: - void read_param() - { - _gnss_prime = 0; - _node_id_instance = -1; - - if (_param_sens_gnss_prime != PARAM_INVALID) { - param_get(_param_sens_gnss_prime, &_gnss_prime); - } - } - - static int8_t find_instance_by_node_id(int32_t node_id) - { - for (uint8_t i = 0; i < 2; i++) { - uORB::Subscription sensor_gnss_sub{ORB_ID(sensor_gnss), i}; - sensor_gnss_s gnss; - - if (sensor_gnss_sub.copy(&gnss) && node_id_matches(node_id, gnss.device_id)) { - return i; - } - } - - return -1; - } - - uORB::Subscription _parameter_update_sub{ORB_ID(parameter_update)}; - const param_t _param_sens_gnss_prime{param_find("SENS_GNSS_PRIME")}; - int32_t _gnss_prime{0}; - int8_t _node_id_instance{-1}; -}; diff --git a/src/modules/mavlink/streams/GPS2_RAW.hpp b/src/modules/mavlink/streams/GPS2_RAW.hpp index 78da3d3f342..27ea30c1b7e 100644 --- a/src/modules/mavlink/streams/GPS2_RAW.hpp +++ b/src/modules/mavlink/streams/GPS2_RAW.hpp @@ -34,8 +34,8 @@ #ifndef GPS2_RAW_HPP #define GPS2_RAW_HPP -#include #include +#include #include using namespace time_literals; @@ -61,17 +61,29 @@ private: uORB::Subscription _sensor_gnss_sub{ORB_ID(sensor_gnss), 1}; uORB::Subscription _vehicle_gnss_heading_sub{ORB_ID(vehicle_gnss_heading)}; - SensorGpsSelector _gps_selector{}; + uORB::Subscription _sensors_status_gnss_sub{ORB_ID(sensors_status_gnss)}; + int8_t _instance{-1}; ///< sensor_gnss instance to report, fixed by the sensors module + static constexpr int8_t kOrder{1}; ///< position of the reported receiver in sensors_status_gnss.order hrt_abstime _last_send_ts {}; static constexpr hrt_abstime kNoGpsSendInterval {1_s}; static constexpr hrt_abstime kHeadingTimeout {1_s}; bool send() override { - const uint8_t secondary = 1 - _gps_selector.primary_instance(); + sensors_status_gnss_s status; - if (secondary != _sensor_gnss_sub.get_instance()) { - _sensor_gnss_sub.ChangeInstance(secondary); + if (_sensors_status_gnss_sub.update(&status)) { + _instance = -1; + + for (int i = 0; i < sensors_status_gnss_s::MAX_RECEIVERS; i++) { + if (status.order[i] == kOrder) { + _instance = i; + } + } + } + + if ((_instance >= 0) && (_instance != _sensor_gnss_sub.get_instance())) { + _sensor_gnss_sub.ChangeInstance(_instance); } sensor_gnss_s gnss; @@ -79,7 +91,7 @@ private: hrt_abstime now{}; // only report the secondary receiver, never another instance's data - if ((_sensor_gnss_sub.get_instance() == secondary) && _sensor_gnss_sub.update(&gnss)) { + if ((_instance >= 0) && (_sensor_gnss_sub.get_instance() == _instance) && _sensor_gnss_sub.update(&gnss)) { if (gnss.time_utc_usec <= 0) { msg.time_usec = gnss.timestamp; diff --git a/src/modules/mavlink/streams/GPS_RAW_INT.hpp b/src/modules/mavlink/streams/GPS_RAW_INT.hpp index d6a96e78b33..63eeea6acaf 100644 --- a/src/modules/mavlink/streams/GPS_RAW_INT.hpp +++ b/src/modules/mavlink/streams/GPS_RAW_INT.hpp @@ -34,8 +34,8 @@ #ifndef GPS_RAW_INT_HPP #define GPS_RAW_INT_HPP -#include #include +#include #include using namespace time_literals; @@ -61,7 +61,9 @@ private: uORB::Subscription _sensor_gnss_sub{ORB_ID(sensor_gnss), 0}; uORB::Subscription _vehicle_gnss_heading_sub{ORB_ID(vehicle_gnss_heading)}; - SensorGpsSelector _gps_selector{}; + uORB::Subscription _sensors_status_gnss_sub{ORB_ID(sensors_status_gnss)}; + int8_t _instance{-1}; ///< sensor_gnss instance to report, fixed by the sensors module + static constexpr int8_t kOrder{0}; ///< position of the reported receiver in sensors_status_gnss.order hrt_abstime _last_send_ts {}; bool _yaw_capable{false}; ///< a heading has been reported at least once static constexpr hrt_abstime kNoGpsSendInterval {1_s}; @@ -69,10 +71,20 @@ private: bool send() override { - const uint8_t primary = _gps_selector.primary_instance(); + sensors_status_gnss_s status; - if (primary != _sensor_gnss_sub.get_instance()) { - _sensor_gnss_sub.ChangeInstance(primary); + if (_sensors_status_gnss_sub.update(&status)) { + _instance = -1; + + for (int i = 0; i < sensors_status_gnss_s::MAX_RECEIVERS; i++) { + if (status.order[i] == kOrder) { + _instance = i; + } + } + } + + if ((_instance >= 0) && (_instance != _sensor_gnss_sub.get_instance())) { + _sensor_gnss_sub.ChangeInstance(_instance); } sensor_gnss_s gnss; @@ -80,7 +92,7 @@ private: hrt_abstime now{}; // only report the primary receiver, never another instance's data - if ((_sensor_gnss_sub.get_instance() == primary) && _sensor_gnss_sub.update(&gnss)) { + if ((_instance >= 0) && (_sensor_gnss_sub.get_instance() == _instance) && _sensor_gnss_sub.update(&gnss)) { if (gnss.time_utc_usec <= 0) { msg.time_usec = gnss.timestamp; diff --git a/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.cpp b/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.cpp index cf3d21d45c7..26df13d786b 100644 --- a/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.cpp +++ b/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.cpp @@ -35,12 +35,25 @@ #include #include -#include +#include #include namespace sensors { +// SENS_GNSS_PRIME values 2-127 designate a DroneCAN receiver by node ID +static bool nodeIdMatches(int32_t gnss_prime, uint32_t device_id) +{ + if ((gnss_prime < 2) || (gnss_prime > 127)) { + return false; + } + + device::Device::DeviceId id{}; + id.devid = device_id; + + return (id.devid_s.bus_type == device::Device::DeviceBusType_UAVCAN) && (id.devid_s.address == gnss_prime); +} + static gnssChecksSample toChecksSample(const sensor_gnss_s &gnss) { gnssChecksSample sample{}; @@ -209,10 +222,14 @@ void VehicleGPSPosition::Run() _receiver_device_id[i] = gnss_data.device_id; _receiver_timestamp[i] = gnss_data.timestamp; + if (_first_publication[i] == 0) { + _first_publication[i] = ++_receivers_published; + } + _gps_blending.setAntennaOffset(antenna_offset, i); _gps_blending.setGnssData(gnss_data, i); - if (SensorGpsSelector::node_id_matches(gnss_prime, gnss_data.device_id)) { + if (nodeIdMatches(gnss_prime, gnss_data.device_id)) { _gps_blending.setPrimaryInstance(i); } @@ -223,6 +240,7 @@ void VehicleGPSPosition::Run() } if (any_gnss_updated) { + _preferred_instance = resolvePreferredInstance(); _gps_blending.update(hrt_absolute_time()); if (_gps_blending.isNewOutputDataAvailable()) { @@ -400,12 +418,30 @@ void VehicleGPSPosition::UpdateVehicleState() void VehicleGPSPosition::PublishStatus() { - static_assert(GPS_MAX_RECEIVERS <= (sizeof(sensors_status_gnss_s::device_ids) / sizeof(sensors_status_gnss_s::device_ids[0])), - "sensors_status_gnss has too few receiver entries"); + static_assert(GPS_MAX_RECEIVERS <= sensors_status_gnss_s::MAX_RECEIVERS, "sensors_status_gnss has too few receiver entries"); + static_assert(sensors_status_gnss_s::MAX_RECEIVERS == (sizeof(sensors_status_gnss_s::order) / sizeof( + sensors_status_gnss_s::order[0])), "MAX_RECEIVERS must match the array length"); sensors_status_gnss_s status{}; status.device_id_selected = _selected_device_id; + // An operator reads GPS_RAW_INT and GPS2_RAW as fixed receivers, so the order doesn't follow the selection: the + // preferred receiver first, the others as they first published. A configured preference keeps position 0 free + // until its receiver publishes. + for (int8_t &order : status.order) { + order = -1; + } + + int8_t next_order = hasConfiguredPreference() ? 1 : 0; + + for (uint8_t publication = 1; publication <= _receivers_published; publication++) { + for (int i = 0; i < GPS_MAX_RECEIVERS; i++) { + if (_first_publication[i] == publication) { + status.order[i] = (i == _preferred_instance) ? 0 : next_order++; + } + } + } + const hrt_abstime now = hrt_absolute_time(); for (int i = 0; i < GPS_MAX_RECEIVERS; i++) { @@ -425,6 +461,28 @@ void VehicleGPSPosition::PublishStatus() _sensors_status_gnss_pub.publish(status); } +int VehicleGPSPosition::resolvePreferredInstance() const +{ + const int gnss_prime = _param_sens_gnss_prime.get(); + + if (math::isInRange(gnss_prime, 0, GPS_MAX_RECEIVERS - 1)) { + return gnss_prime; + } + + for (int i = 0; i < GPS_MAX_RECEIVERS; i++) { + if ((_receiver_device_id[i] != 0) && nodeIdMatches(gnss_prime, _receiver_device_id[i])) { + return i; + } + } + + return -1; +} + +bool VehicleGPSPosition::hasConfiguredPreference() const +{ + return _param_sens_gnss_prime.get() != -1; +} + const VehicleGPSPosition::GpsParamSlot *VehicleGPSPosition::findParamSlot(uint32_t device_id, int instance) const { for (const GpsParamSlot &slot : _gnss_param_slots) { diff --git a/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.hpp b/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.hpp index 9c29d8270bf..a5c60b9224c 100644 --- a/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.hpp +++ b/src/modules/sensors/vehicle_gps_position/VehicleGPSPosition.hpp @@ -101,6 +101,12 @@ private: // SENS_GNSSn_* slot for a receiver, by device_id or (when no IDs are configured) by sensor_gnss instance const GpsParamSlot *findParamSlot(uint32_t device_id, int instance) const; + // sensor_gnss instance of the receiver SENS_GNSS_PRIME designates, -1 for none or not yet published + int resolvePreferredInstance() const; + + // SENS_GNSS_PRIME names a receiver, whether or not it has published yet + bool hasConfiguredPreference() const; + #if defined(CONFIG_SENSORS_VEHICLE_GNSS_HEADING) struct HeadingSample { uint64_t timestamp_sample; @@ -163,6 +169,9 @@ private: uint32_t _receiver_device_id[GPS_MAX_RECEIVERS] {}; hrt_abstime _receiver_timestamp[GPS_MAX_RECEIVERS] {}; uint32_t _selected_device_id{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}; // The checks run the strict thresholds while disarmed on the ground and the drift checks only at rest bool _armed{false}; diff --git a/src/modules/sensors/vehicle_gps_position/module.yaml b/src/modules/sensors/vehicle_gps_position/module.yaml index 0b39b4c76ec..be55f932974 100644 --- a/src/modules/sensors/vehicle_gps_position/module.yaml +++ b/src/modules/sensors/vehicle_gps_position/module.yaml @@ -16,8 +16,8 @@ parameters: To select a DroneCAN GPS, set this to the node ID. - The primary receiver is reported in the GPS_RAW_INT MAVLink - message and the secondary in GPS2_RAW. + GPS_RAW_INT reports the preferred receiver, or without one the first receiver + to publish, and GPS2_RAW the other one. Neither follows the selection. type: int32 default: 0 min: -1