ROS 学习踩坑笔记8-rviz marker /markerArray 显示 及删除问题

RVIZ提供了两种官方支持的RVIZ可视化插件,分别是rviz_marker和rviz_marker_array, 作用是显示一些圆形,圆柱等可视化marker,前者是一次显示一个marker对象,后者是一次显示一系列的marker对象。

具体使用方法可以见ROS tutorial: https://wiki.ros.org/rviz/Tutorials/Markers%3A%20Points%20and%20Lines

问题描述: 在添加了Marker之后,设置marker的 duration_time不起作用,表现形式是:marker一直显示,不会消失;

                   使用delete指令也不能将已经显示在rviz内部的marker删除;

                    以下两条语句均不起作用

marker.lifetime = ros::Duration(0.01);
marker_array_msg.markers[i].action = visualization_msgs::Marker::ADD;

解决方法: 

在同一个Marker topic中,发布颜色为透明的marker,即可起到将marker删除的功能(将每个marker的颜色.a设为0)。

具体见下边的代码:

#include "ros/ros.h"
#include "visualization_msgs/Marker.h"
#include 

int
main (int argc, char** argv)
{

visualization_msgs::Marker marker;
visualization_msgs::MarkerArray marker_array_msg;

  // Initialize ROS
  ros::init (argc, argv, "pcl_tabletop");
  ros::NodeHandle nh;
  ros::Rate r(5);
  ros::Publisher pub_marker = nh.advertise("normals_marker_array", 100);
//pub_marker1 = nh.advertise("normals_marker", 0);
/*while(1)
{

marker.header.frame_id = "base_link";
marker.header.stamp = ros::Time();
marker.ns = "my_namespace";
marker.id = 120;
marker.type = visualization_msgs::Marker::SPHERE;
marker.action = visualization_msgs::Marker::ADD;
marker.pose.position.x = 1;
marker.pose.position.y = 1;
marker.pose.position.z = 1;
marker.pose.orientation.x = 0.0;
marker.pose.orientation.y = 0.0;
marker.pose.orientation.z = 0.0;
marker.pose.orientation.w = 1.0;
marker.scale.x = 1;
marker.scale.y = 0.1;
marker.scale.z = 0.1;
marker.color.a = 1.0;
marker.color.r = 1.0;
marker.color.g = 0.0;
marker.color.b = 0.0;

//pub_marker.publish(marker_array_msg);

pub_marker1.publish(marker);
}*/

marker_array_msg.markers.resize(5);//final->width * final->height);

while(1)
{

for ( int i = 0; i < 5; i++)
{
    marker_array_msg.markers[i].header.frame_id = "base_link";
    marker_array_msg.markers[i].header.stamp = ros::Time();
    marker_array_msg.markers[i].ns = "my_namespace";
    marker_array_msg.markers[i].id = i;
    marker_array_msg.markers[i].type = visualization_msgs::Marker::SPHERE;
    marker_array_msg.markers[i].action = visualization_msgs::Marker::ADD;
    marker_array_msg.markers[i].pose.position.x = i+1;
    marker_array_msg.markers[i].pose.position.y = 1+i;
    marker_array_msg.markers[i].pose.position.z = 1+i;
    marker_array_msg.markers[i].pose.orientation.x = 0.0;
    marker_array_msg.markers[i].pose.orientation.y = 0.0;
    marker_array_msg.markers[i].pose.orientation.z = 0.0;
    marker_array_msg.markers[i].pose.orientation.w = 1.0;
    marker_array_msg.markers[i].scale.x = 1;
    marker_array_msg.markers[i].scale.y = 0.1;
    marker_array_msg.markers[i].scale.z = 0.1;
    marker_array_msg.markers[i].color.a = 1.0;
    marker_array_msg.markers[i].color.r = 0.0;
   // marker_array_msg.markers[i].lifetime = ros::Duration(1);
    if (i == 0)
    {
        marker_array_msg.markers[i].color.g = 0.1;
    }
    else
    {
        marker_array_msg.markers[i].color.g = i * 0.15;
    }
    marker_array_msg.markers[i].color.b = 0.0;

     //marker_array_msg.markers.push_back(mark);
}
 pub_marker.publish(marker_array_msg);
 r.sleep();

 for ( int i = 0; i < 5; i++)
{
    marker_array_msg.markers[i].header.frame_id = "base_link";
    marker_array_msg.markers[i].header.stamp = ros::Time();
    marker_array_msg.markers[i].ns = "my_namespace";
    marker_array_msg.markers[i].id = i;
    marker_array_msg.markers[i].type = visualization_msgs::Marker::SPHERE;
    marker_array_msg.markers[i].action = visualization_msgs::Marker::ADD;
    marker_array_msg.markers[i].pose.position.x = i+1;
    marker_array_msg.markers[i].pose.position.y = 1+i;
    marker_array_msg.markers[i].pose.position.z = 1+i;
    marker_array_msg.markers[i].pose.orientation.x = 0.0;
    marker_array_msg.markers[i].pose.orientation.y = 0.0;
    marker_array_msg.markers[i].pose.orientation.z = 0.0;
    marker_array_msg.markers[i].pose.orientation.w = 1.0;
    marker_array_msg.markers[i].scale.x = 1;
    marker_array_msg.markers[i].scale.y = 0.1;
    marker_array_msg.markers[i].scale.z = 0.1;
    marker_array_msg.markers[i].color.a = 0.0;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    marker_array_msg.markers[i].color.r = 0.0;
   // marker_array_msg.markers[i].lifetime = ros::Duration(1);
    if (i == 0)
    {
        marker_array_msg.markers[i].color.g = 0.1;
    }
    else
    {
        marker_array_msg.markers[i].color.g = i * 0.15;
    }
    marker_array_msg.markers[i].color.b = 0.0;

     //marker_array_msg.markers.push_back(mark);
}
 pub_marker.publish(marker_array_msg);
 r.sleep();





}

  // Spin
  ros::spin ();
}

 

你可能感兴趣的:(ROS学习历程,ROS踩坑及解决方法记录)