ros读取bag文件(C++)

#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <std_msgs/Int32.h>
#include <std_msgs/String.h>

#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>

#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>
#include <pcl/visualization/cloud_viewer.h>

#include <sensor_msgs/Imu.h>
#include <livox_ros_driver/CustomMsg.h>

ros::Publisher pub;
void rgb_set(int wr, int wg, int wb, int br, int bg, int bb)
{                                                                                 //设置RGB
        printf("\033[38;2;%d;%d;%dm\033[48;2;%d;%d;%dm", wr, wg, wb, br, bg, bb); //\033[38表示前景,\033[48表示背景,三个%d表示混合的数
}

int main(int argc, char **argv)
{
        // Initialize ROS
        ros::init(argc, argv, "my_pcl_tutorial");
        ros::NodeHandle nh;

        rosbag::Bag bag;
        bag.open("/home/l/dataset/CamVox.bag", rosbag::bagmode::Read); //打开一个bag文件

        std::vector<std::string> topics; //设置需要遍历的topic

        topics.push_back(std::string("/isee_rgb"));
        topics.push_back(std::string("/livox/lidar"));
        topics.push_back(std::string("/livox/imu"));

        rosbag::View view(bag, rosbag::TopicQuery(topics));
        //使用迭代器的方式遍历,注意:每一个迭代器为一帧数据
        rosbag::View::iterator it = view.begin();

        for (; it != view.end(); ++it)
        {
                //获取一帧数据内容,运用auto使其自动满足对应类型
                auto m = *it;
                //得到该帧数据的topic
                std::string topic = m.getTopic();
                if (topic == "/isee_rgb")
                {
                        sensor_msgs::Image::ConstPtr rgb_msg = m.instantiate<sensor_msgs::Image>();
                        cout << "/isee_rgb   " << setprecision(19) << rgb_msg->header.stamp.toSec() << endl;
                }
                if (topic == "/livox/lidar")
                {
                        livox_ros_driver::CustomMsg::ConstPtr livox_msg = m.instantiate<livox_ros_driver::CustomMsg>();
                        cout << "/livox/lidar   " << setprecision(19) << livox_msg->header.stamp.toSec() << endl;
                }
                 if (topic == "/livox/imu")
                 {
                         sensor_msgs::Imu::ConstPtr imu_msg = m.instantiate<sensor_msgs::Imu>();
                 } 
        cout << endl;

        bag.close();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值