0
$\begingroup$

Rosanswers logo

Is there an example for it? Thanks!


Originally posted by ahtsan on ROS Answers with karma: 112 on 2019-07-26

Post score: 2

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

The message_filters package contains a C++ type trait to determine if a message type has a header field: see https://github.com/ros2/message_filters/blob/a18a34a2242e6d44dd6c66e4cb2679d0fe07a40b/include/message_filters/message_traits.h#L43-L49


Originally posted by Dirk Thomas with karma: 16276 on 2019-07-29

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by ahtsan on 2019-07-29:
Thanks for the pointer! Is there an example on how to use it?

Comment by Dirk Thomas on 2019-07-29:
As any type trait you can use it with std::enable_if. It is used twice in the same file below.

Comment by ahtsan on 2019-07-29:
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?

Comment by ahtsan on 2019-07-29:
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.

$\endgroup$
0
$\begingroup$

Rosanswers logo

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


Originally posted by pavel92 with karma: 1655 on 2019-07-27

This answer was NOT ACCEPTED on the original site

Post score: 2


Original comments

Comment by ahtsan on 2019-07-27:
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.

Comment by pavel92 on 2019-07-28:
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.

Comment by ahtsan on 2019-07-28:
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.

Comment by gvdhoorn on 2019-07-28:
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).

Comment by ahtsan on 2019-07-28:
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.