ROS学习手记:
ref: http://wiki.ros.org/hydro/Installation/Ubuntu
ref: http://wiki.ros.org/catkin/Tutorials
下面我就具体展开。读者可以直接看上面俩链接步步操作还好点。
跟着这个教程一步步来:http://wiki.ros.org/ROS/Tutorials/CreatingPackage
一个catkin Package包含的东西:1. package.xml file; 2.CMakeLists.txt; 3.其他文件.
Package不共用一个文件夹,nest(嵌套)也不行。
现在我们来Create一个Package:
$ cd ~/catkin_ws/src
$ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
Created file beginner_tutorials/CMakeLists.txt
Created file beginner_tutorials/package.xml
Created folder beginner_tutorials/include/beginner_tutorials
Created folder beginner_tutorials/src
Successfully created files in /home/exbot/catkin_ws/src/beginner_tutorials. Please adjust the values in package.xml.
Catkin_create_pkg requires that you give it a package_name and optionally a list of dependencies on which that package depends.
use the catkin_create_pkg script to create a new package called 'beginner_tutorials' which depends on std_msgs, roscpp, and rospy.
关于dependencies,这里有一段解释:
Programs often use some of the same files as each other. Rather than putting these files into each package, a separate package can be installed to provide them for all of the programs that need them. So, to install a program which needs one of these files, the package containing those files must also be installed. When a package depends on another in this way, it is known as a package dependency. By specifying dependencies, packages can be made smaller and simpler, and duplicates of files and programs are mostly removed. When you install a program, its dependencies must be installed at the same time. Usually, most of the required dependencies will already be installed, but a few extras may be needed, too. So, when you install a package, don't be surprised if several other packages are installed too - these are just dependencies which are needed for your chosen package to function properly. |
<buildtool_depend>
<build_depend>
<run_depend>
<test_depend>
<package> <name>foo_core</name> <version>1.2.4</version> <description> This package provides foo capability. </description> <maintainer email="[email protected]">Ivana Bildbotz</maintainer> <license>BSD</license> <url>http://ros.org/wiki/foo_core</url> <author>Ivana Bildbotz</author> <buildtool_depend>catkin</buildtool_depend> <build_depend>message_generation</build_depend> <build_depend>roscpp</build_depend> <build_depend>std_msgs</build_depend> <run_depend>message_runtime</run_depend> <run_depend>roscpp</run_depend> <run_depend>rospy</run_depend> <run_depend>std_msgs</run_depend> <test_depend>python-mock</test_depend> </package>
It is often convenient to group multiple packages as a single logical package. This can be accomplished throughmetapackages. A metapackage is a normal package with the following export tag in the package.xml: 为了方便,多个package可以group成单个的逻辑package。就用matapackage来实现。一个metapackage就是一个普通package带了下面的export tag 在它的package.xml里,就变成了metapackage。
<export> <metapackage /> </export>Other than a required <buildtool_depends> dependency on catkin, metapackages can only have run dependencies on packages of which they group.
cmake_minimum_required(VERSION 2.8.3) project(<PACKAGE_NAME>) find_package(catkin REQUIRED) catkin_metapackage()Note: replace <PACKAGE_NAME> with the name of the metapackage.
<url> - A URL for information on the package, typically a wiki page on ros.org.
<author> - The author(s) of the package
它就是关于Tags的文档,所以看看下面这篇文章即可。
http://wiki.ros.org/ROS/Tutorials/CreatingPackage
到Dependency Tags就完成了学习。最后的 package.xml 文件内容如下:
<?xml version="1.0"?> <package> <name>beginner_tutorials</name> <version>0.1.0</version> <description>The beginner_tutorials package</description> <maintainer email="[email protected]">Your Name</maintainer> <license>BSD</license> <url type="website">http://wiki.ros.org/beginner_tutorials</url> <author email="[email protected]">Jane Doe</author> <buildtool_depend>catkin</buildtool_depend> <build_depend>roscpp</build_depend> <build_depend>rospy</build_depend> <build_depend>std_msgs</build_depend> <run_depend>roscpp</run_depend> <run_depend>rospy</run_depend> <run_depend>std_msgs</run_depend> </package>
Now that the package.xml, which contains meta information, has been tailored to your package, you are ready to move on in the tutorials. TheCMakeLists.txt file created bycatkin_create_pkg will be covered in the later tutorials about building ROS code.
Now that you've made a new ROS package, let's build our ROS package.[Repository1]
[Stack1]
[Package1]
[Nodes]
[Messages]
[Services]
[Libraries]
[Tools]
[/Package1]
[Package2]
[Nodes]
[Messages]
[Services]
[Libraries]
[Tools]
[/Package2]
[/Stack1]
[Stack2]
...
[/Stack2]
[/Repository1]
我的exbot帮我建立的ubuntu for ros版本是ros_hydro版本,
在/opt/ros/hydro/bin目录下,可以看到有个rosstack命令,
$ rosstack list
可以列出本机已经安装的stacks, 我们还可以安装第三方 stacks完成想要的功能。
== 管理ROS环境变量 ==
ref: Configure ROS workspace - Catkin & Rosbuild
1. Check the ROS env. variables like ROS_ROOT/ROS_PACKAGE_PATH
$ export | grep ROS
-----------
declare -x ROSLISP_PACKAGE_DIRECTORIES="/home/exbot/catkin_ws/devel/share/common-lisp"
declare -x ROS_DISTRO="hydro"
declare -x ROS_ETC_DIR="/opt/ros/hydro/etc/ros"
declare -x ROS_MASTER_URI="http://localhost:11311"
declare -x ROS_PACKAGE_PATH="/home/exbot/catkin_ws/src:/opt/ros/hydro/share:/opt/ros/hydro/stacks"
declare -x ROS_ROOT="/opt/ros/hydro/share/ros"
declare -x ROS_TEST_RESULTS_DIR="/home/exbot/catkin_ws/build/test_results"
-----------
2. source the setup.sh files
# source /opt/ros/<distro>/setup.bash
== 建立ROS Workspace (catkin)==
1. create a catkin workspace
$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src
$ catkin_init_workspace
2. Make
Even though the workspace is empty (there are no packages in the 'src' folder, just a single CMakeLists.txt link) you can still "build" the workspace:
$ cd ~/catkin_ws/
$ catkin_make
3. Source
$ source devel/setup.bash
4. Check:
$ echo $ROS_PACKAGE_PATH
/home/youruser/catkin_ws/src:/opt/ros/indigo/share:/opt/ros/indigo/stacks
5. Mixing catkin and rosbuild Workspaces
refer: "ROS by Example" 4.7 and ROS wiki: Overlaying
=== 创建ROS Package (catkin) ===
1. 确认你的catkin Workspace:~/catkin_ws/
2. $ cd ~/catkin_ws/src
3. 创建package
catkin_create_pkg <package_name> [depend1] [depend2] [depend3] ... [dependn]
4. 编译catkin Workspace:
cd ~/catkin_ws
catkin_make
5. Source the generated setup file:
$ . ~/catkin_ws/devel/setup.bash
6. 工具:
rospack
roscd <package_name>
7. Customize Your Packages:
http://wiki.ros.org/ROS/Tutorials/CreatingPackage#ROS.2BAC8-Tutorials.2BAC8-catkin.2BAC8-CreatingPackage.Customizing_Your_Package
===============
再来思考一个问题:你有一个或者多个catkin workspace,同时你也有一个或者多个rosbuild workspace;而某个名字叫example_package的package在ROS库,在每个workspace里面都存在,当你运行rosrun example_package example_package 的时候,到底会运行哪个位置的example_package呢?
这就关系到overlay的概念。overlay就是一种操作,可以让不同的workspace层层覆盖,最底层的是ROS库。通过这种覆盖关系,当寻找某个package的时候,ROS会先从顶层的workspace找,如果找不到再依次往下找。也可以看成是一个路径链条,当我们想把通过overlay链接起来的workspace和ROS库的路径配入环境的时候,我们只需要source overlay链条顶层的workspace的setup.bash脚本即可。
最常见的配置是一个rosbuild workspace和一个catkin workspace了(对应的文件夹分别为 rosbuild_ws 和catkin_ws )。因为有时候我们需要使用这两种workspace里面的package,而往往是自己的package优先于ROS库里面自带的package的。
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ~/catkin_ws/
catkin_make
第一条语句创建catkin_ws文件夹,以及src文件夹;第二条和第三条语句是在src路径下初始化catkin workspace,并指定src路径是package存放的路径;第四条和最后一条语句是在catkin_ws路径下编译整个workspace,将会生成catkin workspace的结构,以及编译src下所有的package。
Note
上面的操作会自动overlay ROS库,即catkin_ws->ROS库。如果不需要再加入rosbuild workspace,则执行echo "source ~/catkin_ws/devel/setup.bash" >>~/.bashrc 就行了,跳过后面的所有步骤。
sudo apt-get install python-rosinstall
在新装的系统第一次运行rosbuild相关操作前,还要先安装一下rosinstall。
mkdir ~/rosbuild_ws
cd ~/rosbuild_ws
rosws init . ~/catkin_ws/devel
mkdir ~/rosbuild_ws/sandbox
rosws set ~/rosbuild_ws/sandbox
前两条语句创建并进入rosbuild_ws文件夹;第三条语句初始化rosbuild_ws并overlay前面创建的catkin workspace;第四条和最后一条语句创建文件夹sandbox并将其设置为package存放的文件夹。
echo "source ~/rosbuild_ws/setup.bash" >> ~/.bashrc
source ~/.bashrc
每次打开terminal,都将会执行 ~/.bashrc 脚本。因此,第一条语句将引号内的内容写入 ~/.bashrc 后,每次打开terminal,overlay顶层的rosbuild_ws的setup.bash都会被source,就不用我们手动source了。
echo $ROS_PACKAGE_PATH
确认是否显示了下面4个路径:
/home/<user>/rosbuild_ws/sandbox:/home/<user>/catkin_ws/src:/opt/ros/<distro>/share:/opt/ros/<distro>/stacks
如果不全,说明之前的overlay出了问题。再进一步做检查:
gedit ~/rosbuild_ws/.rosinstall
看看有没有下面的内容:
- setup-file: {local-name: /home/<user>/catkin_ws/devel/setup.sh} - other: {local-name: sandbox}
第一条指被overlay的catkin workspace路径,第二条指该rosbuild workspace的package存放目录。
更多的overlay概念,可以参考 [catkin_workspace_overlaying] 和 [using_rosbuild_with_catkin] 。
[catkin_conceptual_overview] | http://wiki.ros.org/catkin/conceptual_overview |
[catkin_workspace_overlaying] | http://wiki.ros.org/catkin/Tutorials/workspace_overlaying |
[using_rosbuild_with_catkin] | http://wiki.ros.org/catkin/Tutorials/using_rosbuild_with_catkin |
Compile and Use the ROS Source Code from Git: