rosbag API c++ [closed]
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);
Please mark my answer as correct if it solved your problem.
To mark answers as correct, klick on the little checkmark below the downvote button.