Rviz教程系列第一章之Markers

本教程大部分来自http://wiki.ros.org/rviz/Tutorials/

1 basic_shapes.cpp

Markers这种显示类型不需要知道数据的来源、意思等任何信息。Markers可以显示任何发送到visualization_msgs / Marker话题上的消息。

多说无益,直接上代码:

pkg: using_markers

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);

  // Set our initial shape type to be a cube
  uint32_t shape = visualization_msgs::Marker::CUBE;

  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();

    // Set the namespace and id for this marker.  This serves to create a unique ID
    // Any marker sent with the same namespace and id will overwrite the old one
    marker.ns = "basic_shapes";
    marker.id = 0;

    // Set the marker type.  Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER
    marker.type = shape;

    // Set the marker action.  Options are ADD, DELETE, and new in ROS Indigo: 3 (DELETEALL)
    marker.action = visualization_msgs::Marker::ADD;

    // Set the pose of the marker.  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;

    // Set the scale of the marker -- 1x1x1 here means 1m on a side
    marker.scale.x = 1.0;
    marker.scale.y = 1.0;
    marker.scale.z = 1.0;

    // Set the color -- 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})

整段程序还是很简单易懂的.

2 visualization_msgs/Marker消息结构

visualization_msgs/Marker的消息类型结构如下所示:

File: visualization_msgs/Marker.msg

# See http://www.ros.org/wiki/rviz/DisplayTypes/Marker and http://www.ros.org/wiki/rviz/Tutorials/Markers%3A%20Basic%20Shapes 
for more information on using this message with rviz


Header header                      # header for time/frame information
string ns                          # Namespace to place this object in... used in conjunction with id to create a unique name for the object
int32 id                           # object ID useful in conjunction with the namespace for manipulating and deleting the object later
int32 type 	                   # Type of object
int32 action        		   # 0 add/modify an object, 1 (deprecated), 2 deletes an object, 3 deletes all objects
geometry_msgs/Pose pose            # Pose of the object
geometry_msgs/Vector3 scale        # Scale of the object 1,1,1 means default (usually 1 meter square)
std_msgs/ColorRGBA color           # Color [0.0-1.0]
duration lifetime                  # How long the object should last before being automatically deleted.  0 means forever
bool frame_locked                  # If this marker should be frame-locked, i.e. retransformed into its frame every timestep
						
 #Only used if the type specified has some use for them (eg. POINTS, LINE_STRIP, ...)
geometry_msgs/Point[] points
 #Only used if the type specified has some use for them (eg. POINTS, LINE_STRIP, ...)
 #number of colors must either be 0 or equal to the number of points
 #NOTE: alpha is not yet used
std_msgs/ColorRGBA[] colors

 # NOTE: only used for text markers
string text

 # NOTE: only used for MESH_RESOURCE markers
string mesh_resource
bool mesh_use_embedded_materials

		   # 0 add/modify an object, 1 (deprecated), 2 deletes an object, 3 deletes all objects
geometry_msgs/Pose pose            # Pose of the object
geometry_msgs/Vector3 scale        # Scale of the object 1,1,1 means default (usually 1 meter square)
std_msgs/ColorRGBA color           # Color [0.0-1.0]
duration lifetime                  # How long the object should last before being automatically deleted.  0 means forever
bool frame_locked                  # If this marker should be frame-locked, i.e. retransformed into its frame every timestep
						
 #Only used if the type specified has some use for them (eg. POINTS, LINE_STRIP, ...)
geometry_msgs/Point[] points
 #Only used if the type specified has some use for them (eg. POINTS, LINE_STRIP, ...)
 #number of colors must either be 0 or equal to the number of points
 #NOTE: alpha is not yet used
std_msgs/ColorRGBA[] colors

 # NOTE: only used for text markers
string text

 # NOTE: only used for MESH_RESOURCE markers
string mesh_resource
bool mesh_use_embedded_materials


Compact Message Definition

uint8 ARROW=0
uint8 CUBE=1
uint8 SPHERE=2
uint8 CYLINDER=3
uint8 LINE_STRIP=4
uint8 LINE_LIST=5
uint8 CUBE_LIST=6
uint8 SPHERE_LIST=7
uint8 POINTS=8
uint8 TEXT_VIEW_FACING=9
uint8 MESH_RESOURCE=10
uint8 TRIANGLE_LIST=11
uint8 ADD=0
uint8 MODIFY=0
uint8 DELETE=2
uint8 DELETEALL=3

std_msgs/Header header
string ns
int32 id
int32 type
int32 action
geometry_msgs/Pose pose
geometry_msgs/Vector3 scale
std_msgs/ColorRGBA color
duration lifetime
bool frame_locked
geometry_msgs/Point[] points
std_msgs/ColorRGBA[] colors
string text
string mesh_resource
bool mesh_use_embedded_materials

3 代码分析

好的,现在我们知道了visualization_msgs/Marker 的消息结构,接下来将目光转回到代码里

前四行标准ROS的C++结构,先初始化节点,然后定义个句柄,定义发布消息的发布变量,还有个循环的频率。

  
  45     visualization_msgs::Marker marker;
  46     // Set the frame ID and timestamp.  See the TF tutorials for information on these.
  47     marker.header.frame_id = "/my_frame";
  48     marker.header.stamp = ros::Time::now();
  49 
  50     // Set the namespace and id for this marker.  This serves to create a unique ID
  51     // Any marker sent with the same namespace and id will overwrite the old one
  52     marker.ns = "basic_shapes";
  53     marker.id = 0;
  54 
  55     // Set the marker type.  Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER
  56     marker.type = shape;
  57 
  58     // Set the marker action.  Options are ADD, DELETE, and new in ROS Indigo: 3 (DELETEALL)
  59     marker.action = visualization_msgs::Marker::ADD;
  60 
  61     // Set the pose of the marker.  This is a full 6DOF pose relative to the frame/time specified in the header
  62     marker.pose.position.x = 0;
  63     marker.pose.position.y = 0;
  64     marker.pose.position.z = 0;
  65     marker.pose.orientation.x = 0.0;
  66     marker.pose.orientation.y = 0.0;
  67     marker.pose.orientation.z = 0.0;
  68     marker.pose.orientation.w = 1.0;
  69 
  70     // Set the scale of the marker -- 1x1x1 here means 1m on a side
  71     marker.scale.x = 1.0;
  72     marker.scale.y = 1.0;
  73     marker.scale.z = 1.0;
  74 
  75     // Set the color -- be sure to set alpha to something non-zero!
  76     marker.color.r = 0.0f;
  77     marker.color.g = 1.0f;
  78     marker.color.b = 0.0f;
  79     marker.color.a = 1.0;

然后这么一大块都是对针对该消息类型的初始化。根据该消息的结构一步一步的进行初始化。

header: 每个消息都有个header,header里储存了该消息的ROS::Time,frame_id(坐标系),seq(序号)等。

ns.id:visualization_msgs/Markers的ns名称空间,id(序号)需要唯一,如果相同的话,新消息会把覆盖原来的消息。

type: 形状的类型,如箭头,box,球等等,内置在该消息结构中(见数据结构)

action: action储存的是该消息要进行的模式,ADD代表创建或修改,DELETE代表删除吧。

pose: geometry_msgs/Pose message consists of a geometry_msgs/Vector3 and a geometry_msgs/Quaternion 

scale:x,y,z三个方向上的比例

color:颜色的r g b,0到1的浮点数。 std_msgs/ColorRGBA.  a代表透明度,0表示完全透明,1表示不透明。

lifetime:在被自动删除之前显示的时间,ros::Duration()代表从不自动删除。如果在达到生命周期之前收到新的标记消息,

则生存期将被重置为新标记消息中的值。

4 rosrun

运行该节点,启动Rviz,添加Markers显示类型,topic选择刚才设置的visualization_marker,不出意外地话将会出现

一个cube。(懒了点,没自己验证下,下图为wiki.ros.org里偷的)

这是老版本的rviz,与现在的rviz界面稍有不同。

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python是一种流行的高级编程语言,特别适合用于机器人控制和自动化。ROS是一种机器人软件框架,主要用于协作机器人的控制和通信。RvizROS中常用的可视化工具,用于显示机器人和环境的三维图形和信息。 Markers是rviz中的一个重要功能,可以用来表示机器人或环境中的物体或实体。Python和ROS的结合提供了一种方便的方式来创建和控制Markers。具体步骤如下: 1.导入Python库和ROS包 首先需要导入Python的相关库以及ROS中的相关软件包。例如,在控制机器人运动的Python程序中,可能需要使用ROS中的消息类型和通信接口。 2.创建Marker 创建Marker是一个关键步骤,它可以用来表示机器人、障碍物、目标等物体。在创建Marker时,需要指定Marker的类型、位置、大小、颜色等属性。例如,可以创建一个球形Marker,指定它的位置在(x,y,z),半径为r,颜色为红色。 3.发布Marker 创建Marker后,需要将它发布到ROS的话题中,以便rviz能够显示它。在Python中,可以使用ROS发布器API将Marker发布到相应的话题中。例如,可以将上述的球形Marker发布到“/visualization_marker”话题中。 4.更新Marker 一旦发布了Marker,就可以通过更新它的属性来控制它的外观和行为。在Python中,可以使用ROS发布器API来更新已发布的Marker。例如,可以将位置属性设置为新的值(x',y',z'),使得球形Marker移动到新的位置。 总的来说,Python、ROSrviz markers的结合提供了一个强大而灵活的工具,可以用来控制机器人、显示环境、演示算法等任务。通过掌握这些技术,可以进一步提高机器人控制和机器人开发的效率和质量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值