Ask Your Question

How to get the serialized message size/length in ROS2

asked 2018-09-21 16:50:03 -0500

nikechristo gravatar image

In ROS1, given a generic ROS message type, you can call ros::serialization::serializationLength() ( ) to get the size of the message in bytes that will get sent over the network on a call to publish. How can I do that same in ROS2?

I am trying to collect the amount of bytes that gets sent out by the ROS 1 - 2 bridge for topics so I know how much network bandwidth is consumed by each topic.

The bridge uses FastRTPS.

Any help is appreciated.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-09-21 19:50:36 -0500

William gravatar image

updated 2018-09-21 19:52:04 -0500

There's no function to do this for you right now.

One thing that is different in ROS 2 is that there's no notion of a "Serializer" class that handles serialization for all configurations, instead serialization is handled by the middleware through an abstraction layer. This is nice because it allows us to use different serialization formats if we want or need to do so, but due to the abstraction layer you don't actually know how big it will be on the wire without asking the middleware implementation.

We already have functions for asking the current middle implementation to do serializing and deserializing of messages:

So specifically for your use case you could try serializing messages yourself, checking the resulting size of the serialized message, and then publishing it as normal

The serialized type is in rmw, but it is actually a typedef in to a general purpose data structure in our C utility package called rcutils:

The rcutils data structure has the size of the serialized result though:

Doing it this way might actually even be preferred, because determining what the serialized size would be might take almost as long as just serializing it and you'll be serializing it anyways (most likely).

There is, however, no way to ask the middleware right now to "predict" the serialized size of a message given the ROS data structure, e.g. an instance of std_msgs::msg::String, without first serializing it. It is probably possible to implement such a function, but we'd need to add it to our middleware interface.

There is also no C++ class that wraps up these related functions or C++ versions of the functions for that matter, but it's something that we'd be interested in having and if you're interested in helping with that I'd suggest starting a discussion on an issue in the rclcpp repository to have an equivalent to the Serializer<T> class from roscpp.

In a related subject, we do have these traits for each message:

Among them is the is_fixed_size trait and the is_bounded_size trait. If the is_fixed_size is true then there is a single size for the message in memory and on the wire (I'm pretty sure about the wire part but not 100%). And if is_bounded_size is true then there is a fixed upper bound on the size of the message in memory and/or on the wire.

But again, there's not yet a function to actually calculate the fixed size or bounded size for messages.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-09-21 16:50:03 -0500

Seen: 369 times

Last updated: Sep 21 '18