Skip to content

Add Topic Keys tutorials #5573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: rolling
Choose a base branch
from

Conversation

Mario-DL
Copy link
Contributor

@Mario-DL Mario-DL commented May 7, 2025

Following the Topic instances support, this PR adds two tutorials for exemplifying the use of topic instances in ROS 2.

  • Topic Keys tutorial
  • A tutorial combining Topic Keys with Subscription Filtering

@Mario-DL Mario-DL changed the title Add tutorials Topic Keys tutorials Add Topic Keys tutorials May 7, 2025
Mario-DL added 2 commits May 7, 2025 11:17
Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>
Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>
@Mario-DL Mario-DL force-pushed the tutorials/topic-keys branch from a14dc46 to 1d46bd0 Compare May 7, 2025 09:18
Copy link
Contributor

@ahcorde ahcorde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some general comments

  • use ..code-block: console, then use $` in every command included in the block
  • Support cmd for al tier 1 platforms
  • ament_target_dependencies is deprecated, please change it to target_link_libraries

Comment on lines 65 to 82
.. code-block:: bash

docker run -it --rm osrt/ros:{DISTRO}-desktop

Then, within the container, source the ROS 2 installation with:

.. code-block:: bash

source /opt/ros/{DISTRO}/setup.bash

#. Running the tutorial on the local host.
Please, follow the :doc:`installation instructions <../../../Installation>` for details on installing ROS 2.

Source the following file to setup the ROS 2 environment:

.. code-block:: bash

source /opt/ros/{DISTRO}/setup.bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. code-block:: bash
docker run -it --rm osrt/ros:{DISTRO}-desktop
Then, within the container, source the ROS 2 installation with:
.. code-block:: bash
source /opt/ros/{DISTRO}/setup.bash
#. Running the tutorial on the local host.
Please, follow the :doc:`installation instructions <../../../Installation>` for details on installing ROS 2.
Source the following file to setup the ROS 2 environment:
.. code-block:: bash
source /opt/ros/{DISTRO}/setup.bash
.. code-block:: console
$ docker run -it --rm osrt/ros:{DISTRO}-desktop
Then, within the container, source the ROS 2 installation with:
.. code-block:: console
$ source /opt/ros/{DISTRO}/setup.bash
#. Running the tutorial on the local host.
Please, follow the :doc:`installation instructions <../../../Installation>` for details on installing ROS 2.
Source the following file to setup the ROS 2 environment:
.. code-block:: console
$ source /opt/ros/{DISTRO}/setup.bash

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also extend source /opt/ros/{DISTRO}/setup.bash with different plantforms (including windows)

Comment on lines 90 to 108
.. code-block:: bash

# Create directory structure
mkdir -p ~/tutorial_ws/src/demo_keys_filtering_cpp
mkdir ~/tutorial_ws/src/demo_keys_filtering_cpp/msg
mkdir ~/tutorial_ws/src/demo_keys_filtering_cpp/src
mkdir ~/tutorial_ws/src/demo_keys_filtering_cpp/launch
cd ~/tutorial_ws/src/demo_keys_filtering_cpp

# Download demo package source code
wget -O CMakeLists.txt https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/CMakeLists.txt
wget -O package.xml https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/package.xml
wget -O README.md https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/README.md
wget -O msg/KeyedSensorDataMsg.msg https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/msg/KeyedSensorDataMsg.msg
wget -O src/filtered_keyed_sensor.cpp https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/src/filtered_keyed_sensor.cpp
wget -O src/filtered_keyed_controller.cpp https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/src/filtered_keyed_controller.cpp
wget -O launch/keyed_sensors_launch.py https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/launch/keyed_sensors_launch.py

The resulting directory structure should be:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. code-block:: bash
# Create directory structure
mkdir -p ~/tutorial_ws/src/demo_keys_filtering_cpp
mkdir ~/tutorial_ws/src/demo_keys_filtering_cpp/msg
mkdir ~/tutorial_ws/src/demo_keys_filtering_cpp/src
mkdir ~/tutorial_ws/src/demo_keys_filtering_cpp/launch
cd ~/tutorial_ws/src/demo_keys_filtering_cpp
# Download demo package source code
wget -O CMakeLists.txt https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/CMakeLists.txt
wget -O package.xml https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/package.xml
wget -O README.md https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/README.md
wget -O msg/KeyedSensorDataMsg.msg https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/msg/KeyedSensorDataMsg.msg
wget -O src/filtered_keyed_sensor.cpp https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/src/filtered_keyed_sensor.cpp
wget -O src/filtered_keyed_controller.cpp https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/src/filtered_keyed_controller.cpp
wget -O launch/keyed_sensors_launch.py https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/launch/keyed_sensors_launch.py
The resulting directory structure should be:
.. code-block:: console
# Create directory structure
$ mkdir -p ~/tutorial_ws/src/demo_keys_filtering_cpp
$ mkdir ~/tutorial_ws/src/demo_keys_filtering_cpp/msg
$ mkdir ~/tutorial_ws/src/demo_keys_filtering_cpp/src
$ mkdir ~/tutorial_ws/src/demo_keys_filtering_cpp/launch
$ cd ~/tutorial_ws/src/demo_keys_filtering_cpp
# Download demo package source code
$ wget -O CMakeLists.txt https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/CMakeLists.txt
$ wget -O package.xml https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/package.xml
$ wget -O README.md https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/README.md
$ wget -O msg/KeyedSensorDataMsg.msg https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/msg/KeyedSensorDataMsg.msg
$ wget -O src/filtered_keyed_sensor.cpp https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/src/filtered_keyed_sensor.cpp
$ wget -O src/filtered_keyed_controller.cpp https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/src/filtered_keyed_controller.cpp
$ wget -O launch/keyed_sensors_launch.py https://raw.githubusercontent.com/ros2/ros2_documentation/{DISTRO}/source/Tutorials/Advanced/Topic-Keys/resources/Filtered/launch/keyed_sensors_launch.py
The resulting directory structure should be:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also CMD support for other platforms


* ``filtered_keyed_controller.cpp``: This is the source code for a controller node that filters keyed sensor data in reception, being the most relevant lines the ones that define the filter expression and Quality of Service settings:

.. code-block:: bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. code-block:: bash
.. code-block:: cpp

* ``filtered_keyed_sensor.cpp``: This is the source code for a sensor node that publishes keyed sensor data.
The most relevant lines are the ones that create the publication with a particular Quality of Service settings that enables the controller to late join the application but still receiving the latest update for every instance with the use of topic keys.

.. code-block:: bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. code-block:: bash
.. code-block:: cpp


Generate the corresponding IDL definition from the provided ``KeyedSensorDataMsg.msg`` file, using the ``msg2idl.py`` script from the ``rosidl_adapter`` package.

.. code-block:: bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. code-block:: bash
.. code-block:: console

// Create a callback function for when messages are received.
setvbuf(stdout, NULL, _IONBF, BUFSIZ);
auto callback =
[this](demo_keys_filtering_cpp::msg::KeyedSensorDataMsg::ConstSharedPtr msg) -> void
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include <memory>

"subscribed to topic \"%s\" with content filter options \"%s, {%s}\"",
sub_->get_topic_name(),
sub_options.content_filter_options.filter_expression.c_str(),
rcpputils::join(sub_options.content_filter_options.expression_parameters, ", ").c_str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include header

period_ = std::chrono::milliseconds(id_ * 1000);
measurement_ = distribution_(generator_);

setvbuf(stdout, NULL, _IONBF, BUFSIZ);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include <cstdio>

uint16_t id_{0};
std::chrono::milliseconds period_{1000};
std::default_random_engine generator_;
std::uniform_real_distribution<float> distribution_{50.0, 90.0};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::uniform_real_distribution<float> distribution_{50.0, 90.0};
std::uniform_real_distribution<float> distribution_{50.0f, 90.0f};

}

private:
uint16_t id_{0};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include <cstdint>

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>
@Mario-DL
Copy link
Contributor Author

Mario-DL commented May 8, 2025

Friendly ping @fujitatomoya, you may also be interested in reviewing this

@Mario-DL Mario-DL requested a review from ahcorde May 8, 2025 09:30
@Mario-DL
Copy link
Contributor Author

Mario-DL commented May 8, 2025

CC: @MiguelCompany

@MiguelCompany
Copy link
Contributor

I suppose @fgallegosalido and @lobolanja, who made the changes in the Connext RMW, might want to take a look

@fgallegosalido
Copy link
Contributor

We'll take a look into it. Thanks for the heads up!

@@ -0,0 +1,51 @@
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copyright year should be 2025. Review other files too.

@fgallegosalido
Copy link
Contributor

Tutorials look good to me. Just a minor comment on the licensing year.

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>
@Mario-DL Mario-DL requested a review from fgallegosalido May 14, 2025 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants