diff --git a/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt b/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt index f90e99b3e0..c9ac4cafd5 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt +++ b/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt @@ -24,20 +24,40 @@ find_package(rosbag2_cpp REQUIRED) find_package(rosbag2_transport REQUIRED) find_package(example_interfaces REQUIRED) +add_executable(compressed_bag_recorder src/compressed_bag_recorder.cpp) +target_link_libraries(compressed_bag_recorder + rosbag2_transport::rosbag2_transport +) + +install(TARGETS + compressed_bag_recorder + DESTINATION lib/${PROJECT_NAME} +) + add_executable(simple_bag_recorder src/simple_bag_recorder.cpp) target_link_libraries(simple_bag_recorder + rosbag2_transport::rosbag2_transport +) + +install(TARGETS + simple_bag_recorder + DESTINATION lib/${PROJECT_NAME} +) + +add_executable(simple_bag_writer src/simple_bag_writer.cpp) +target_link_libraries(simple_bag_writer rclcpp::rclcpp rosbag2_cpp::rosbag2_cpp ${example_interfaces_TARGETS} ) install(TARGETS - simple_bag_recorder + simple_bag_writer DESTINATION lib/${PROJECT_NAME} ) -add_executable(compressed_bag_recorder src/compressed_bag_recorder.cpp) -target_link_libraries(compressed_bag_recorder +add_executable(compressed_bag_writer.cpp src/compressed_bag_writer.cpp) +target_link_libraries(compressed_bag_writer.cpp rclcpp::rclcpp rosbag2_cpp::rosbag2_cpp rosbag2_compression::rosbag2_compression @@ -45,7 +65,7 @@ target_link_libraries(compressed_bag_recorder ) install(TARGETS - compressed_bag_recorder + compressed_bag_writer.cpp DESTINATION lib/${PROJECT_NAME} ) diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/compressed_bag_recorder.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/compressed_bag_recorder.cpp index b95bbd0030..044301f96f 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/compressed_bag_recorder.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/compressed_bag_recorder.cpp @@ -1,4 +1,4 @@ -// Copyright 2025 Open Source Robotics Foundation +// Copyright 2026 Open Source Robotics Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,54 +12,31 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include +#include "rosbag2_transport/reader_writer_factory.hpp" +#include "rosbag2_transport/recorder.hpp" -#include "example_interfaces/msg/string.hpp" -#include "rclcpp/rclcpp.hpp" - -#include "rosbag2_compression/compression_options.hpp" -#include "rosbag2_compression/sequential_compression_writer.hpp" - -#include "rosbag2_cpp/writer.hpp" - -using std::placeholders::_1; - -class CompressedBagRecorder : public rclcpp::Node { -public: - CompressedBagRecorder() - : Node("compressed_bag_recorder") - { - rosbag2_compression::CompressionOptions compression_options; - - compression_options.compression_format = "zstd"; - compression_options.compression_mode = rosbag2_compression::CompressionMode::FILE; - - auto compressed_writer = std::make_unique( - compression_options); - - writer_ = std::make_unique(std::move(compressed_writer)); - writer_->open("my_bag"); +int main(int argc, char *argv[]) +{ + rclcpp::init(argc, argv); - subscription_ = this->create_subscription( - "chatter", 10, std::bind(&CompressedBagRecorder::topic_callback, this, _1)); - } + rosbag2_storage::StorageOptions storage_options; + storage_options.uri = "my_bag"; -private: - void topic_callback(std::shared_ptr msg) const - { - rclcpp::Time time_stamp = this->now(); + rosbag2_transport::RecordOptions record_options; + record_options.all_topics = true; + record_options.rmw_serialization_format = "cdr"; + record_options.compression_format = "zstd"; + record_options.compression_mode = "file"; - writer_->write(msg, "chatter", "example_interfaces/msg/String", time_stamp); - } + // Calling ReaderWriterFactory creates a writer with compression parameters + auto writer = rosbag2_transport::ReaderWriterFactory::make_writer(record_options); + auto recorder = std::make_shared( + std::move(writer), storage_options, record_options); - rclcpp::Subscription::SharedPtr subscription_; - std::unique_ptr writer_; -}; + recorder->record(); + rclcpp::spin(recorder); -int main(int argc, char *argv[]) -{ - rclcpp::init(argc, argv); - rclcpp::spin(std::make_shared()); + recorder->stop(); rclcpp::shutdown(); return 0; } diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/compressed_bag_writer.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/compressed_bag_writer.cpp new file mode 100644 index 0000000000..42fbaf04d5 --- /dev/null +++ b/rosbag2_examples/rosbag2_examples_cpp/src/compressed_bag_writer.cpp @@ -0,0 +1,65 @@ +// Copyright 2025 Open Source Robotics Foundation +// +// 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 + +#include "example_interfaces/msg/string.hpp" +#include "rclcpp/rclcpp.hpp" + +#include "rosbag2_compression/compression_options.hpp" +#include "rosbag2_compression/sequential_compression_writer.hpp" + +#include "rosbag2_cpp/writer.hpp" + +using std::placeholders::_1; + +class CompressedBagWriter : public rclcpp::Node { +public: + CompressedBagWriter() + : Node("compressed_bag_writer") + { + rosbag2_compression::CompressionOptions compression_options; + + compression_options.compression_format = "zstd"; + compression_options.compression_mode = rosbag2_compression::CompressionMode::FILE; + + auto compressed_writer = std::make_unique( + compression_options); + + writer_ = std::make_unique(std::move(compressed_writer)); + writer_->open("my_bag"); + + subscription_ = this->create_subscription( + "chatter", 10, std::bind(&CompressedBagWriter::topic_callback, this, _1)); + } + +private: + void topic_callback(std::shared_ptr msg) const + { + rclcpp::Time time_stamp = this->now(); + + writer_->write(msg, "chatter", "example_interfaces/msg/String", time_stamp); + } + + rclcpp::Subscription::SharedPtr subscription_; + std::unique_ptr writer_; +}; + +int main(int argc, char *argv[]) +{ + rclcpp::init(argc, argv); + rclcpp::spin(std::make_shared()); + rclcpp::shutdown(); + return 0; +} diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp index 2e04c96912..3aa91180f7 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp @@ -1,4 +1,4 @@ -// Copyright 2021 Open Source Robotics Foundation +// Copyright 2026 Open Source Robotics Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,44 +12,27 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include +#include "rosbag2_transport/recorder.hpp" -#include "example_interfaces/msg/string.hpp" -#include "rclcpp/rclcpp.hpp" - -#include "rosbag2_cpp/writer.hpp" - -using std::placeholders::_1; - -class SimpleBagRecorder : public rclcpp::Node +int main(int argc, char *argv[]) { -public: - SimpleBagRecorder() - : Node("simple_bag_recorder") - { - writer_ = std::make_unique(); + rclcpp::init(argc, argv); - writer_->open("my_bag"); + rosbag2_storage::StorageOptions storage_options; + storage_options.uri = "my_bag"; - subscription_ = create_subscription( - "chatter", 10, std::bind(&SimpleBagRecorder::topic_callback, this, _1)); - } + rosbag2_transport::RecordOptions record_options; + record_options.all_topics = true; + record_options.rmw_serialization_format = "cdr"; -private: - void topic_callback(std::shared_ptr msg) const - { - rclcpp::Time time_stamp = this->now(); - writer_->write(msg, "chatter", "example_interfaces/msg/String", time_stamp); - } + auto writer = std::make_unique(); + auto recorder = std::make_shared( + std::move(writer), storage_options, record_options); - rclcpp::Subscription::SharedPtr subscription_; - std::unique_ptr writer_; -}; + recorder->record(); + rclcpp::spin(recorder); -int main(int argc, char * argv[]) -{ - rclcpp::init(argc, argv); - rclcpp::spin(std::make_shared()); + recorder->stop(); rclcpp::shutdown(); return 0; } diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_writer.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_writer.cpp new file mode 100644 index 0000000000..c20e227734 --- /dev/null +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_writer.cpp @@ -0,0 +1,55 @@ +// Copyright 2021 Open Source Robotics Foundation +// +// 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 + +#include "example_interfaces/msg/string.hpp" +#include "rclcpp/rclcpp.hpp" + +#include "rosbag2_cpp/writer.hpp" + +using std::placeholders::_1; + +class SimpleBagWriter : public rclcpp::Node +{ +public: + SimpleBagWriter() + : Node("simple_bag_writer") + { + writer_ = std::make_unique(); + + writer_->open("my_bag"); + + subscription_ = create_subscription( + "chatter", 10, std::bind(&SimpleBagWriter::topic_callback, this, _1)); + } + +private: + void topic_callback(std::shared_ptr msg) const + { + rclcpp::Time time_stamp = this->now(); + writer_->write(msg, "chatter", "example_interfaces/msg/String", time_stamp); + } + + rclcpp::Subscription::SharedPtr subscription_; + std::unique_ptr writer_; +}; + +int main(int argc, char * argv[]) +{ + rclcpp::init(argc, argv); + rclcpp::spin(std::make_shared()); + rclcpp::shutdown(); + return 0; +} diff --git a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/compressed_bag_recorder.py b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/compressed_bag_recorder.py index a25707afed..65f64d8649 100644 --- a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/compressed_bag_recorder.py +++ b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/compressed_bag_recorder.py @@ -1,4 +1,4 @@ -# Copyright 2025 Open Source Robotics Foundation, Inc. +# Copyright 2026 Open Source Robotics Foundation, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -11,60 +11,37 @@ # 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. - import rclpy from rclpy.executors import ExternalShutdownException -from rclpy.node import Node -from rclpy.serialization import serialize_message import rosbag2_py -from std_msgs.msg import String - - -class CompressedBagRecorder(Node): - - def __init__(self): - super().__init__('compressed_bag_recorder') - compression_options = rosbag2_py.CompressionOptions( - compression_format='zstd', - compression_mode=rosbag2_py.CompressionMode.MESSAGE) - storage_options = rosbag2_py.StorageOptions( - uri='my_bag', - storage_id='sqlite3') - converter_options = rosbag2_py.ConverterOptions('', '') - - self.compressed_writer = rosbag2_py.SequentialCompressionWriter(compression_options) - self.compressed_writer.open(storage_options, converter_options) +def main(args=None): + rclpy.init(args=args) - topic_info = rosbag2_py.TopicMetadata( - id=0, - name='chatter', - type='std_msgs/msg/String', - serialization_format='cdr') - self.compressed_writer.create_topic(topic_info) + storage_options = rosbag2_py.StorageOptions(uri='my_bag') - self.subscription = self.create_subscription( - String, - 'chatter', - self.topic_callback, - 10) - self.subscription + record_options = rosbag2_py.RecordOptions() + record_options.all_topics = True + record_options.is_discovery_disabled = False + record_options.rmw_serialization_format = 'cdr' + record_options.compression_format = 'zstd' + record_options.compression_mode = 'file' - def topic_callback(self, msg): - self.compressed_writer.write( - 'chatter', - serialize_message(msg), - self.get_clock().now().nanoseconds) + recorder = rosbag2_py.Recorder( + storage_options, + record_options, + 'info', + 'compressed_recorder_demo') + recorder.start_spin() + recorder.record() -def main(args=None): try: - with rclpy.init(args=args): - cbr = CompressedBagRecorder() - rclpy.spin(cbr) + while (rclpy.ok()): + pass except (KeyboardInterrupt, ExternalShutdownException): - pass + recorder.stop_spin() if __name__ == '__main__': diff --git a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/compressed_bag_writer.py b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/compressed_bag_writer.py new file mode 100644 index 0000000000..b4c38787f3 --- /dev/null +++ b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/compressed_bag_writer.py @@ -0,0 +1,71 @@ +# Copyright 2025 Open Source Robotics Foundation, Inc. +# +# 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. + +import rclpy +from rclpy.executors import ExternalShutdownException +from rclpy.node import Node +from rclpy.serialization import serialize_message +import rosbag2_py +from std_msgs.msg import String + + +class CompressedBagWriter(Node): + + def __init__(self): + super().__init__('compressed_bag_recorder') + + compression_options = rosbag2_py.CompressionOptions( + compression_format='zstd', + compression_mode=rosbag2_py.CompressionMode.MESSAGE) + + storage_options = rosbag2_py.StorageOptions( + uri='my_bag', + storage_id='sqlite3') + converter_options = rosbag2_py.ConverterOptions('', '') + + self.compressed_writer = rosbag2_py.SequentialCompressionWriter(compression_options) + self.compressed_writer.open(storage_options, converter_options) + + topic_info = rosbag2_py.TopicMetadata( + id=0, + name='chatter', + type='std_msgs/msg/String', + serialization_format='cdr') + self.compressed_writer.create_topic(topic_info) + + self.subscription = self.create_subscription( + String, + 'chatter', + self.topic_callback, + 10) + self.subscription + + def topic_callback(self, msg): + self.compressed_writer.write( + 'chatter', + serialize_message(msg), + self.get_clock().now().nanoseconds) + + +def main(args=None): + try: + with rclpy.init(args=args): + cbr = CompressedBagWriter() + rclpy.spin(cbr) + except (KeyboardInterrupt, ExternalShutdownException): + pass + + +if __name__ == '__main__': + main() diff --git a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_recorder.py b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_recorder.py index b92b23a4eb..4b06c5877c 100644 --- a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_recorder.py +++ b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_recorder.py @@ -1,4 +1,4 @@ -# Copyright 2023 Open Source Robotics Foundation, Inc. +# Copyright 2026 Open Source Robotics Foundation, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,52 +13,33 @@ # limitations under the License. import rclpy from rclpy.executors import ExternalShutdownException -from rclpy.node import Node -from rclpy.serialization import serialize_message import rosbag2_py -from std_msgs.msg import String -class SimpleBagRecorder(Node): - - def __init__(self): - super().__init__('simple_bag_recorder') - self.writer = rosbag2_py.SequentialWriter() - - storage_options = rosbag2_py.StorageOptions( - uri='my_bag', - storage_id='sqlite3') - converter_options = rosbag2_py.ConverterOptions('', '') - self.writer.open(storage_options, converter_options) +def main(args=None): + rclpy.init(args=args) - topic_info = rosbag2_py.TopicMetadata( - id=0, - name='chatter', - type='std_msgs/msg/String', - serialization_format='cdr') - self.writer.create_topic(topic_info) + storage_options = rosbag2_py.StorageOptions(uri='my_bag') - self.subscription = self.create_subscription( - String, - 'chatter', - self.topic_callback, - 10) - self.subscription + record_options = rosbag2_py.RecordOptions() + record_options.all_topics = True + record_options.is_discovery_disabled = False + record_options.rmw_serialization_format = 'cdr' - def topic_callback(self, msg): - self.writer.write( - 'chatter', - serialize_message(msg), - self.get_clock().now().nanoseconds) + recorder = rosbag2_py.Recorder( + storage_options, + record_options, + 'info', + 'simple_recorder_demo') + recorder.start_spin() + recorder.record() -def main(args=None): try: - with rclpy.init(args=args): - sbr = SimpleBagRecorder() - rclpy.spin(sbr) + while rclpy.ok(): + pass except (KeyboardInterrupt, ExternalShutdownException): - pass + recorder.stop_spin() if __name__ == '__main__': diff --git a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_writer.py b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_writer.py new file mode 100644 index 0000000000..1959c179eb --- /dev/null +++ b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_writer.py @@ -0,0 +1,65 @@ +# Copyright 2023 Open Source Robotics Foundation, Inc. +# +# 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. +import rclpy +from rclpy.executors import ExternalShutdownException +from rclpy.node import Node +from rclpy.serialization import serialize_message +import rosbag2_py +from std_msgs.msg import String + + +class SimpleBagWriter(Node): + + def __init__(self): + super().__init__('simple_bag_writer') + self.writer = rosbag2_py.SequentialWriter() + + storage_options = rosbag2_py.StorageOptions( + uri='my_bag', + storage_id='sqlite3') + converter_options = rosbag2_py.ConverterOptions('', '') + self.writer.open(storage_options, converter_options) + + topic_info = rosbag2_py.TopicMetadata( + id=0, + name='chatter', + type='std_msgs/msg/String', + serialization_format='cdr') + self.writer.create_topic(topic_info) + + self.subscription = self.create_subscription( + String, + 'chatter', + self.topic_callback, + 10) + self.subscription + + def topic_callback(self, msg): + self.writer.write( + 'chatter', + serialize_message(msg), + self.get_clock().now().nanoseconds) + + +def main(args=None): + try: + with rclpy.init(args=args): + sbw = SimpleBagWriter() + rclpy.spin(sbw) + except (KeyboardInterrupt, ExternalShutdownException): + pass + + +if __name__ == '__main__': + main() diff --git a/rosbag2_examples/rosbag2_examples_py/setup.py b/rosbag2_examples/rosbag2_examples_py/setup.py index 8f438ca8e2..2ee62b3453 100644 --- a/rosbag2_examples/rosbag2_examples_py/setup.py +++ b/rosbag2_examples/rosbag2_examples_py/setup.py @@ -25,11 +25,13 @@ entry_points={ 'console_scripts': [ 'rosbag2csv = rosbag2_examples_py.rosbag2csv:main', - 'simple_bag_recorder = rosbag2_examples_py.simple_bag_recorder:main', + 'simple_bag_writer = rosbag2_examples_py.simple_bag_writer:main', 'simple_bag_reader = rosbag2_examples_py.simple_bag_reader:main', + 'simple_bag_recorder = rosbag2_examples_py.simple_bag_recorder:main', + 'compressed_bag_recorder = rosbag2_examples_py.compressed_bag_recorder:main', 'data_generator_node = rosbag2_examples_py.data_generator_node:main', 'data_generator_executable = rosbag2_examples_py.data_generator_executable:main', - 'compressed_bag_recorder = rosbag2_examples_py.compressed_bag_recorder:main', + 'compressed_bag_writer = rosbag2_examples_py.compressed_bag_writer:main', ], }, )