首页 > 其他分享 >foxy与galactic解析rosbag的不同之处

foxy与galactic解析rosbag的不同之处

时间:2024-03-23 22:34:41浏览次数:14  
标签:galactic msgs auto storage iter foxy message include rosbag

前言

foxy和galactic版本在rosbag2_storage这个包的调整有点大(头文件及接口的命名空间),下面的代码仅供参考使用

foxy

#include "db3_reader.h"

#include <pcl/common/transforms.h>
#include <pcl/point_types.h>
#include <pcl_conversions/pcl_conversions.h>

#include <rosbag2_cpp/converter_interfaces/serialization_format_converter.hpp>
#include <rosbag2_cpp/reader.hpp>
#include <rosbag2_cpp/readers/sequential_reader.hpp>
#include <rosbag2_cpp/typesupport_helpers.hpp>
#include <rosbag2_storage/metadata_io.hpp>
#include <rosbag2_storage/storage_factory.hpp>
#include <rosbag2_storage/serialized_bag_message.hpp>
#include <sensor_msgs/point_cloud2_iterator.hpp>
#include <rclcpp/serialization.hpp>

namespace LidarViewRos2 {
namespace SensorProc {

void Db3Reader::Init(const Config& conf)
{
    const std::string slash = (conf.db3FilePath.back() == '/') ? "" : "/";
    const std::string filePath = conf.db3FilePath + slash + conf.db3FileName;
    ReadDatas(filePath, conf);
}

void Db3Reader::ReadDatas(const std::string& file_path, const Config& conf)
{
    rosbag2_cpp::StorageOptions storage_options{};

    storage_options.uri = file_path;
    storage_options.storage_id = "sqlite3";

    rosbag2_cpp::ConverterOptions converter_options{};
    converter_options.input_serialization_format = "cdr";
    converter_options.output_serialization_format = "cdr";

    rosbag2_cpp::readers::SequentialReader reader;
    reader.open(storage_options, converter_options);

    rosbag2_storage::StorageFilter storage_filter;
    storage_filter.topics = conf.topics;
    reader.set_filter(storage_filter);

    const auto topics = reader.get_all_topics_and_types();

    for (const auto& topic : topics) {
        topics_name2type_[topic.name] = topic.type;
    }

    int num = 1;

    while (reader.has_next()) {
        auto message = reader.read_next();
        auto serialized_message = rclcpp::SerializedMessage(*message->serialized_data);
        auto type = topics_name2type_[message->topic_name];
        auto frame_id = conf.frameIdMap.at(message->topic_name);
        if (type == "sensor_msgs/msg/PointCloud2") {
            auto serializer = rclcpp::Serialization<sensor_msgs::msg::PointCloud2>();
            sensor_msgs::msg::PointCloud2 pc2;
            serializer.deserialize_message(&serialized_message, &pc2);
            pc2.header.frame_id = frame_id;
            Eigen::Affine3d transform = conf.extrinsicMap.at(message->topic_name);
            TransformPointCloud(pc2, transform);
            pc_proc_->Input(message->topic_name, std::move(pc2));
            continue;
        }
        if (type == "sensor_msgs/msg/Imu") {
            // sensor_msgs::msg::Imu
            auto serializer = rclcpp::Serialization<sensor_msgs::msg::Imu>();
            sensor_msgs::msg::Imu imu_data;
            serializer.deserialize_message(&serialized_message, &imu_data);
            imu_data.header.frame_id = frame_id;
            pc_proc_->Input(message->topic_name, std::move(imu_data));
            continue;
        }
    }
    pc_proc_->SetEndTime();
}

void Db3Reader::TransformPointCloud(sensor_msgs::msg::PointCloud2& pc, Eigen::Affine3d& affine)
{
    sensor_msgs::PointCloud2Iterator<float> iter_x(pc, "x");
    sensor_msgs::PointCloud2Iterator<float> iter_y(pc, "y");
    sensor_msgs::PointCloud2Iterator<float> iter_z(pc, "z");

    while (iter_x != iter_x.end() && iter_y != iter_y.end() && iter_z != iter_z.end()) {
        Eigen::Vector3d point(*iter_x, *iter_y, *iter_z);
        auto new_point = affine * point;
        *iter_x = new_point[0];
        *iter_y = new_point[1];
        *iter_z = new_point[2];
        ++iter_x;
        ++iter_y;
        ++iter_z;
    }
}

} // namespace SensorProc
} // namespace LidarViewRos2

galactic

#include "db3_reader.h"

#include <pcl/common/transforms.h>
#include <pcl/point_types.h>
#include <pcl_conversions/pcl_conversions.h>

#include <rosbag2_cpp/converter_interfaces/serialization_format_converter.hpp>
#include <rosbag2_cpp/reader.hpp>
#include <rosbag2_cpp/readers/sequential_reader.hpp>
#include <rosbag2_cpp/typesupport_helpers.hpp>
#include <rosbag2_storage/metadata_io.hpp>
#include <rosbag2_storage/storage_factory.hpp>
#include <rosbag2_storage/storage_options.hpp>
#include <sensor_msgs/point_cloud2_iterator.hpp>

namespace LidarViewRos2 {
namespace SensorProc {

void Db3Reader::Init(const Config& conf)
{
    const std::string slash = (conf.db3FilePath.back() == '/') ? "" : "/";
    const std::string filePath = conf.db3FilePath + slash + conf.db3FileName;
    ReadDatas(filePath, conf);
}

void Db3Reader::ReadDatas(const std::string& file_path, const Config& conf)
{
    rosbag2_storage::StorageOptions storage_options{};

    storage_options.uri = file_path;
    storage_options.storage_id = "sqlite3";

    rosbag2_cpp::ConverterOptions converter_options{};
    converter_options.input_serialization_format = "cdr";
    converter_options.output_serialization_format = "cdr";

    rosbag2_cpp::readers::SequentialReader reader;
    reader.open(storage_options, converter_options);

    rosbag2_storage::StorageFilter storage_filter;
    storage_filter.topics = conf.topics;
    reader.set_filter(storage_filter);

    const auto topics = reader.get_all_topics_and_types();

    for (const auto& topic : topics) {
        topics_name2type_[topic.name] = topic.type;
    }

    int num = 1;

    while (reader.has_next()) {
        auto message = reader.read_next();
        auto serialized_message = rclcpp::SerializedMessage(*message->serialized_data);
        auto type = topics_name2type_[message->topic_name];
        auto frame_id = conf.frameIdMap.at(message->topic_name);
        if (type == "sensor_msgs/msg/PointCloud2") {
            auto serializer = rclcpp::Serialization<sensor_msgs::msg::PointCloud2>();
            sensor_msgs::msg::PointCloud2 pc2;
            serializer.deserialize_message(&serialized_message, &pc2);
            pc2.header.frame_id = frame_id;
            Eigen::Affine3d transform = conf.extrinsicMap.at(message->topic_name);
            TransformPointCloud(pc2, transform);
            pc_proc_->Input(message->topic_name, std::move(pc2));
            continue;
        }
        if (type == "sensor_msgs/msg/Imu") {
            // sensor_msgs::msg::Imu
            auto serializer = rclcpp::Serialization<sensor_msgs::msg::Imu>();
            sensor_msgs::msg::Imu imu_data;
            serializer.deserialize_message(&serialized_message, &imu_data);
            imu_data.header.frame_id = frame_id;
            pc_proc_->Input(message->topic_name, std::move(imu_data));
            continue;
        }
    }
    pc_proc_->SetEndTime();
}

void Db3Reader::TransformPointCloud(sensor_msgs::msg::PointCloud2& pc, Eigen::Affine3d& affine)
{
    sensor_msgs::PointCloud2Iterator<float> iter_x(pc, "x");
    sensor_msgs::PointCloud2Iterator<float> iter_y(pc, "y");
    sensor_msgs::PointCloud2Iterator<float> iter_z(pc, "z");

    while (iter_x != iter_x.end() && iter_y != iter_y.end() && iter_z != iter_z.end()) {
        Eigen::Vector3d point(*iter_x, *iter_y, *iter_z);
        auto new_point = affine * point;
        *iter_x = new_point[0];
        *iter_y = new_point[1];
        *iter_z = new_point[2];
        ++iter_x;
        ++iter_y;
        ++iter_z;
    }
}

} // namespace SensorProc
} // namespace LidarViewRos2

标签:galactic,msgs,auto,storage,iter,foxy,message,include,rosbag
From: https://www.cnblogs.com/hywing/p/18091819

相关文章

  • foxy rviz2 "rviz_common/Time"报错问题
    报错内容Theclassrequiredforthispanel,'rviz_common/Time',couldnotbeloaded.Error:Accordingtotheloadedplugindescriptionstheclassrviz_common/Timewithbaseclasstyperviz_common::Paneldoesnotexist.DeclaredtypesareTeleopPanel......
  • ros2 foxy订阅话题问题
    代码片段这部分代码在galactic版本编译是OK的,可在foxy下编译就出了问题TeleopPanel::TeleopPanel(QWidget*parent):rviz_common::Panel(parent),playRate_(1.0){signalPub_=nh_->create_publisher<std_msgs::msg::Int16>("/pixel/lv/run_signal",5);beginPub_......
  • rosbag 包提取图片和点云数据
    环境Ubuntu20.04ROSnoeticPython3.8.10⚠️注意:Python版本很重要,建议用3.8.10版本,如果使用更新的版本,会导致程序需要的库版本不对应,会报错。建议使用conda创建一个虚拟环境最佳,创建指令:condacreate-nros_envpython=3.8.10。安装所需的包在创建的Python3.8.......
  • foxy与galactic解析rosbag的不同之处
    前言foxy和galactic版本在rosbag2_storage这个包的调整有点大(头文件及接口的命名空间),下面的代码仅供参考使用foxy#include"db3_reader.h"#include<pcl/common/transforms.h>#include<pcl/point_types.h>#include<pcl_conversions/pcl_conversions.h>#include<rosba......
  • foxy rviz2 "rviz_common/Time"报错问题
    报错内容Theclassrequiredforthispanel,'rviz_common/Time',couldnotbeloaded.Error:Accordingtotheloadedplugindescriptionstheclassrviz_common/Timewithbaseclasstyperviz_common::Paneldoesnotexist.DeclaredtypesareTeleopPanel......
  • ROS2 foxy 单目相机标定方法(笔记本电脑摄像头)
    环境:Ubuntu20.04、ROS2foxy相机标定使用的是棋盘格的内部顶点,因此"12x9"的棋盘板,其内部顶点参数为"11x8"。安装ImagePipeline安装相机标定所需软件包:sudoaptinstallros-galactic-camera-calibration-parserssudoaptinstallros-galactic-camera-info-managers......
  • ORBSLAM3+ROS2foxy 调用笔记本摄像头跑单目相机程序 (Ubuntu20.04)
    环境要求:Ubuntu20.04、ROS2foxy、OpenCV4.4.01.安装ORB_SLAM3首先安装ORB_SLAM3:https://github.com/zang09/ORB-SLAM3-STEREO-FIXED。安装方法参考:https://www.cnblogs.com/xiaoaug/p/17766112.html安装完成并且测试数据集也能够跑通后即可。2.下载ROS2foxy版ORB_......
  • 图像和IMU数据与rosbag互转
    1.图像和IMU数据-->rosbagkalibr_bagcreater--folder/home/xue/桌面/cali/storage06011455/.--output-bagcamimu.bag 2.rosbag-->图像和IMU数据kalibr_bagextractor--imu-topics/imu0--output-folderTMP-data--bagcamimu.bagkalibr_bagextractor--image-topics......
  • rosbag
    rosbag是ROS系统中一个常用的工具,用于记录和回放ROS节点的消息数据 1. rosbagrecord[topic]:记录指定的ROStopic。例如,要记录一个名为/scan的topic,可以使用以下命令rosbagrecord/scanrosbagrecord-Odata.bag/scan2. rosbagplay[bagfile]:回放指定的r......
  • ROS_rosbag命令行以及检查topic
    rosbagrosbag既可以指命令行中数据包相关命令,也可以指c++/python的rosbag库。这里的rosbag是指前者rosbag-基于离线数据快速重现曾经的实际场景,进行可重复......