Rviz教程(五):Interactive Markers: Writing a Simple Interactive Marker Server

本文介绍如何设置一个简单的交互标记服务器,管理单个交互标记,并通过RViz进行操作。文章详细解释了代码实现过程,包括如何定义反馈处理函数、初始化ROS C++、创建交互标记及其控制元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Interactive Markers: Writing a Simple Interactive Marker Server

Description:  This tutorial explains how to setup a minimalist server which manages a single interactive marker.

Tutorial Level:  BEGINNER

Next Tutorial:   Interactive Markers: Basic Controls  

The simple_marker tutorial explained

If you run the simple_marker example from interactive_marker_tutorials as described in the previous tutorial, you will see this in RViz:

The simple_marker example showing in RViz

It shows the single interactive marker provided by the server node. Click on the arrow to move the box. What you will also see is that the server node prints out the current position of the marker each time you change it in RViz.

This is the code of the server node:

https://raw.githubusercontent.com/ros-visualization/visualization_tutorials/indigo-devel/interactive_marker_tutorials/src/simple_marker.cpp

切换行号显示
#include <ros/ros.h>

#include <interactive_markers/interactive_marker_server.h>

void processFeedback(
    const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
{
  ROS_INFO_STREAM( feedback->marker_name << " is now at "
      << feedback->pose.position.x << ", " << feedback->pose.position.y
      << ", " << feedback->pose.position.z );
}

int main(int argc, char** argv)
{
  ros::init(argc, argv, "simple_marker");

  // create an interactive marker server on the topic namespace simple_marker
  interactive_markers::InteractiveMarkerServer server("simple_marker");

  // create an interactive marker for our server
  visualization_msgs::InteractiveMarker int_marker;
  int_marker.header.frame_id = "base_link";
  int_marker.header.stamp=ros::Time::now();
  int_marker.name = "my_marker";
  int_marker.description = "Simple 1-DOF Control";

  // create a grey box marker
  visualization_msgs::Marker box_marker;
  box_marker.type = visualization_msgs::Marker::CUBE;
  box_marker.scale.x = 0.45;
  box_marker.scale.y = 0.45;
  box_marker.scale.z = 0.45;
  box_marker.color.r = 0.5;
  box_marker.color.g = 0.5;
  box_marker.color.b = 0.5;
  box_marker.color.a = 1.0;

  // create a non-interactive control which contains the box
  visualization_msgs::InteractiveMarkerControl box_control;
  box_control.always_visible = true;
  box_control.markers.push_back( box_marker );

  // add the control to the interactive marker
  int_marker.controls.push_back( box_control );

  // create a control which will move the box
  // this control does not contain any markers,
  // which will cause RViz to insert two arrows
  visualization_msgs::InteractiveMarkerControl rotate_control;
  rotate_control.name = "move_x";
  rotate_control.interaction_mode =
      visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;

  // add the control to the interactive marker
  int_marker.controls.push_back(rotate_control);

  // add the interactive marker to our collection &
  // tell the server to call processFeedback() when feedback arrives for it
  server.insert(int_marker, &processFeedback);

  // 'commit' changes and send to all clients
  server.applyChanges();

  // start the ROS main loop
  ros::spin();
}

What this does is the following:

  • Define a function processFeedback which handles feedback messages from RViz by printing out the position.

  • Initialize roscpp.
  • Create an interactive marker server object.
  • Setup the interactive marker and add it to the server's collection.
  • Enter the ROS message loop.

Note that when calling insert, the server object will internally only push the new marker onto a waiting list. Once you call applyChanges, it will incorporate it into it's publicly visible set of interactive markers and send it to all connected clients.

That's all there is. You are now ready to go on to the next tutorial: Interactive Markers: Basic Controls.

[rviz2-20] [ERROR] [1751273079.904134286] [rviz2]: PluginlibFactory: The plugin for class 'grid_map_rviz_plugin/GridMap' failed to load. Error: According to the loaded plugin descriptions the class grid_map_rviz_plugin/GridMap with base class type rviz_common::Display does not exist. Declared types are autoware_auto_perception_rviz_plugin/DetectedObjects autoware_auto_perception_rviz_plugin/PredictedObjects autoware_auto_perception_rviz_plugin/TrackedObjects rviz_default_plugins/AccelStamped rviz_default_plugins/Axes rviz_default_plugins/Camera rviz_default_plugins/DepthCloud rviz_default_plugins/Effort rviz_default_plugins/FluidPressure rviz_default_plugins/Grid rviz_default_plugins/GridCells rviz_default_plugins/Illuminance rviz_default_plugins/Image rviz_default_plugins/InteractiveMarkers rviz_default_plugins/LaserScan rviz_default_plugins/Map rviz_default_plugins/Marker rviz_default_plugins/MarkerArray rviz_default_plugins/Odometry rviz_default_plugins/Path rviz_default_plugins/PointCloud rviz_default_plugins/PointCloud2 rviz_default_plugins/PointStamped rviz_default_plugins/Polygon rviz_default_plugins/Pose rviz_default_plugins/PoseArray rviz_default_plugins/PoseWithCovariance rviz_default_plugins/Range rviz_default_plugins/RelativeHumidity rviz_default_plugins/RobotModel rviz_default_plugins/TF rviz_default_plugins/Temperature rviz_default_plugins/TwistStamped rviz_default_plugins/Wrench rviz_plugins/AccelerationMeter rviz_plugins/ConsoleMeter rviz_plugins/Float32MultiArrayStampedPieChart rviz_plugins/MaxVelocity rviz_plugins/MrmSummaryOverlayDisplay rviz_plugins/Path rviz_plugins/PathWithLaneId rviz_plugins/PolarGridDisplay rviz_plugins/PoseWithUuidStamped rviz_plugins/SteeringAngle rviz_plugins/Trajectory rviz_plugins/TurnSignal rviz_plugins/VelocityHistory rviz_plugins::PoseHistory rviz_plugins::PoseHistoryFootprint
最新发布
07-01
<think>我们正在解决rviz2中GridMap插件加载失败的问题。根据错误信息“PluginlibFactory:GridMapplugin loaderror”,这通常与插件未正确安装、依赖缺失或配置问题有关。参考站内引用中提到的插件加载错误案例,我们可以总结一些常见解决步骤:1.确保插件已正确安装(参考引用[1]和[3]中提到的插件安装问题)2.检查依赖项是否满足(参考引用[1]中提到的依赖问题)3.清除缓存并重新构建(参考引用[2]中的快速解决方案)4.检查环境变量和路径设置(参考引用[1]中关于平台插件的路径设置)具体到rviz2的GridMap插件,我们可以按照以下步骤排查:步骤1:确认GridMap插件已安装在ROS2中,GridMap插件通常由`grid_map_rviz_plugin`包提供。请确保已通过apt或源码安装该包。例如,在Ubuntu上,可以尝试安装:sudo aptinstall ros-<distro>-grid-map-rviz-plugin请将`<distro>`替换为你的ROS2发行版名称(如foxy、galactic、humble等)。步骤2:检查插件是否被正确加载运行以下命令查看rviz2的插件列表,确认GridMap插件是否在其中:ros2 runrviz2 rviz2--display-config/path/to/config.rviz或者在rviz2启动后,在“Add”面板中查看是否有GridMap相关的显示类型。步骤3:检查依赖项确保GridMap相关的ROS2包已正确安装,包括`grid_map_core`、`grid_map_ros`等。如果从源码构建,请确保这些包在同一个工作空间中,并且已经构建。步骤4:清除缓存并重新构建如果是从源码构建,尝试清除构建文件夹并重新构建:rm-rfbuild installlogcolcon build--symlink-install步骤5:检查环境变量确保环境变量设置正确,特别是`LD_LIBRARY_PATH`和`ROS_PACKAGE_PATH`。在ROS2中,通常通过`sourceinstall/setup.bash`来设置环境。步骤6:查看日志运行rviz2时,查看终端输出是否有更详细的错误信息。例如:ros2run rviz2rviz2如果上述步骤无法解决,可能需要查看插件的具体实现或寻求社区帮助。根据引用[4]中提到的插件初始化错误,有时插件本身在初始化过程中可能会失败,这可能需要检查插件的代码或配置文件。生成相关问题:1.如何安装ROS2的GridMap插件?2.如何查看rviz2已加载的插件列表?3.如何从源码构建GridMap插件?4.在ROS2中,如何解决插件加载失败的一般性问题?</think>### rviz2 GridMap插件加载失败解决方案 当在rviz2中遇到`PluginlibFactory: GridMap plugin load error`错误时,通常由以下原因引起: #### 1. **插件未正确安装** - **检查安装**:确认已安装`grid_map_rviz_plugin`包: ```bash sudo apt install ros-<distro>-grid-map-rviz-plugin ``` 将`<distro>`替换为你的ROS 2发行版(如`humble`、`foxy`)。若从源码构建,确保在`colcon build`时包含该包[^1]。 #### 2. **依赖缺失** - **关键依赖**:GridMap插件依赖`grid_map_core`和`grid_map_ros`。验证安装: ```bash sudo apt install ros-<distro>-grid-map-core ros-<distro>-grid-map-ros ``` - **链接库路径**:检查动态链接库路径是否包含插件: ```bash echo $LD_LIBRARY_PATH | grep grid_map ``` #### 3. **环境配置问题** - **工作空间激活**:若从源码构建,每次打开新终端需激活工作空间: ```bash source install/setup.bash ``` - **插件描述文件**:确认`grid_map_rviz_plugin.xml`存在于: ``` /opt/ros/<distro>/share/grid_map_rviz_plugin/plugin_description.xml ``` #### 4. **缓存冲突** - **清除缓存**:删除rviz2的本地配置缓存: ```bash rm ~/.local/share/rviz/* ``` - **重建索引**:强制重新加载插件: ```bash rosclean purge # 谨慎使用,会删除所有ROS日志 ``` #### 5. **版本兼容性** - **检查兼容性**:确认`grid_map_rviz_plugin`版本与ROS 2发行版匹配。例如: - ROS 2 Humble → `grid_map_rviz_plugin` ≥ 1.6.4 - **源码编译**:若版本不匹配,从源码编译: ```bash git clone https://github.com/ANYbotics/grid_map.git colcon build --packages-select grid_map_rviz_plugin ``` #### 6. **调试输出** 启动rviz2时启用详细日志: ```bash export RVIZ_LOGLEVEL=DEBUG ros2 run rviz2 rviz2 ``` 查看输出中是否有`ClassLoader`相关的加载错误线索。 --- ### 验证步骤 1. 检查插件是否在列表中: ```bash ros2 run rviz2 rviz2 --display-config /tmp/test.rviz # 在GUI中查看"Add"面板是否有"GridMap"选项 ``` 2. 通过命令行验证: ```bash ros2 plugin info grid_map_rviz_plugin/GridMapDisplay ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值