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

Revision history [back]

click to hide/show revision 1
initial version

ROS doesn't currently have native support for optional fields, but there are a few ways that you could go about building something that would work.

You could build the system you've suggested, where you have all the the possible fields defined in the message, and a final bitmask field that indicates which fields are valid; maybe something like:

int32 a
int32 b
float32 c
string d

int32 A_VALID = 1
int32 B_VALID = 2
int32 C_VALID = 4
int32 D_VALID = 8
int32 valid_fields

You could also build a message consisting of an array of key-value pairs. This has the advantage that you only send those fields that valid across the network, and would allow you to add new fields later without changing the message type. It also has the disadvantage that the values are restricted to a single type, and it has more overhead than a fixed message with the same number of fields. It might look something like this (compressed for readability):

package/KeyValue[] pairs
  string key
  int32 value

Long-term, there has been talk of changing the message serialization scheme used by ROS to use an off-the-shelf serialization library which might support a feature like this, but that's still quite a ways out (at least 6 months, probably much more).