Rviz中使用Marker工具

上海硅步ROS连载系列43期 RViz工具使用实操

https://mp.weixin.qq.com/s?__biz=MzA5NjkyMzI2OA==&mid=2651197718&idx=2&sn=8d070f799a1af5e61fe733d4b027c89f&chksm=8b5a0afdbc2d83eb769a9940cc75cb659caae47b721a286d8f66001bcf2cf40a2ed08d649666&mpshare=1&scene=23&srcid=01064eHZkk3FH5BeL83PPE0P#rd

Rviz教程-Marker:发送基本的形状(C++)

http://blog.csdn.net/wilylcyu/article/details/56489676

rviz–显示类型-Marker

http://blog.csdn.net/wilylcyu/article/details/57080917

ROS学习笔记(八)::RVIZ::Interactive Markers: Writing a Simple Interactive Marker Server

http://blog.csdn.net/u011104647/article/details/50424687

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace

cd ~/catkin_ws/
catkin_make
source devel/setup.bash
echo $ROS_PACKAGE_PATH

echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
cd src
catkin_create_pkg using_markers roscpp visualization_msgs

粘贴下面代码到src/basic_shapes.cpp:

#include 
#include 

int main( int argc, char** argv )
{
  ros::init(argc, argv, "basic_shapes");
  ros::NodeHandle n;
  ros::Rate r(1);
  ros::Publisher marker_pub = n.advertise("visualization_marker", 1);

  // 设置初始形状为立方体
  uint32_t shape = visualization_msgs::Marker::CUBE;

  while (ros::ok())
  {
    visualization_msgs::Marker marker;
    // 设置帧 ID和时间戳
    marker.header.frame_id = "/my_frame";
    marker.header.stamp = ros::Time::now();

// 设置该标记的命名空间和ID,ID应该是独一无二的
// 具有相同命名空间和ID的标记将会覆盖前一个
    marker.ns = "basic_shapes";
    marker.id = 0;

    // 设置标记类型,初始值为立方体。在立方体、球体、箭头和 圆柱体之间循环
    marker.type = shape;

// 设置标记行为:ADD(添 加),DELETE(删 除)
marker.action = visualization_msgs::Marker::ADD;

    //设置标记位姿。 
    marker.pose.position.x = 0;
    marker.pose.position.y = 0;
    marker.pose.position.z = 0;
    marker.pose.orientation.x = 0.0;
    marker.pose.orientation.y = 0.0;
    marker.pose.orientation.z = 0.0;
    marker.pose.orientation.w = 1.0;

// 设置标记的比例,所有方向上尺度1表示1米
marker.scale.x = 1.0;
    marker.scale.y = 1.0;
    marker.scale.z = 1.0;

    //设置标记颜色,确保alpha(不透明度)值不为0
    marker.color.r = 0.0f;
    marker.color.g = 1.0f;
    marker.color.b = 0.0f;
    marker.color.a = 1.0;

    marker.lifetime = ros::Duration();

    // 发布该标记
    while (marker_pub.getNumSubscribers() < 1)
    {
      if (!ros::ok())
      {
        return 0;
      }
      ROS_WARN_ONCE("Please create a subscriber to the marker");
      sleep(1);
    }
    marker_pub.publish(marker);

    // 在不同形状之间循环
    switch (shape)
    {
    case visualization_msgs::Marker::CUBE:
      shape = visualization_msgs::Marker::SPHERE;
      break;
    case visualization_msgs::Marker::SPHERE:
      shape = visualization_msgs::Marker::ARROW;
      break;
    case visualization_msgs::Marker::ARROW:
      shape = visualization_msgs::Marker::CYLINDER;
      break;
    case visualization_msgs::Marker::CYLINDER:
      shape = visualization_msgs::Marker::CUBE;
      break;
    }

    r.sleep();
  }
}

编辑using_markers功能包中的CMakeLists.txt,在文件底部添加如下语句:

add_executable(basic_shapes src/basic_shapes.cpp)
target_link_libraries(basic_shapes ${catkin_LIBRARIES})
cd ~/catkin_ws
catkin_make
rosrun using_markers basic_shapes
rviz

在Rviz界面左侧的显示面板中,将固定坐标系域设置为“/my_frame”,接着点击左下侧的“Add”按钮添加一个标记显示(默认主题名为visualization_marker),你将在3D视图中看到一个每秒随时间变换的标记,如图
Rviz中使用Marker工具_第1张图片

你可能感兴趣的:(ROS,ubuntu,Rviz)