您的位置:首页 > 编程语言 > C语言/C++

RVIZ(一):Markers:sending Basic Shapes (C++)

2016-07-01 11:02 681 查看
参考:http://wiki.ros.org/rviz/Tutorials/Markers%3A%20Basic%20Shapes

Description:Shows
how to use visualization_msgs/Marker messages
to send basic shapes (cube, sphere, cylinder, arrow) to rviz.

描述:展示如何利用 visualization_msgs/Marker 消息发送基本形状(立方体,球体,圆柱体,箭头)到RVIZ

1.Intro

与其他显示不同,标记显示(Marker
Display)让您rviz可视化数据,而无需rviz知道有关这些数据的解释。相反,原始对象是通过visualization_msgs/标记的邮件发送到显示器,这让你展现的东西似箭,盒子,球体和线路。

本教程将告诉您如何发送的四个基本形式(盒,球体,圆柱体和箭头)。我们将创建一个每一秒散发出新的标记的程序,用不同形状替换最后一个。

2.Create
a Package

在开始之前,先创建一个package
名为 using_markers
catkin_create_pkg using_markers roscpp visualization_msgs

3.Sending
Markers

3.1
The code

粘贴以下到的src
/ basic_shapes.cpp:

https://raw.github.com/ros-visualization/visualization_tutorials/indigo-devel/visualization_marker_tutorials/src/basic_shapes.cpp

#include <ros/ros.h>
#include <visualization_msgs/Marker.h>

int main( int argc, char** argv )
{
ros::init(argc, argv, "basic_shapes");
ros::NodeHandle n;
ros::Rate r(1);
ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 1);

// 初始化形状类型为CUBE
uint32_t shape = visualization_msgs::Marker::CUBE;

while (ros::ok())
{
visualization_msgs::Marker marker;
// 设置坐标系ID和时间戳。参考TF turtorials
marker.header.frame_id = "/my_frame";
marker.header.stamp = ros::Time::now();

// <span style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; line-height: 18px;">为标志物设置名称空间和独一无二的ID</span>
// <span style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; line-height: 18px; background-color: rgb(248, 248, 248);">所有的标志物设置相同的名称空间,而且id会覆盖旧的。</span>
marker.ns = "basic_shapes";
marker.id = 0;

// 设置标志物类型 Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER
marker.type = shape;

// 设置标志物的行为(action)   Options are ADD, DELETE, and new in ROS Indigo: 3 (DELETEALL)
marker.action = visualization_msgs::Marker::ADD;

// 设置标志物的姿态.  This is a full 6DOF pose relative to the frame/time specified in the header
marker.pose.position.x = 0;
marker.pose.position.y = 0;
marker.pose.position.z = 0;
marker.pose.orientation.x = 0.0;
marker.pose.orientation.y = 0.0;
marker.pose.orientation.z = 0.0;
marker.pose.orientation.w = 1.0;

// <span style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; line-height: 18px;">设置标记物的尺度</span> -- 1x1x1 here means 1m on a side
marker.scale.x = 1.0;
marker.scale.y = 1.0;
marker.scale.z = 1.0;

// 设置颜色 -- be sure to set alpha to something non-zero!
marker.color.r = 0.0f;
marker.color.g = 1.0f;
marker.color.b = 0.0f;
marker.color.a = 1.0;

marker.lifetime = ros::Duration();

// Publish the marker
while (marker_pub.getNumSubscribers() < 1)
{
if (!ros::ok())
{
return 0;
}
ROS_WARN_ONCE("Please create a subscriber to the marker");
sleep(1);
}
marker_pub.publish(marker);

// Cycle between different shapes
switch (shape)
{
case visualization_msgs::Marker::CUBE:
shape = visualization_msgs::Marker::SPHERE;
break;
case visualization_msgs::Marker::SPHERE:
shape = visualization_msgs::Marker::ARROW;
break;
case visualization_msgs::Marker::ARROW:
shape = visualization_msgs::Marker::CYLINDER;
break;
case visualization_msgs::Marker::CYLINDER:
shape = visualization_msgs::Marker::CUBE;
break;
}

r.sleep();
}
}
下面编辑CMakeLists.txt文件,在文件底部添加:
add_executable(basic_shapes src/basic_shapes.cpp)
target_link_libraries(basic_shapes ${catkin_LIBRARIES})
3.2
The Code Explained
#include <ros/ros.h>
#include <visualization_msgs/Marker.h>
包含ROS头文件,并且包含<span style="font-family: 'Microsoft YaHei';">visualization_msgs 消息定义</span>
int main( int argc, char** argv )
{
ros::init(argc, argv, "basic_shapes");
ros::NodeHandle n;
ros::Rate r(1);
ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 1);
这一部分类似于之前的ROS代码,初始化ROS,创建一个Ros::Publisher的topic
uint32_t shape = visualization_msgs::Marker::CUBE;
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">在这里,我们创建了一个整数来跟踪我们要去发布的形状。我们将这里的所有四种形式以同样visualization_msgs/标记信息的方式,所以我们可以简单地切换形状类型来演示四种不同的形状。</span>
while (ros::ok())
{
visualization_msgs::Marker marker;
// Set the frame ID and timestamp.  See the TF tutorials for information on these.
marker.header.frame_id = "/my_frame";
marker.header.stamp = ros::Time::now();
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: pre;"> </span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">这开始程序的内容。首先,我们创建一个visualization_msgs/标记,并开始填写。这里头是roslib/头,如果你已经做了TF教程应该是熟悉的。我们的frame_id成员设置为“/my_frame”作为一个例子。在运行系统中,这应该是和你想中止的marker的姿态相关的框架。</span>
marker.ns = "basic_shapes";
marker.id = 0;
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">命名空间(NS)和id用来为标记创建一个唯一的名称。如果标记邮件收到相同的NS和标识,新标识将取代旧的。</span>
marker.type = shape;
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">这种类型的字段是我们发送的一种特定标记。可用的类型列举在visualization_msgs/标记信息。在这里,我们将类型设置为我们的形状,这个形状将通过循环每次改变。</span>
marker.action = visualization_msgs::Marker::ADD;
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">该行动域是指定用标记做什么。选项有visualization_msgs::标记:: ADD和visualization_msgs::标记::DELETE。ADD是有些用词不当,它真正的意思是“创建或修改”。</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: pre;"></span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">NEW in Indigo:已经添加一个新动作用于删除在特定的Rviz display的所有标记,无论ID或命名空间。该值是3,并且在未来ROS的版本将改变为具有价值visualization_msgs::标记::DELETEALL。</span>
marker.pose.position.x = 0;
marker.pose.position.y = 0;
marker.pose.position.z = 0;
marker.pose.orientation.x = 0.0;
marker.pose.orientation.y = 0.0;
marker.pose.orientation.z = 0.0;
marker.pose.orientation.w = 1.0;
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">在这里,我们设置标记的姿态。该geometry_msgs/姿态邮件由geometry_msgs/ Vector3指定的位置和geometry_msgs/Quaternion(四元)指定的方向组成。在这里,我们设置相对于到原点的位置,和相对于特定方向的方向(注意w是1.0 )。</span>
marker.scale.x = 1.0;
marker.scale.y = 1.0;
marker.scale.z = 1.0;
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">现在我们指定标记的比例尺。对于基本形状,在一面,所有方向上的1刻度是1米</span>
marker.color.r = 0.0f;
marker.color.g = 1.0f;
marker.color.b = 0.0f;
marker.color.a = 1.0;
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">标记的颜色被指定为一个std_msgs/ColorRGBA。每个成员应在0和1之间,一种0值表示完全透明(不可见),和1是完全不透明。</span>
marker.lifetime = ros::Duration();
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">寿命字段指定这标志被自动删除前停留的时间。ros::Duration()是指从不自动删除。</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; white-space: pre;"></span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">如果在寿命已达到之前接收到一个新的标志消息,其寿命值将在新的标志消息中重置。</span>
while (marker_pub.getNumSubscribers() < 1)
{
if (!ros::ok())
{
return 0;
}
ROS_WARN_ONCE("Please create a subscriber to the marker");
sleep(1);
}
marker_pub.publish(marker);
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">我们等待标记有一个用户,我们然后发布标记。请注意,您也可以使用锁定的发布者代码以代替该代码。</span>
switch (shape)
{
case visualization_msgs::Marker::CUBE:
shape = visualization_msgs::Marker::SPHERE;
break;
case visualization_msgs::Marker::SPHERE:
shape = visualization_msgs::Marker::ARROW;
break;
case visualization_msgs::Marker::ARROW:
shape = visualization_msgs::Marker::CYLINDER;
break;
case visualization_msgs::Marker::CYLINDER:
shape = visualization_msgs::Marker::CUBE;
break;
}
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">这段代码可以让我们在只发布一个标志信息时候就知道所有的四个形状,。根据目前的形状,我们设置什么下一个将要发布的形状</span>
r.sleep();<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">睡眠和环回顶端。</span>
}
}

3.3
building the code

构建代码:
$ cd %TOP_DIR_YOUR_CATKIN_WORKSPACE%
$ catkin_make

3.4 Run the code

rosrun using_markers basic_shapes

4. Viewing the Markers

现在发布标志物,需要设置Rviz去查看,首先,需要确保Rviz建立了
rosmake rviz
运行Rviz
rosrun using_markers basic_shapes
rosrun rviz rviz
如果之前从来没有使用过Rviz,可以参考 User's
Guide 

因为我们没有任何TF转换设置,首先要做的事情是设置 Fixed
Frames 为我们设置标志物所在的坐标系,/ my_frame坐标系。为了做到这一点,设置固定坐标系字段为“/ my_frame”。
接下来,添加一个 Markers 显示。注意,指定的默认主题(visualization_marker),与发布的主题相同。

 你现在在原点看到一个标记物,标记每秒改变一次形状。





5. More information

For more information on the different types of markers beyond the ones
shown here, please see the Markers
Display Page
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Rviz ROS