From c70cd8bfa238af33fef644b694aa62424e2282e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Math=C3=A9o=20Taillandier?= <123255318+Matheo-13@users.noreply.github.com> Date: Fri, 30 Oct 2026 18:54:22 +0100 Subject: [PATCH] fix(simulation): fix range finder orientation (#29172) * fix(simulation): change the way the laserScantoLidarSensorCallback determines orientation * style(simulation): ran make format * fix(simulation): fix orientation computation * fix(gz_bridge): drop lidar link warning on vehicles without a lidar SIM_GZ_EN_LIDAR defaults to enabled, so every vehicle without a lidar (gz_x500, standard_vtol, rovers) printed a lidar_sensor_link warning at startup even though nothing was wrong. Assisted-by: Claude:claude-opus-5-5 * fix(gz_bridge): publish lidar orientation in FRD Collision prevention reads distance_sensor.q as FRD, but the custom quaternion was published in Gazebo's FLU frame, so a right-facing lidar landed in the left bins. Right, backward and upward lidars are also mapped to their enums now: an upward lidar left as ROTATION_CUSTOM bypassed the up/down exclusion in collision prevention and ended up in the horizontal obstacle map. Assisted-by: Claude:claude-opus-5-5 * fix(gz_bridge): look up lidar link pose before subscribing The scan subscription started before the scene/info request, so scans arriving during the request (up to 1 s) read _lidar_link_q while init was still writing it, and a down-facing lidar's first samples went out as forward-facing. Assisted-by: Claude:claude-opus-5-5 * fix(gz_bridge): skip lidar when its link orientation is unknown When scene/info failed, timed out or did not contain the model, _lidar_link_q stayed identity and every in-tree down-facing lidar (link pitched 90 deg, sensor rolled 180 deg) was published as forward-facing, so EKF2 dropped it. Publishing nothing is safer than guessing. Models without a lidar_sensor_link are now skipped silently instead of subscribing to a topic that has no publisher. Assisted-by: Claude:claude-opus-5-5 * docs(gz_bridge): explain the frame of the lidar world_pose field Assisted-by: Claude:claude-opus-5-5 --- src/modules/simulation/gz_bridge/GZBridge.cpp | 86 ++++++++++++++++--- src/modules/simulation/gz_bridge/GZBridge.hpp | 1 + 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/src/modules/simulation/gz_bridge/GZBridge.cpp b/src/modules/simulation/gz_bridge/GZBridge.cpp index f2b67f68778..824558a26cd 100644 --- a/src/modules/simulation/gz_bridge/GZBridge.cpp +++ b/src/modules/simulation/gz_bridge/GZBridge.cpp @@ -325,6 +325,48 @@ bool GZBridge::subscribeDistanceSensor(bool required) { std::string lidar_sensor = buildBaseTopic("lidar_sensor_link") + "lidar/scan"; + // Find the lidar link orientation relative to the base_link + gz::msgs::Empty req; + gz::msgs::Scene scene; + bool result = false; + + // Without the link orientation a down-facing lidar would be reported as forward-facing, so publish nothing + if (!_node.Request("/world/" + _world_name + "/scene/info", req, 1000, scene, result) || !result) { + PX4_WARN("scene/info request failed, lidar orientation unknown, not publishing distance_sensor"); + return required ? false : true; + } + + const auto model = std::find_if(scene.model().begin(), scene.model().end(), + [this](const gz::msgs::Model & m) { return m.name() == _model_name; }); + + if (model == scene.model().end()) { + PX4_WARN("%s not found in scene/info, lidar orientation unknown, not publishing distance_sensor", _model_name.c_str()); + return required ? false : true; + } + + // link poses are relative to the model frame + gz::math::Quaterniond q_model_base{gz::math::Quaterniond::Identity}; + gz::math::Quaterniond q_model_lidar{gz::math::Quaterniond::Identity}; + bool lidar_found = false; + + for (const auto &link : model->link()) { + if (link.name() == "base_link") { + q_model_base = gz::msgs::Convert(link.pose()).Rot(); + + } else if (link.name() == "lidar_sensor_link") { + q_model_lidar = gz::msgs::Convert(link.pose()).Rot(); + lidar_found = true; + } + } + + // models without a lidar have no lidar_sensor_link, nothing to subscribe to + if (!lidar_found) { + return true; + } + + _lidar_link_q = q_model_base.Inverse() * q_model_lidar; + + // subscribe only once _lidar_link_q is set, so the callback never reads it mid-update if (!_node.Subscribe(lidar_sensor, &GZBridge::laserScantoLidarSensorCallback, this)) { PX4_WARN("failed to subscribe to %s", lidar_sensor.c_str()); return required ? false : true; @@ -910,6 +952,8 @@ void GZBridge::laserScantoLidarSensorCallback(const gz::msgs::LaserScan &msg) _px4_rangefinder.set_max_distance(static_cast(msg.range_max())); _px4_rangefinder.set_rangefinder_type(distance_sensor_s::MAV_DISTANCE_SENSOR_LASER); + // despite its name, world_pose is the sensor pose relative to its parent link (lidar_sensor_link), + // which is why it is composed with _lidar_link_q below to get the orientation in the body frame gz::msgs::Quaternion pose_orientation = msg.world_pose().orientation(); gz::math::Quaterniond q_sensor = gz::math::Quaterniond( pose_orientation.w(), @@ -917,33 +961,49 @@ void GZBridge::laserScantoLidarSensorCallback(const gz::msgs::LaserScan &msg) pose_orientation.y(), pose_orientation.z()); - const gz::math::Quaterniond q_left(0.7071068, 0, 0, -0.7071068); - - const gz::math::Quaterniond q_front(0.7071068, 0.7071068, 0, 0); - - const gz::math::Quaterniond q_down(0, 1, 0, 0); - const float distance = static_cast(msg.ranges()[0]); - if (q_sensor.Equal(q_front, 0.03)) { + const gz::math::Quaterniond q_body_flu = _lidar_link_q * q_sensor; + + // Gazebo uses FLU, distance_sensor.q is expected in FRD (rotate 180 deg about x on both sides) + const gz::math::Quaterniond q_body(q_body_flu.W(), q_body_flu.X(), -q_body_flu.Y(), -q_body_flu.Z()); + const gz::math::Vector3d dir = q_body.RotateVector(gz::math::Vector3d::UnitX); + + auto points_like = [&](const gz::math::Vector3d & ref) { + return dir.Dot(ref) > std::cos(3.0 * M_PI / 180.0); + }; + + if (points_like(gz::math::Vector3d::UnitX)) { _px4_rangefinder.set_orientation(distance_sensor_s::ROTATION_FORWARD_FACING); _px4_rangefinder.update(hrt_absolute_time(), distance); - } else if (q_sensor.Equal(q_down, 0.03)) { + } else if (points_like(gz::math::Vector3d::UnitZ)) { _px4_rangefinder.set_orientation(distance_sensor_s::ROTATION_DOWNWARD_FACING); _px4_rangefinder.update(hrt_absolute_time(), distance); - } else if (q_sensor.Equal(q_left, 0.03)) { + } else if (points_like(-gz::math::Vector3d::UnitZ)) { + _px4_rangefinder.set_orientation(distance_sensor_s::ROTATION_UPWARD_FACING); + _px4_rangefinder.update(hrt_absolute_time(), distance); + + } else if (points_like(-gz::math::Vector3d::UnitY)) { _px4_rangefinder.set_orientation(distance_sensor_s::ROTATION_LEFT_FACING); _px4_rangefinder.update(hrt_absolute_time(), distance); + } else if (points_like(gz::math::Vector3d::UnitY)) { + _px4_rangefinder.set_orientation(distance_sensor_s::ROTATION_RIGHT_FACING); + _px4_rangefinder.update(hrt_absolute_time(), distance); + + } else if (points_like(-gz::math::Vector3d::UnitX)) { + _px4_rangefinder.set_orientation(distance_sensor_s::ROTATION_BACKWARD_FACING); + _px4_rangefinder.update(hrt_absolute_time(), distance); + } else { _px4_rangefinder.set_orientation(distance_sensor_s::ROTATION_CUSTOM); const float q[4] = { - static_cast(q_sensor.W()), - static_cast(q_sensor.X()), - static_cast(q_sensor.Y()), - static_cast(q_sensor.Z()) + static_cast(q_body.W()), + static_cast(q_body.X()), + static_cast(q_body.Y()), + static_cast(q_body.Z()) }; _px4_rangefinder.update(hrt_absolute_time(), distance, -1, q, 4); } diff --git a/src/modules/simulation/gz_bridge/GZBridge.hpp b/src/modules/simulation/gz_bridge/GZBridge.hpp index c10c2cde900..e023b6c245c 100644 --- a/src/modules/simulation/gz_bridge/GZBridge.hpp +++ b/src/modules/simulation/gz_bridge/GZBridge.hpp @@ -206,6 +206,7 @@ private: const std::string _world_name; const std::string _model_name; + gz::math::Quaterniond _lidar_link_q{gz::math::Quaterniond::Identity}; float _temperature{15.0f}; // default temperature in Celsius