Skip to content

Commit 37f0960

Browse files
mergify[bot]ghanta1996ahcorde
authored
Add qos parameter for wait_for_message function (#2903) (#2906)
(cherry picked from commit 2fcef70) Signed-off-by: Sriharsha Ghanta <[email protected]> Signed-off-by: Alejandro Hernandez Cordero <[email protected]> Co-authored-by: Sriharsha Ghanta <[email protected]> Co-authored-by: Alejandro Hernandez Cordero <[email protected]>
1 parent b6a0b00 commit 37f0960

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

rclcpp/include/rclcpp/wait_for_message.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef RCLCPP__WAIT_FOR_MESSAGE_HPP_
1616
#define RCLCPP__WAIT_FOR_MESSAGE_HPP_
1717

18+
#include <future>
1819
#include <memory>
1920
#include <string>
2021

@@ -23,6 +24,7 @@
2324
#include "rclcpp/node.hpp"
2425
#include "rclcpp/visibility_control.hpp"
2526
#include "rclcpp/wait_set.hpp"
27+
#include "rclcpp/qos.hpp"
2628

2729
namespace rclcpp
2830
{
@@ -79,10 +81,11 @@ bool wait_for_message(
7981
/**
8082
* Wait for the next incoming message to arrive on a specified topic before the specified timeout.
8183
*
82-
* \param[out] out is the message to be filled when a new message is arriving.
84+
* \param[out] out is the message to be filled when a new message is arriving
8385
* \param[in] node the node pointer to initialize the subscription on.
8486
* \param[in] topic the topic to wait for messages.
8587
* \param[in] time_to_wait parameter specifying the timeout before returning.
88+
* \param[in] qos parameter specifying QoS settings for the subscription.
8689
* \return true if a message was successfully received, false if message could not
8790
* be obtained or shutdown was triggered asynchronously on the context.
8891
*/
@@ -91,9 +94,10 @@ bool wait_for_message(
9194
MsgT & out,
9295
rclcpp::Node::SharedPtr node,
9396
const std::string & topic,
94-
std::chrono::duration<Rep, Period> time_to_wait = std::chrono::duration<Rep, Period>(-1))
97+
std::chrono::duration<Rep, Period> time_to_wait = std::chrono::duration<Rep, Period>(-1),
98+
const rclcpp::QoS & qos = rclcpp::SystemDefaultsQoS())
9599
{
96-
auto sub = node->create_subscription<MsgT>(topic, 1, [](const std::shared_ptr<const MsgT>) {});
100+
auto sub = node->create_subscription<MsgT>(topic, qos, [](const std::shared_ptr<const MsgT>) {});
97101
return wait_for_message<MsgT, Rep, Period>(
98102
out, sub, node->get_node_options().context(), time_to_wait);
99103
}

rclcpp/test/rclcpp/test_wait_for_message.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,29 @@ TEST(TestUtilities, wait_for_message_twice_one_sub) {
108108

109109
rclcpp::shutdown();
110110
}
111+
112+
TEST(TestUtilities, wait_for_last_message) {
113+
rclcpp::init(0, nullptr);
114+
115+
auto node = std::make_shared<rclcpp::Node>("wait_for_last_message_node");
116+
auto qos = rclcpp::QoS(1).reliable().transient_local();
117+
118+
using MsgT = test_msgs::msg::Strings;
119+
auto pub = node->create_publisher<MsgT>("wait_for_last_message_topic", qos);
120+
pub->publish(*get_messages_strings()[0]);
121+
122+
MsgT out;
123+
auto received = false;
124+
auto wait = std::async(
125+
[&]() {
126+
auto ret = rclcpp::wait_for_message(out, node, "wait_for_last_message_topic", 5s, qos);
127+
EXPECT_TRUE(ret);
128+
received = true;
129+
});
130+
131+
ASSERT_NO_THROW(wait.get());
132+
ASSERT_TRUE(received);
133+
EXPECT_EQ(out, *get_messages_strings()[0]);
134+
135+
rclcpp::shutdown();
136+
}

0 commit comments

Comments
 (0)