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