Skip to content

Commit 693b82c

Browse files
committed
Add Topic Keys tutorial
Signed-off-by: Mario Dominguez <[email protected]>
1 parent f42056c commit 693b82c

21 files changed

+5472
-0
lines changed

source/Tutorials/Advanced.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Advanced
55
:maxdepth: 1
66

77
Advanced/Topic-Statistics-Tutorial/Topic-Statistics-Tutorial
8+
Advanced/Topic-Keys/Topic-Keys-Tutorial
89
Advanced/Discovery-Server/Discovery-Server
910
Advanced/Allocator-Template-Tutorial
1011
Advanced/Ament-Lint-For-Clean-Code

source/Tutorials/Advanced/Topic-Keys/Topic-Keys-Tutorial.rst

Lines changed: 350 additions & 0 deletions
Large diffs are not rendered by default.
Loading
Loading
Loading
Loading

source/Tutorials/Advanced/Topic-Keys/figures/tutorial_diagram.svg

Lines changed: 4460 additions & 0 deletions
Loading
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cmake_minimum_required(VERSION 3.12)
16+
17+
project(demo_keys_cpp)
18+
19+
# Default to C++17
20+
if(NOT CMAKE_CXX_STANDARD)
21+
set(CMAKE_CXX_STANDARD 17)
22+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
23+
endif()
24+
25+
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
26+
add_compile_options(-Wall -Wextra -Wpedantic)
27+
endif()
28+
29+
find_package(ament_cmake REQUIRED)
30+
find_package(example_interfaces REQUIRED)
31+
find_package(rcl REQUIRED)
32+
find_package(rcl_interfaces REQUIRED)
33+
find_package(rclcpp REQUIRED)
34+
find_package(rclcpp_components REQUIRED)
35+
find_package(rcpputils REQUIRED)
36+
find_package(rcutils REQUIRED)
37+
find_package(rmw REQUIRED)
38+
find_package(rosidl_default_generators REQUIRED)
39+
40+
# Generate message
41+
rosidl_generate_interfaces(${PROJECT_NAME}
42+
"msg/SensorDataMsg.idl"
43+
"msg/KeyedSensorDataMsg.idl"
44+
)
45+
46+
ament_export_dependencies(rosidl_default_runtime)
47+
48+
# Prepare libraries
49+
add_library(single_topic_sensor_component src/single_topic_sensor.cpp)
50+
ament_target_dependencies(single_topic_sensor_component rclcpp rclcpp_components)
51+
52+
add_library(single_topic_controller_component src/single_topic_controller.cpp)
53+
ament_target_dependencies(single_topic_controller_component rclcpp rclcpp_components)
54+
55+
add_library(multiple_topic_sensor_component src/multiple_topic_sensor.cpp)
56+
ament_target_dependencies(multiple_topic_sensor_component rclcpp rclcpp_components)
57+
58+
add_library(multiple_topic_controller_component src/multiple_topic_controller.cpp)
59+
ament_target_dependencies(multiple_topic_controller_component rclcpp rclcpp_components)
60+
61+
add_library(keyed_sensor_component src/keyed_sensor.cpp)
62+
ament_target_dependencies(keyed_sensor_component rclcpp rclcpp_components)
63+
64+
add_library(keyed_controller_component src/keyed_controller.cpp)
65+
ament_target_dependencies(keyed_controller_component rclcpp rclcpp_components)
66+
67+
# Declare components
68+
rclcpp_components_register_node(
69+
single_topic_sensor_component
70+
PLUGIN "demo_keys_cpp::SingleTopicSensorNode"
71+
EXECUTABLE single_topic_sensor
72+
)
73+
74+
rclcpp_components_register_node(
75+
single_topic_controller_component
76+
PLUGIN "demo_keys_cpp::SingleTopicControllerNode"
77+
EXECUTABLE single_topic_controller
78+
)
79+
80+
rclcpp_components_register_node(
81+
multiple_topic_sensor_component
82+
PLUGIN "demo_keys_cpp::MultipleTopicSensorNode"
83+
EXECUTABLE multiple_topic_sensor
84+
)
85+
86+
rclcpp_components_register_node(
87+
multiple_topic_controller_component
88+
PLUGIN "demo_keys_cpp::MultipleTopicControllerNode"
89+
EXECUTABLE multiple_topic_controller
90+
)
91+
92+
rclcpp_components_register_node(
93+
keyed_sensor_component
94+
PLUGIN "demo_keys_cpp::KeyedSensorNode"
95+
EXECUTABLE keyed_sensor
96+
)
97+
98+
rclcpp_components_register_node(
99+
keyed_controller_component
100+
PLUGIN "demo_keys_cpp::KeyedControllerNode"
101+
EXECUTABLE keyed_controller
102+
)
103+
104+
# Attach to cpp generation target
105+
rosidl_get_typesupport_target(cpp_typesupport_target
106+
${PROJECT_NAME} rosidl_typesupport_cpp)
107+
108+
# Link against the generated source files
109+
target_link_libraries(single_topic_sensor_component "${cpp_typesupport_target}")
110+
target_link_libraries(single_topic_controller_component "${cpp_typesupport_target}")
111+
target_link_libraries(multiple_topic_sensor_component "${cpp_typesupport_target}")
112+
target_link_libraries(multiple_topic_controller_component "${cpp_typesupport_target}")
113+
target_link_libraries(keyed_sensor_component "${cpp_typesupport_target}")
114+
target_link_libraries(keyed_controller_component "${cpp_typesupport_target}")
115+
116+
# Install
117+
install(TARGETS
118+
single_topic_sensor_component
119+
single_topic_controller_component
120+
multiple_topic_sensor_component
121+
multiple_topic_controller_component
122+
keyed_sensor_component
123+
keyed_controller_component
124+
ARCHIVE DESTINATION lib
125+
LIBRARY DESTINATION lib
126+
RUNTIME DESTINATION bin)
127+
128+
129+
# Install launch files.
130+
install(DIRECTORY
131+
launch
132+
DESTINATION share/${PROJECT_NAME}/
133+
)
134+
135+
ament_package()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Fast DDS - ROS 2 Topic Keys Demo
2+
3+
This project contains code for demonstrating the how-to-use topic keys feature in ROS 2 with Fast DDS.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from launch import LaunchDescription
16+
from launch_ros.actions import Node
17+
18+
def generate_launch_description():
19+
20+
sensor_nodes = []
21+
22+
for id in range(1, 11):
23+
24+
sensor_node = Node(
25+
package="demo_keys_cpp",
26+
executable="keyed_sensor",
27+
parameters=[
28+
{"id": id}
29+
])
30+
31+
sensor_nodes.append(sensor_node)
32+
33+
return LaunchDescription(sensor_nodes)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from launch import LaunchDescription
16+
from launch_ros.actions import Node
17+
18+
def generate_launch_description():
19+
20+
sensor_nodes = []
21+
22+
for id in range(1, 11):
23+
24+
sensor_node = Node(
25+
package="demo_keys_cpp",
26+
executable="multiple_topic_sensor",
27+
parameters=[
28+
{"id": id}
29+
])
30+
31+
sensor_nodes.append(sensor_node)
32+
33+
return LaunchDescription(sensor_nodes)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from launch import LaunchDescription
16+
from launch_ros.actions import Node
17+
18+
def generate_launch_description():
19+
20+
sensor_nodes = []
21+
22+
for id in range(1, 11):
23+
24+
sensor_node = Node(
25+
package="demo_keys_cpp",
26+
executable="single_topic_sensor",
27+
parameters=[
28+
{"id": id}
29+
])
30+
31+
sensor_nodes.append(sensor_node)
32+
33+
return LaunchDescription(sensor_nodes)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
int16 sensor_id
2+
string data
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
int16 sensor_id
2+
string data
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="3">
4+
<name>demo_keys_cpp</name>
5+
<version>0.0.0</version>
6+
<description>
7+
C++ demo package for showing the topic keys feature.
8+
</description>
9+
10+
<maintainer email="[email protected]">Mario Dominguez</maintainer>
11+
<license>Apache License 2.0</license>
12+
<author email="[email protected]">Mario Dominguez</author>
13+
<buildtool_depend>ament_cmake</buildtool_depend>
14+
15+
<build_depend>example_interfaces</build_depend>
16+
<build_depend>rcl_interfaces</build_depend>
17+
<build_depend>rcl</build_depend>
18+
<build_depend>rclcpp_components</build_depend>
19+
<build_depend>rclcpp</build_depend>
20+
<build_depend>rcpputils</build_depend>
21+
<build_depend>rcutils</build_depend>
22+
<build_depend>rmw</build_depend>
23+
<build_depend>rosidl_default_generators</build_depend>
24+
25+
<exec_depend>example_interfaces</exec_depend>
26+
<exec_depend>launch_ros</exec_depend>
27+
<exec_depend>launch_xml</exec_depend>
28+
<exec_depend>rcl_interfaces</exec_depend>
29+
<exec_depend>rcl</exec_depend>
30+
<exec_depend>rclcpp_components</exec_depend>
31+
<exec_depend>rclcpp</exec_depend>
32+
<exec_depend>rcpputils</exec_depend>
33+
<exec_depend>rcutils</exec_depend>
34+
<exec_depend>rmw</exec_depend>
35+
<exec_depend>rosidl_default_runtime</exec_depend>
36+
37+
<member_of_group>rosidl_interface_packages</member_of_group>
38+
39+
<test_depend>ament_cmake_pytest</test_depend>
40+
<test_depend>ament_lint_auto</test_depend>
41+
<test_depend>ament_lint_common</test_depend>
42+
<test_depend>launch</test_depend>
43+
<test_depend>launch_testing</test_depend>
44+
<test_depend>launch_testing_ament_cmake</test_depend>
45+
<test_depend>launch_testing_ros</test_depend>
46+
47+
<export>
48+
<build_type>ament_cmake</build_type>
49+
</export>
50+
</package>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
// Copyright 2024 Open Source Robotics Foundation, Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#include <demo_keys_cpp/msg/keyed_sensor_data_msg.hpp>
17+
#include <rclcpp_components/register_node_macro.hpp>
18+
#include <rclcpp/rclcpp.hpp>
19+
20+
namespace demo_keys_cpp
21+
{
22+
23+
class KeyedControllerNode : public rclcpp::Node
24+
{
25+
public:
26+
27+
explicit KeyedControllerNode(const rclcpp::NodeOptions & options)
28+
: Node("keyed_controller", options)
29+
{
30+
// Create a callback function for when messages are received.
31+
setvbuf(stdout, NULL, _IONBF, BUFSIZ);
32+
auto callback =
33+
[this](demo_keys_cpp::msg::KeyedSensorDataMsg::ConstSharedPtr msg) -> void
34+
{
35+
RCLCPP_INFO(this->get_logger(), "Received data from sensor [%d]",(int)msg->sensor_id);
36+
};
37+
38+
sub_ = create_subscription<demo_keys_cpp::msg::KeyedSensorDataMsg>("/robot/sensors",
39+
rclcpp::QoS(rclcpp::KeepLast(1)).reliable().transient_local(),
40+
callback);
41+
}
42+
43+
private:
44+
rclcpp::Subscription<demo_keys_cpp::msg::KeyedSensorDataMsg>::SharedPtr sub_;
45+
};
46+
47+
} // namespace demo_keys_cpp
48+
49+
RCLCPP_COMPONENTS_REGISTER_NODE(demo_keys_cpp::KeyedControllerNode)

0 commit comments

Comments
 (0)