ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Default values inside a message or srv definition

asked 2012-02-14 07:27:15 -0600

kbabu gravatar image

elements in a srv or msg definition are assigned zero values by the default constructor. What about when I wish to assign a non-zero or other special value as the default initialization value to a msg element.

using a construct such as:

int32 x=100

regards x as a constant when the msg headers are generated.

Is what I'm looking for possible at all? Thanks much

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2012-02-14 09:41:52 -0600

joq gravatar image

updated 2012-02-14 09:42:54 -0600

Not in the message definition, itself. The problem is that message generation must support multiple languages.

For any specific language (like Python or C++), you can probably define your own derived class for the message with a constructor that fills in useful default values.

edit flag offensive delete link more

answered 2021-02-19 17:54:36 -0600

eRCaGuy gravatar image

updated 2021-02-19 17:57:02 -0600

I just confirmed through experimentation that adding an = sign after any variable name in a ROS *.msg message definition file appears to be how to set constants, and all variables with an = after them show up in the autogenerated C++ *.h files as public static constexpr constants inside the autogenerated class (struct actually).

edit flag offensive delete link more



Yes, this is documented at wiki/msg: Constants.

But the OP here seems to ask for defaults, which constants cannot be used for.

As @joq mentions though: in C++, custom constructors can be used to provide alternatives to the default zero-initialisation of many fields. See wiki/roscpp/Overview/MessagesSerializationAndAdaptingTypes/Example: defining a custom constructor.

gvdhoorn gravatar image gvdhoorn  ( 2021-02-20 01:09:28 -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: 2012-02-14 07:27:15 -0600

Seen: 7,078 times

Last updated: Feb 19 '21