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

ros2: embedding a msg as a field in a custom msg

asked 2019-02-05 01:10:36 -0600

Obeseturtle gravatar image

updated 2019-02-05 08:22:17 -0600

gvdhoorn gravatar image

Hi,

In ROS, I could import other messages using headers.

Header header
int32 cout
int32 nch
int32 lenght

This way I could include std_msg's stamp and frame_id. Other wise, I have to add them manually but I am not sure if this is ideal or not.

unit32 seq
builtin_interfaces/Time stamp
string frame_id    
int32 count
int32 nch
int32 lenght

I tried using std_msgs/Header header but I received error message.

Message interface 'std_msgs/Header' contains an unknown field type: std_msgs/Header header

I added find_package(std_msgs REQUIRED) to my CMakelist, I am not sure if I am missing anything else

I looked at how other ROS2 msg's used the messages I wanted to use but it seems they are doing it manually as well link text. I was wondering if it would be okay for me to do this too(writing it manually).

edit retag flag offensive close merge delete

Comments

I was wondering if it would be okay for me to do this too(writing it manually).

"manually"?

Are you referring to prefixing the field type with the fully qualified package name that hosts the message?

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

If by prefixing you mean adding the std_msgs/Header to my custom message then no. Doing this will result in an error as pointed in the description. By "manually" I meant coping std_msgs/Header message definition to my custom message. I feel like this is wrong though.

Obeseturtle gravatar image Obeseturtle  ( 2019-02-05 02:43:18 -0600 )edit

No, copying (sub) message types into your own custom message is not needed, and would undermine the entire idea of software reuse (which also applies to message/type definitions).

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

So, that would mean there is a way of importing the message I want to use to the custom message(I am sorry for not using the correct terms) Since I want to add the messages defined in std_msgs/Header to my custom message, is there a line of code I am missing?

Obeseturtle gravatar image Obeseturtle  ( 2019-02-05 03:22:40 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
8

answered 2019-02-05 03:36:40 -0600

Obeseturtle gravatar image

The issue I was having was caused by not adding the std_msgs to the dependencies of my CMakelist.

find_package(std_msgs REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  ${msg_files}
  DEPENDENCIES builtin_interfaces std_msgs

Using std_msgs/Header on my custom message's .msg file no longer produces an error.

std_msgs/Header header
string frame_id    
int32 count
int32 nch
int32 lenght
edit flag offensive delete link more

Comments

Although I am able to use the message now, when I try ros2 topic echo , the embedded message is not being printed, to be more specific std_msgs/Header header's stamp. Although I receive no error messages when assigning a value to it when publishing the message, echo will return an empty field.

Obeseturtle gravatar image Obeseturtle  ( 2019-02-06 03:08:08 -0600 )edit

Just what I was looking for - Thanks! Was having same error.

fiji3119 gravatar image fiji3119  ( 2020-05-28 11:32:04 -0600 )edit

I was having the same error. My problem was that in the .msg file I had Header header instead of std_msgs/Header header. After fixing it, my package for the interfaces builds (I am on ROS2 too).

fabbro gravatar image fabbro  ( 2022-03-08 01:43:17 -0600 )edit

@Obeseturtle Could you figure out "empty field" problem ? I am also not seeing any values under stamp and frame_id.

aniltirli gravatar image aniltirli  ( 2023-03-24 08:04:46 -0600 )edit

Question Tools

4 followers

Stats

Asked: 2019-02-05 01:10:36 -0600

Seen: 6,537 times

Last updated: Feb 05 '19