Ask Your Question
1

rosbag API c++ [closed]

asked 2012-11-30 00:02:59 -0500

Jep gravatar image

updated 2014-01-28 17:14:26 -0500

ngrennan gravatar image

Hi all,

I'm writing a piece of code that is reading a bagfile, creating a new one, copying all the messages I'm interested from the old one to the new one, and adding a couple of topics more.

Until here everything is fine, but I would be able to copy all the messages from the old bagfile, without having to know the name of the topics and the type of message, but I can't find the way to do this with the C++ API...

I copy a piece of code just to illustrate the way my code works:

rosbag::Bag bag_read;
rosbag::Bag bag_write;
bag_read.open(old_bag, rosbag::bagmode::Read);
bag_write.open(new_bag, rosbag::bagmode::Write);

// First we copy all the topic we need to the new bagfile.

rosbag::View view_all(bag_read);    

BOOST_FOREACH(rosbag::MessageInstance const m, view_all){
    if (m.getTopic() == "/nav/odometry"){
            nav_msgs::Odometry::ConstPtr odom_msg = m.instantiate<nav_msgs::Odometry>();
            if (odom_msg != NULL){              
                ros::Time   time= m.getTime();
                bag_write.write("/nav/odometry", time, odom_msg);
            }
     }
    else if .... /* For every topic I'm interested on */

Thank you!!

SOLVED:

As Lorenz says there is no need to instantiate the message:

BOOST_FOREACH(rosbag::MessageInstance const m, view_all)
     bag_write.write(m.getTopic(),m.getTime(),m);
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Jep
close date 2012-11-30 03:47:25

Comments

Please mark my answer as correct if it solved your problem.

Lorenz gravatar image Lorenz  ( 2012-11-30 03:47:26 -0500 )edit

To mark answers as correct, klick on the little checkmark below the downvote button.

Lorenz gravatar image Lorenz  ( 2012-11-30 03:53:20 -0500 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2012-11-30 00:09:01 -0500

Lorenz gravatar image

Just don't instantiate the message but write the message instance itself. It should implement the Message C++ interface, i.e. provide all required traits so it should be no problem to write it directly. The MessageInstance also provides information about the topic, MD5 sum and message definition.

edit flag offensive delete link more
0

answered 2012-11-30 01:47:55 -0500

Jep gravatar image

Thanks lorenz,

I tried it like:

    BOOST_FOREACH(rosbag::MessageInstance const m, view_all){
bag_write.write(m.getTopic(),m);

But it doesn't work as this kind of write is just possible with a ros::MessageEvent and not rosbag::MessageInstance

void    write (std::string const &topic, ros::Time const &time, boost::shared_ptr< T > const &msg, boost::shared_ptr< ros::M_string > connection_header=boost::shared_ptr< ros::M_string >())

void    write (std::string const &topic, ros::Time const &time, boost::shared_ptr< T const > const &msg, boost::shared_ptr< ros::M_string > connection_header=boost::shared_ptr< ros::M_string >())

void    write (std::string const &topic, ros::Time const &time, T const &msg, boost::shared_ptr< ros::M_string > connection_header=boost::shared_ptr< ros::M_string >())

void    write (std::string const &topic, ros::MessageEvent< T > const &event)
edit flag offensive delete link more

Comments

Please read the support page before posting. Do not create answers for subsequent questions, updates or comments. You want to use the third version of write, i.e. you need to pass the instance's time in addition.

Lorenz gravatar image Lorenz  ( 2012-11-30 01:50:52 -0500 )edit

Ok! Thanks Solved now! Sorry

Jep gravatar image Jep  ( 2012-11-30 03:40:38 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2012-11-30 00:02:59 -0500

Seen: 2,620 times

Last updated: Nov 30 '12