RVIZ教程1:显示Markers

一、创建功能包

catkin_create_pkg using_markers roscpp visualization_msgs

二、键入代码

新增CMakeLists.txt

1 add_executable(basic_shapes src/basic_shapes.cpp)
2 target_link_libraries(basic_shapes ${catkin_LIBRARIES})

在功能包的src下新增代码basic_shapes.cpp:

 1 //
 2 // Created by sry on 2021/7/27.
 3 //
 4 
 5 #include<ros/ros.h>
 6 #include<visualization_msgs/Marker.h>
 7 
 8 int main(int argc, char** argv)
 9 {
10     ros::init(argc, argv, "basic_shapes");
11     ros::NodeHandle n;
12     ros::Rate r(1);
13     ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 1);
14 
15     // 初始化shape为cube
16     uint32_t  shape = visualization_msgs::Marker::CUBE;
17 
18     while(ros::ok())
19     {
20         visualization_msgs::Marker marker;
21         // 设置帧ID和时间戳
22         marker.header.frame_id = "/my_frame";
23         marker.header.stamp = ros::Time::now();
24         // Set the namespace and id for this marker.  This serves to create a unique ID
25         // Any marker sent with the same namespace and id will overwrite the old one
26         marker.ns = "basic_shapes";
27         marker.id = 0;
28         // Set the marker type.  Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER
29         marker.type = shape;
30         // Set the marker action.  Options are ADD, DELETE, and new in ROS Indigo: 3 (DELETEALL)
31         marker.action = visualization_msgs::Marker::ADD;
32         // Set the pose of the marker.  This is a full 6DOF pose relative to the frame/time specified in the header
33         marker.pose.position.x = 0;
34         marker.pose.position.y = 0;
35         marker.pose.position.z = 0;
36         marker.pose.orientation.x = 0.0;
37         marker.pose.orientation.y = 0.0;
38         marker.pose.orientation.z = 0.0;
39         marker.pose.orientation.w = 1.0;
40 
41         // 设置缩放
42         marker.scale.x = 1.0;
43         marker.scale.y = 1.0;
44         marker.scale.z = 1.0;
45 
46         // 设置颜色
47         marker.color.r = 0.0f;
48         marker.color.g = 1.0f;
49         marker.color.b = 0.0f;
50         marker.color.a = 1.0;
51 
52         marker.lifetime = ros::Duration(); // 生命周期
53         // 发布marker
54         while(marker_pub.getNumSubscribers() < 1)
55         {
56             if (!ros::ok())
57             {
58                 return 0;
59             }
60             ROS_WARN_ONCE("Please create a subscriber to the marker");
61             sleep(1);
62         }
63         marker_pub.publish(marker);
64 
65         // Cycle between different shapes
66         switch (shape)
67         {
68             case visualization_msgs::Marker::CUBE:
69                 shape = visualization_msgs::Marker::SPHERE;
70                 break;
71 
72             case visualization_msgs::Marker::SPHERE:
73                 shape = visualization_msgs::Marker::ARROW;
74                 break;
75 
76             case visualization_msgs::Marker::ARROW:
77                 shape = visualization_msgs::Marker::CYLINDER;
78                 break;
79 
80             case visualization_msgs::Marker::CYLINDER:
81                 shape = visualization_msgs::Marker::MESH_RESOURCE;
82                 break;
83 
84             case visualization_msgs::Marker::MESH_RESOURCE:
85                 shape = visualization_msgs::Marker::CUBE;
86                 break;
87         }
88         r.sleep();
89     }
90 
91 
92     return -1;
93 }

三、利用RVIZ显示

  在命令行启动RVIZ,点击Add -> Marker -> 选择topic为:visualization_marker,将Fixed Frame修改为:/my_frame 。

 

 

posted @ 2021-07-27 15:43  佚名12  阅读(135)  评论(0编辑  收藏  举报