ROS学习笔记(二)-- 创建开发环境和ROS程序包

Contents

  1. 一个catkin程序包由什么组成?
  2. 在catkin工作空间中的程序包
  3. 创建一个catkin程序包
  4. 程序包依赖关系
    1. 一级依赖
    2. 间接依赖
  5. 自定义你的程序包
    1. 自定义 package.xml
      1. 描述标签
      2. 维护者标签
      3. 许可标签
      4. 依赖项标签
      5. 最后完成的 package.xml
    2. 自定义 CMakeLists.txt

 

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

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

  • 该程序包必须包含catkin compliant package.xml文件

    • 这个package.xml文件提供有关程序包的元信息。
  • 程序包必须包含一个catkin 版本的CMakeLists.txt文件,而Catkin metapackages中必须包含一个对CMakeList.txt文件的引用。

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

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

  • my_package/
      CMakeLists.txt
      package.xml

 

在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

在继续本教程之前请先按照创建catkin工作空间教程创建一个空白的catkin工作空间。


补充:创建catkin工作空间

Prerequisites

This tutorial assumes that you have installed catkin and sourced your environment. If you installed catkin via apt-get for ROS kinetic, your command would look like this:

$ source /opt/ros/kinetic/setup.bash

Let's create and build a catkin workspace:

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

The catkin_make command is a convenience tool for working with catkin workspaces. Running it the first time in your workspace, it will create a CMakeLists.txt link in your 'src' folder. Additionally, if you look in your current directory you should now have a 'build' and 'devel' folder. Inside the 'devel' folder you can see that there are now several setup.*sh files. Sourcing any of these files will overlay this workspace on top of your environment. To understand more about this see the general catkin documentation: catkin. Before continuing source your new setup.*sh file:

$ source devel/setup.bash

To make sure your workspace is properly overlayed by the setup script, make sure ROS_PACKAGE_PATH environment variable includes the directory you're in.

$ echo $ROS_PACKAGE_PATH
/home/youruser/catkin_ws/src:/opt/ros/kinetic/share

Next you should go ahead and learn how to use the workspace.

If you are following the ROS tutorials series instead of the catkin tutorials, please continue with Creating a ROS Package.

 


创建一个catkin程序包

本部分教程将演示如何使用catkin_create_pkg命令来创建一个新的catkin程序包以及创建之后都能做些什么。

首先切换到之前通过创建catkin工作空间教程创建的catkin工作空间中的src目录下:

 

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

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

 

$ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

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

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

 

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

catkin_create_pkg命令也有更多的高级功能,这些功能在catkin/commands/catkin_create_pkg中有描述。

 

程序包依赖关系

 

一级依赖

之前在使用catkin_create_pkg命令时提供了几个程序包作为依赖包,现在我们可以使用rospack命令工具来查看一级依赖包。

 

(Jan 9, 2013) There is a bug reported and already fixed in rospack in groovy, which takes sometime until the change gets reflected on your computer. If you see a similar issue like this with the next command, you can skip to the next command.

 

$ rospack depends1 beginner_tutorials 
  • std_msgs
    rospy
    roscpp

就像你看到的,rospack列出了在运行catkin_create_pkg命令时作为参数的依赖包,这些依赖包随后保存在package.xml文件中。

 

$ roscd beginner_tutorials
$ cat package.xml
  • 
    ...
      catkin
      roscpp
      rospy
      std_msgs
    ...
    

 

间接依赖

在很多情况中,一个依赖包还会有它自己的依赖包,比如,rospy还有其它依赖包。

 

(Jan 9, 2013) There is a bug reported and already fixed in rospack in groovy, which takes sometime until the change gets reflected on your computer. If you see a similar issue like this with the next command, you can skip to the next command.

 

$ rospack depends1 rospy
  • genpy
    rosgraph
    rosgraph_msgs
    roslib
    std_msgs

一个程序包还可以有好几个间接的依赖包,幸运的是使用rospack可以递归检测出所有的依赖包。

 

$ rospack depends beginner_tutorials
cpp_common
rostime
roscpp_traits
roscpp_serialization
genmsg
genpy
message_runtime
rosconsole
std_msgs
rosgraph_msgs
xmlrpcpp
roscpp
rosgraph
catkin
rospack
roslib
rospy

 

自定义你的程序包

本部分教程将剖析catkin_create_pkg命令生成的每个文件并详细描述这些文件的组成部分以及如何自定义这些文件。

 

自定义 package.xml

自动生成的package.xml文件应该在你的新程序包中。现在让我们一起来看看新生成的package.xml文件以及每一个需要你注意的标签元素。

 

描述标签

首先更新描述标签:

 

Toggle line numbers

   5   The beginner_tutorials package

 

将描述信息修改为任何你喜欢的内容,但是按照约定第一句话应该简短一些,因为它覆盖了程序包的范围。如果用一句话难以描述完全那就需要换行了。

 

维护者标签

接下来是维护者标签:

 

Toggle line numbers

   7    
   8   
   9   
  10   user

 

这是package.xml中要求填写的一个重要标签,因为它能够让其他人联系到程序包的相关人员。至少需要填写一个维护者名称,但如果有需要的话你可以添加多个。除了在标签里面填写维护者的名称外,还应该在标签的email属性中填写邮箱地址:

 

Toggle line numbers

   7   Your Name

 

 

许可标签

再接下来是许可标签,同样的也需要:

 

Toggle line numbers

  12   
  13   
  14   
  15   TODO

 

你应该选择一种许可协议并将它填写到这里。一些常见的开源许可协议有BSD、MIT、Boost Software License、GPLv2、GPLv3、LGPLv2.1和LGPLv3。你可以在Open Source Initiative中阅读其中的若干个许可协议的相关信息。对于本教程我们将使用BSD协议,因为ROS核心组件的剩余部分已经使用了该协议:

 

Toggle line numbers

   8   BSD

 

 

依赖项标签

接下来的标签用来描述程序包的各种依赖项,这些依赖项分为build_depend、buildtool_depend、run_depend、test_depend。关于这些标签的更详细介绍请参考Catkin Dependencies相关的文档。在之前的操作中,因为我们将 std_msgs、 roscpp、 和 rospy作为catkin_create_pkg命令的参数,所以生成的依赖项看起来如下:

 

Toggle line numbers

  27   
  28   
  29   
  30   
  31   
  32   
  33   
  34   
  35   
  36   
  37   
  38   catkin
  39   roscpp
  40   rospy
  41   std_msgs

 

除了catkin中默认提供的buildtool_depend,所有我们列出的依赖包都已经被添加到build_depend标签中。在本例中,因为在编译和运行时我们需要用到所有指定的依赖包,因此还需要将每一个依赖包分别添加到run_depend标签中:

 

Toggle line numbers

  12   catkin
  13 
  14   roscpp
  15   rospy
  16   std_msgs
  17 
  18   roscpp
  19   rospy
  20   std_msgs

 

 

最后完成的 package.xml

现在看下面最后去掉了注释和未使用标签后的package.xml文件就显得更加简洁了:

 

Toggle line numbers

   1 
   2 
   3   beginner_tutorials
   4   0.1.0
   5   The beginner_tutorials package
   6 
   7   Your Name
   8   BSD
   9   http://wiki.ros.org/beginner_tutorials
  10   Jane Doe
  11 
  12   catkin
  13 
  14   roscpp
  15   rospy
  16   std_msgs
  17 
  18   roscpp
  19   rospy
  20   std_msgs
  21 
  22 

 

 

自定义 CMakeLists.txt

到此,这个包含程序包元信息的package.xml文件已经按照需要完成了裁剪整理,现在你可以继续下面的教程了。catkin_create_pkg命令生成的CMakeLists.txt文件将在后续关于编译ROS程序代码的教程中讲述。

现在你已经创建了一个新的ROS程序包,接下来我们开始编译这个程序包

你可能感兴趣的:(ROS学习笔记)