如何在ros2中同步2个或多个节点?

问题描述 投票:0回答:0

class Combine(Node):
    def _init_(self):
        super()._init_("combine")
        self.subs_hello_ = self.create_subscription(String, '/hello', self.callback_msg_hello)
        self.subs_world_ = self.create_subscription(String, '/world', self.callback_msg_world)
        self.received_hello_msg = None
        self.received_world_msg = None

    def callback_msg_hello(self, msg):
        self.received_hello_msg = msg
        self.combine_and_process_messages()

   # Callback for world topic


    def combine_and_process_messages(self):
        # Check if both messages have been received
        if self.received_hello_msg is not None and self.received_world_msg is not None:
            combined_message = f"Hello: {self.received_hello_msg.data}, World: {self.received_world_msg.data}"
            self.get_logger().info(f"Combined Message: {combined_message}")

我有订阅两个主题 /hello 和 /world 的订阅者节点,我想将两者合并为一,并且我希望节点同步到发布者,但是当我运行它时,结果不同步,当我停止两者或就一次它仍在运行并收到最后一条消息。那么我怎样才能像这样同步msg ros2呢?

[ros2] 你好1,世界3

[ros2] 你好2,世界4

停下来:

[ros2] 你好3,无

同时停止: ........................

等待两者再次发布

string synchronization ros ros2
© www.soinside.com 2019 - 2024. All rights reserved.