0
$\begingroup$

Rosanswers logo

Is the extraction of a timestamp from the header of a sensor_msgs::Image that easy:

void imageCb(const sensor_msgs::ImageConstPtr& imsg) {
    cv_bridge::CvImagePtr cv_ptr;
    try {
        cv_ptr = cv_bridge::toCvCopy(imsg, sensor_msgs::image_encodings::TYPE_16UC1);
    }
    catch (cv_bridge::Exception& e) {
        ROS_ERROR("cv_bridge exception: %s", e.what());
        return;
    }
    
    //get header info
    std_msgs::Header h = imsg->header;
    cout<<h<<endl; //all at once
    cout<<h.stamp<<endl; //specific parts of it
    cout<<h.stamp.sec<<endl;
    cout<<h.stamp.nsec<<endl;
    cout<<h.seq<<endl;

    //do some with it


This is the output

seq: 11536  //all info
stamp: 1455792836.004090186
frame_id: camera_depth_optical_frame

1455792836.004090186 //stamp
1455792836 //sec
4090186 //nsec
11536 //seq

Originally posted by user23fj239 on ROS Answers with karma: 748 on 2016-02-18

Post score: 2

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Yes.

I wouldn't call it extraction though. You're basically just accessing the .header.stamp member variable.

This of course depends on your message having a field of type Header.


Originally posted by gvdhoorn with karma: 86574 on 2016-02-18

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by user23fj239 on 2016-02-18:
How to check if its there to prevent errors, segfaults etc, when trying to access it and its not there?

Comment by gvdhoorn on 2016-02-18:
ROS messages in C++ are auto-generated from the msg files. The compiler will error-out if you try to use fields that are not part of the struct definition. For dynamically typed languages you'll have to use the facilities of the language to check for presence of fields.

$\endgroup$

Your Answer

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