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

来源:互联网 发布:淘宝网人工客服热线 编辑:程序博客网 时间:2024/06/04 17:48

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, androspy 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 thepackage.xml, which contains meta information, has been tailored to your package, you are ready to move on in the tutorials. TheCMakeLists.txt file created bycatkin_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_wsSource space: /home/user/catkin_ws/srcBuild space: /home/user/catkin_ws/buildDevel space: /home/user/catkin_ws/develInstall 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
  • builddevelsrc
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.

Toggle line numbers
   1 <launch>

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.

Toggle line numbers
  16 </launch>

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


0 0
原创粉丝点击