ROS学习笔记(十九):Markers: Points and Lines (C++)

1.Intro

在“标记:基本形状”中,您学习了如何使用可视化标记将简单形状发送到rviz。 您不仅可以发送简单的形状,而且本教程将向您介绍POINTS,LINE_STRIP和LINE_LIST标记类型。 有关类型的完整列表,请参见“标记显示”页面。

2.Using Points, Line Strips, and Line Lists

POINTS,LINE_STRIP和LINE_LIST标记都使用visualization_msgs / Marker消息的点成员。POINTS类型在添加的每个点处放置一个点。 LINE_STRIP类型将每个点用作一组连接的线中的点,其中点0连接到点1、1、2、2、3等。LINE_LIST类型在每对点中创建未连接的线,即点 0至1、2至3等。

2.1 The Code

  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 }

2.2 Viewing the Markers

设置rviz的方法与上一教程中的方法相同,如下所示:
编辑using_markers包中的CMakeLists.txt文件,并将其添加到底部:

add_executable(points_and_lines src/points_and_lines.cpp)
target_link_libraries(points_and_lines ${catkin_LIBRARIES})

then,

$ catkin_make

then,

$ rosrun rviz rviz &
$ rosrun using_markers points_and_lines

您应该看到一个看起来像这样的旋转螺旋:
在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值