ROS笔记之rosbag的快速切片(C++实现)

ROS笔记之rosbag的快速切片(C++实现)

—— 杭州 2023-12-21 夜


code review

文章目录

  • ROS笔记之rosbag的快速切片(C++实现)
    • 1.运行效果
    • 2.文件结构
    • 3.fast_rosbag_slice.cpp
    • 4.CMakeLists.txt
    • 5.package.xml
    • 6.对fast_rosbag_slice.cpp进行函数封装

正常该功能是ROS官方命令行:rosbag filter来实现,但速度太慢.

代码抄自大佬的Github:https://github.com/berndpfrommer/fast_rosbag_slice.git


图片名称

1.运行效果

  • input_bag的情况
    在这里插入图片描述

  • 运行,想得到后50s的bag

rosrun fast_rosbag_slice fast_rosbag_slice -i a.bag -o output.bag -s 1686903228.56 -e 1686903278.56

在这里插入图片描述

耗时8.68s(windows虚拟机环境)

  • output_bag的情况
    在这里插入图片描述

2.文件结构

在这里插入图片描述

3.fast_rosbag_slice.cpp

// -*-c++-*--------------------------------------------------------------------
// Copyright 2022 Bernd Pfrommer <bernd.pfrommer@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <unistd.h>#include <chrono>
#include <iostream>
#include <limits>void usage()
{std::cout << "usage:" << std::endl;std::cout << "fast_rosbag_slice -i input_bag -o output_bag -s start_time -e stop_time "<< std::endl;
}static size_t process_bag(const std::string & inBagName, const std::string & outBagName, const double startTime,const double endTime)
{std::cout << "reading from bag: " << inBagName << std::endl;std::cout << "writing to bag: " << outBagName << std::endl;rosbag::Bag inBag;inBag.open(inBagName, rosbag::bagmode::Read);rosbag::Bag outBag;outBag.open(outBagName, rosbag::bagmode::Write);rosbag::View view(inBag);size_t numMessages(0);for (const rosbag::MessageInstance & m : view) {if (m.getTime().toSec() > endTime) {break;}if (m.getTime().toSec() >= startTime) {outBag.write(m.getTopic(), m.getTime(), m);numMessages++;}}inBag.close();outBag.close();return (numMessages);
}int main(int argc, char ** argv)
{int opt;ros::Time::init();std::string inBag;std::string outBag;double startTime(0);double endTime(std::numeric_limits<double>::max());while ((opt = getopt(argc, argv, "i:o:s:e:h")) != -1) {switch (opt) {case 'i':inBag = optarg;break;case 'o':outBag = optarg;break;case 's':startTime = atof(optarg);break;case 'e':endTime = atof(optarg);break;case 'h':usage();return (-1);default:std::cout << "unknown option: " << opt << std::endl;usage();return (-1);break;}}if (inBag.empty() || outBag.empty()) {std::cout << "missing input or output bag name!" << std::endl;usage();return (-1);}const auto start = std::chrono::high_resolution_clock::now();size_t numMsg = process_bag(inBag, outBag, startTime, endTime);const auto end = std::chrono::high_resolution_clock::now();auto total_duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();std::cout << "total time: " << total_duration * 1e-6 << " s" << std::endl;std::cout << "message processing rate: " << numMsg * 1e6 / total_duration << " hz" << std::endl;return (0);
}

4.CMakeLists.txt

#
# Copyright 2022 Bernd Pfrommer <bernd.pfrommer@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.5)
project(fast_rosbag_slice)set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wextra -Wpedantic -Werror")
set (CMAKE_CXX_STANDARD 14)find_package(catkin REQUIRED COMPONENTSroscpprosbag)catkin_package()include_directories(${catkin_INCLUDE_DIRS}
)# --------- sync test
add_executable(fast_rosbag_slice src/fast_rosbag_slice.cpp)
target_link_libraries(fast_rosbag_slice ${catkin_LIBRARIES})
#
# volumetric tracking node and nodelet
#
install(TARGETS fast_rosbag_sliceARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
)

5.package.xml

<?xml version="1.0"?>
<package format="3"><name>fast_rosbag_slice</name><version>1.0.0</version><description>fast rosbag time slicer</description><maintainer email="bernd.pfrommer@gmail.com">Bernd Pfrommer</maintainer><license>Apache2</license><buildtool_depend condition="$ROS_VERSION == 1">catkin</buildtool_depend><depend condition="$ROS_VERSION == 1">roscpp</depend><depend condition="$ROS_VERSION == 1">rosbag</depend><export><build_type condition="$ROS_VERSION == 1">catkin</build_type></export></package>

6.对fast_rosbag_slice.cpp进行函数封装

在这里插入图片描述

运行
在这里插入图片描述

代码

#include <chrono>
#include <iostream>
#include <limits>
#include <rosbag/bag.h>
#include <rosbag/view.h>static size_t process_bag(const std::string &inBagName, const std::string &outBagName, const double startTime,const double endTime) {std::cout << "reading from bag: " << inBagName << std::endl;std::cout << "writing to bag: " << outBagName << std::endl;rosbag::Bag inBag;inBag.open(inBagName, rosbag::bagmode::Read);rosbag::Bag outBag;outBag.open(outBagName, rosbag::bagmode::Write);rosbag::View view(inBag);size_t numMessages(0);for (const rosbag::MessageInstance &m : view) {if (m.getTime().toSec() > endTime) {break;}if (m.getTime().toSec() >= startTime) {outBag.write(m.getTopic(), m.getTime(), m);numMessages++;}}inBag.close();outBag.close();return (numMessages);
}int main() {std::string inBag = "/home/user/bag/a.bag";std::string outBag = "/home/user/bag/output.bag";double startTime = 1686903228.56;double endTime = 1686903278.56;const auto start = std::chrono::high_resolution_clock::now();size_t numMsg = process_bag(inBag, outBag, startTime, endTime);const auto end = std::chrono::high_resolution_clock::now();auto total_duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();std::cout << "total time: " << total_duration * 1e-6 << " s" << std::endl;std::cout << "message processing rate: " << numMsg * 1e6 / total_duration << " hz" << std::endl;return (0);
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/237326.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C#合并多个Word文档(微软官方免费openxml接口)

g /// <summary>/// 合并多个word文档&#xff08;合并到第一文件&#xff09;/// </summary>/// <param name"as_word_paths">word文档完整路径</param>/// <param name"breakNewPage">true(默认值)&#xff0c;合并下一个…

华为路由配置值——通过流策略实现策略路由(重定向到不同的下一跳)

组网图形 图1 配置策略路由组网图 策略路由简介配置注意事项组网需求配置思路操作步骤配置文件 策略路由简介 传统的路由转发原理是首先根据报文的目的地址查找路由表&#xff0c;然后进行报文转发。但是目前越来越多的用户希望能够在传统路由转发的基础上根据自己定义的策略…

React-Native环境搭建(IOS)

系列文章目录 React-Native环境搭建&#xff08;IOS&#xff09; 目录 系列文章目录前言一、IOS环境搭建前置说明二、环境搭建步骤1.Homebrew安装2.安装Xcode3.Node4.watchman5.CocoaPods 三、项目启动1.初始化项目2.项目启动 总结 前言 React-Native开发环境的搭建&#xff…

c# 使用OpenCV

C#和OpenCV的结合主要通过一个名为OpenCVSharp的库实现。OpenCVSharp是一个C#包装器&#xff0c;它提供了对OpenCV&#xff08;一个开源的计算机视觉和机器学习库&#xff09;功能的访问。 安装OpenCVSharp NuGet包&#xff1a; 在Visual Studio中&#xff0c;右键点击你的项目…

在 Mac 上使用 Python

在运行 macOS 的 Mac 上的 Python 原则上与在其他 Unix 平台上的 Python 非常相似&#xff0c;但有一些额外的特性&#xff0c;如 IDE 和包管理器&#xff0c;值得指出。 5.1. 获取和安装 MacPython macOS 曾经在 10.8 至 12.3 版中预装了 Python 2.7。 建议你从 Python 网站…

【已解决】vs2015下c++对sqlite的操作

本博文源于笔者操作sqlite3&#xff0c;借鉴了很多文章的思路&#xff0c;这里并整理了c常用的对数据库的操作供大家点赞收藏以后备用。包含了&#xff1a;c对sqlite3的创建数据库、创建数据表、写入数据表、读取数据表、删除数据表。也包括了最基础的让c运行sqlite3.内容供读者…

手机无人直播的兴起

近年来&#xff0c;随着科技的不断进步和智能手机的普及&#xff0c;手机无人直播成为了一种新兴的传媒方式。手持手机&#xff0c;不经过镜头操作人员的干预&#xff0c;通过直播平台实时分享自己的所见所闻&#xff0c;成为了越来越多人的选择。手机无人直播的盛行离不开以下…

[node]Node.js 中REPL简单介绍

[node]Node.js 中REPL简单介绍 什么是REPL为什么使用REPL如何使用REPL 命令REPL模式node的全局内容展示node全局所有模块查看全局模块具体内容其它命令 实践 什么是REPL Node.js REPL(Read Eval Print Loop:交互式解释器) 表示电脑的环境&#xff0c;类似 Windows 系统的终端或…

Hardhat环境搭建(六)---无需翻墙

Hardhat环境搭建 官方地址 node环境 npm环境 git环境 安装hardhat npm init npminit是什么 在node开发中使用npm init会生成一个pakeage.json文件&#xff0c;这个文件主要是用来记录这个项目的详细信息的&#xff0c;它会将我们在项目开发中所要用到的包&#xff0c;以…

Mac查询本机ip地址

Mac系统版本和网络配置不同&#xff0c;可能会有一些细微差别。 一、 使用系统偏好设置 1、点击屏幕左上角的Apple图标&#xff0c;选择“系统偏好设置”。 2、点击“网络”。 3、 在左侧选择当前连接的网络&#xff08;如Wi-Fi或以太网&#xff09;&#xff0c;在右侧界面&a…

达梦到达梦的外部链接dblink(DM-DM DBLINK)

一. 使用场景&#xff1a; 部链接对象&#xff08;LINK&#xff09;是 DM 中的一种特殊的数据库实体对象&#xff0c;它记录了远程数据库的连接和路径信息&#xff0c;用于建立与远程数据的联系。通过多台数据库主库间的相互通讯&#xff0c;用户可以透明地操作远程数据库的数…

Java Web3J :使用web3j调用自己的智能合约的方法(教程)

代码世界有很多令人大呼小叫的技巧!有的代码像魔术师一样巧妙地隐藏了自己,有的像魔法师一样让你眼花缭乱,还有的像瑜伽大师一样灵活自如。它们让我们惊叹不已,让我们觉得自己仿佛置身于编码的魔幻世界。快来分享你见过哪些令你膛目结舌的代码技巧吧! 目录 web3j调用智能合…

前端开发新趋势:Web3 与虚拟现实的技术融合

在当今互联网技术日新月异的时代&#xff0c;Web技术也在不断地发展和变革。从前端开发的角度来看&#xff0c;新技术的涌现和旧技术的迭代让前端开发者们面临着前所未有的挑战和机遇。Web3 与虚拟现实&#xff08;VR&#xff09;的技术融合&#xff0c;正是当前前端开发领域的…

​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化

2022年亚马逊云科技re:Invent盛会于近日在拉斯维加斯成功召开&#xff0c;吸引了众多业界精英和创新者。亚马逊云科技边缘服务副总裁Jan Hofmeyr在演讲中分享了关于亚马逊云科技海外服务器边缘计算的最新发展和创新成果&#xff0c;引发与会者热烈关注。 re:Invent的核心主题是…

九、W5100S/W5500+RP2040之MicroPython开发<HTTPOneNET示例>

文章目录 1. 前言2. 平台操作流程2.1 创建设备2.2 创建数据流模板 3. WIZnet以太网芯片4. 示例讲解以及使用4.1 程序流程图4.2 测试准备4.3 连接方式4.4 相关代码4.5 烧录验证 5. 注意事项6. 相关链接 1. 前言 在这个智能硬件和物联网时代&#xff0c;MicroPython和树莓派PICO正…

Kubernetes 的用法和解析 -- 7

k8s之共享存储pv&pvc 1 存储资源管理 在基于k8s容器云平台上&#xff0c;对存储资源的使用需求通常包括以下几方面&#xff1a; 1.应用配置文件、密钥的管理&#xff1b; 2.应用的数据持久化存储&#xff1b; 3.在不同的应用间共享数据存储&#xff1b; k8s的Volume抽象概…

Curl多线程https访问,崩溃问题修复

Curl: &#xfffd;&#xfffd;: SSL and multithread crash on windows, how to use mutex on windows? SSL and multithread crash on windows, how to use mutex on windows? From: mao mao <lmjrd_at_hotmail.com> Date: Fri, 25 Nov 2016 09:50:48 0000 Thank…

昨天下午学习的是mysql-约束条件 数据类型

常见的数据类型 整数 浮点型 日期 字符串文本类型 比如id后面要是int类型的 手机号就得是bigint 或 varchar 主键 primary key 自增 auto_increment 非空notnull 唯一约束 unique 参照refernces 用法 create database System; use System; create table stude…

【昆明*线上同步】最新ChatGPT/GPT4科研实践应用与AI绘图技术及论文高效写作

详情点击查看福利&#xff1a;【昆明*线上同步】最新ChatGPT/GPT4科研实践应用与AI绘图技术及论文高效写作 目标&#xff1a; 1、熟练掌握ChatGPT提示词技巧及各种应用方法&#xff0c;并成为工作中的助手。 2、通过案例掌握ChatGPT撰写、修改论文及工作报告&#xff0c;提供…

AI Native工程化:百度App AI互动技术实践

作者 | GodStart 导读 随着AI浪潮的兴起&#xff0c;越来越多的应用都在利用大模型重构业务形态&#xff0c;在设计和优化Prompt的过程中&#xff0c;我们发现整个Prompt测评和优化周期非常长&#xff0c;因此&#xff0c;我们提出了一种Prompt生成、评估与迭代的一体化解决方案…