ROS2怎样实现各节点运行时间同步。使用python3编程
-
如题,版本Ubuntu22.04 humble
各位大佬,目前我知道的学长学姐做得时候用的ROS1里message_filters中的TimeSynchronizer时间同步器。
网上没搜到ROS2对应的。
想问问,ROS2里怎么实现。 -
@熵
看过来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
callbackmyCallback
.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"}
-
@小鱼 谢谢鱼哥,是我傻了,怪不得没搜到,原来这函数名字就没变...
-
-
@熵 加油
-
@小鱼 嗯嗯,一定