一、在using_marker/src中编写点和线代码

vim ~/catkin_ws/src/using_marker/src/points_and_lines.cpp

  编写代码,其中有注释

#include <ros/ros.h>
#include <visualization_msgs/Marker.h> #include <cmath> 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", ); ros::Rate r(); 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; //分配3个id
points.id = ;
line_strip.id = ;
line_list.id = ; //初始化形状
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 = ; i < ; ++i)
{
float y = * sin(f + i / 100.0f * * M_PI);
float z = * cos(f + i / 100.0f * * M_PI); geometry_msgs::Point p;
p.x = (int32_t)i - ;
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;
}
}

  在CMakeLists.txt中添加

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

二、编译工程

cd ~/catkin_ws
catkin_make

三、测试

1、运行编写的发布器

rosrun using_markers points_and_lines

2、运行rviz进行设置

rosrun rviz rviz

rviz学习笔记(二)——Markers: Points and Lines (C++) 点和线-LMLPHP

3、运行结果

rviz学习笔记(二)——Markers: Points and Lines (C++) 点和线-LMLPHP

05-02 07:13