ROS学习笔记一:安装配置ROS环境

ROS学习笔记一:安装配置ROS环境

在安装完成ROS indigo之后,需要查看环境变量是否设置正确,并通过创建一个简单的实例来验证ROS能否正常运行。

1 查看环境变量

在ROS的安装过程中,我们执行了如下命令:

echo "source /opt/ros/indigo/setup.bash" >> ~/.bashrc
source ~/.bashrc

上述命令就是向当前用户添加ROS的环境变量,确认环境变量添加成功:printenv | grep ROS,如果结果类似如下,即说明环境变量设置成功:

ROS_ROOT=/opt/ros/indigo/share/ros
ROS_PACKAGE_PATH=/opt/ros/indigo/share:/opt/ros/indigo/stacks
ROS_MASTER_URI=http://localhost:11311
ROSLISP_PACKAGE_DIRECTORIES=
ROS_DISTRO=indigo
ROS_ETC_DIR=/opt/ros/indigo/etc/ros

如果没有,可能需要重新执行添加环境变量的命令。

2 创建ROS工作空间

我这里用的是ROS indigo,下面的流程适用于ROS groovy及其以后的版本。
创建一个catkin工作空间

$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src
$ catkin_init_workspace

catkin_init_workspace命令把当前目录初始化为一个ROS工作空间。此时我们来查看一下仅进行初始化之后的工作空间内容是什么样的。
src目录

mountzf@ubuntu:~/catkin_ws/src$ ls -l
total 0
lrwxrwxrwx 1 mountzf mountzf 49 Jul 17 11:23 CMakeLists.txt -> /opt/ros/indigo/share/catkin/cmake/toplevel.cmake
mountzf@ubuntu:~/catkin_ws/src$

catkin_ws目录

mountzf@ubuntu:~/catkin_ws$ ls -l
total 4
drwxrwxr-x 2 mountzf mountzf 4096 Jul 17 11:23 src

发现catkin_ws目录下仅仅有一个刚才创建的src目录,src目录下也只有一个指向一个cmake文件的符号连接文件。尽管现在catkin_ws目录是空的,但是我们仍可以编译该工作空间。

$ cd ~/catkin_ws/
$ catkin_make
Base path: /home/mountzf/catkin_ws
Source space: /home/mountzf/catkin_ws/src
Build space: /home/mountzf/catkin_ws/build
Devel space: /home/mountzf/catkin_ws/devel
Install space: /home/mountzf/catkin_ws/install
####
#### Running command: "cmake /home/mountzf/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/mountzf/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/mountzf/catkin_ws/install -G Unix Makefiles" in "/home/mountzf/catkin_ws/build"
####
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Using CATKIN_DEVEL_PREFIX: /home/mountzf/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/indigo
-- This workspace overlays: /opt/ros/indigo
-- Found PythonInterp: /usr/bin/python (found version "2.7.6")
-- Using PYTHON_EXECUTABLE: /usr/bin/python
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/mountzf/catkin_ws/build/test_results
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Found gtest sources under '/usr/src/gtest': gtests will be built
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.6.18
-- BUILD_SHARED_LIBS is on
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mountzf/catkin_ws/build
####

catkin_make命令是catkin工作空间非常有力的一个工具。此时再查看catkin_ws目录,发现多了两个文件夹:

mountzf@ubuntu:~/catkin_ws$ ls -l
total 12
drwxrwxr-x 7 mountzf mountzf 4096 Jul 17 12:10 build
drwxrwxr-x 3 mountzf mountzf 4096 Jul 17 12:10 devel
drwxrwxr-x 2 mountzf mountzf 4096 Jul 17 11:23 src

mountzf@ubuntu:~/catkin_ws/build$ ls -l
total 64
drwxrwxr-x 3 mountzf mountzf  4096 Jul 17 12:10 catkin
drwxrwxr-x 4 mountzf mountzf  4096 Jul 17 12:10 catkin_generated
-rw-rw-r-- 1 mountzf mountzf     0 Jul 17 12:10 CATKIN_IGNORE
-rw-rw-r-- 1 mountzf mountzf   125 Jul 17 12:10 catkin_make.cache
-rw-rw-r-- 1 mountzf mountzf 16071 Jul 17 12:10 CMakeCache.txt
drwxrwxr-x 9 mountzf mountzf  4096 Jul 17 12:10 CMakeFiles
-rw-rw-r-- 1 mountzf mountzf  7365 Jul 17 12:10 cmake_install.cmake
-rw-rw-r-- 1 mountzf mountzf   284 Jul 17 12:10 CTestTestfile.cmake
drwxrwxr-x 3 mountzf mountzf  4096 Jul 17 12:10 gtest
-rw-rw-r-- 1 mountzf mountzf  8578 Jul 17 12:10 Makefile
drwxrwxr-x 2 mountzf mountzf  4096 Jul 17 12:10 test_results

mountzf@ubuntu:~/catkin_ws/devel$ ls -l
total 36
-rwxr-xr-x 1 mountzf mountzf   506 Jul 17 12:10 env.sh
drwxrwxr-x 2 mountzf mountzf  4096 Jul 17 12:10 lib
-rw-r--r-- 1 mountzf mountzf   260 Jul 17 12:10 setup.bash
-rw-r--r-- 1 mountzf mountzf  2542 Jul 17 12:10 setup.sh
-rwxr-xr-x 1 mountzf mountzf 12378 Jul 17 12:10 _setup_util.py
-rw-r--r-- 1 mountzf mountzf   270 Jul 17 12:10 setup.zsh

可以看到在devel目录下,有很多setup.*sh文件,读取这些文件中的任何一个都会将当前工作空间的环境变量置于所有环境变量的最上层。如果我们打开这些文件会发现,最终都是要读取setup.sh文件,这个文件中定义了catkin_ws空间所需要的环境变量。
读取setup.sh文件:source devel/setup.sh
确认已经加载catkin工作空间环境变量:

echo $ROS_PACKAGE_PATH
/home/mountzf/catkin_ws/src:/opt/ros/indigo/share:/opt/ros/indigo/stacks

这时ROS的环境变量已经创建好了。

3 总结

初始化ROS的catkin工作空间:catkin_init_workspace
编译ROS的catkin工作空间:catkin_make
读取当前catkin工作空间的环境变量:source devel/setup.sh
验证ROS工作空间的环境变量加载成功:echo $ROS_PACKAGE_PATH

祝枫
2016年7月17日于深圳

你可能感兴趣的:(ROS,ROS学习笔记)