ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

how to achieve message polymorphism in ROS?

asked 2013-10-03 05:26:45 -0500

fferri gravatar image

updated 2013-10-17 01:15:53 -0500

if I want a message or service to handle 2 or more types, what's the best way to do it?

it would be similar to tagged unions in C...

since message fields cannot be null, I was thinking to a scheme like this:

uint8 TYPE_1=1
...
uint8 TYPE_n=n
uint8 type
pkg/Message1[] msg_1 
...
pkg/MessageN[] msg_n

with the constraint (which must be enforced by the client application) that at most one array has one element, and all other arrays have zero elements.

it looks quite an ugly workaround.

is there anything better that could be done?

@DamienJadeDuff the motivation is that I need to represent one very generic type, which is a logic term (Term), which is composed by a functional (string) and an array of args of type Atom. Now the problem is that the type Atom can be int, float, string, or even a Term itself.

edit retag flag offensive close merge delete

Comments

1

I wonder if you could further elucidate why you want this to help potential answerers? I should have thought using multiple message types would've had the same effect? Any particular reason you want them published/called over 1 topic? Good luck.

DamienJadeDuff gravatar image DamienJadeDuff  ( 2013-10-03 19:34:15 -0500 )edit
1

Without knowing the exact criteria, this may be of interest: http://wiki.ros.org/topic_tools/mux

DamienJadeDuff gravatar image DamienJadeDuff  ( 2013-10-03 22:35:00 -0500 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2013-10-17 01:35:12 -0500

updated 2013-10-17 01:37:18 -0500

For your use case (an Atom that can either be an int, float, string etc.) there isn't a better way to do it in ROS messages, unfortunately. We have a similar situation in one of our projects.

The only comment I have is that you don't need arrays, if each array has at most one element. I'd do it along these lines (Atom.msg):

uint8 TYPE_INT=1
uint8 TYPE_FLOAT=2
uint8 TYPE_TERM=3
...

# don't call this "type", because it's a reserved keyword in python
unit8 msg_type

int32 data_int
float32 data_float
Term data_term

Then correct usage of this message type (i.e., exactly one element is filled) depends on all nodes adhering to the contract. But that's the case for all complex message types anyway.

edit flag offensive delete link more

Comments

Thanks for your feedback Martin. I initially used your approach, but I was concerned in case of many possible types, the message would carry data for all those fields initialized with default values. With arrays, it just would carry N-1 empty arrays (however I'm not sure how those would serialize)

fferri gravatar image fferri  ( 2013-10-17 03:18:25 -0500 )edit
1

You're right that the message needs to carry all the unused fields, which is ugly. To me, the opportunity for bugs that this creates (publisher fills in field A, but subscriber reads B) is worse than a few bytes traffic overhead.

Martin Günther gravatar image Martin Günther  ( 2013-10-18 23:58:30 -0500 )edit

Not sure about serialization either, but an empty array will also take at least 32 bytes (which is what a int32, float32 etc. take).

Martin Günther gravatar image Martin Günther  ( 2013-10-19 00:00:02 -0500 )edit
0

answered 2013-10-14 14:52:42 -0500

Thomas gravatar image

First it feels a bit strange to want that, a topic is designed to send the result of a computation (i.e the same computation) so the type should never change.

However, if you really want to do that, why just not switching your publisher/subscriber by publishing different types? I never did that but I think that if your subscriber receives over the topic a wrong type it will probably throw some kind of exception which can be handled somehow.

edit flag offensive delete link more

Comments

that would only work for polymorphic topics and services, not for polymorphic fields of topics and services

fferri gravatar image fferri  ( 2013-10-17 01:16:51 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2013-10-03 05:26:45 -0500

Seen: 1,545 times

Last updated: Oct 17 '13