Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
安装和配置ROS环境
Description: 本教程将指导您在计算机上安装ROS和配置ROS环境。Tutorial Level: BEGINNER
Next Tutorial: ROS文件系统导览
注意:本ROS Wiki基本只针对ROS 1!如果你要使用ROS 2,请参见ROS 2 documentation website。
安装ROS
在开始学习这些教程之前,请先按照 ROS安装指南完成安装。
注意: 如果你是用软件包管理器(例如“apt”)来安装ROS的,那么这些软件包将不具备写入权限,且用户(例如你)也不应该修改编辑它们。当涉及到ROS软件包源码层面的操作或创建新ROS软件包时,你应该始终在具备读写权限的目录下工作,例如主目录(Home)。
管理环境
在安装ROS期间,你会看到提示说需要source多个setup.*sh文件中的某一个,或者添加这条命令到你的shell启动脚本里面。这些操作是必须的,因为ROS依赖于使用shell环境组合空间的概念,这使得针对不同版本的ROS或不同的软件包集开发变得更加容易。
如果你在查找和使用ROS软件包方面遇到了问题,请确保已经正确配置了环境。有个好办法可以检查,确保ROS_ROOT和ROS_PACKAGE_PATH这两个环境变量正确设置:
$ printenv | grep ROS
如果没有的话,这个时候你就需要重新source一下了。
环境变量设置文件是为你生成的,但可能来自不同的地方:
用软件包管理器安装的ROS软件包已经提供了setup.*sh文件
在rosbuild workspaces中使用类似rosws的工具生成
注意: 在整个教程中你将会经常看到分别针对rosbuild和catkin的不同操作说明,这是组织和构建ROS代码的两种可用方法。rosbuild不再维护且不推荐了,而是作为传统保留。catkin现在是组织代码的推荐方式,它使用更标准的CMake约定,并具有更大的灵活性,特别是对于希望集成外部代码库或希望发布其软件的用户。如需了解详细信息,请访问catkin or rosbuild。
如果你是通过Ubuntu上的apt工具来安装ROS的,那么你将会在/opt/ros/<distro>/目录中看到setup.*sh文件,以执行下面的source命令:
$ source /opt/ros/<distro>/setup.bash
请使用具体的ROS发行版简称代替<distro>。比如你安装的是ROS Noetic Ninjemys,则上述命令改为:
$ source /opt/ros/noetic/setup.bash
在每次打开终端时你都需要先运行上面这条后才能访问ROS相关的命令,为了避免这一繁琐过程,你可以事先在.bashrc文件中添加这条命令。这样做也可以方便你在同一台计算机上安装并随时切换到不同版本的ROS(比如kinetic和noetic)。
对于其他平台,无论你把ROS安装在哪里,都应该能找到这些setup.*sh文件。
创建ROS工作空间
这些操作方法只适用于ROS Groovy及更新版本。如果你是新用户,请选择catkin。对于ROS Fuerte及早期版本,请选择rosbuild。
下面我们开始创建和构建一个catkin工作空间:
$ mkdir -p ~/catkin_ws/src $ cd ~/catkin_ws/ $ catkin_make
catkin_make命令在catkin工作空间中是一个非常方便的工具。在你的工作空间中第一次运行它时,它会在src目录下创建一个CMakeLists.txt的链接。
对于ROS Melodic和早期版本的Python 3用户:注意,如果你从源代码构建ROS来实现Python 3的兼容性,并适当地设置您的系统(即:安装所有必需的ROS Python包的Python 3版本,例如catkin),那么在干净的catkin工作区中的第一个catkin_make命令必须是:
$ catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3
这将会配置catkin_make使用Python 3,在以后的构建中可以只使用catkin_make。
另外,如果你查看一下当前目录应该能看到build和devel这两个目录。在devel文件夹里面你可以看到几个setup.*sh文件。source这些文件中的任何一个都可以将当前工作空间设置在环境的最顶层。想要了解更多,请参考catkin文档。接下来首先source一下新生成的setup.*sh文件:
$ source devel/setup.bash
要保证工作区被安装脚本正确覆盖,需确定ROS_PACKAGE_PATH环境变量包含你当前的工作空间目录:
$ echo $ROS_PACKAGE_PATH /home/<username>/catkin_ws/src:/opt/ros/<distro>/share
When working with ROS source code, it is often useful to do so in a "workspace". For the following ROS tutorials you will need an area for working on tutorials and creating new ROS stacks and packages.
rosws is a tool that provides a uniform interface to various version control systems such as SVN, Git and Mercurial and for managing all packages installed in a ROS overlay. An extensive tutorial on rosws can be found here.
Creating a new workspace
The following command creates a new workspace in ~/fuerte_workspace which extends the set of packages installed in /opt/ros/fuerte:
rosws init ~/fuerte_workspace /opt/ros/fuerte
Note: rosws is part of the rosinstall package, which is not installed by default. The following command downloads it using the Ubuntu package manager:
sudo apt-get install python-rosinstall
Creating a sandbox directory for new packages
New packages need to be put in a path that is in the variable ROS_PACKAGE_PATH. All directories that are managed by rosws, i.e. that have been added using rosws are automatically added to the ROS_PACKAGE_PATH when the file setup.bash of the corresponding workspace is sourced. Although new packages should always be put in repositories that have been installed using rosws, it can be very convenient to have a sandbox directory where for instance packages created during the tutorials can be put without requiring any additional rosws commands. For that we create a new directory sandbox and add it to the hidden .rosinstall file with rosws:
mkdir ~/fuerte_workspace/sandbox rosws set ~/fuerte_workspace/sandbox
Every time the entries in the workspace change, it is necessary to re-source ~/fuerte_workspace/setup.bash to make sure that the updated ROS_PACKAGE_PATH is used.
source ~/fuerte_workspace/setup.bash
It is very common to replace the line source /opt/ros/fuerte/setup.bash to source the setup.bash in ~/fuerte_workspace or whichever workspace you use most often.
A more complete ROS Workspace tutorial can be found here.
Confirmation
To confirm that your package path has been set, echo the ROS_PACKAGE_PATH variable.
$ echo $ROS_PACKAGE_PATH
You should see something similar to:
/home/your_user_name/fuerte_workspace/sandbox:/opt/ros/fuerte/share:/opt/ros/fuerte/stacks
至此,环境已经搭建完成,接下来可以继续学习 ROS文件系统导览。