Skip to content

Commit

Permalink
Get content of topic from launch_testing_ros::WaitForTopics
Browse files Browse the repository at this point in the history
ros2#346
Signed-off-by: Pintaudi Giorgio <[email protected]>
  • Loading branch information
LastStarDust committed May 22, 2023
1 parent b106d2e commit 8656009
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions launch_testing_ros/launch_testing_ros/wait_for_topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def method_2():
print('Given topics are receiving messages !')
print(wait_for_topics.topics_not_received()) # Should be an empty set
print(wait_for_topics.topics_received()) # Should be {'topic_1', 'topic_2'}
print(wait_for_topics.messages_received('topic_1')) # Should be [message_1, ...]
wait_for_topics.shutdown()
"""

Expand Down Expand Up @@ -87,6 +88,12 @@ def topics_not_received(self):
"""Topics that did not receive any messages."""
return self.__ros_node.expected_topics - self.__ros_node.received_topics

def messages_received(self, topic_name):
"""List of received messages of a specific topic."""
if topic_name not in self.__ros_node.received_messages:
raise KeyError("No message received with topic " + topic_name)
return self.__ros_node.received_messages[topic_name]

def __enter__(self):
if not self.wait():
raise RuntimeError('Did not receive messages on these topics: ',
Expand All @@ -108,6 +115,7 @@ def start_subscribers(self, topic_tuples):
self.subscriber_list = []
self.expected_topics = {name for name, _ in topic_tuples}
self.received_topics = set()
self.received_messages = {}

for topic_name, topic_type in topic_tuples:
# Create a subscriber
Expand All @@ -126,6 +134,7 @@ def topic_callback(data):
if topic_name not in self.received_topics:
self.get_logger().debug('Message received for ' + topic_name)
self.received_topics.add(topic_name)
self.received_messages.setdefault(topic_name, []).append(data)
if self.received_topics == self.expected_topics:
self.msg_event_object.set()

Expand Down

0 comments on commit 8656009

Please sign in to comment.