在做位姿相关的研究时,我们希望直观的可视化轨迹,尤其是对比不同的轨迹。
本教程将向您介绍POINTS
,LINE_STRIP
和LINE_LIST
标记类型。有关类型的完整列表,请参见“标记显示”页面。
POINTS
,LINE_STRIP
和LINE_LIST
标记都使用visualization_msgs/Marker消息的点成员。
POINTS
类型在添加的每个点处放置一个点。LINE_STRIP
类型将每个点用作一组连接的线中的顶点,其中点0连接到点1,点1连接到点2,点2连接到点3等。LINE_LIST
类型在每对点(即点0到1、2到3等)中创建未连接的线。
#include
#include
#include
int main( int argc, char** argv )
{
ros::init(argc, argv, "points_and_lines");
ros::NodeHandle n;
ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 10);
ros::Rate r(30);
float f = 0.0;
while (ros::ok())
{
visualization_msgs::Marker points, line_strip, line_list;
points.header.frame_id = line_strip.header.frame_id = line_list.header.frame_id = "/my_frame";
points.header.stamp = line_strip.header.stamp = line_list.header.stamp = ros::Time::now();
points.ns = line_strip.ns = line_list.ns = "points_and_lines";
points.action = line_strip.action = line_list.action = visualization_msgs::Marker::ADD;
points.pose.orientation.w = line_strip.pose.orientation.w = line_list.pose.orientation.w = 1.0;
points.id = 0;
line_strip.id = 1;
line_list.id = 2;
points.type = visualization_msgs::Marker::POINTS;
line_strip.type = visualization_msgs::Marker::LINE_STRIP;
line_list.type = visualization_msgs::Marker::LINE_LIST;
// POINTS markers use x and y scale for width/height respectively
points.scale.x = 0.2;
points.scale.y = 0.2;
// LINE_STRIP/LINE_LIST markers use only the x component of scale, for the line width
line_strip.scale.x = 0.1;
line_list.scale.x = 0.1;
// Points are green
points.color.g = 1.0f;
points.color.a = 1.0;
// Line strip is blue
line_strip.color.b = 1.0;
line_strip.color.a = 1.0;
// Line list is red
line_list.color.r = 1.0;
line_list.color.a = 1.0;
// Create the vertices for the points and lines
for (uint32_t i = 0; i < 100; ++i)
{
float y = 5 * sin(f + i / 100.0f * 2 * M_PI);
float z = 5 * cos(f + i / 100.0f * 2 * M_PI);
geometry_msgs::Point p;
p.x = (int32_t)i - 50;
p.y = y;
p.z = z;
points.points.push_back(p);
line_strip.points.push_back(p);
// The line list needs two points for each line
line_list.points.push_back(p);
p.z += 1.0;
line_list.points.push_back(p);
}
marker_pub.publish(points);
marker_pub.publish(line_strip);
marker_pub.publish(line_list);
r.sleep();
f += 0.04;
}
}
现在,让我们分解代码,跳过上一教程中解释的内容。产生的总体效果是一个旋转的螺旋线,其线从每个顶点向上伸出。
visualization_msgs::Marker points, line_strip, line_list;
points.header.frame_id = line_strip.header.frame_id = line_list.header.frame_id = "/my_frame";
points.header.stamp = line_strip.header.stamp = line_list.header.stamp = ros::Time::now();
points.ns = line_strip.ns = line_list.ns = "points_and_lines";
points.action = line_strip.action = line_list.action = visualization_msgs::Marker::ADD;
points.pose.orientation.w = line_strip.pose.orientation.w = line_list.pose.orientation.w = 1.0;
在这里,我们创建三个visualization_msgs/Marker消息并初始化它们的所有共享数据。我们利用消息成员默认为0且仅设置位姿的成员w这一事实。
points.id = 0;
line_strip.id = 1;
line_list.id = 2;
我们为三个标记分配了三个不同的ID。使用points_and_lines
命名空间可确保它们不会与其他广播发生冲突。
points.type = visualization_msgs::Marker::POINTS;
line_strip.type = visualization_msgs::Marker::LINE_STRIP;
line_list.type = visualization_msgs::Marker::LINE_LIST;
在这里,我们将标记类型设置为POINTS
,LINE_STRIP
和LINE_LIST
。
// POINTS markers use x and y scale for width/height respectively
points.scale.x = 0.2;
points.scale.y = 0.2;
// LINE_STRIP/LINE_LIST markers use only the x component of scale, for the line width
line_strip.scale.x = 0.1;
line_list.scale.x = 0.1;
标尺(scale)成员对这些标记类型表示不同的含义。POINTS
标记分别将x和y成员用于宽度和高度,而LINE_STRIP
和LINE_LIST
标记仅使用x分量来定义线宽。比例尺值以米为单位。
// Points are green
points.color.g = 1.0f;
points.color.a = 1.0;
// Line strip is blue
line_strip.color.b = 1.0;
line_strip.color.a = 1.0;
// Line list is red
line_list.color.r = 1.0;
line_list.color.a = 1.0;
在这里,我们将点设置为绿色,将线条设置为蓝色,并将线列表设置为红色。
// Create the vertices for the points and lines
for (uint32_t i = 0; i < 100; ++i)
{
float y = 5 * sin(f + i / 100.0f * 2 * M_PI);
float z = 5 * cos(f + i / 100.0f * 2 * M_PI);
geometry_msgs::Point p;
p.x = (int32_t)i - 50;
p.y = y;
p.z = z;
points.points.push_back(p);
line_strip.points.push_back(p);
// The line list needs two points for each line
line_list.points.push_back(p);
p.z += 1.0;
line_list.points.push_back(p);
}
我们使用正弦和余弦生成一个螺旋。POINTS
和LINE_STRIP
标记每个顶点只需要一个点,而LINE_LIST
标记则需要2个。
(1)创建一个名为using_markers
的包
catkin_create_pkg using_markers roscpp visualization_msgs
(2)编辑using_markers
包中的CMakeLists.txt文件,并将其添加到底部
add_executable(points_and_lines src/points_and_lines.cpp)
target_link_libraries(points_and_lines ${catkin_LIBRARIES})
(3)编译
$ catkin_make
(4)运行
$ rosrun rviz rviz&
$ rosrun using_markers points_and_lines
点击【Add】,添加【Marker】,将【Fixed Frame】设置为【my_frame】,最后应该看到一个看起来像这样的旋转螺旋:
http://wiki.ros.org/rviz/Tutorials/Markers%3A%20Points%20and%20Lines