rosbag APIは、 `rosbag :: Bag :: close() 'への未定義の参照を報告します...

http://wiki.ros.org/rosbag/Code%20API

http://docs.ros.org/melodic/api/rosbag_storage/html/c++/

https://github.com/sofiathefirst/imagesCpp/tree/master/bagdemo_ros_img

writebag.cpp

#include "ros/ros.h"
#include "geometry_msgs/Point.h"
#include <rosbag/bag.h>
#include <std_msgs/Int32.h>
#include <std_msgs/String.h>
 
/**
 * This tutorial demonstrates simple sending of messages over the ROS system.
 */
int main(int argc, char **argv)
{
 
  ros::init(argc, argv, "eyeInScreen");
 
  /**
   * NodeHandle is the main access point to communications with the ROS system.
   * The first NodeHandle constructed will fully initialize this node, and the last
   * NodeHandle destructed will close down the node.
   */
  ros::NodeHandle n;
 
 
 // ros::Publisher chatter_pub = n.advertise<geometry_msgs::Point>("/point", 10);
 
  ros::Rate loop_rate(10);
    rosbag::Bag bag;
    bag.open("test.bag", rosbag::bagmode::Write);
  int count = 0;
 
  while (ros::ok() && count<1000)
  {
    count++;
    std_msgs::String str;
    str.data = std::string("foo");
 
    std_msgs::Int32 i;
    i.data = count;
    bag.write("chatter", ros::Time::now(), str);
    bag.write("numbers", ros::Time::now(), i);
    ros::spinOnce();
 
    loop_rate.sleep();
    ++count;
  }
 
 
    bag.close();
  return 0;
}

readbag.cpp

#include "ros/ros.h"
#include "geometry_msgs/Point.h"
#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <std_msgs/Int32.h>
#include <std_msgs/String.h>
 
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
 
/**
 * This tutorial demonstrates simple sending of messages over the ROS system.
 */
int main(int argc, char **argv)
{
 
  ros::init(argc, argv, "eyeInScreen");
 
  /**
   * NodeHandle is the main access point to communications with the ROS system.
   * The first NodeHandle constructed will fully initialize this node, and the last
   * NodeHandle destructed will close down the node.
   */
  ros::NodeHandle n;
 
 
  ros::Publisher chatter_pub = n.advertise<geometry_msgs::Point>("/point", 10);
 
  ros::Rate loop_rate(10);
 
  int count = 0;
 
  rosbag::Bag bag;
  bag.open("test.bag", rosbag::bagmode::Read);
 
  std::vector<std::string> topics;
  topics.push_back(std::string("chatter"));
  topics.push_back(std::string("numbers"));
 
  rosbag::View view(bag, rosbag::TopicQuery(topics));
  foreach(rosbag::MessageInstance const m, view)
  {
      std_msgs::String::ConstPtr s = m.instantiate<std_msgs::String>();
      if (s != NULL)
          std::cout << s->data << std::endl;
 
      std_msgs::Int32::ConstPtr i = m.instantiate<std_msgs::Int32>();
      if (i != NULL)
          std::cout << i->data << std::endl;
  }
 
  bag.close();
 
 
  return 0;
}

コンパイル時間

... `rosbag :: Bag :: close() '
への未定義の参照...` rosbag :: View :: 〜View()'
への未定義の参照... `rosbag :: Bag :: 〜Bagへの未定義の参照() '
.....
CmakeLists.txtファイルはrosbag依存関係を追加しないため、CMakeLists.txtおよびpackage.xmlにrosbagアイテムを追加するだけです。

find_package(catkin REQUIRED COMPONENTS 
roscpp 
rosbag 
std_msgs 
....
)
  <build_depend>roscpp</build_depend> 
  <build_depend>std_msgs</build_depend>
  <build_depend>rosbag</build_depend> 
....

または、次のコマンドを使用してプロジェクトを再構築します。

~/cat_ws/src$catkin_create_pkg bagdemo roscpp rospy rosbag std_msgs geometry_msgs

さらに、コンピュータのタイムゾーン設定が正しい必要があります。そうでない場合、操作中にエラーが報告される可能性があります。ros:: TIME_MIN より短い 時間でメッセージを挿入しようとし ました。中止 (コアダンプ)

 

おすすめ

転載: blog.csdn.net/sunlin972913894/article/details/103674042