Catkin工作空间的机制
参考以下链接:[1]https://subscription.packtpub.com/book/hardware_and_creative/9781783554713/1/ch01lvl1sec14/setting-the-ros-workspace
ROS workspace是用来keep ROS packages. 而通常我们用catkin_based的工作空间来建立和安装ROS packages. Catkin system是ROS的官方build system,帮助我们将source code弄成在ROS workspace中的a target executable 或者libraries, 具体可以看看catkin的发展历史。
A build system is responsible for generating ‘targets’ from raw source code that can be used by an end user. These targets may be in the form of libraries, executable programs, generated scripts, exported interfaces (e.g. C++ header files) or anything else that is not static code. In ROS terminology, source code is organized into ‘packages’ where each package typically consists of one or more targets when built.
Popular build systems that are used widely in software development are GNU Make, GNU Autotools, CMake, and Apache Ant (used mainly for Java).
To build targets, the build system needs information such as the locations of tool chain components (e.g. C++ compiler), source code locations, code dependencies, external dependencies, where those dependencies are located, which targets should be built, where targets should be built, and where they should be installed. This is typically expressed in some set of configuration files read by the build system. In an IDE, this information is typically stored as part of the workspace/project meta-information (e.g. Visual C++ project file). With CMake, it is specified in a file typically called ‘CMakeLists.txt’ and with GNU Make it is within a file typically called ‘Makefile’. The build system utilizes this information to process and build source code in the appropriate order to generate targets.
ROS utilizes a custom build system, catkin, that extends CMake to manage dependencies between packages.
The core Catkin meta-build system was originally designed in order to efficiently build numerous inter-dependent, but separately developed, CMake projects.
确认ROS是否安装好,可以通过观察环境变量,如ROS_ROOT和ROS_PACKAGE_PATH
printenv | grep ROS
为了可以使用到ROS命令,如rosbuild,roscd之类,每打开一个terminal,都要跑如下命令(根据你用的ROS distribution,就用相应的名字来取代 <distro>)
source /opt/ros/<distro>/setup.bash
为了避免每打开一terminal都要跑以上的命令,我们可以将以上命令加入到.bashrc的文件去。因为每次打开一个terminal,文件.bashrc是自动会跑的。
当然了,如果你电脑上安装了多个ROS版本, 那么你可以通过以上命令来切换使用不同版本的ROS。
如何建立ROS Workspace
- 建立一个空的workspace文件夹:catkin_ws, 以及它的子文件夹src(用来 store the ROS package in,注意不是catkin_ws,而是在它的子文件夹src)
mkdir -p ~/catkin_ws/src
2. 有了src之后,我们就可以在src文件夹下create ROS packages,并且build packages了,使用的命令就是catkin_make. 就算你文件夹src下没有任何ros package,依然可以build:
cd ~/catkin_ws
catkin_make
如果是第一次运行catkin_make, 我们将会在~/catkin_ws下产生两个新的文件夹,一个是build,一个是devel。后面就不会了。每次有新package,都要跑catkin_make.
运行完catkin_make之后,~/catkin_ws/devel下会产生一个setup.bash的文件,那么我们需要在~/catkin_ws路径下运行如下命令以确保环境变量的准确性:
source devel/setup.bash
注意这里与刚才能够使用roscd等命令而跑的source /opt/ros/setup.bash不同,它负责设置ROS的global的环境变量。而source devel/setup.bash是为了设置你的ros workspace的在哪里的。
可以举一个例子,观察$ROS_PACKAGE_PATH在运行source devel/setup.bash前后的变化:
同样地,source ~/catkin_ws/devel/setup.bash也是每打开一个terminal都要跑的,因此也可以加入到.bashrc(每打开一个terminal,~/.bashrc都会自动在背后运行)里面去。同样地,如果你电脑上安装了多个ROS的workspace,那么你就要改成你要使用的工作空间:
source /path/to/your/workspace/devel/setup.bash
为了确保正确性,打印出环境变量$ROS_PACKAGE_PATH,看看是否包含了你想要用的workspace(source /path/to/your/workspace/devel/setup.bash),和你想要用的ROS版本(source /opt/ros/<distro>/setup.bash,only the system-installed packages are available. (本质上/opt/ros/<distro>/
其实也是一个catkin workspace,里面包含里ROS系统的自带的packages,因此你可以使用那些ROS commands,如roscore、roslaunch等等命令)。
看一看catkin_space的结构: