【ROS】读写rosbag(c++)
0x00 前言rosbag是通过ros记录的数据格式,在使用ros框架的使用方便读写,但如果换了别的框架,就需要进行转换了。直接用c++的流来读写是很不方便的,借用rosbag模块来读写比较方便0x01获取rosbag中的topic首先要知道rosbag里面包含的topic,可是使用ros的工具来查看先启动roscoreroscore然后在另一个terminal里面运行rqt_bag 你的完整路径
·
0x00 前言
rosbag是通过ros记录的数据格式,在使用ros框架的使用方便读写,但如果换了别的框架,就需要进行转换了。
直接用c++的流来读写是很不方便的,借用rosbag模块来读写比较方便
0x01获取rosbag中的topic
首先要知道rosbag里面包含的topic,可是使用ros的工具来查看
- 先启动roscore
roscore
- 然后在另一个terminal里面运行
rqt_bag 你的完整路径.bag
可以看到里面包含的很多topic

0x02 读取rosbag中指定的数据
c++代码
// main.cc
#include "rosbag/bag.h"
#include "rosbag/view.h"
#include "sensor_msgs/Imu.h"
#include "sensor_msgs/PointCloud2.h"
std::string src_bag = "/home/xxx/DataSet/2022-03-17/2022-03-17.bag";
std::string new_bag = "/home/xxx/DataSet/2022-03-17/imu_pcd.bag";
std::string imu_topic = "/imu/data_raw";
std::string pcd2_topic = "/hesai/pandar";
int main(int argc, char **argv) {
rosbag::Bag i_bag, o_bag;
i_bag.open(src_bag, rosbag::bagmode::Read);
o_bag.open(new_bag, rosbag::bagmode::Write);
std::vector<std::string> topics;
topics.push_back(std::string(imu_topic));
topics.push_back(std::string(pcd2_topic));
rosbag::View view(i_bag, rosbag::TopicQuery(topics));
// rosbag::View view(i_bag);
for (auto m : view) {
sensor_msgs::Imu::ConstPtr imu = m.instantiate<sensor_msgs::Imu>();
if (imu == nullptr) {
std::cout << "imu null " << std::endl;
} else {
std::cout << "imu stamp:" << imu->header.stamp << std::endl;
o_bag.write(imu_topic, imu->header.stamp, imu);
}
sensor_msgs::PointCloud2::ConstPtr pcd =
m.instantiate<sensor_msgs::PointCloud2>();
if (pcd == nullptr) {
std::cout << "pcd null " << std::endl;
} else {
std::cout << "pcd stamp:" << pcd->header.stamp << std::endl;
o_bag.write(pcd2_topic, pcd->header.stamp, pcd);
}
}
return 0;
CMakeList
cmake_minimum_required(VERSION 3.14)
project(to_rosbag)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_STANDARD 14)
# ros pkg dep
set(ROS_DEP roscpp sensor_msgs rosbag)
# when create new message, build with message_generation
# find_package(catkin REQUIRED COMPONENTS message_generation ${ROS_DEP})
find_package(catkin REQUIRED COMPONENTS ${ROS_DEP})
# catkin_package() 是catkin提供的CMake的宏,这个指令是用来提供一些catkin的信息来创建pkg-config和CMake文件。
# 这个指令必须在 add_library() 或 add_executable()前执行, 这个指令有 5 个可选参数:
# INCLUDE_DIRS - 被这个package导出的include路径
# LIBRARIES - 被这个package导出的library
# CATKIN_DEPENDS - 这个工程依赖的其他catkin projects
# DEPENDS - 这个工程依赖的Non-catkin CMake projects
# CFG_EXTRAS - 其他的构建选项。
# catkin_package(CATKIN_DEPENDS ${ROS_DEP} message_runtime)
catkin_package(CATKIN_DEPENDS ${ROS_DEP})
# file()
aux_source_directory(convert_to_rosbag ALL_SRC)
add_executable(${PROJECT_NAME} ${ALL_SRC})
target_link_libraries(${PROJECT_NAME} PUBLIC ${catkin_LIBRARIES})
target_include_directories(${PROJECT_NAME} PUBLIC ./convert_to_rosbag ${catkin_INCLUDE_DIRS})
#
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ./)
package.xml
<?xml version="1.0"?>
<package format="3">
<name>to_rosbag</name>
<version>1.0.0</version>
<description>
c++ project with ros
</description>
<maintainer email="xxx@xxx.com">
</maintainer>
<license>Apache 2.0</license>
<buildtool_depend>catkin</buildtool_depend>
<!-- <build_depend>message_generation</build_depend> -->
<depend>roscpp</depend>
<depend>sensor_msgs</depend>
<depend>rosbag</depend>
<!-- <exec_depend>message_runtime</exec_depend> -->
</package>
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)