ROS Tutorial 2 (Workspace / Catkin_create/ Catkin_make / Package / ROS nodes, topics, master etc)

Note that ‘roscd’, like other ROS tools, will only find ROS packages that are within the directories listed in your ROS_PACKAGE_PATH. To see what is in your ROS_PACKAGE_PATH, type:

$ echo $ROS_PACKAGE_PATH

一、创建Workspace

Catkin可以提供一个workspaces的概念,以编译多个互相依赖的包。workspace就是一个文件夹,你可以编译、安装、修改其中的packages。

(Catkin packages can be built as a standalone project, in the same way that normal cmake projects can be built, but catkin also provides the concept of workspaces, where you can build multiple, interdependent packages together all at once. A catkin workspace is a folder where you modify, build, and install catkin packages)

workspaces包含四个部分。

http://wiki.ros.org/catkin/workspaces

1.1 Source Space

存放源码

(The source space contains the source code of catkin package. Each folder within the source space contains one or more catkin packages. This space should remain unchanged by configuring, building, or installing. The root of the source space contains a symbolic link to catkin's boiler-plate 'toplevel' CMakeLists.txt file. This file is invoked by CMake during the configuration of the catkin projects in the workspace.)

1.2 Build Space

存放编译工具、缓存信息和即时文件。

Build space is where cmake and make are called(调用) to configure and build your packages.CMake and catkin keep their cache information and other intermediate files here. The build space is where CMake is invoked to build the catkin packages in the source space.

1.3 Development (Devel) Space

存放编译时需要的依赖项和目标项 header file / library / executable

Devel space is where your executables and libraries go before you install your packages.The development space (or devel space) is where built targets are placed prior to being installed. The way targets are organized in the devel space is the same as their layout when they are installed.

1.4 Install Space

Once targets are built, they can be installed into the install space by invoking the install target, usually with make install.

1.5 下面我们开始创建一个catkin 工作空间

$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src

即使这个工作空间是空的(在'src'目录中没有任何软件包,只有一个CMakeLists.txt链接文件),你依然可以catkin编译它:

$ cd ~/catkin_ws/
$ catkin_make

成功后能看到'build'和'devel'这两个文件夹。

在'devel'文件夹里面你可以看到几个setup.*sh文件。source这些文件中的任何一个都可以将当前工作空间(指刚刚创建的workspace)设置在ROS工作环境的最顶层。接下来首先source一下新生成的setup.*sh文件:

$ source devel/setup.bash

要想保证工作空间已配置正确需确保ROS_PACKAGE_PATH环境变量包含你的工作空间目录,采用以下命令查看:

$ echo $ROS_PACKAGE_PATH
/home/<youruser>/catkin_ws/src:/opt/ros/indigo/share:/opt/ros/indigo/stacks

个人解读:打个比方,ROS是一套有各种属性的武器库,有水属性刀,火属性刀等等。如果要选用武器,首先知道武器都在哪些地方'source  /opt/ros/kinetic/setup.bash' (已经设置在bashrc中),然后识别武器都有什么,分别是什么属性的(setup.bash有描述)。现在地球出现了一个怪兽(package),首先看一下他出现的地方‘ source workspace/devel/setup.bash’,然后识别一下这怪兽是什么属性的(setup file描述package属性),然后就可以在武器库(ROS)里面采用相应的武器(build tool)来攻击这个怪兽(package)啦!Workspace就是我们的战场(文件编译的空间)。

 

二、利用Catkin创建和编译Package

为甚需要Catkin?

ROS是一个非常松散的、互相有关联的包的大集合。这意味着许多依赖于彼此的独立软件包,利用各种编程语言、工具和代码组织约定。因此,某个包中的目标的构建过程可能与构建另一个目标的方式完全不同。Catkin专门尝试以一致的和常规的方式改进大量相关包的开发。换句话说,过去的Rosbuild和现在的Catkin旨在通过使用工具和约定来简化编译过程,从而更容易构建和运行ROS代码。没有ROS,高效地共享基于ROS的代码将更加困难。

(Catkin is the official build system of ROS and the successor to the original ROS build system, rosbuild. catkin combines CMake macros and Python scripts to provide some functionality on top of CMake's normal workflow. catkin was designed to be more conventional than rosbuild, allowing for better distribution of packages, better cross-compiling support, and better portability. catkin's workflow is very similar to CMake's but adds support for automatic 'find package' infrastructure and building multiple, dependent projects at the same time. )

 

2.1一个catkin程序包由什么组成?

Package是ROS应用程序代码的最小载体。(Packages: Packages are the software organization unit of ROS code. Each package can contain libraries, executables, scripts, or other artifacts. )

一个程序包要想称为catkin程序包必须符合以下要求:

  • 该程序包必须包含catkin compliant package.xml文件,提供有关程序包的元信息。(Manifests (package.xml): A manifest is a description of a package. It serves to define dependencies between packages and to capture meta information about the package like version, maintainer, license, etc.)

  • 程序包必须包含一个catkin版本的CMakeLists.txt文件,而Catkin metapackages中必须包含一个对CMakeList.txt文件的引用。

  • 每个目录下只能有一个程序包。
    • 这意味着在同一个目录下不能有嵌套的或者多个程序包存在。

最简单的程序包也许看起来就像这样:

  • my_package/
      CMakeLists.txt
      package.xml

2.2 在catkin工作空间中的程序包 #

开发catkin程序包的一个推荐方法是使用catkin工作空间,但是你也可以单独开发(standalone)catkin 软件包。一个简单的工作空间也许看起来像这样:

  • workspace_folder/        -- WORKSPACE
      src/                   -- SOURCE SPACE
        CMakeLists.txt       -- 'Toplevel' CMake file, provided by catkin
        package_1/
          CMakeLists.txt     -- CMakeLists.txt file for package_1
          package.xml        -- Package manifest for package_1
        ...
        package_n/
          CMakeLists.txt     -- CMakeLists.txt file for package_n
          package.xml        -- Package manifest for package_n

 

2.3 创建一个catkin程序包 #

首先切换到之前通过创建catkin工作空间教程创建的catkin工作空间中的src目录下(注意,如果不在src下创建包就不能用ros的命令操作这些包,比如下面的rospackage):

# You should have created this in the Creating a Workspace Tutorial
$ cd ~/catkin_ws/src

现在使用catkin_create_pkg命令(script)来创建一个名为'beginner_tutorials'的新程序包,这个程序包依赖于std_msgs、roscpp和rospy

$ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

这将会创建一个名为beginner_tutorials的文件夹,这个文件夹里面包含一个package.xml文件和一个CMakeLists.txt文件,这两个文件都已经自动包含了部分你在执行catkin_create_pkg命令时提供的信息。上面的std_msgs rospy roscpp都是一些依赖包。

(catkin_create_pkg命令会要求你输入package_name,如果有需要你还可以在后面添加一些需要依赖的其它程序包:)

# This is an example, do not try to run this
# catkin_create_pkg <package_name> [depend1] [depend2] [depend3]

 

2.4程序包依赖关系:一级依赖 #

(注意,如果不在src下创建包就不能用ros的命令操作这些包,比如这里的rospackage)

之前在使用catkin_create_pkg命令时提供了几个程序包作为依赖包,现在我们可以使用rospack命令工具来查看一级依赖包。这些依赖包的名称随后保存在package.xml文件中。

$ rospack depends1 beginner_tutorials 
  • std_msgs
    rospy
    roscpp

如果要查看上面三个依赖包本身的依赖包,可以继续用rospack depends1来查看。

 

2.5 自定义包

可以看官网,不赘述。

 

2.6编译包(Building ROS Packages)

在workspace的目录中(catkin_ws)执行catkin_make即可(确保所要编译的包已经在src中)

如果你的源代码不在默认工作空间中(~/catkin_ws/src),比如说存放在了my_src中,那么你可以这样来使用catkin_make:

注意: 运行以下命令时无效的,因为my_src不存在。

# In a catkin workspace
$ catkin_make --source my_src
$ catkin_make install --source my_src  # (optionally)

对于catkin_make更高级的使用方法,请参考http://wiki.ros.org/catkin/commands/catkin_make

 

三、理解ROS节点

  • Nodes: A node is an executable that uses ROS to communicate with other nodes.

  • Messages: ROS data type used when subscribing or publishing to a topic.

  • Topics: Nodes can publish messages to a topic as well as subscribe to a topic to receive messages.

  • Master: Name service for ROS (i.e. helps nodes find each other)

  • rosout: ROS equivalent of stdout/stderr

  • roscore: Master + rosout + parameter server (parameter server will be introduced later)

ROS的Master给nodes存储了topic和service的注册信息。nodes与master通信从而报告它们的注册信息。当这些node与master通信的时候,它们可以接收关于其他已注册及nodes的信息并且建立与其它已注册nodes之间的联系。当这些注册信息改变时Master也会回馈这些nodes,同时允许nodes动态创建与新nodes之间的连接。nodes之间的连接是直接的,Master仅仅提供了查询信息,就像一个DNS服务器。nodes订阅一个主题将会要求建立一个与publish该主题的nodes的连接,并且将会在同意连接协议的基础上建立该连接。

 

3.1 Parameter service

A parameter server is a shared, multi-variate dictionary that is accessible via network APIs. Nodes use this server to store and retrieve parameters at runtime. As it is not designed for high-performance, it is best used for static, non-binary data such as configuration parameters. It is meant to be globally viewable so that tools can easily inspect the configuration state of the system and modify if necessary.

The Parameter Server is implemented using XMLRPC and runs inside of the ROS Master, which means that its API is accessible via normal XMLRPC libraries.

(Parameter server 是一个共用的、多变量的dictionary,可通过网络API来使用。Nodes运行时,利用server来储存、检索参数。它最好用于静态的、非二位的数据如configuration参数。这也说明其为全局可查看的,可以查看并修改系统的configuration状态。)

 

Parameters

Parameters are named using the normal ROS naming convention. This means that ROS parameters have a hierarchy that matches the namespaces used for topics and nodes. This hierarchy is meant to protect parameter names from colliding. The hierarchical scheme also allows parameters to be accessed individually or as a tree.

(parameter具有结构性,可以为topic和node匹配namespaces,从而保护parameter的命名来防止同名冲突。)

 

四、节点循环终止条件

一个节点中循环的终止条件(利用ros:: ok 判断):

ros::ok() will return false if:

  • a SIGINT is received (Ctrl-C)
  • we have been kicked off the network by another node with the same name
  • ros::shutdown() has been called by another part of the application.

  • all ros::NodeHandles have been destroyed

参考: https://blog.csdn.net/zong596568821xp/article/details/77876725?locationNum=8&fps=1

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值