創建ROS程序包(三)

本教程介紹如何使用roscreate-pkg或catkin創建一個新程序包,並使用rospack查看程序包的依賴關係。

內容

一個catkin程序包由什麼組成?
在catkin工作空間中的程序包
創建一個catkin程序包
程序包依賴關係
    一級依賴
    間接依賴
自定義你的程序包
    自定義 package.xml
        描述標籤
        維護者標籤
        許可標籤
        依賴項標籤
        最後完成的 package.xml
    自定義 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程序包

本部分教程將演示如何使用catkin_create_pkg命令來創建一個新的catkin程序包以及創建之後都能做些什麼。

首先切換到之前通過創建catkin工作空間教程創建的catkin工作空間中的src目錄下:

$ 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,如果有需要你還可以在後面添加一些需要依賴的其它程序包:

# catkin_create_pkg <package_name> [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
<package>
...
  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
...
</package>

間接依賴

在很多情況中,一個依賴包還會有它自己的依賴包,比如,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文件以及每一個需要你注意的標籤元素。

描述標籤

首先更新描述標籤:

切換行號顯示

5 The beginner_tutorials package

將描述信息修改爲任何你喜歡的內容,但是按照約定第一句話應該簡短一些,因爲它覆蓋了程序包的範圍。如果用一句話難以描述完全那就需要換行了。

維護者標籤

接下來是維護者標籤:

切換行號顯示

   7   <!-- One maintainer tag required, multiple allowed, one person per tag --> 
   8   <!-- Example:  -->
   9   <!-- <maintainer email="[email protected]">Jane Doe</maintainer> -->
  10   <maintainer email="[email protected]">user</maintainer>

這是package.xml中要求填寫的一個重要標籤,因爲它能夠讓其他人聯繫到程序包的相關人員。至少需要填寫一個維護者名稱,但如果有需要的話你可以添加多個。除了在標籤裏面填寫維護者的名稱外,還應該在標籤的email屬性中填寫郵箱地址:

切換行號顯示

7   <maintainer email="[email protected]">Your Name</maintainer>

許可標籤

再接下來是許可標籤,同樣的也需要:

切換行號顯示

  12   <!-- One license tag required, multiple allowed, one license per tag -->
  13   <!-- Commonly used license strings: -->
  14   <!--   BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
  15   <license>TODO</license>

你應該選擇一種許可協議並將它填寫到這裏。一些常見的開源許可協議有BSD、MIT、Boost Software License、GPLv2、GPLv3、LGPLv2.1和LGPLv3。你可以在Open Source Initiative中閱讀其中的若干個許可協議的相關信息。對於本教程我們將使用BSD協議,因爲ROS核心組件的剩餘部分已經使用了該協議:

切換行號顯示

   8   <license>BSD</license>

依賴項標籤

接下來的標籤用來描述程序包的各種依賴項,這些依賴項分爲build_depend、buildtool_depend、run_depend、test_depend。關於這些標籤的更詳細介紹請參考Catkin Dependencies相關的文檔。在之前的操作中,因爲我們將 std_msgs、 roscpp、 和 rospy作爲catkin_create_pkg命令的參數,所以生成的依賴項看起來如下:

切換行號顯示

  27   <!-- The *_depend tags are used to specify dependencies -->
  28   <!-- Dependencies can be catkin packages or system dependencies -->
  29   <!-- Examples: -->
  30   <!-- Use build_depend for packages you need at compile time: -->
  31   <!--   <build_depend>genmsg</build_depend> -->
  32   <!-- Use buildtool_depend for build tool packages: -->
  33   <!--   <buildtool_depend>catkin</buildtool_depend> -->
  34   <!-- Use run_depend for packages you need at runtime: -->
  35   <!--   <run_depend>python-yaml</run_depend> -->
  36   <!-- Use test_depend for packages you need only for testing: -->
  37   <!--   <test_depend>gtest</test_depend> -->
  38   <buildtool_depend>catkin</buildtool_depend>
  39   <build_depend>roscpp</build_depend>
  40   <build_depend>rospy</build_depend>
  41   <build_depend>std_msgs</build_depend>

除了catkin中默認提供的buildtool_depend,所有我們列出的依賴包都已經被添加到build_depend標籤中。在本例中,因爲在編譯和運行時我們需要用到所有指定的依賴包,因此還需要將每一個依賴包分別添加到run_depend標籤中:

切換行號顯示

  12   <buildtool_depend>catkin</buildtool_depend>
  13 
  14   <build_depend>roscpp</build_depend>
  15   <build_depend>rospy</build_depend>
  16   <build_depend>std_msgs</build_depend>
  17 
  18   <run_depend>roscpp</run_depend>
  19   <run_depend>rospy</run_depend>
  20   <run_depend>std_msgs</run_depend>

最後完成的 package.xml

現在看下面最後去掉了註釋和未使用標籤後的package.xml文件就顯得更加簡潔了:

切換行號顯示

   1 <?xml version="1.0"?>
   2 <package>
   3   <name>beginner_tutorials</name>
   4   <version>0.1.0</version>
   5   <description>The beginner_tutorials package</description>
   6 
   7   <maintainer email="[email protected]">Your Name</maintainer>
   8   <license>BSD</license>
   9   <url type="website">http://wiki.ros.org/beginner_tutorials</url>
  10   <author email="[email protected]">Jane Doe</author>
  11 
  12   <buildtool_depend>catkin</buildtool_depend>
  13 
  14   <build_depend>roscpp</build_depend>
  15   <build_depend>rospy</build_depend>
  16   <build_depend>std_msgs</build_depend>
  17 
  18   <run_depend>roscpp</run_depend>
  19   <run_depend>rospy</run_depend>
  20   <run_depend>std_msgs</run_depend>
  21 
  22 </package>

自定義 CMakeLists.txt

到此,這個包含程序包元信息的package.xml文件已經按照需要完成了裁剪整理,現在你可以繼續下面的教程了。catkin_create_pkg命令生成的CMakeLists.txt文件將在後續關於編譯ROS程序代碼的教程中講述。

現在你已經創建了一個新的ROS程序包,接下來我們開始編譯這個程序包

原文地址:http://wiki.ros.org/cn/ROS/Tutorials/CreatingPackage

發佈了112 篇原創文章 · 獲贊 162 · 訪問量 36萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章