Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,48 @@ 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
${example_interfaces_TARGETS}
)

install(TARGETS
compressed_bag_recorder
compressed_bag_writer.cpp
DESTINATION lib/${PROJECT_NAME}
)

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -12,54 +12,31 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <memory>
#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<rosbag2_compression::SequentialCompressionWriter>(
compression_options);

writer_ = std::make_unique<rosbag2_cpp::Writer>(std::move(compressed_writer));
writer_->open("my_bag");
int main(int argc, char *argv[])
{
rclcpp::init(argc, argv);

subscription_ = this->create_subscription<example_interfaces::msg::String>(
"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<const rclcpp::SerializedMessage> 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<rosbag2_transport::Recorder>(
std::move(writer), storage_options, record_options);

rclcpp::Subscription<example_interfaces::msg::String>::SharedPtr subscription_;
std::unique_ptr<rosbag2_cpp::Writer> writer_;
};
recorder->record();
rclcpp::spin(recorder);

int main(int argc, char *argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<CompressedBagRecorder>());
recorder->stop();
rclcpp::shutdown();
return 0;
}
Original file line number Diff line number Diff line change
@@ -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 <memory>

#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<rosbag2_compression::SequentialCompressionWriter>(
compression_options);

writer_ = std::make_unique<rosbag2_cpp::Writer>(std::move(compressed_writer));
writer_->open("my_bag");

subscription_ = this->create_subscription<example_interfaces::msg::String>(
"chatter", 10, std::bind(&CompressedBagWriter::topic_callback, this, _1));
}

private:
void topic_callback(std::shared_ptr<const rclcpp::SerializedMessage> msg) const
{
rclcpp::Time time_stamp = this->now();

writer_->write(msg, "chatter", "example_interfaces/msg/String", time_stamp);
}

rclcpp::Subscription<example_interfaces::msg::String>::SharedPtr subscription_;
std::unique_ptr<rosbag2_cpp::Writer> writer_;
};

int main(int argc, char *argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<CompressedBagWriter>());
rclcpp::shutdown();
return 0;
}
47 changes: 15 additions & 32 deletions rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -12,44 +12,27 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <memory>
#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<rosbag2_cpp::Writer>();
rclcpp::init(argc, argv);

writer_->open("my_bag");
rosbag2_storage::StorageOptions storage_options;
storage_options.uri = "my_bag";

subscription_ = create_subscription<example_interfaces::msg::String>(
"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<const rclcpp::SerializedMessage> msg) const
{
rclcpp::Time time_stamp = this->now();
writer_->write(msg, "chatter", "example_interfaces/msg/String", time_stamp);
}
auto writer = std::make_unique<rosbag2_cpp::Writer>();
auto recorder = std::make_shared<rosbag2_transport::Recorder>(
std::move(writer), storage_options, record_options);

rclcpp::Subscription<example_interfaces::msg::String>::SharedPtr subscription_;
std::unique_ptr<rosbag2_cpp::Writer> writer_;
};
recorder->record();
rclcpp::spin(recorder);

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<SimpleBagRecorder>());
recorder->stop();
rclcpp::shutdown();
return 0;
}
55 changes: 55 additions & 0 deletions rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_writer.cpp
Original file line number Diff line number Diff line change
@@ -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 <memory>

#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<rosbag2_cpp::Writer>();

writer_->open("my_bag");

subscription_ = create_subscription<example_interfaces::msg::String>(
"chatter", 10, std::bind(&SimpleBagWriter::topic_callback, this, _1));
}

private:
void topic_callback(std::shared_ptr<const rclcpp::SerializedMessage> msg) const
{
rclcpp::Time time_stamp = this->now();
writer_->write(msg, "chatter", "example_interfaces/msg/String", time_stamp);
}

rclcpp::Subscription<example_interfaces::msg::String>::SharedPtr subscription_;
std::unique_ptr<rosbag2_cpp::Writer> writer_;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<SimpleBagWriter>());
rclcpp::shutdown();
return 0;
}
Loading
Loading