ROS 2 Getting Started
This section serves as a comprehensive guide to assist in creating and understanding the key points of a basic Safe DDS application that successfully communicates with a ROS2 application.
Note
It is recommended to first read the Getting Started section to gain a better understanding of the basic concepts and terminology used in this section.
Download Code
Try this example by downloading the code from this CMake project.
Create a CMake project
Create a CMake project with a folder structure as shown below:
ros2_example
├── CMakeLists.txt
└── src
└── main.cpp
After the project structure folder has been created, a minimal version of the CMakeLists.txt file can be generated as follows:
mkdir ros2_example
cd ros2_example
touch CMakeLists.txt
The CMakeLists.txt file is a crucial component, as it contains the build instructions for the project.
The following example CMakeLists.txt file is provided to assist in properly configuring the project’s build infrastructure:
cmake_minimum_required(VERSION 3.5)
project(getting_started_ros2)
find_package(safedds REQUIRED)
file(GLOB SRCS ./src/*.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -fno-exceptions -fno-rtti -Wall -Werror -Wextra -Wpedantic")
add_executable(${CMAKE_PROJECT_NAME} ${SRCS})
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE include)
target_link_libraries(${CMAKE_PROJECT_NAME} safedds)
Safe DDS application
After the project folder and CMakeLists.txt file have been set up, the next step is to create a main.cpp file that will contain the application’s source code:
mkdir src
cd src
touch main.cpp
To further illustrate a successful communication between Safe DDS and ROS 2, a simple application that publishes a ROS 2 std_msgs/String message on HelloWorldTopicROS2 topic is presented.
To achieve this, the application requires both the DDS headers and a type support.
This tutorial will provide a detailed focus on the essential considerations when establishing connections with ROS 2. For additional information, please refer to the Getting Started section.
TypeSupport
To successfully integrate with ROS 2, it is necessary to register a Type Support that aligns with a compatible message format in Safe DDS. Detailed instructions on managing Type Supports in Safe DDS can be found in the Typesupport section.
Type Name
To ensure compatibility between DDS types and ROS 2 types, it is necessary to adhere to a specific convention.
For instance, when using a DDS type that matches the ROS 2 type std_msgs/String, the corresponding DDS type name would be std_msgs::msg::dds_::String_.
This convention requires using the DDS type name version of the ROS 2 type.
In this case, the DDS type name is derived from the ROS 2 type by appending ::msg::dds_:: before the actual type name.
This convention is fully documented in the official ROS 2 Desing Documents.
// Type name
memory::container::StaticString<100> type_name("std_msgs::msg::dds_::String_");
Topic Name
In ROS 2, topic names are specified using a specific convention, as detailed in the official ROS 2 Desing Documents.
To properly define a ROS 2 topic name, it is necessary to prepend rt/ to it.
// Topic name
memory::container::StaticString<100> topic_name("rt/HelloWorldTopicROS2");
Running the application
To run the application, first navigate to the project directory, create a build folder (GETTING_STARTED_ROS2_FOLDER on the example) and generate the build files with CMake.
cmake "$GETTING_STARTED_ROS2_FOLDER" -Bbuild -DCMAKE_PREFIX_PATH="$INSTALL_SAFEDDS_FOLDER"
cmake --build build
This command will configure the project with the default settings, including the path to the Safe DDS library, and it will then build the application. Once the build is complete, the application can be run by executing the binary file from the build directory:
./ros2_example
If the application is successfully connected to the network and running, the console output should show messages similar to the following:
[DW: 0] Message: HelloWorld with index: 1
[DW: 0] Message: HelloWorld with index: 2
[DW: 0] Message: HelloWorld with index: 3
[DW: 0] Message: HelloWorld with index: 4
[DW: 0] Message: HelloWorld with index: 5
[DW: 0] Message: HelloWorld with index: 6
[DW: 0] Message: HelloWorld with index: 7
[DW: 0] Message: HelloWorld with index: 8
[DW: 0] Message: HelloWorld with index: 9
[DW: 0] Message: HelloWorld with index: 10
[DW: 0] Message: HelloWorld with index: 11
[DW: 0] Message: HelloWorld with index: 12
[DW: 0] Message: HelloWorld with index: 13
The program will continue publishing messages every second until it is manually stopped.
At this point, it can be checked at the ROS 2 CLI if the topic is available:
root@machine:/# ros2 topic list
/HelloWorldTopicROS2
/parameter_events
/rosout
In order to receive the publisher messages at ROS 2 CLI, the following command can be executed:
root@machine:/# ros2 topic echo /HelloWorldTopicROS2
data: HelloWorld with index: 1
---
data: HelloWorld with index: 2
---
data: HelloWorld with index: 3
---
Finally, ROS 2 side can publish messages that will be received by Safe DDS with the following command:
root@machine:/# ros2 topic pub /HelloWorldTopicROS2 std_msgs/String 'data: Hi Safe DDS, I am ROS 2'
publisher: beginning loop
publishing #1: std_msgs.msg.String(data='Hi Safe DDS, I am ROS 2')
publishing #2: std_msgs.msg.String(data='Hi Safe DDS, I am ROS 2')
publishing #3: std_msgs.msg.String(data='Hi Safe DDS, I am ROS 2')
publishing #4: std_msgs.msg.String(data='Hi Safe DDS, I am ROS 2')
You will be able to see incoming messages from ROS 2 in the example terminal.
[DW: 0] Message: HelloWorld with index: 22
[DW: 0] Message: HelloWorld with index: 23
[DW: 16777229] Message: Hi Safe DDS, I am ROS 2
[DW: 0] Message: HelloWorld with index: 24
[DW: 16777229] Message: Hi Safe DDS, I am ROS 2
[DW: 0] Message: HelloWorld with index: 25
[DW: 16777229] Message: Hi Safe DDS, I am ROS 2
[DW: 0] Message: HelloWorld with index: 26
[DW: 16777229] Message: Hi Safe DDS, I am ROS 2
[DW: 0] Message: HelloWorld with index: 27
[DW: 16777229] Message: Hi Safe DDS, I am ROS 2
[DW: 0] Message: HelloWorld with index: 28
[DW: 16777229] Message: Hi Safe DDS, I am ROS 2
[DW: 0] Message: HelloWorld with index: 29
[DW: 16777229] Message: Hi Safe DDS, I am ROS 2
[DW: 0] Message: HelloWorld with index: 30
[DW: 16777229] Message: Hi Safe DDS, I am ROS 2
[DW: 0] Message: HelloWorld with index: 31
[DW: 16777229] Message: Hi Safe DDS, I am ROS 2
[DW: 0] Message: HelloWorld with index: 32
[DW: 16777229] Message: Hi Safe DDS, I am ROS 2
[DW: 0] Message: HelloWorld with index: 33
[DW: 16777229] Message: Hi Safe DDS, I am ROS 2
[DW: 0] Message: HelloWorld with index: 34
[DW: 16777229] Message: Hi Safe DDS, I am ROS 2
[DW: 0] Message: HelloWorld with index: 35