鱼香ROS社区
    • 版块
    • 最新
    • 未解决
    • 已解决
    • 群组
    • 注册
    • 登录
    紧急通知:禁止一切关于政治&VPN翻墙等话题,发现相关帖子会立马删除封号
    提问前必看的发帖注意事项: 社区问答规则(小鱼个人)更新 | 高质量帖子发布指南

    ROS2怎样实现各节点运行时间同步。使用python3编程

    已定时 已固定 已锁定 已移动 已解决
    ROS 2相关问题
    ros2 时间同步
    2
    5
    1.9k
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • 熵熵
      熵
      最后由 编辑

      如题,版本Ubuntu22.04 humble
      各位大佬,目前我知道的学长学姐做得时候用的ROS1里message_filters中的TimeSynchronizer时间同步器。
      d0dd9118-1baa-4413-b762-1549b5ff9528-图片.png
      网上没搜到ROS2对应的。
      想问问,ROS2里怎么实现。

      小鱼小 1 条回复 最后回复 回复 引用 0
      • 小鱼小
        小鱼 技术大佬 @熵
        最后由 编辑

        @熵
        看过来

        • https://github.com/ros2/message_filters

        Message Filters --- chained message processing

        message_filters{.interpreted-text role="mod"} is a collection of
        message "filters" which take messages in, either from a ROS
        subscription or another filter, and may or may not output the message at
        some time in the future, depending on a policy defined for that filter.

        message_filters also defines a common interface for these filters,
        allowing you to chain them together.

        The filters currently implemented in this package are:

        • message_filters.Subscriber{.interpreted-text role="class"} - A
          source filter, which wraps a ROS subscription. Most filter chains
          will begin with a Subscriber.
        • message_filters.Cache{.interpreted-text role="class"} - Caches
          messages which pass through it, allowing later lookup by time
          stamp.
        • message_filters.TimeSynchronizer{.interpreted-text
          role="class"} - Synchronizes multiple messages by their
          timestamps, only passing them through when all have arrived.
        • message_filters.TimeSequencer{.interpreted-text role="class"} -
          Tries to pass messages through ordered by their timestamps, even
          if some arrive out of order.

        Here's a simple example of using a Subscriber with a Cache:

        def myCallback(posemsg):
           print posemsg
        
        sub = message_filters.Subscriber(node_object, "pose_topic", robot_msgs.msg.Pose)
        cache = message_filters.Cache(sub, 10)
        cache.registerCallback(myCallback)
        

        The Subscriber here acts as the source of messages. Each message is
        passed to the cache, which then passes it through to the user's
        callback myCallback.

        Using the time synchronizer:

        from message_filters import TimeSynchronizer, Subscriber
        
        def gotimage(image, camerainfo):
            assert image.header.stamp == camerainfo.header.stamp
            print "got an Image and CameraInfo"
        
        tss = TimeSynchronizer(Subscriber(node_object, "/wide_stereo/left/image_rect_color", sensor_msgs.msg.Image),
                               Subscriber(node_object, "/wide_stereo/left/camera_info", sensor_msgs.msg.CameraInfo))
        tss.registerCallback(gotimage)
        

        Another example for syncronizing one image topic and one pointcloud2
        topic using approximate synchronizer:

        from rclpy.node import Node
        from rclpy.qos import qos_profile_sensor_data
        from message_filters import ApproximateTimeSynchronizer, Subscriber
        from sensor_msgs.msg import Image, PointCloud2
        
        class ExampleNode(Node):
            def __init__(self):
                super().__init__("ExampleNode")
        
                self.image_sub = Subscriber(self, Image, "/image_topic")
                self.plc_sub = Subscriber(
                    self, PointCloud2, "/point_cloud_topic", qos_profile=qos_profile_sensor_data
                )
                queue_size = 30
                # you can use ApproximateTimeSynchronizer if msgs dont have exactly the same timestamp
                self.ts = ApproximateTimeSynchronizer(
                    [self.image_sub, self.plc_sub],
                    queue_size,
                    0.01,  # defines the delay (in seconds) with which messages can be synchronized
                )
        
            def callback(self, image_msg, pcl_msg):
                # do your stuff here
                pass
        

        Note: It is VERY IMPORTANT that each subscriber has the same
        qos_profile as the one specified in the corresponding publisher code
        for each topic you want to subscribe to. If they don't match, the
        callback won't be executed (without any warning) and you will be very
        frustrated.

        The message filter interface

        For an object to be usable as a message filter, it needs to have one
        method, registerCallback. To collect messages from a message filter,
        register a callback with:

        anyfilter.registerCallback(my_callback)
        

        The signature of my_callback varies according to the message filter.
        For many filters it is simply:

        def my_callback(msg):
        

        where msg is the message.

        Message filters that accept input from an upstream message filter (e.g.
        message_filters.Cache{.interpreted-text role="class"}) register their
        own message handler as a callback.

        Output connections are registered through the registerCallback()
        function.

        ::: {.automodule members="Subscriber, Cache, TimeSynchronizer, TimeSequencer" inherited-members=""}
        message_filters
        :::

        Indices and tables

        • genindex{.interpreted-text role="ref"}
        • search{.interpreted-text role="ref"}

        新书配套视频:https://www.bilibili.com/video/BV1GW42197Ck/

        熵熵 1 条回复 最后回复 回复 引用 0
        • 熵熵
          熵 @小鱼
          最后由 编辑

          @小鱼 谢谢鱼哥,是我傻了,怪不得没搜到,原来这函数名字就没变...

          小鱼小 1 条回复 最后回复 回复 引用 0
          • 熵熵 熵 将这个主题标记为已解决,在
          • 小鱼小
            小鱼 技术大佬 @熵
            最后由 编辑

            @熵 😊 加油

            新书配套视频:https://www.bilibili.com/video/BV1GW42197Ck/

            熵熵 1 条回复 最后回复 回复 引用 0
            • 熵熵
              熵 @小鱼
              最后由 编辑

              @小鱼 嗯嗯,一定

              1 条回复 最后回复 回复 引用 0
              • 第一个帖子
                最后一个帖子
              皖ICP备16016415号-7
              Powered by NodeBB | 鱼香ROS