[ROS2] How to extract header from a ROS2 message? [closed]
Is there an example for it? Thanks!
ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
Is there an example for it? Thanks!
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_filte...
As any type trait you can use it with std::enable_if
. It is used twice in the same file below.
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
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.
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).
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.
Asked: 2019-07-26 16:04:45 -0600
Seen: 3,112 times
Last updated: Jul 29 '19
Is there a release date of ros 2 or more informations about it?
Is the planned ROS1 to ROS2/DDS bridge available yet?
ROS2: content-based topic subscriptions?
Building Ros 2.0 without OpenCV?
ros2 rttest example build error
ROS 2 Alpha 2 ros1_bridge using DDS between two computers