foxy与galactic解析rosbag的不同之处
前言
foxy
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
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
阅读剩余
版权声明:
作者:hywing
链接:https://iotstuff.cn/foxy%e4%b8%8egalactic%e8%a7%a3%e6%9e%90rosbag%e7%9a%84%e4%b8%8d%e5%90%8c%e4%b9%8b%e5%a4%84/
文章版权归作者所有,未经允许请勿转载。
THE END