Skip to content

Commit 34cfd2f

Browse files
authored
Allow specifying qos (#1225)
* Allow specifying qos Signed-off-by: Tim Clephas <[email protected]>
1 parent 2ec6797 commit 34cfd2f

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

rclpy/rclpy/wait_for_message.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from typing import Union
16+
1517
from rclpy.impl.implementation_singleton import rclpy_implementation as _rclpy
1618
from rclpy.node import Node
19+
from rclpy.qos import QoSProfile
1720
from rclpy.signals import SignalHandlerGuardCondition
1821
from rclpy.utilities import timeout_sec_to_nsec
1922

@@ -22,6 +25,8 @@ def wait_for_message(
2225
msg_type,
2326
node: 'Node',
2427
topic: str,
28+
*,
29+
qos_profile: Union[QoSProfile, int] = 1,
2530
time_to_wait=-1
2631
):
2732
"""
@@ -30,6 +35,7 @@ def wait_for_message(
3035
:param msg_type: message type
3136
:param node: node to initialize the subscription on
3237
:param topic: topic name to wait for message
38+
:param qos_profile: QoS profile to use for the subscription
3339
:param time_to_wait: seconds to wait before returning
3440
:returns: (True, msg) if a message was successfully received, (False, None) if message
3541
could not be obtained or shutdown was triggered asynchronously on the context.
@@ -38,7 +44,7 @@ def wait_for_message(
3844
wait_set = _rclpy.WaitSet(1, 1, 0, 0, 0, 0, context.handle)
3945
wait_set.clear_entities()
4046

41-
sub = node.create_subscription(msg_type, topic, lambda _: None, 1)
47+
sub = node.create_subscription(msg_type, topic, lambda _: None, qos_profile=qos_profile)
4248
try:
4349
wait_set.add_subscription(sub.handle)
4450
sigint_gc = SignalHandlerGuardCondition(context=context)

rclpy/test/test_wait_for_message.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import unittest
1818

1919
import rclpy
20+
from rclpy.qos import QoSProfile
2021
from rclpy.wait_for_message import wait_for_message
2122
from test_msgs.msg import BasicTypes
2223

@@ -51,13 +52,22 @@ def _publish_message(self):
5152
def test_wait_for_message(self):
5253
t = threading.Thread(target=self._publish_message)
5354
t.start()
54-
ret, msg = wait_for_message(BasicTypes, self.node, TOPIC_NAME)
55+
ret, msg = wait_for_message(BasicTypes, self.node, TOPIC_NAME, qos_profile=1)
56+
self.assertTrue(ret)
57+
self.assertEqual(msg.int32_value, MSG_DATA)
58+
t.join()
59+
60+
def test_wait_for_message_qos(self):
61+
t = threading.Thread(target=self._publish_message)
62+
t.start()
63+
ret, msg = wait_for_message(
64+
BasicTypes, self.node, TOPIC_NAME, qos_profile=QoSProfile(depth=1))
5565
self.assertTrue(ret)
5666
self.assertEqual(msg.int32_value, MSG_DATA)
5767
t.join()
5868

5969
def test_wait_for_message_timeout(self):
60-
ret, _ = wait_for_message(BasicTypes, self.node, TOPIC_NAME, 1)
70+
ret, _ = wait_for_message(BasicTypes, self.node, TOPIC_NAME, time_to_wait=1)
6171
self.assertFalse(ret)
6272

6373

0 commit comments

Comments
 (0)