Ask Your Question

Creating an array/list of existing message type without defining custom message type?

asked 2019-02-04 16:16:01 -0600

jwhendy gravatar image

updated 2019-02-04 16:17:33 -0600

Is there a way to create an array of an existing message type without defining another message type?

As a use-case, say you have many .bag files, each with a username. You compile them into a summary bag file and would like all unique usernames in a metadata topic for convenience. Is there any way to accomplish something like this without a custom message type?

users = get_unique_users() # a function returns a list of strings: [user1, user2, ..., userN]
outbag = rosbag.Bag('/path/to/file.bag', 'w')
outbag.write('/metadata/unique_users', std_msgs.msg.String(users))

It seems that an array of an existing message is such a simple extension that I'd figure such a mechanism exists. My current approach was to create a new package, std_msgs_array with:

$ rosmsg show std_msgs_array/StringArray 
string[] data

$ rosmsg show std_msgs_array/Float64Array 
float64[] data

Is this expected or am I overlooking something? I do note the following from the ROS Wiki:'s usually "better" (in the sense of making the code easier to understand, etc.) when developers use or create non-generic message types

Perhaps the intention is to force more project-specific message creation rather than relying on built-in, canned examples. This use-case might be somewhat fringe where I truly am looking for a "throwaway"; all I care about is getting a list of very basic values into the output .bag vs. something like sensor data specific to a more complicated process.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-02-05 00:42:43 -0600

mgruhler gravatar image

You already cite the correct wiki page about the purpose of std_msgs.

For integer/float values, there are the MultiArray messages, which could suite your needs for numbers. For strings, you either have to create a new custom message, or wriggle your way through something like adding a seperator between all users concatenated into a single string...

In short: There is no way to create a message type to be sent over ROS or written to the bag file without properly creating a .msg file. This is required for creation of the language specific representations, such that you can #include or import them.

edit flag offensive delete link more


[..] such that you can #include or import them.

I would go one step further: being able to #include or import them is an implementation detail. The main point of having to be explicit about message types (ie: having to create them) is to make sure both syntax and semantics are ..

gvdhoorn gravatar image gvdhoorn  ( 2019-02-05 02:05:14 -0600 )edit

.. properly encoded/stored somewhere.

@jwhendy: how would we do that without having a message type defined?

gvdhoorn gravatar image gvdhoorn  ( 2019-02-05 02:05:57 -0600 )edit

@gvdhoorn: agreed!

mgruhler gravatar image mgruhler  ( 2019-02-05 03:12:44 -0600 )edit

@gvdhoom: the question was prompted by the fact that a foo msg corresponding to some Foo.msg can be used as a foo[]. Is a foo[] a "custom message type" in your mind? My question asks if one must use a custom message to consume or provide an array of an existing message.

jwhendy gravatar image jwhendy  ( 2019-02-09 20:18:20 -0600 )edit

It seems that answer is "Yes," so now I know. For this particular scenario it seemed "heavy" to do this when I really just want a list of strings or floats. Still, it seems this is the way, so now I know for sure.

jwhendy gravatar image jwhendy  ( 2019-02-09 20:19:24 -0600 )edit

No: foo[] is not a custom message.

The message containing a field named foo that is an arbitrary length array (ie: a list) would be a custom message.

Pedantic perhaps, but two very different things.

re: heavy: thing is, a list of floats is not semantically meaningful. Unless ..

gvdhoorn gravatar image gvdhoorn  ( 2019-02-10 14:09:49 -0600 )edit

.. you give the message a semantically meaningful name. That is what you should create a custom message.

Besides, it's technically impossible to send, receive or store message data without an associated message type, so even if you see it as a lot of overhead, there wouldn't be any other way.

gvdhoorn gravatar image gvdhoorn  ( 2019-02-10 14:11:39 -0600 )edit

Thanks for the reasoning. This is indeed a narrow use case. For the purpose of storing metadata, the field name doesn't matter to me as it's named by the topic (e.g. /metadata/foo) I assign. Again, pretty fringe use case.

jwhendy gravatar image jwhendy  ( 2019-02-10 15:01:26 -0600 )edit

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: 2019-02-04 16:16:01 -0600

Seen: 1,892 times

Last updated: Feb 05 '19