我试图在Rviz中显示一个网格资源。它肯定很简单,就像在ROS1中一样,但是我不知道为什么它不起作用。下面是我尝试过的一个示例代码:
// meshpub.cpp
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/color_rgba.hpp>
#include <geometry_msgs/msg/pose.hpp>
#include <visualization_msgs/msg/marker.hpp>
#include <ament_index_cpp/get_package_share_directory.hpp>
using namespace std;
int main(int argc, char ** argv)
{
(void) argc;
(void) argv;
rclcpp::init(argc, argv);
rclcpp::Node node("shape_pubilsher");
string base_frame = "base_link";
string topic = "/marker";
// Getting the model file path:
auto package_share_directory = ament_index_cpp::get_package_share_directory("package");
auto file_name = package_share_directory.append("/meshes/model.obj");
auto qos = rclcpp::QoS(1000);
auto publisher = node.create_publisher<visualization_msgs::msg::Marker>(topic, qos);
RCLCPP_INFO(node.get_logger(), "Waiting for Rviz to load...");
while(node.get_node_graph_interface()->count_subscribers(topic) == 0) {
rclcpp::sleep_for(200ms);
}
// Creating the marker and initialising its fields
geometry_msgs::msg::Pose pose;
pose.position.x = 0;
pose.position.y = 0;
pose.position.z = 0;
pose.orientation.x = 0;
pose.orientation.y = 0;
pose.orientation.z = 0;
pose.orientation.w = 0;
std_msgs::msg::ColorRGBA colour;
colour.a = 1;
colour.r = 1;
colour.g = 0;
colour.b = 0;
visualization_msgs::msg::Marker marker;
marker.header.frame_id = base_frame;
marker.header.stamp = node.now();
marker.action = visualization_msgs::msg::Marker::ADD;
marker.type = visualization_msgs::msg::Marker::MESH_RESOURCE;
marker.pose = pose;
marker.id = 0;
marker.mesh_resource = file_name;
marker.scale.x = 2;
marker.scale.y = 2;
marker.scale.z = 2;
marker.color = colour;
RCLCPP_INFO(node.get_logger(), "Attempting to publish mesh");
publisher->publish(marker);
while(rclcpp::ok());
return 0;
}
我已经将包含3D模型文件的meshes
文件夹安装到了包共享目录中。我尝试过不同的模型文件格式,如.obj
、.fbx
、.blend
和.dae
(都在ROS1 Rviz中得到了支持),但是Rviz拒绝显示它,我得到的只是一条温馨的祝贺信息:
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [rviz2-1]: process started with pid [4733]
[INFO] [meshpub-2]: process started with pid [4735]
[meshpub-2] [INFO] [1664444431.191595500] [meshpub]: Waiting for Rviz to load...
[rviz2-1] QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-amirint'
[rviz2-1] [INFO] [1664444431.789824400] [rviz2]: Stereo is NOT SUPPORTED
[rviz2-1] [INFO] [1664444431.790017200] [rviz2]: OpenGl version: 3.1 (GLSL 1.4)
[rviz2-1] [INFO] [1664444431.863061600] [rviz2]: Stereo is NOT SUPPORTED
[meshpub-2] [INFO] [1664444433.394043100] [meshpub]: Attempting to publish mesh
[rviz2-1] [ERROR] [1664444434.023035400] [rviz2]: Could not load resource [/mnt/e/avnv/ros2_visually/install/package/share/package/meshes/model.obj]: Unable to open file "/mnt/e/avnv/ros2_visually/install/package/share/package/meshes/model.obj".
尽管/mnt/e/avnv/ros2_visually/install/package/share/package/meshes/model.obj
是资源的正确路径,但错误消息并没有具体说明这是一个--没有这样的文件或目录,还是文件格式不支持之类的东西。它只写着Unable to open file ...
。
我在wsl2和ROS_DISTRO=galactic上使用Ubuntu20.04。
发布于 2022-09-29 20:20:38
文件路径必须以file:///path/to/file
或package://path/to/file
形式出现。我错误地假设ament_index_cpp::get_package_share_directory("package")
将在默认情况下返回上述表单中的路径。
因此,只需手动将一个file://
附加到返回的路径字符串,就可以解决这个问题。
https://stackoverflow.com/questions/73894743
复制相似问题