ROS学习笔记(五)::RVIZ:Markers::Points and Lines (C++)
1标记:点线(C ++)
说明:教如何使用visualization_msgs/标记信息向rviz发送点和线。
2Intro
In the Markers: Basic Shapes(第一段)你学会了如何发送简单的形状的基本形状,使用可视化标记物(visualization markers)。你不仅仅可以发送简单的形状,并且本教程将介绍POINTS,LINE_STRIP和LINE_LIST标记物类型。有关完整列表类型,看标记显示页面。
3.Using Points, Line Strips, and Line Lists
POINTS, LINE_STRIP and LINE_LIST标记物都使用visulazation_msgs/Marker消息的点成员。POINTS类型在每个增加的点处放置一个点。LINE_STRIP类型使用每个点作为在线路连接上的点,其中,点0是连接一个顶点1,1至2,2至3等。LINE_LIST类型创建出每对点都不连接的线,即点0到1,2至3等。
下面看代码:
   30 #include <ros/ros.h>
    
   31 #include <visualization_msgs/Marker.h>
    
   32 
    
   33 #include <cmath>
    
   34 
    
   35 int main( int argc, char** argv )
    
   36 {
    
   37   ros::init(argc, argv, "points_and_lines");
    
   38   ros::NodeHandle n;
    
   39   ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 10);
    
   40 
    
   41   ros::Rate r(30);
    
   42 
    
   43   float f = 0.0;
    
   44   while (ros::ok())
    
   45   {
    
   46 
    
   47     visualization_msgs::Marker points, line_strip, line_list;
    
   48     points.header.frame_id = line_strip.header.frame_id = line_list.header.frame_id = "/my_frame";
    
   49     points.header.stamp = line_strip.header.stamp = line_list.header.stamp = ros::Time::now();
    
   50     points.ns = line_strip.ns = line_list.ns = "points_and_lines";
    
   51     points.action = line_strip.action = line_list.action = visualization_msgs::Marker::ADD;
    
   52     points.pose.orientation.w = line_strip.pose.orientation.w = line_list.pose.orientation.w = 1.0;
    
   53 
    
   54 
    
   55 
    
   56     points.id = 0;
    
   57     line_strip.id = 1;
    
   58     line_list.id = 2;
    
   59 
    
   60 
    
   61 
    
   62     points.type = visualization_msgs::Marker::POINTS;
    
   63     line_strip.type = visualization_msgs::Marker::LINE_STRIP;
    
   64     line_list.type = visualization_msgs::Marker::LINE_LIST;
    
   65 
    
   66 
    
   67 
    
   68     // POINTS markers use x and y scale for width/height respectively
    
   69     points.scale.x = 0.2;
    
   70     points.scale.y = 0.2;
    
   71 
    
   72     // LINE_STRIP/LINE_LIST markers use only the x component of scale, for the line width
    
   73     line_strip.scale.x = 0.1;
    
   74     line_list.scale.x = 0.1;
    
   75 
    
   76 
    
   77 
    
   78     // Points are green
    
   79     points.color.g = 1.0f;
    
   80     points.color.a = 1.0;
    
   81 
    
   82     // Line strip is blue
    
   83     line_strip.color.b = 1.0;
    
   84     line_strip.color.a = 1.0;
    
   85 
    
   86     // Line list is red
    
   87     line_list.color.r = 1.0;
    
   88     line_list.color.a = 1.0;
    
   89 
    
   90 
    
   91 
    
   92     // Create the vertices for the points and lines
    
   93     for (uint32_t i = 0; i < 100; ++i)
    
   94     {
    
   95       float y = 5 * sin(f + i / 100.0f * 2 * M_PI);
    
   96       float z = 5 * cos(f + i / 100.0f * 2 * M_PI);
    
   97 
    
   98       geometry_msgs::Point p;
    
   99       p.x = (int32_t)i - 50;
    
  100       p.y = y;
    
  101       p.z = z;
    
  102 
    
  103       points.points.push_back(p);
    
  104       line_strip.points.push_back(p);
    
  105 
    
  106       // The line list needs two points for each line
    
  107       line_list.points.push_back(p);
    
  108       p.z += 1.0;
    
  109       line_list.points.push_back(p);
    
  110     }
    
  111 
    
  112 
    
  113     marker_pub.publish(points);
    
  114     marker_pub.publish(line_strip);
    
  115     marker_pub.publish(line_list);
    
  116 
    
  117     r.sleep();
    
  118 
    
  119     f += 0.04;
    
  120   }
    
  121 }
        2The Code Explained
现在,让我们打破了代码,跳过了在前面的教程解释的事情。产生的总的效果是一个旋转的螺旋用线从每个顶点向上伸出。
   47     visualization_msgs::Marker points, line_strip, line_list;
    
   48     points.header.frame_id = line_strip.header.frame_id = line_list.header.frame_id = "/my_frame";
    
   49     points.header.stamp = line_strip.header.stamp = line_list.header.stamp = ros::Time::now();
    
   50     points.ns = line_strip.ns = line_list.ns = "points_and_lines";
    
   51     points.action = line_strip.action = line_list.action = visualization_msgs::Marker::ADD;
    
   52     points.pose.orientation.w = line_strip.pose.orientation.w = line_list.pose.orientation.w = 1.0;
        在这里,我们创建三个visualization_msgs/Marker的消息并初始化所有的共享数据。我们利用消息的成员的默认为0的这一事实,只设置角速度成员w的姿态。
   56     points.id = 0;
    
   57     line_strip.id = 1;
    
   58     line_list.id = 2;
        我们给三个标记物分配三个不同的ID。points_and_lines命名空间的使用确保它们不会与其他广播碰撞。
   62     points.type = visualization_msgs::Marker::POINTS;
    
   63     line_strip.type = visualization_msgs::Marker::LINE_STRIP;
    
   64     line_list.type = visualization_msgs::Marker::LINE_LIST;
        在这里,我们设置的标记物类型为POINTS,LINE_STRIP和LINE_LIST。
  68     // POINTS markers use x and y scale for width/height respectively
    
   69     points.scale.x = 0.2;
    
   70     points.scale.y = 0.2;
    
   71 
    
   72     // LINE_STRIP/LINE_LIST markers use only the x component of scale, for the line width
    
   73     line_strip.scale.x = 0.1;
    
   74     line_list.scale.x = 0.1;
        刻度成员为这些标记物类型表示不同的东西。POINTS的标记物使用x和y成员分别对应于宽度和高度,而LINE_STRIP和LINE_LIST标记只使用x分量,它定义了线宽。刻度值是米。
   78     // Points are green
    
   79     points.color.g = 1.0f;
    
   80     points.color.a = 1.0;
    
   81 
    
   82     // Line strip is blue
    
   83     line_strip.color.b = 1.0;
    
   84     line_strip.color.a = 1.0;
    
   85 
    
   86     // Line list is red
    
   87     line_list.color.r = 1.0;
    
   88     line_list.color.a = 1.0;
        这里我们设置点POINTS颜色为绿色,LINE_STRIP为蓝色,LINE_LIST为红色。
  92     // Create the vertices for the points and lines
    
   93     for (uint32_t i = 0; i < 100; ++i)
    
   94     {
    
   95       float y = 5 * sin(f + i / 100.0f * 2 * M_PI);
    
   96       float z = 5 * cos(f + i / 100.0f * 2 * M_PI);
    
   97 
    
   98       geometry_msgs::Point p;
    
   99       p.x = (int32_t)i - 50;
    
  100       p.y = y;
    
  101       p.z = z;
    
  102 
    
  103       points.points.push_back(p);
    
  104       line_strip.points.push_back(p);
    
  105 
    
  106       // The line list needs two points for each line
    
  107       line_list.points.push_back(p);
    
  108       p.z += 1.0;
    
  109       line_list.points.push_back(p);
    
  110     }
        使用sin与cos产生螺旋线。POINTS与LINE_STRIP标记物为每个顶点都只需要一个point,而LINE_LIST标记物需要两个。
3.3Viewing the Markers
用之前教程的方法设置rviz,不同的地方如下:
3.3.1编辑using_markers包中的CMakeLists.txt文件,并添加下列两句到文件末尾:
catkin:编辑器的话
add_executable(points_and_lines src/points_and_lines.cpp)
target_link_libraries(points_and_lines ${catkin_LIBRARIES})
rosbuild:编辑器的话
rosbuild_add_executable(points_and_lines src/points_and_lines.cpp)
3.2然后:
$ rosmake using_markers
3.3.3
 rosrun rviz rviz &  
 rosrun using_markers points_and_lines
你就能看到一个旋转的螺旋线,如图:
4Next Steps
The Marker Display page has a list of all the markers and options supported by rviz. Try out some of the other markers!
