PointCloudCrafter
Loading...
Searching...
No Matches
rosbag_reader.hpp
1/*
2 * Copyright 2024 2024 Markus Pielmeier, Florian Sauerbeck,
3 * Dominik Kulmer, Maximilian Leitenstern
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
20#include <functional>
21#include <memory>
22#include <rclcpp/clock.hpp>
23#include <rclcpp/logging.hpp>
24#include <rclcpp/utilities.hpp>
25#include <rosbag2_cpp/reader.hpp>
26#include <string>
27#include <unordered_map>
28#include <vector>
29
30#include "message_filters/message_event.h"
31#include "message_filters/simple_filter.h"
32namespace pointcloudcrafter::tools
33{
40template <typename T>
42{
43 const rosbag2_storage::SerializedBagMessage & bag_msg;
44 const T & ros_msg;
45};
46
50{
51public:
52 virtual void handle_serialized_message(
53 const rosbag2_storage::SerializedBagMessage & bag_msg,
54 const rclcpp::SerializedMessage & ser_msg) = 0;
55};
56
60template <typename T>
62{
63public:
68 explicit SerializedMessageHandler(std::function<void(const RosbagReaderMsg<T> &)> callback)
69 : callback_(callback)
70 {
71 }
72
78 const rosbag2_storage::SerializedBagMessage & bag_msg,
79 const rclcpp::SerializedMessage & ser_msg) override
80 {
81 T msg;
82 serialization_.deserialize_message(&ser_msg, &msg);
83 callback_({bag_msg, msg});
84 }
85
86private:
87 rclcpp::Serialization<T> serialization_;
88 std::function<void(const RosbagReaderMsg<T> &)> callback_;
89};
91{
92public:
97 explicit RosbagReader(const std::string & BAG_PATH) { reader_.open(BAG_PATH); }
104 template <typename T>
106 const std::string & topic_name, std::function<void(const RosbagReaderMsg<T> &)> callback)
107 {
108 handlers_[topic_name].push_back(std::make_unique<SerializedMessageHandler<T>>(callback));
109 }
110
114 void set_state(bool state) { running_ = state; }
119 void process()
120 {
121 int64_t first_msg_time = -1;
122 rclcpp::Clock wall_clock{};
123
124 // Configure the reader to only read the topics we are interested in
125 std::vector<std::string> topics{};
126 topics.reserve(handlers_.size());
127 for (auto & entry : handlers_) {
128 topics.push_back(entry.first);
129 }
130 reader_.set_filter({topics});
131
132 // Initialize progress logging
133 double total_duration = static_cast<double>((reader_.get_metadata().duration.count()) / 1.0e9);
134 running_ = true;
135
136 // Process the bag files
137 while (reader_.has_next() && rclcpp::ok() && running_) {
138 auto bag_msg = reader_.read_next();
139 rclcpp::SerializedMessage ser_msg{*bag_msg->serialized_data};
140
141 // Get the first message time
142 if (first_msg_time < 0) {
143 first_msg_time = bag_msg->send_timestamp;
144 }
145
146 // Log progress
147 double time_done = static_cast<double>((bag_msg->send_timestamp - first_msg_time) / 1e9);
148 RCLCPP_INFO_THROTTLE(
149 logger_, wall_clock, 1000, "Processed %.3f sec of bag [% 5.1f%%]", time_done,
150 100 * time_done / total_duration);
151
152 // Call the handlers
153 auto it = handlers_.find(bag_msg->topic_name);
154 if (it == handlers_.end()) {
155 continue;
156 }
157
158 for (auto & handler : it->second) {
159 handler->handle_serialized_message(*bag_msg, ser_msg);
160 }
161 }
162 }
163
164private:
165 // Map to store the handlers for each topic
166 std::unordered_map<std::string, std::vector<std::shared_ptr<SerializedMessageHandlerBase>>>
167 handlers_;
168 // Logger to print progress
169 rclcpp::Logger logger_ = rclcpp::get_logger("rosbag_reader");
170 // Reader to read the bag file
171 rosbag2_cpp::Reader reader_;
172 // State flag
173 bool running_{true};
174};
175
181template <typename T>
182class MsgFilter : public message_filters::SimpleFilter<T>
183{
184public:
185 MsgFilter(const std::string & topic_name, RosbagReader & reader)
186 {
187 reader.add_listener<T>(topic_name, [this](const RosbagReaderMsg<T> & msg) {
188 auto msg_ptr = std::make_shared<T>(msg.ros_msg);
189 this->signalMessage(msg_ptr);
190 });
191 }
192};
193} // namespace pointcloudcrafter::tools
Definition rosbag_reader.hpp:91
RosbagReader(const std::string &BAG_PATH)
Constructor from a bag file path.
Definition rosbag_reader.hpp:97
void set_state(bool state)
Set the state flag to false to stop processing.
Definition rosbag_reader.hpp:114
void process()
Process the bag file.
Definition rosbag_reader.hpp:119
void add_listener(const std::string &topic_name, std::function< void(const RosbagReaderMsg< T > &)> callback)
Add a listener for a specific topic.
Definition rosbag_reader.hpp:105
Base class for message handling.
Definition rosbag_reader.hpp:50
Class to handle serialized messages with a callback.
Definition rosbag_reader.hpp:62
void handle_serialized_message(const rosbag2_storage::SerializedBagMessage &bag_msg, const rclcpp::SerializedMessage &ser_msg) override
Deserialize the message and call the callback.
Definition rosbag_reader.hpp:77
SerializedMessageHandler(std::function< void(const RosbagReaderMsg< T > &)> callback)
Constructor from a callback function.
Definition rosbag_reader.hpp:68
Struct to hold a ROS message and its corresponding rosbag message.
Definition rosbag_reader.hpp:42