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

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

3 Answers

Sort by ยป oldest newest most voted
8

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
1

answered 2022-12-06 16:30:30 -0600

Collin Hays gravatar image

This is not a feature in ROS1 but this is a feature in ROS2. Interface definition using .msg / .srv / .action files Scroll down to the "Default values" section

In this ROS2 tutorial Creating custom msg and srv files They have you put the following in a .msg file

int64 num

By default the default value of a numerical datatype is 0. If you want to change the default value then put the desired default value after the name separated by a space. For example if you want to make the default value 1 then put the following in the .msg file.

int64 num 1

Now when you publish your message without defining the value of num it will publish num with a value of 1 instead of 0.

ros2 topic pub /numtopic tutorial_interfaces/msg/Num '{}'

You can confirm this by using ros2 topic echo.

ros2 topic echo /numtopic

num: 1
---
edit flag offensive delete link more
0

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

Comments

2

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

Question Tools

1 follower

Stats

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

Seen: 10,457 times

Last updated: Feb 19 '21