創建ros的程序包(原創博文,轉載請標明出處--周學偉http://www.cnblogs.com/zxouxuewei/)
1.一個catkin程序包由什么組成?
一個程序包要想稱為catkin程序包必須符合以下要求:
-
- 該程序包必須包含catkin compliant package.xml文件
- 這個package.xml文件提供有關程序包的元信息。
-
程序包必須包含一個catkin 版本的CMakeLists.txt文件,而Catkin metapackages中必須包含一個對CMakeList.txt文件的引用。
- 每個目錄下只能有一個程序包。
- 這意味着在同一個目錄下不能有嵌套的或者多個程序包存在。
最簡單的程序包也許看起來就像這樣:
my_package/
CMakeLists.txt
package.xml
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
在繼續本教程之前請先按照創建catkin工作空間教程創建一個空白的catkin工作空間。
3.創建一個catkin程序包
本部分教程將演示如何使用catkin_create_pkg命令來創建一個新的catkin程序包以及創建之后都能做些什么。
首先切換到之前通過創建catkin工作空間教程創建的catkin工作空間中的src目錄下:
# You should have created this in the Creating a Workspace Tutorial root@zxwubuntu-Aspire-V3-572G: cd ~/catkin_ws/src
現在使用catkin_create_pkg命令來創建一個名為'beginner_tutorials'的新程序包,這個程序包依賴於std_msgs、roscpp和rospy:
root@zxwubuntu-Aspire-V3-572G: 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 <package_name> [depend1] [depend2] [depend3]
catkin_create_pkg命令也有更多的高級功能,這些功能在catkin/commands/catkin_create_pkg中有描述。
4.程序包依賴關系
4.1.一級依賴
之前在使用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.
root@zxwubuntu-Aspire-V3-572G: rospack depends1 beginner_tutorials
就像你看到的,rospack列出了在運行catkin_create_pkg命令時作為參數的依賴包,這些依賴包隨后保存在package.xml文件中。
root@zxwubuntu-Aspire-V3-572G: roscd beginner_tutorials
root@zxwubuntu-Aspire-V3-572G: cat package.xml
4.2.間接依賴
(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.
root@zxwubuntu-Aspire-V3-572G: rospack depends1 rospy
一個程序包還可以有好幾個間接的依賴包,幸運的是使用rospack可以遞歸檢測出所有的依賴包。
root@zxwubuntu-Aspire-V3-572G: 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
5.自定義你的程序包
本部分教程將剖析catkin_create_pkg命令生成的每個文件並詳細描述這些文件的組成部分以及如何自定義這些文件。
5.1.自定義 package.xml
自動生成的package.xml文件應該在你的新程序包中。現在讓我們一起來看看新生成的package.xml文件以及每一個需要你注意的標簽元素。
首先用vim編輯器打開package.xml
root@zxwubuntu-Aspire-V3-572G: vim package.xml
5.1.1.描述標簽::
將描述信息修改為任何你喜歡的內容,但是按照約定第一句話應該簡短一些,因為它覆蓋了程序包的范圍。如果用一句話難以描述完全那就需要換行了
5.1.2.維護者標簽::
這是package.xml中要求填寫的一個重要標簽,因為它能夠讓其他人聯系到程序包的相關人員。至少需要填寫一個維護者名稱,但如果有需要的話你可以添加多個。除了在標簽里面填寫維護者的名稱 外,還應該在標簽的email屬性中填寫郵箱地址:
5.1.3.許可標簽::
你應該選擇一種許可協議並將它填寫到這里。一些常見的開源許可協議有BSD、MIT、Boost Software License、GPLv2、GPLv3、LGPLv2.1和LGPLv3。你可以在Open Source Initiative中閱讀其中 的若干個許可協議的相關信息。對於本教程我們將使用BSD協議,因為ROS核心組件的剩余部分已經使用了該協議:
5.1.4.依賴項標簽::
除了catkin中默認提供的buildtool_depend,所有我們列出的依賴包都已經被添加到build_depend標簽中。在本例中,因為在編譯和運行時我們需要用到所有指定的依賴包,因此還需要將每一個 依賴包分別添加到run_depend標簽中:去掉注釋和沒有的依賴 后最終的文件內容::
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="you@yourdomain.tld">Your Name</maintainer> 8 <license>BSD</license> 9 <url type="website">http://wiki.ros.org/beginner_tutorials</url> 10 <author email="you@yourdomain.tld">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
5.2.自定義 CMakeLists.txt
到此,這個包含程序包元信息的package.xml文件已經按照需要完成了裁剪整理,現在你可以繼續下面的教程了。catkin_create_pkg命令生成的CMakeLists.txt文件將在后續關於編譯ROS程序代碼的教程中講述。
現在你已經創建了一個新的ROS程序包,接下來我們開始編譯這個程序包