rosbag code API

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

 

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

 

$catkin_create_pkg bagdemo roscpp rospy rosbag std_msgs geometry_msgs

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;
}
#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;
}

遇到的错误:

运行时报错:Tried to insert a message with time less than ros::TIME_MIN Aborted (core dumped)
我的原因是,时区问题,设置错误。将时区改成上海,重新启动电脑,或者重新启动roscore。代码正常工作。

readbag.cpp:(.text+0x19f): undefined reference to `rosbag::Bag::Bag()'
readbag.cpp:(.text+0x1e7): undefined reference to `rosbag::Bag::open(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int)'
readbag.cpp:(.text+0x2f6): undefined reference to `rosbag::TopicQuery::TopicQuery(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)'
readbag.cpp:(.text+0x346): undefined reference to `rosbag::View::View(rosbag::Bag const&, boost::function<bool (rosbag::ConnectionInfo const*)>, ros::Time const&, ros::Time const&, bool const&)'
readbag.cpp:(.text+0x718): undefined reference to `rosbag::Bag::close()'
readbag.cpp:(.text+0x7a6): undefined reference to `rosbag::View::~View()'
readbag.cpp:(.text+0x7c4): undefined reference to `rosbag::Bag::~Bag()'
readbag.cpp:(.text+0x984): undefined reference to `rosbag::View::~View()'
readbag.cpp:(.text+0x9af): undefined reference to `rosbag::View::~View()'
readbag.cpp:(.text+0xa66): undefined reference to `rosbag::View::~View()'
readbag.cpp:(.text+0xa8e): undefined reference to `rosbag::Bag::~Bag()'

 

没有添加rosbag依赖。CMakeLists.txt 和  package.xml  中添加rosbag 项。

或者重新建软件包。

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值