(一)创建工作空间
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
工作空间创建完成,在根目录下,编译整个工作空间
cd ~/catkin_ws/
catkin_make
此时,工作空间中自动生成两个文件夹:devel,build
devel文件夹中产生几个setup.*sh形成的环境变量设置脚本,使用source命令运行这些脚本文件,则工作空间中的环境变量得以生效。
source devel/setup.sh
使用命令查看环境是否设置成功:
echo $ROS_PACKAGE_PATH
环境变量成功设置如下显示:
$ echo $ROS_PACKAGE_PATH
/home/wmj/catkin_ws/src:/opt/ros/kinetic/share
如果希望环境变量再所有终端中有效,需要在终端配置文件中加入环境变量的设置:
echo "source ~/catkin_ws/devel/setup.bash">>~/.bashrc
上面的意思就是将环境变量设置到/.bashrc文件中,我们使用以下方式打开更为清晰:
gedit ~/.bashrc
在打开的文件最下面粘贴以下代码即可设置环境变量:
source ~/catkin_ws/devel/setup.bash
功能包
一个功能包的目录下包含如下几个文件:cmakelists,package等文件,功能包不能嵌套放置,只能平行放置在代码空间中。
package:描述功能包属性信息;
cmakelists:记录功能包编译规则。
创建功能包:
cd ~/catkin_ws/src
catkin_create_pkg learning std_msgs rospy roscpp
创建完功能包,回到根目录,编译并设置环境变量:
cd ~/catkin_ws
catkin_make
source ~/catkin_ws/devel/setup.sh
注意:每次新增加了功能包,都需要重新编译才可以,否则无法使用新功能包。
(二)下载实例代码
git clone https://github.com/huchunxu/ros_exploring.git
将实例代码中的功能包,都复制到工作空间中的src文件夹下,并编译:
(1)报错:
-- Could not find the required component 'ecto'. The following CMake error indicates that you either need to install the package with the same name or change your environment so that it can be found.
CMake Error at /opt/ros/kinetic/share/catkin/cmake/catkinConfig.cmake:83 (find_package):
Could not find a package configuration file provided by "ecto" with any of
the following names:
ectoConfig.cmake
ecto-config.cmake
Add the installation prefix of "ecto" to CMAKE_PREFIX_PATH or set
"ecto_DIR" to a directory containing one of the above files. If "ecto"
provides a separate development package or SDK, be sure it has been
installed.
Call Stack (most recent call first):
ork_tutorials/CMakeLists.txt:4 (find_package)