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

[ROS2] How to extract header from a ROS2 message? [closed]

asked 2019-07-26 16:04:45 -0600

ahtsan gravatar image

Is there an example for it? Thanks!

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by ahtsan
close date 2020-07-27 18:08:57.327759

2 Answers

Sort by ยป oldest newest most voted

answered 2019-07-29 16:37:03 -0600

Dirk Thomas gravatar image

The message_filterspackage contains a C++ type trait to determine if a message type has a header field: see

edit flag offensive delete link more


Thanks for the pointer! Is there an example on how to use it?

ahtsan gravatar image ahtsan  ( 2019-07-29 16:47:04 -0600 )edit

As any type trait you can use it with std::enable_if. It is used twice in the same file below.

Dirk Thomas gravatar image Dirk Thomas  ( 2019-07-29 16:48:57 -0600 )edit

Do you mind giving a small code snippet on how to check if a message has header and get the header timestamp from the message if it has one?

ahtsan gravatar image ahtsan  ( 2019-07-29 17:16:53 -0600 )edit

For others who are looking for the answer, you can do auto header_timestamp = message_filters::message_traits::TimeStamp<T>::value(*msg); and you get a rclcpp::Time object.

ahtsan gravatar image ahtsan  ( 2019-07-29 19:13:02 -0600 )edit

answered 2019-07-27 10:00:58 -0600

pavel92 gravatar image

what do you mean by extract? Access the header field from a message? For example you can create a PoseStamped message from geometry_msgs with:

geometry_msgs::msg::PoseStamped pose;

Then you can access the stamp or frame_id with pose.header.stamp and pose.header.frame_id Note that (sequence )seq is removed from the header in ROS2

edit flag offensive delete link more


Is there a way to check if the message type has header or not? I only want to get the header if it has one.

ahtsan gravatar image ahtsan  ( 2019-07-27 15:10:42 -0600 )edit

Depends on you usecase. Where do you want to do the check? You should know the message type since you have to specify it in your subscriber (if you are checking the header there). For instance if you check this example:

auto node = rclcpp::Node::make_shared("listener_float");
auto sub = node->create_subscription<std_msgs::msg::Float32>(
    "chatter", chatterCallback);

You can see that the message is std_msgs::msg::Float32 and this message does not have a header (has only float32 data). It is always good to know the message types. You can also try to extract the msg type in your code as shown in this question.

pavel92 gravatar image pavel92  ( 2019-07-28 06:35:58 -0600 )edit

Can it be done programmatically? I am writing a template class which accepts any message type and want to check if it has header at runtime.

ahtsan gravatar image ahtsan  ( 2019-07-28 11:35:56 -0600 )edit

runtime or compile-time? Your mention of templates makes it seem like compile-time would make more sense.

Afaik this is currently not possible, but I'm not an expert ROS 2 user. There is a rosidl_generator_cpp/traits.hpp header that gets specialised for each generated msg/srv header, but that doesn't appear to contain a trait for messages that have a header. You could perhaps check for a field header, but there is no requirement for a field with such a name to exist (it could be called hdr or something else).

gvdhoorn gravatar image gvdhoorn  ( 2019-07-28 12:22:29 -0600 )edit

Oh yes I meant compile time. Thanks a lot for your explanation. Do you think explicitly checking for the header field would be the closest workaround for the purpose? Main goal of my application is to perform some operation with the header timestamp if the message has a header, and the class needs to be able to take any kind of message, thus a template class and I need compile time checking.

ahtsan gravatar image ahtsan  ( 2019-07-28 13:37:08 -0600 )edit

Question Tools



Asked: 2019-07-26 16:04:45 -0600

Seen: 2,720 times

Last updated: Jul 29 '19