7 #include <QApplication>
8 #include <QProgressDialog>
13 #include <QElapsedTimer>
16 #include <rosidl_typesupport_introspection_cpp/identifier.hpp>
17 #include <rosidl_typesupport_introspection_cpp/message_introspection.hpp>
18 #include <rosidl_typesupport_introspection_cpp/field_types.hpp>
19 #include <rosidl_typesupport_cpp/identifier.hpp>
20 #include <rosbag2_cpp/typesupport_helpers.hpp>
21 #include <rosbag2_cpp/types/introspection_message.hpp>
22 #include <unordered_map>
23 #include <rclcpp/rclcpp.hpp>
29 #include <rosbag2_storage/storage_options.hpp>
30 #include <rosbag2_transport/reader_writer_factory.hpp>
46 auto metadata_io = std::make_unique<rosbag2_storage::MetadataIo>();
51 bagDir = finfo.dir().path();
54 rosbag2_storage::StorageOptions storageOptions;
55 const auto bag_metadata = metadata_io->read_metadata(bagDir.toStdString());
56 storageOptions.uri = bagDir.toStdString();
57 storageOptions.storage_id = bag_metadata.storage_identifier;
58 rosbag2_cpp::ConverterOptions converterOptions;
59 converterOptions.input_serialization_format =
"cdr";
60 converterOptions.output_serialization_format = rmw_get_serialization_format();
62 std::shared_ptr<rosbag2_cpp::Reader> temp_bag_reader =
63 rosbag2_transport::ReaderWriterFactory::make_reader(storageOptions);
65 QString oldPath = QDir::currentPath();
66 QDir::setCurrent(QDir::cleanPath(bagDir + QDir::separator() +
".."));
70 temp_bag_reader->open(storageOptions, converterOptions);
72 catch (std::runtime_error& ex)
74 QMessageBox::warning(
nullptr, tr(
"Error"), QString(
"rosbag::open thrown an exception:\n") + QString(ex.what()));
78 QDir::setCurrent(oldPath);
81 std::vector<rosbag2_storage::TopicMetadata> topic_metadata = temp_bag_reader->get_all_topics_and_types();
83 std::unordered_map<std::string, std::string> topicTypesByName;
85 std::vector<std::pair<QString, QString>> all_topics_qt;
87 std::vector<TopicInfo> topics_info;
89 std::set<std::string> blacklist_topic_name;
90 std::set<std::string> failed_topic_type;
92 for (
const rosbag2_storage::TopicMetadata&
topic : topic_metadata)
94 all_topics_qt.push_back( {QString::fromStdString(
topic.name),
95 QString::fromStdString(
topic.type)} );
96 topicTypesByName.emplace(
topic.name,
topic.type);
98 const auto& typesupport_identifier = rosidl_typesupport_cpp::typesupport_identifier;
104 failed_topic_type.insert(
topic.type);
105 blacklist_topic_name.insert(
topic.type);
109 if(!failed_topic_type.empty())
111 QString
msg(
"Can not recognize the following message types and those topics will be ignored:\n\n");
112 for(
const auto&
type: failed_topic_type)
114 msg +=
" " + QString::fromStdString(
type) +
"\n";
116 msg +=
"\nHave you forgotten to source your workspace?";
117 QMessageBox::warning(
nullptr, tr(
"Error"),
msg);
131 if (dialog->exec() !=
static_cast<int>(QDialog::Accepted))
155 std::set<std::string> topic_selected;
158 std::string topic_name = topic_qt.toStdString();
159 std::string topic_type = topicTypesByName.at(topic_name);
160 topic_selected.insert(topic_name);
163 topic_name, topic_type, plot_map);
164 parser.addParser(topic_name, ros2_parser);
170 QProgressDialog progress_dialog;
171 progress_dialog.setWindowTitle(
"Loading the rosbag");
172 progress_dialog.setLabelText(
"Loading... please wait");
173 progress_dialog.setWindowModality(Qt::ApplicationModal);
174 progress_dialog.setRange(0, bag_metadata.message_count);
175 progress_dialog.show();
179 plot_map.
addUserDefined(
"plotjuggler::rosbag2_cpp::consecutive_messages")->second;
181 plot_map.
addUserDefined(
"plotjuggler::rosbag2_cpp::topics_metadata")->second;
184 metadata_storage.
pushBack( {0, std::any(topics_info) } );
186 auto time_prev = std::chrono::high_resolution_clock::now();
191 const std::string& topic_name =
msg->topic_name;
192 if(blacklist_topic_name.count(topic_name))
196 const double msg_timestamp = 1e-9 * double(
msg->time_stamp);
199 if (msg_count++ % 100 == 0)
201 progress_dialog.setValue(msg_count);
202 QApplication::processEvents();
204 if (progress_dialog.wasCanceled())
211 if (topic_selected.find(topic_name) == topic_selected.end())
216 double timestamp = msg_timestamp;
218 parser.parseMessage(topic_name, msg_ref, timestamp);
222 plot_consecutive.
pushBack(data_point);
224 auto plot_pair = plot_map.
user_defined.find(topic_name);
233 auto now = std::chrono::high_resolution_clock::now();
234 double diff = std::chrono::duration_cast<std::chrono::milliseconds>(now - time_prev).count();
236 qDebug() <<
"The rosbag loaded the data in " << diff <<
" milliseconds";