ROS_catkin创建工作空间_官网资料整理

本文详细介绍如何在本地计算机上创建ROS工作空间,并通过catkin工具进行管理。此外,还介绍了如何使用catkin_create_pkg脚本创建新的ROS包,包括定制package.xml文件和CMakeLists.txt文件等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Create a ROS Workspace

Let's create a catkin workspace:

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

Even though the workspace is empty (there are no packages in the 'src' folder, just a singleCMakeLists.txt link) you can still "build" the workspace:

$ cd ~/catkin_ws/
$ catkin_make

The catkin_make command is a convenience tool for working with catkin workspaces. If you look in your current directory you should now have a 'build' and 'devel' folder. Inside the 'devel' folder you can see that there are now several setup.*sh files. Sourcing any of these files will overlay this workspace on top of your environment. To understand more about this see the general catkin documentation:catkin. Before continuing source your new setup.*sh file:

$ source devel/setup.bash

To make sure your workspace is properly overlayed by the setup script, make sureROS_PACKAGE_PATH environment variable includes the directory you're in. 

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

Now that your environment is setup


Creating a catkin Package

This tutorial will demonstrate how to use the catkin_create_pkg script to create a new catkin package, and what you can do with it after it has been created.

First change to the source space directory of the catkin workspace you created in theCreating a Workspace for catkin tutorial:

# You should have created this in the Creating a Workspace Tutorial
$ cd ~/catkin_ws/src

Now use the catkin_create_pkg script to create a new package called 'beginner_tutorials' which depends on std_msgs, roscpp, and rospy:

$ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

This will create a beginner_tutorials folder which contains apackage.xml and aCMakeLists.txt, which have been partially filled out with the information you gavecatkin_create_pkg.

catkin_create_pkg requires that you give it apackage_name and optionally a list of dependencies on which that package depends:

# This is an example, do not try to run this
# catkin_create_pkg <package_name> [depend1] [depend2] [depend3]

catkin_create_pkg also has more advanced functionalities which is described incatkin/commands/catkin_create_pkg.

Building a catkin workspace and sourcing the setup file

Now you need to build the packages in the catkin workspace:

$ cd ~/catkin_ws
$ catkin_make

After the workspace has been built it has created a similar structure in thedevel subfolder as you usually find under/opt/ros/$ROSDISTRO_NAME.

To add the workspace to your ROS environment you need to source the generated setup file:

$ . ~/catkin_ws/devel/setup.bash

Customizing Your Package

This part of the tutorial will look at each file generated by catkin_create_pkg and describe, line by line, each component of those files and how you can customize them for your package.

Customizing the package.xml

The generated package.xml should be in your new package. Now lets go through the new package.xml and touch up any elements that need your attention.

description tag

First update the description tag:

Toggle line numbers
   5   <description>The beginner_tutorials package</description>

Change the description to anything you like, but by convention the first sentence should be short while covering the scope of the package. If it is hard to describe the package in a single sentence then it might need to be broken up.

maintainer tags

Next comes the maintainer tag:

Toggle line numbers
   7   <!-- One maintainer tag required, multiple allowed, one person per tag --> 
   8   <!-- Example:  -->
   9   <!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
  10   <maintainer email="user@todo.todo">user</maintainer>

This is a required and important tag for the package.xml because it lets others know who to contact about the package. At least one maintainer is required, but you can have many if you like. The name of the maintainer goes into the body of the tag, but there is also an email attribute that should be filled out:

Toggle line numbers
   7   <maintainer email="you@yourdomain.tld">Your Name</maintainer>

license tags

Next is the license tag, which is also required:

Toggle line numbers
  12   <!-- One license tag required, multiple allowed, one license per tag -->
  13   <!-- Commonly used license strings: -->
  14   <!--   BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
  15   <license>TODO</license>

You should choose a license and fill it in here. Some common open source licenses are BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, and LGPLv3. You can read about several of these at theOpen Source Initiative. For this tutorial we'll use the BSD license because the rest of the core ROS components use it already:

Toggle line numbers
   8   <license>BSD</license>

dependencies tags

The next set of tags describe the dependencies of your package. The dependencies are split intobuild_depend,buildtool_depend,run_depend, test_depend. For a more detailed explanation of these tags see the documentation aboutCatkin Dependencies. Since we passedstd_msgs, roscpp, and rospy as arguments to catkin_create_pkg, the dependencies will look like this:

Toggle line numbers
  27   <!-- The *_depend tags are used to specify dependencies -->
  28   <!-- Dependencies can be catkin packages or system dependencies -->
  29   <!-- Examples: -->
  30   <!-- Use build_depend for packages you need at compile time: -->
  31   <!--   <build_depend>genmsg</build_depend> -->
  32   <!-- Use buildtool_depend for build tool packages: -->
  33   <!--   <buildtool_depend>catkin</buildtool_depend> -->
  34   <!-- Use run_depend for packages you need at runtime: -->
  35   <!--   <run_depend>python-yaml</run_depend> -->
  36   <!-- Use test_depend for packages you need only for testing: -->
  37   <!--   <test_depend>gtest</test_depend> -->
  38   <buildtool_depend>catkin</buildtool_depend>
  39   <build_depend>roscpp</build_depend>
  40   <build_depend>rospy</build_depend>
  41   <build_depend>std_msgs</build_depend>

All of our listed dependencies have been added as a build_depend for us, in addition to the default buildtool_depend on catkin. In this case we want all of our specified dependencies to be available at build and run time, so we'll add arun_depend tag for each of them as well:

Toggle line numbers
  12   <buildtool_depend>catkin</buildtool_depend>
  13 
  14   <build_depend>roscpp</build_depend>
  15   <build_depend>rospy</build_depend>
  16   <build_depend>std_msgs</build_depend>
  17 
  18   <run_depend>roscpp</run_depend>
  19   <run_depend>rospy</run_depend>
  20   <run_depend>std_msgs</run_depend>

Final package.xml

As you can see the final package.xml, without comments and unused tags, is much more concise:

Toggle line numbers
   1 <?xml version="1.0"?>
   2 <package>
   3   <name>beginner_tutorials</name>
   4   <version>0.1.0</version>
   5   <description>The beginner_tutorials package</description>
   6 
   7   <maintainer email="you@yourdomain.tld">Your Name</maintainer>
   8   <license>BSD</license>
   9   <url type="website">http://wiki.ros.org/beginner_tutorials</url>
  10   <author email="you@yourdomain.tld">Jane Doe</author>
  11 
  12   <buildtool_depend>catkin</buildtool_depend>
  13 
  14   <build_depend>roscpp</build_depend>
  15   <build_depend>rospy</build_depend>
  16   <build_depend>std_msgs</build_depend>
  17 
  18   <run_depend>roscpp</run_depend>
  19   <run_depend>rospy</run_depend>
  20   <run_depend>std_msgs</run_depend>
  21 
  22 </package>

Customizing the CMakeLists.txt

Now that the package.xml, which contains meta information, has been tailored to your package, you are ready to move on in the tutorials. The CMakeLists.txt file created by catkin_create_pkg will be covered in the later tutorials about building ROS code


Building Packages

As long as all of the system dependencies of your package are installed, we can now build your new package.

Note: If you installed ROS using apt or some other package manager, you should already have all of your dependencies.

Before continuing remember to source your environment setup file if you have not already. On Ubuntu it would be something like this:

$ source /opt/ros/%YOUR_ROS_DISTRO%/setup.bash
$ source /opt/ros/indigo/setup.bash             (For Indigo for instance) 
Using catkin_make

catkin_make is a command line tool which adds some convenience to the standard catkin workflow. You can imagine thatcatkin_make combines the calls tocmake and make in the standard CMake workflow.

Usage:

# In a catkin workspace
$ catkin_make [make_targets] [-DCMAKE_VARIABLES=...]

For people who are unfamiliar with the standard CMake workflow, it breaks down as follows:

Note: If you run the below commands it will not work, as this is just an example of how CMake generally works.

# In a CMake project
$ mkdir build
$ cd build
$ cmake ..
$ make
$ make install  # (optionally)

This process is run for each CMake project. In contrast catkin projects can be built together in workspaces. Building zero to many catkin packages in a workspace follows this work flow:

# In a catkin workspace
$ catkin_make
$ catkin_make install  # (optionally)

The above commands will build any catkin projects found in thesrc folder. This follows the recommendations set byREP128. If your source code is in a different place, say my_src then you would call catkin_make like this:

Note: If you run the below commands it will not work, as the directorymy_src does not exist.

# In a catkin workspace
$ catkin_make --source my_src
$ catkin_make install --source my_src  # (optionally)

For more advanced uses of catkin_make see the documentation: catkin/commands/catkin_make

Building Your Package

For readers of this page who are about to build your own codes, please also take a look at later tutorial(C++)/(Python) since you may need to modifyCMakeLists.txt.

You should already have a catkin workspace and a new catkin package called beginner_tutorials from the previous tutorial,Creating a Package. Go into the catkin workspace if you are not already there and look in thesrc folder:

$ cd ~/catkin_ws/
$ ls src
  • beginner_tutorials/  CMakeLists.txt@  

You should see that there is a folder called beginner_tutorials which you created with catkin_create_pkg in the previous tutorial. We can now build that package usingcatkin_make:

$ catkin_make

You should see a lot of output from cmake and themmake, which should be similar to this:

  • Base path: /home/user/catkin_ws
    Source space: /home/user/catkin_ws/src
    Build space: /home/user/catkin_ws/build
    Devel space: /home/user/catkin_ws/devel
    Install space: /home/user/catkin_ws/install
    ####
    #### Running command: "cmake /home/user/catkin_ws/src
    -DCATKIN_DEVEL_PREFIX=/home/user/catkin_ws/devel
    -DCMAKE_INSTALL_PREFIX=/home/user/catkin_ws/install" in "/home/user/catkin_ws/build"
    ####
    -- The C compiler identification is GNU 4.2.1
    -- The CXX compiler identification is Clang 4.0.0
    -- Checking whether C compiler has -isysroot
    -- Checking whether C compiler has -isysroot - yes
    -- Checking whether C compiler supports OSX deployment target flag
    -- Checking whether C compiler supports OSX deployment target flag - yes
    -- Check for working C compiler: /usr/bin/gcc
    -- Check for working C compiler: /usr/bin/gcc -- 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: /tmp/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.1") 
    -- Found PY_em: /usr/lib/python2.7/dist-packages/em.pyc
    -- Found gtest: gtests will be built
    -- catkin 0.5.51
    -- BUILD_SHARED_LIBS is on
    -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    -- ~~  traversing packages in topological order:
    -- ~~  - beginner_tutorials
    -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    -- +++ add_subdirectory(beginner_tutorials)
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/user/catkin_ws/build
    ####
    #### Running command: "make -j4" in "/home/user/catkin_ws/build"
    ####

Note that catkin_make first displays what paths it is using for each of the 'spaces'. The spaces are described in theREP128 and by documentation about catkin workspaces on the wiki:catkin/workspaces. The important thing to notice is that because of these default values several folders have been created in your catkin workspace. Take a look withls:

$ ls
  • build
    devel
    src
The build folder is the default location of the build space and is where cmake and make are called to configure and build your packages. The devel folder is the default location of the devel space, which is where your executables and libraries go before you install your packages.


Using roslaunch

roslaunch starts nodes as defined in a launch file.

Usage:

$ roslaunch [package] [filename.launch]

First go to the beginner_tutorials package wecreated andbuilt earlier:

$ roscd beginner_tutorials

If roscd says similar to roscd: No such package/stack 'beginner_tutorials', you will need tosource the environment setup file like you did at the end of thecreate_a_workspace tutorial:

$ cd ~/catkin_ws
$ source devel/setup.bash
$ roscd beginner_tutorials

Then let's make a launch directory:

$ mkdir launch
$ cd launch
  • NOTE: The directory to store launch files don't necessarily have to be named aslaunch. In fact you don't even need to store them in a directory.roslaunch command automatically looks into the passed package and detect available launch files. However, it turned out to be a good practice.

The Launch File

Now let's create a launch file called turtlemimic.launch and paste the following:

Toggle line numbers
   1 <launch>
   2 
   3   <group ns="turtlesim1">
   4     <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
   5   </group>
   6 
   7   <group ns="turtlesim2">
   8     <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
   9   </group>
  10 
  11   <node pkg="turtlesim" name="mimic" type="mimic">
  12     <remap from="input" to="turtlesim1/turtle1"/>
  13     <remap from="output" to="turtlesim2/turtle1"/>
  14   </node>
  15 
  16 </launch>

The Launch File Explained

Now, let's break the launch xml down.

Here we start the launch file with the launch tag, so that the file is identified as a launch file.

Toggle line numbers
   3   <group ns="turtlesim1">
   4     <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
   5   </group>
   6 
   7   <group ns="turtlesim2">
   8     <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
   9   </group>

Here we start two groups with a namespace tag of turtlesim1 and turtlesim2 with a turtlesim node with a name of sim. This allows us to start two simulators without having name conflicts.

Toggle line numbers
  11   <node pkg="turtlesim" name="mimic" type="mimic">
  12     <remap from="input" to="turtlesim1/turtle1"/>
  13     <remap from="output" to="turtlesim2/turtle1"/>
  14   </node>

Here we start the mimic node with the topics input and output renamed to turtlesim1 and turtlesim2. This renaming will cause turtlesim2 to mimic turtlesim1.

This closes the xml tag for the launch file.

roslaunching

Now let's roslaunch the launch file:

$ roslaunch beginner_tutorials turtlemimic.launch

Two turtlesims will start and in a new terminal send therostopic command:

For ROS Hydro and later,

  • $ rostopic pub /turtlesim1/turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'

For ROS Groovy and earlier,

  • $ rostopic pub /turtlesim1/turtle1/command_velocity turtlesim/Velocity -r 1 -- 2.0  -1.8

You will see the two turtlesims start moving even though the publish command is only being sent to turtlesim1.

mimic.png

We can also use rqt_graph to better understand what our launch file did. Run rqt's main window and select rqt_graph:

$ rqt

Or simply:

$ rqt_graph

mimiclaunch.jpg


<think>嗯,用户在创建ROScatkin_ws工作空间时遇到了问题。首先,我需要回忆一下ROS创建工作空间的标准步骤。根据引用[4],正确的步骤应该是先创建src目录,然后进入工作空间根目录执行catkin_make。但用户可能没有按照这个步骤来,或者在错误的位置运行了命令。 用户提到的错误信息是“catkin_make必须在工作空间的根目录下运行”,而系统检测到存在CMakeLists.txt。这可能是因为用户在src子目录下误放了CMakeLists.txt,或者在错误的位置执行了catkin_make。根据引用[3]中的情况,用户可能在某个已有CMakeLists.txt的目录下运行命令,导致冲突。 接下来,我需要思考解决方案。首先,确认工作空间的目录结构是否正确。正确结构应该是catkin_ws/src,没有其他多余的CMakeLists.txt文件。用户需要检查是否在src目录下误创建了CMakeLists.txt,如果有的话,需要删除它。然后,确保在catkin_ws目录下运行catkin_make,而不是在src或其他子目录里。 另外,可能用户的环境变量没有正确设置,或者ROS安装不完整。需要检查是否source了setup.bash文件,以及ROS的安装步骤是否正确完成,如引用[1]提到的安装后的初始化工序。 最后,如果问题依旧存在,可能需要彻底删除工作空间并重新创建,或者查看是否有权限问题导致无法生成必要的文件。同时,参考引用[3]提到的错误解决方法,确保所有步骤都正确执行。</think>### 分步解答 #### 问题分析 在创建ROS的`catkin_ws`工作空间时,若出现错误提示**“The specified base path contains a CMakeLists.txt but catkin_make must be invoked in the root of workspace”**,通常是由于以下原因: 1. 工作空间目录结构错误(如缺少`src`子目录)[^3][^4]。 2. 在错误的路径下执行`catkin_make`命令(如未进入工作空间根目录)。 3. 工作空间内已存在冲突的`CMakeLists.txt`文件[^3]。 --- #### 解决方法 ##### 1. 检查目录结构 正确的ROS工作空间目录应为: ``` catkin_ws/ └── src/ ``` 若未创建`src`目录,需执行: ```bash mkdir -p ~/catkin_ws/src ``` ##### 2. 进入正确路径后编译 确保在**工作空间根目录**(即`catkin_ws`)执行命令: ```bash cd ~/catkin_ws # 进入根目录 catkin_make # 编译 ``` ##### 3. 清理冲突文件 若`src`目录下存在自行添加的`CMakeLists.txt`,需删除: ```bash rm ~/catkin_ws/src/CMakeLists.txt ``` ##### 4. 重新初始化工作空间 若仍失败,可彻底删除旧目录并重建: ```bash rm -rf ~/catkin_ws # 删除旧工作空间 mkdir -p ~/catkin_ws/src cd ~/catkin_ws catkin_make ``` ##### 5. 验证ROS环境 确认已正确配置ROS环境变量: ```bash source /opt/ros/<ROS版本>/setup.bash # 如melodic/noetic ``` --- #### 附:完整流程示例 ```bash # 创建目录 mkdir -p ~/catkin_ws/src # 编译工作空间 cd ~/catkin_ws catkin_make # 配置环境变量 echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc source ~/.bashrc ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值