Getting Started with ROS on Embedded Systems - User Guide - C++ - Package set up

From RidgeRun Developer Connection
Jump to: navigation, search




Previous: C++ User Guide Index Next: User Guide/C++/Initialization




Introduction

This serves an introduction on how to create and build simple packages using ROS with catkin build system.

A ROS package is simply one folder located under a workspace that can be constructed by using catkin. Its two basic components are the following:

A catkin compliant package.xml file: This file provides metadata about the package, like its name, maintainer, dependencies, etc. A CMakeLists.txt which will be invoked by the catkin build system: This just needs to be a boilerplate CMakeLists.txt file, it will be very similar to a normal CMakeLists.txt but with some custom flags used by catking.

These two files need to be contained inside the package folder, without nesting or sharing folders between packages. A normal structure can look like this:

ros_workspace
└── src
    └── rr_package1
        ├── CMakeLists.txt
        └── package.xml

The top level src folder needs to be created manually, and you should always have a layout like this, and one folder for each package inside the src folder.

Creating a new package

Creating a new package is simple[1], you can create one by using the command line tool:

catkin_create_pkg <package_name> <dependencies>

For example:

catkin_create_pkg rr_package2 std_msgs roscpp

This will create a ROS package with std_msgs and roscpp packages as dependencies.

Configuring packge

The first step on configuring the package, is to modify the package.xml, you will have entries similar to the following:

<?xml version="1.0"?>
<package format="2">
  <name>rr_package2</name>
  <version>0.1.0</version>
  <description>Description of the package</description>
  <maintainer email="example@example.com">Maintainer name</maintainer>
  <license>GPL</license>
  <buildtool_depend>catkin</buildtool_depend>
  <depend>roscpp</depend>
  <depend>std_msgs</depend>
</package>

You can get more information on the ROS tutorials[2].

For now, all fields are self explanatory, besides the buildtool_depend or depend, these are just simplifications of saying which package this package depends on at build time, or at both build and execution.

Not you can also modify the CMakeLists.txt, which will be used by catkin later, to compile any code you need, normally using ROS features, this would normally have the following structure:

cmake_minimum_required(VERSION 3.5)
project(rr_package2)
set(CMAKE_CXX_STANDARD 14)

find_package(catkin REQUIRED COMPONENTS roscpp std_msgs)

add_compile_options(-Wall -pedantic -Werror)
catkin_package(
    LIBRARIES ${PROJECT_NAME}
    CATKIN_DEPENDS roscpp std_msgs
)

include_directories(${catkin_INCLUDE_DIRS})
add_executable(node_main main.cpp)
add_dependencies(node_main ${catkin_EXPORTED_TARGETS})

target_include_directories(node_main PUBLIC
    include
    ${catkin_INCLUDE_DIRS}
)

Building a package

To build a package, you will make usage of the catking_make command inside the workspace directory [3] (in the same directory as the src folder is located):

catkin_make [make_targets] [-DCMAKE_VARIABLES=...]

make_targets are the commands you would pass to make, and -DCMAKE_VARIABLES stand for any flag you want to pass to the cmake call, for example, include directories flag:

catkin_make -DCMAKE_CXX_FLAGS="-I ..."

Running this command the first time will generate 2 more folders:

  1. build: Where cmake and make are called to configure and build your packages.
  2. devel: Where the executables and libraries go before you install your packages.

Execution

To make usage of anything inside the package, you can source the workspace:

source ./devel/setup.sh

Then you can run the node or executable you have using rosrun:

rosrun rr_package2 node_main

References

Previous: C++ User Guide Index Next: User Guide/C++/Initialization