From b6b62a94727652d0796afddfbd0e9c1abbde9407 Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Thu, 29 Feb 2024 06:12:17 +0000 Subject: [PATCH 01/19] Add simple bag reader example Signed-off-by: Sangtaek Lee --- .../rosbag2_examples_cpp/CMakeLists.txt | 15 +++- .../rosbag2_examples_cpp/package.xml | 1 + .../src/simple_bag_reader.cpp | 70 +++++++++++++++++++ .../src/simple_bag_recorder.cpp | 8 +-- 4 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp diff --git a/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt b/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt index 3e68057661..6f593b4947 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt +++ b/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt @@ -20,13 +20,14 @@ endif() find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(rosbag2_cpp REQUIRED) +find_package(std_msgs REQUIRED) find_package(example_interfaces REQUIRED) add_executable(simple_bag_recorder src/simple_bag_recorder.cpp) target_link_libraries(simple_bag_recorder rclcpp::rclcpp rosbag2_cpp::rosbag2_cpp - ${example_interfaces_TARGETS} + ${std_msgs_TARGETS} ) install(TARGETS @@ -58,6 +59,18 @@ install(TARGETS DESTINATION lib/${PROJECT_NAME} ) +add_executable(simple_bag_reader src/simple_bag_reader.cpp) +target_link_libraries(simple_bag_reader + rclcpp::rclcpp + rosbag2_cpp::rosbag2_cpp + ${std_msgs_TARGETS} +) + +install(TARGETS + simple_bag_reader + DESTINATION lib/${PROJECT_NAME} +) + if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) ament_lint_auto_find_test_dependencies() diff --git a/rosbag2_examples/rosbag2_examples_cpp/package.xml b/rosbag2_examples/rosbag2_examples_cpp/package.xml index 81b204f613..6a2f6a265c 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/package.xml +++ b/rosbag2_examples/rosbag2_examples_cpp/package.xml @@ -13,6 +13,7 @@ rclcpp rosbag2_cpp + std_msgs example_interfaces ament_lint_auto diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp new file mode 100644 index 0000000000..fce7a33e4f --- /dev/null +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include + +#include "rclcpp/rclcpp.hpp" +#include "rclcpp/serialization.hpp" +#include "rosbag2_cpp/reader.hpp" +#include "std_msgs/msg/string.hpp" + +using namespace std::chrono_literals; + +class PlaybackNode : public rclcpp::Node +{ + public: + PlaybackNode(const std::string & bag_filename) + : Node("playback_node") + { + publisher_ = this->create_publisher("chatter", 10); + + timer_ = this->create_wall_timer(100ms, + [this](){return this->timer_callback();} + ); + + reader_.open(bag_filename); + } + + private: + void timer_callback() + { + while (reader_.has_next()) { + rosbag2_storage::SerializedBagMessageSharedPtr msg = reader_.read_next(); + + if (msg->topic_name != "/turtle1/pose") { + continue; + } + + rclcpp::SerializedMessage serialized_msg(*msg->serialized_data); + std_msgs::msg::String::SharedPtr ros_msg = std::make_shared(); + + serialization_.deserialize_message(&serialized_msg, ros_msg.get()); + + publisher_->publish(*ros_msg); + std::cout << ros_msg->data << "\n"; + + break; + } + } + + rclcpp::TimerBase::SharedPtr timer_; + rclcpp::Publisher::SharedPtr publisher_; + + rclcpp::Serialization serialization_; + rosbag2_cpp::Reader reader_; +}; + +int main(int argc, char ** argv) +{ + if (argc != 2) { + std::cerr << "Usage: " << argv[0] << " " << std::endl; + return 1; + } + + rclcpp::init(argc, argv); + rclcpp::spin(std::make_shared(argv[1])); + rclcpp::shutdown(); + + return 0; +} \ No newline at end of file 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 b6535d40db..b2910ab0c4 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp @@ -14,7 +14,7 @@ #include -#include "example_interfaces/msg/string.hpp" +#include "std_msgs/msg/string.hpp" #include "rclcpp/rclcpp.hpp" #include "rosbag2_cpp/writer.hpp" @@ -31,7 +31,7 @@ class SimpleBagRecorder : public rclcpp::Node writer_->open("my_bag"); - subscription_ = create_subscription( + subscription_ = create_subscription( "chatter", 10, std::bind(&SimpleBagRecorder::topic_callback, this, _1)); } @@ -39,10 +39,10 @@ class SimpleBagRecorder : public rclcpp::Node void topic_callback(std::shared_ptr msg) const { rclcpp::Time time_stamp = this->now(); - writer_->write(msg, "chatter", "example_interfaces/msg/String", time_stamp); + writer_->write(msg, "chatter", "std_msgs/msg/String", time_stamp); } - rclcpp::Subscription::SharedPtr subscription_; + rclcpp::Subscription::SharedPtr subscription_; std::unique_ptr writer_; }; From 2ba65462b9f34e6e7c1ff0099cda81ee2f21d36e Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Fri, 1 Mar 2024 07:07:03 +0000 Subject: [PATCH 02/19] revert topic interface Signed-off-by: Sangtaek Lee --- rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt | 5 ++--- .../rosbag2_examples_cpp/src/simple_bag_reader.cpp | 12 ++++++------ .../rosbag2_examples_cpp/src/simple_bag_recorder.cpp | 8 ++++---- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt b/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt index 6f593b4947..6c9dc2c532 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt +++ b/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt @@ -20,14 +20,13 @@ endif() find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(rosbag2_cpp REQUIRED) -find_package(std_msgs REQUIRED) find_package(example_interfaces REQUIRED) add_executable(simple_bag_recorder src/simple_bag_recorder.cpp) target_link_libraries(simple_bag_recorder rclcpp::rclcpp rosbag2_cpp::rosbag2_cpp - ${std_msgs_TARGETS} + ${example_interfaces_TARGETS} ) install(TARGETS @@ -63,7 +62,7 @@ add_executable(simple_bag_reader src/simple_bag_reader.cpp) target_link_libraries(simple_bag_reader rclcpp::rclcpp rosbag2_cpp::rosbag2_cpp - ${std_msgs_TARGETS} + ${example_interfaces_TARGETS} ) install(TARGETS diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp index fce7a33e4f..614d30b1b9 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp @@ -7,7 +7,7 @@ #include "rclcpp/rclcpp.hpp" #include "rclcpp/serialization.hpp" #include "rosbag2_cpp/reader.hpp" -#include "std_msgs/msg/string.hpp" +#include "example_interfaces/msg/string.hpp" using namespace std::chrono_literals; @@ -17,7 +17,7 @@ class PlaybackNode : public rclcpp::Node PlaybackNode(const std::string & bag_filename) : Node("playback_node") { - publisher_ = this->create_publisher("chatter", 10); + publisher_ = this->create_publisher("chatter", 10); timer_ = this->create_wall_timer(100ms, [this](){return this->timer_callback();} @@ -32,12 +32,12 @@ class PlaybackNode : public rclcpp::Node while (reader_.has_next()) { rosbag2_storage::SerializedBagMessageSharedPtr msg = reader_.read_next(); - if (msg->topic_name != "/turtle1/pose") { + if (msg->topic_name != "/chatter") { continue; } rclcpp::SerializedMessage serialized_msg(*msg->serialized_data); - std_msgs::msg::String::SharedPtr ros_msg = std::make_shared(); + example_interfaces::msg::String::SharedPtr ros_msg = std::make_shared(); serialization_.deserialize_message(&serialized_msg, ros_msg.get()); @@ -49,9 +49,9 @@ class PlaybackNode : public rclcpp::Node } rclcpp::TimerBase::SharedPtr timer_; - rclcpp::Publisher::SharedPtr publisher_; + rclcpp::Publisher::SharedPtr publisher_; - rclcpp::Serialization serialization_; + rclcpp::Serialization serialization_; rosbag2_cpp::Reader reader_; }; 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 b2910ab0c4..b6535d40db 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp @@ -14,7 +14,7 @@ #include -#include "std_msgs/msg/string.hpp" +#include "example_interfaces/msg/string.hpp" #include "rclcpp/rclcpp.hpp" #include "rosbag2_cpp/writer.hpp" @@ -31,7 +31,7 @@ class SimpleBagRecorder : public rclcpp::Node writer_->open("my_bag"); - subscription_ = create_subscription( + subscription_ = create_subscription( "chatter", 10, std::bind(&SimpleBagRecorder::topic_callback, this, _1)); } @@ -39,10 +39,10 @@ class SimpleBagRecorder : public rclcpp::Node void topic_callback(std::shared_ptr msg) const { rclcpp::Time time_stamp = this->now(); - writer_->write(msg, "chatter", "std_msgs/msg/String", time_stamp); + writer_->write(msg, "chatter", "example_interfaces/msg/String", time_stamp); } - rclcpp::Subscription::SharedPtr subscription_; + rclcpp::Subscription::SharedPtr subscription_; std::unique_ptr writer_; }; From b2c9893aa8658c5cca6c4e05628bbe18ccce7961 Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Fri, 1 Mar 2024 07:13:32 +0000 Subject: [PATCH 03/19] change topic name Signed-off-by: Sangtaek Lee --- rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp index 614d30b1b9..77ae805a6d 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp @@ -32,7 +32,7 @@ class PlaybackNode : public rclcpp::Node while (reader_.has_next()) { rosbag2_storage::SerializedBagMessageSharedPtr msg = reader_.read_next(); - if (msg->topic_name != "/chatter") { + if (msg->topic_name != "chatter") { continue; } From ed349bea610201579de6be8e30a79e1fd31aeb77 Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Fri, 1 Mar 2024 07:39:21 +0000 Subject: [PATCH 04/19] delete unnecessary dependency Signed-off-by: Sangtaek Lee --- rosbag2_examples/rosbag2_examples_cpp/package.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/rosbag2_examples/rosbag2_examples_cpp/package.xml b/rosbag2_examples/rosbag2_examples_cpp/package.xml index 6a2f6a265c..81b204f613 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/package.xml +++ b/rosbag2_examples/rosbag2_examples_cpp/package.xml @@ -13,7 +13,6 @@ rclcpp rosbag2_cpp - std_msgs example_interfaces ament_lint_auto From 2a2b97b4d09a4cdc01a0f00d6463562864bddada Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Tue, 5 Mar 2024 06:53:01 +0000 Subject: [PATCH 05/19] update simple_bag_player Signed-off-by: Sangtaek Lee --- .../rosbag2_examples_cpp/CMakeLists.txt | 6 ++-- ...e_bag_reader.cpp => simple_bag_player.cpp} | 30 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) rename rosbag2_examples/rosbag2_examples_cpp/src/{simple_bag_reader.cpp => simple_bag_player.cpp} (63%) diff --git a/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt b/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt index 6c9dc2c532..de761a5af2 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt +++ b/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt @@ -58,15 +58,15 @@ install(TARGETS DESTINATION lib/${PROJECT_NAME} ) -add_executable(simple_bag_reader src/simple_bag_reader.cpp) -target_link_libraries(simple_bag_reader +add_executable(simple_bag_player src/simple_bag_player.cpp) +target_link_libraries(simple_bag_player rclcpp::rclcpp rosbag2_cpp::rosbag2_cpp ${example_interfaces_TARGETS} ) install(TARGETS - simple_bag_reader + simple_bag_player DESTINATION lib/${PROJECT_NAME} ) diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp similarity index 63% rename from rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp rename to rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp index 77ae805a6d..c5e7dcb763 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp @@ -6,6 +6,7 @@ #include "rclcpp/rclcpp.hpp" #include "rclcpp/serialization.hpp" +#include "rosbag2_storage/qos.hpp" #include "rosbag2_cpp/reader.hpp" #include "example_interfaces/msg/string.hpp" @@ -17,13 +18,19 @@ class PlaybackNode : public rclcpp::Node PlaybackNode(const std::string & bag_filename) : Node("playback_node") { - publisher_ = this->create_publisher("chatter", 10); + reader_.open(bag_filename); + + auto topics = reader_.get_all_topics_and_types(); + + for (const auto & topic : topics) { + if (topic.name == TOPIC_NAME) { + publisher_ = create_generic_publisher(topic.name, topic.type, rosbag2_storage::Rosbag2QoS{}); + } + } timer_ = this->create_wall_timer(100ms, [this](){return this->timer_callback();} ); - - reader_.open(bag_filename); } private: @@ -31,28 +38,21 @@ class PlaybackNode : public rclcpp::Node { while (reader_.has_next()) { rosbag2_storage::SerializedBagMessageSharedPtr msg = reader_.read_next(); - - if (msg->topic_name != "chatter") { + if (msg->topic_name != TOPIC_NAME) { continue; } - - rclcpp::SerializedMessage serialized_msg(*msg->serialized_data); - example_interfaces::msg::String::SharedPtr ros_msg = std::make_shared(); - - serialization_.deserialize_message(&serialized_msg, ros_msg.get()); - - publisher_->publish(*ros_msg); - std::cout << ros_msg->data << "\n"; + publisher_->publish(rclcpp::SerializedMessage(*msg->serialized_data)); break; } } rclcpp::TimerBase::SharedPtr timer_; - rclcpp::Publisher::SharedPtr publisher_; + std::shared_ptr publisher_; - rclcpp::Serialization serialization_; rosbag2_cpp::Reader reader_; + + const std::string TOPIC_NAME {"chatter"}; }; int main(int argc, char ** argv) From 7f360444ff2eba2c02a57b792abd565fd60324bc Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Tue, 5 Mar 2024 07:13:04 +0000 Subject: [PATCH 06/19] Revert "update simple_bag_player" This reverts commit 24f9565af84e3bc01509e485bba6c417b34b268d. Signed-off-by: Sangtaek Lee --- .../rosbag2_examples_cpp/CMakeLists.txt | 6 ++-- ...e_bag_player.cpp => simple_bag_reader.cpp} | 30 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) rename rosbag2_examples/rosbag2_examples_cpp/src/{simple_bag_player.cpp => simple_bag_reader.cpp} (63%) diff --git a/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt b/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt index de761a5af2..6c9dc2c532 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt +++ b/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt @@ -58,15 +58,15 @@ install(TARGETS DESTINATION lib/${PROJECT_NAME} ) -add_executable(simple_bag_player src/simple_bag_player.cpp) -target_link_libraries(simple_bag_player +add_executable(simple_bag_reader src/simple_bag_reader.cpp) +target_link_libraries(simple_bag_reader rclcpp::rclcpp rosbag2_cpp::rosbag2_cpp ${example_interfaces_TARGETS} ) install(TARGETS - simple_bag_player + simple_bag_reader DESTINATION lib/${PROJECT_NAME} ) diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp similarity index 63% rename from rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp rename to rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp index c5e7dcb763..77ae805a6d 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp @@ -6,7 +6,6 @@ #include "rclcpp/rclcpp.hpp" #include "rclcpp/serialization.hpp" -#include "rosbag2_storage/qos.hpp" #include "rosbag2_cpp/reader.hpp" #include "example_interfaces/msg/string.hpp" @@ -18,19 +17,13 @@ class PlaybackNode : public rclcpp::Node PlaybackNode(const std::string & bag_filename) : Node("playback_node") { - reader_.open(bag_filename); - - auto topics = reader_.get_all_topics_and_types(); - - for (const auto & topic : topics) { - if (topic.name == TOPIC_NAME) { - publisher_ = create_generic_publisher(topic.name, topic.type, rosbag2_storage::Rosbag2QoS{}); - } - } + publisher_ = this->create_publisher("chatter", 10); timer_ = this->create_wall_timer(100ms, [this](){return this->timer_callback();} ); + + reader_.open(bag_filename); } private: @@ -38,21 +31,28 @@ class PlaybackNode : public rclcpp::Node { while (reader_.has_next()) { rosbag2_storage::SerializedBagMessageSharedPtr msg = reader_.read_next(); - if (msg->topic_name != TOPIC_NAME) { + + if (msg->topic_name != "chatter") { continue; } - publisher_->publish(rclcpp::SerializedMessage(*msg->serialized_data)); + + rclcpp::SerializedMessage serialized_msg(*msg->serialized_data); + example_interfaces::msg::String::SharedPtr ros_msg = std::make_shared(); + + serialization_.deserialize_message(&serialized_msg, ros_msg.get()); + + publisher_->publish(*ros_msg); + std::cout << ros_msg->data << "\n"; break; } } rclcpp::TimerBase::SharedPtr timer_; - std::shared_ptr publisher_; + rclcpp::Publisher::SharedPtr publisher_; + rclcpp::Serialization serialization_; rosbag2_cpp::Reader reader_; - - const std::string TOPIC_NAME {"chatter"}; }; int main(int argc, char ** argv) From 21876b53ee63260af36fc267514bb0b60adce16d Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Tue, 5 Mar 2024 07:15:45 +0000 Subject: [PATCH 07/19] rename reader Signed-off-by: Sangtaek Lee --- rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt | 6 +++--- .../src/{simple_bag_reader.cpp => simple_bag_player.cpp} | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) rename rosbag2_examples/rosbag2_examples_cpp/src/{simple_bag_reader.cpp => simple_bag_player.cpp} (91%) diff --git a/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt b/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt index 6c9dc2c532..de761a5af2 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt +++ b/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt @@ -58,15 +58,15 @@ install(TARGETS DESTINATION lib/${PROJECT_NAME} ) -add_executable(simple_bag_reader src/simple_bag_reader.cpp) -target_link_libraries(simple_bag_reader +add_executable(simple_bag_player src/simple_bag_player.cpp) +target_link_libraries(simple_bag_player rclcpp::rclcpp rosbag2_cpp::rosbag2_cpp ${example_interfaces_TARGETS} ) install(TARGETS - simple_bag_reader + simple_bag_player DESTINATION lib/${PROJECT_NAME} ) diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp similarity index 91% rename from rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp rename to rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp index 77ae805a6d..21b19d54f5 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_reader.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp @@ -11,10 +11,10 @@ using namespace std::chrono_literals; -class PlaybackNode : public rclcpp::Node +class SimpleBagPlayer : public rclcpp::Node { public: - PlaybackNode(const std::string & bag_filename) + SimpleBagPlayer(const std::string & bag_filename) : Node("playback_node") { publisher_ = this->create_publisher("chatter", 10); @@ -63,7 +63,7 @@ int main(int argc, char ** argv) } rclcpp::init(argc, argv); - rclcpp::spin(std::make_shared(argv[1])); + rclcpp::spin(std::make_shared(argv[1])); rclcpp::shutdown(); return 0; From 67fe8fb37bb04820b44e99ce99dfe6c394bfa32d Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Tue, 5 Mar 2024 07:49:58 +0000 Subject: [PATCH 08/19] add pytho simple bag player Signed-off-by: Sangtaek Lee --- .../rosbag2_examples_py/simple_bag_player.py | 55 +++++++++++++++++++ rosbag2_examples/rosbag2_examples_py/setup.py | 1 + 2 files changed, 56 insertions(+) create mode 100644 rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py diff --git a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py new file mode 100644 index 0000000000..75e8bdf37d --- /dev/null +++ b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py @@ -0,0 +1,55 @@ +# 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 sys +import rclpy +from rclpy.node import Node +from rclpy.serialization import deserialize_message +import rosbag2_py +from std_msgs.msg import String + + +class SimpleBagPlayer(Node): + + def __init__(self, bag_filename): + super().__init__('simple_bag_player') + self.reader = rosbag2_py.SequentialReader() + storage_options = rosbag2_py._storage.StorageOptions( + uri='my_bag', + storage_id='sqlite3') + converter_options = rosbag2_py._storage.ConverterOptions('', '') + self.reader.open(storage_options, converter_options) + + self.publisher = self.create_publisher(String, 'chatter', 10) + self.timer = self.create_timer(0.1, self.timer_callback) + + def timer_callback(self): + while self.reader.has_next(): + msg = self.reader.read_next() + if msg[0] != 'chatter': + continue + ros_msg = deserialize_message(msg[1], String) + self.publisher.publish(ros_msg) + self.get_logger().info(ros_msg.data) + + + +def main(args=None): + rclpy.init(args=args) + sbr = SimpleBagPlayer(sys.argv[1]) + rclpy.spin(sbr) + rclpy.shutdown() + + +if __name__ == '__main__': + main() diff --git a/rosbag2_examples/rosbag2_examples_py/setup.py b/rosbag2_examples/rosbag2_examples_py/setup.py index 1fa813cd71..16dfe1c72c 100644 --- a/rosbag2_examples/rosbag2_examples_py/setup.py +++ b/rosbag2_examples/rosbag2_examples_py/setup.py @@ -21,6 +21,7 @@ entry_points={ 'console_scripts': [ 'simple_bag_recorder = rosbag2_examples_py.simple_bag_recorder:main', + 'simple_bag_player = rosbag2_examples_py.simple_bag_player:main', 'data_generator_node = rosbag2_examples_py.data_generator_node:main', 'data_generator_executable = rosbag2_examples_py.data_generator_executable:main', ], From cbac86202fa9fecd93c043522044ecf6de00592a Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Tue, 5 Mar 2024 07:51:16 +0000 Subject: [PATCH 09/19] get filename from arguments Signed-off-by: Sangtaek Lee --- .../rosbag2_examples_py/simple_bag_player.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py index 75e8bdf37d..da897e1d69 100644 --- a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py +++ b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py @@ -25,7 +25,7 @@ def __init__(self, bag_filename): super().__init__('simple_bag_player') self.reader = rosbag2_py.SequentialReader() storage_options = rosbag2_py._storage.StorageOptions( - uri='my_bag', + uri=bag_filename, storage_id='sqlite3') converter_options = rosbag2_py._storage.ConverterOptions('', '') self.reader.open(storage_options, converter_options) From 76f6b4b346818ae25916333acb011baf65f80a7f Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Wed, 6 Mar 2024 06:14:43 +0000 Subject: [PATCH 10/19] change msg type to std_msgs Signed-off-by: Sangtaek Lee --- rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt | 5 +++-- rosbag2_examples/rosbag2_examples_cpp/package.xml | 1 + .../rosbag2_examples_cpp/src/simple_bag_player.cpp | 10 +++++----- .../rosbag2_examples_cpp/src/simple_bag_recorder.cpp | 8 ++++---- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt b/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt index de761a5af2..24cf155337 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt +++ b/rosbag2_examples/rosbag2_examples_cpp/CMakeLists.txt @@ -21,12 +21,13 @@ find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(rosbag2_cpp REQUIRED) find_package(example_interfaces REQUIRED) +find_package(std_msgs REQUIRED) add_executable(simple_bag_recorder src/simple_bag_recorder.cpp) target_link_libraries(simple_bag_recorder rclcpp::rclcpp rosbag2_cpp::rosbag2_cpp - ${example_interfaces_TARGETS} + ${std_msgs_TARGETS} ) install(TARGETS @@ -62,7 +63,7 @@ add_executable(simple_bag_player src/simple_bag_player.cpp) target_link_libraries(simple_bag_player rclcpp::rclcpp rosbag2_cpp::rosbag2_cpp - ${example_interfaces_TARGETS} + ${std_msgs_TARGETS} ) install(TARGETS diff --git a/rosbag2_examples/rosbag2_examples_cpp/package.xml b/rosbag2_examples/rosbag2_examples_cpp/package.xml index 81b204f613..1db0095690 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/package.xml +++ b/rosbag2_examples/rosbag2_examples_cpp/package.xml @@ -14,6 +14,7 @@ rclcpp rosbag2_cpp example_interfaces + std_msgs ament_lint_auto ament_lint_common diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp index 21b19d54f5..124c005853 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp @@ -7,7 +7,7 @@ #include "rclcpp/rclcpp.hpp" #include "rclcpp/serialization.hpp" #include "rosbag2_cpp/reader.hpp" -#include "example_interfaces/msg/string.hpp" +#include "std_msgs/msg/string.hpp" using namespace std::chrono_literals; @@ -17,7 +17,7 @@ class SimpleBagPlayer : public rclcpp::Node SimpleBagPlayer(const std::string & bag_filename) : Node("playback_node") { - publisher_ = this->create_publisher("chatter", 10); + publisher_ = this->create_publisher("chatter", 10); timer_ = this->create_wall_timer(100ms, [this](){return this->timer_callback();} @@ -37,7 +37,7 @@ class SimpleBagPlayer : public rclcpp::Node } rclcpp::SerializedMessage serialized_msg(*msg->serialized_data); - example_interfaces::msg::String::SharedPtr ros_msg = std::make_shared(); + std_msgs::msg::String::SharedPtr ros_msg = std::make_shared(); serialization_.deserialize_message(&serialized_msg, ros_msg.get()); @@ -49,9 +49,9 @@ class SimpleBagPlayer : public rclcpp::Node } rclcpp::TimerBase::SharedPtr timer_; - rclcpp::Publisher::SharedPtr publisher_; + rclcpp::Publisher::SharedPtr publisher_; - rclcpp::Serialization serialization_; + rclcpp::Serialization serialization_; rosbag2_cpp::Reader reader_; }; 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 b6535d40db..b2910ab0c4 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp @@ -14,7 +14,7 @@ #include -#include "example_interfaces/msg/string.hpp" +#include "std_msgs/msg/string.hpp" #include "rclcpp/rclcpp.hpp" #include "rosbag2_cpp/writer.hpp" @@ -31,7 +31,7 @@ class SimpleBagRecorder : public rclcpp::Node writer_->open("my_bag"); - subscription_ = create_subscription( + subscription_ = create_subscription( "chatter", 10, std::bind(&SimpleBagRecorder::topic_callback, this, _1)); } @@ -39,10 +39,10 @@ class SimpleBagRecorder : public rclcpp::Node void topic_callback(std::shared_ptr msg) const { rclcpp::Time time_stamp = this->now(); - writer_->write(msg, "chatter", "example_interfaces/msg/String", time_stamp); + writer_->write(msg, "chatter", "std_msgs/msg/String", time_stamp); } - rclcpp::Subscription::SharedPtr subscription_; + rclcpp::Subscription::SharedPtr subscription_; std::unique_ptr writer_; }; From 6b276b10407a7a5f047afeec6b898ceddb286e5f Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Wed, 6 Mar 2024 06:35:01 +0000 Subject: [PATCH 11/19] update style after running tests Signed-off-by: Sangtaek Lee --- .../src/simple_bag_player.cpp | 80 +++++++++++-------- .../rosbag2_examples_py/simple_bag_player.py | 2 +- 2 files changed, 48 insertions(+), 34 deletions(-) diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp index 124c005853..522059bfb6 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp @@ -1,5 +1,18 @@ +// Copyright 2024 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 #include #include #include @@ -13,46 +26,47 @@ using namespace std::chrono_literals; class SimpleBagPlayer : public rclcpp::Node { - public: - SimpleBagPlayer(const std::string & bag_filename) - : Node("playback_node") - { - publisher_ = this->create_publisher("chatter", 10); - - timer_ = this->create_wall_timer(100ms, - [this](){return this->timer_callback();} - ); - - reader_.open(bag_filename); - } +public: + explicit SimpleBagPlayer(const std::string & bag_filename) + : Node("playback_node") + { + publisher_ = this->create_publisher("chatter", 10); + + timer_ = this->create_wall_timer( + 100ms, + [this](){return this->timer_callback();} + ); + + reader_.open(bag_filename); + } - private: - void timer_callback() - { - while (reader_.has_next()) { - rosbag2_storage::SerializedBagMessageSharedPtr msg = reader_.read_next(); +private: + void timer_callback() + { + while (reader_.has_next()) { + rosbag2_storage::SerializedBagMessageSharedPtr msg = reader_.read_next(); - if (msg->topic_name != "chatter") { - continue; - } + if (msg->topic_name != "chatter") { + continue; + } - rclcpp::SerializedMessage serialized_msg(*msg->serialized_data); - std_msgs::msg::String::SharedPtr ros_msg = std::make_shared(); + rclcpp::SerializedMessage serialized_msg(*msg->serialized_data); + std_msgs::msg::String::SharedPtr ros_msg = std::make_shared(); - serialization_.deserialize_message(&serialized_msg, ros_msg.get()); + serialization_.deserialize_message(&serialized_msg, ros_msg.get()); - publisher_->publish(*ros_msg); - std::cout << ros_msg->data << "\n"; + publisher_->publish(*ros_msg); + std::cout << ros_msg->data << "\n"; - break; - } + break; } + } - rclcpp::TimerBase::SharedPtr timer_; - rclcpp::Publisher::SharedPtr publisher_; + rclcpp::TimerBase::SharedPtr timer_; + rclcpp::Publisher::SharedPtr publisher_; - rclcpp::Serialization serialization_; - rosbag2_cpp::Reader reader_; + rclcpp::Serialization serialization_; + rosbag2_cpp::Reader reader_; }; int main(int argc, char ** argv) @@ -67,4 +81,4 @@ int main(int argc, char ** argv) rclcpp::shutdown(); return 0; -} \ No newline at end of file +} diff --git a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py index da897e1d69..e09da9e333 100644 --- a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py +++ b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import sys + import rclpy from rclpy.node import Node from rclpy.serialization import deserialize_message @@ -43,7 +44,6 @@ def timer_callback(self): self.get_logger().info(ros_msg.data) - def main(args=None): rclpy.init(args=args) sbr = SimpleBagPlayer(sys.argv[1]) From c2e071649eaf6b516514d4ac88514867cf48fbb8 Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Wed, 6 Mar 2024 20:36:04 +0000 Subject: [PATCH 12/19] reformatting Signed-off-by: Sangtaek Lee --- rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp index 522059bfb6..0b06d52340 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp @@ -34,7 +34,7 @@ class SimpleBagPlayer : public rclcpp::Node timer_ = this->create_wall_timer( 100ms, - [this](){return this->timer_callback();} + [this]() {return this->timer_callback();} ); reader_.open(bag_filename); From 6cfd64514749386b78a46b1752aeec089345212a Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Sun, 10 Mar 2024 11:35:56 +0000 Subject: [PATCH 13/19] add comment, rename node Signed-off-by: Sangtaek Lee --- .../rosbag2_examples_cpp/src/simple_bag_player.cpp | 3 ++- .../rosbag2_examples_py/simple_bag_player.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp index 0b06d52340..0959f1bb42 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp @@ -28,10 +28,11 @@ class SimpleBagPlayer : public rclcpp::Node { public: explicit SimpleBagPlayer(const std::string & bag_filename) - : Node("playback_node") + : Node("simple_bag_player") { publisher_ = this->create_publisher("chatter", 10); + // ignore timestamp and publish at a fixed rate (10 Hz). timer_ = this->create_wall_timer( 100ms, [this]() {return this->timer_callback();} diff --git a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py index e09da9e333..46a9ba80c0 100644 --- a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py +++ b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py @@ -1,4 +1,4 @@ -# Copyright 2023 Open Source Robotics Foundation, Inc. +# Copyright 2024 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. @@ -32,6 +32,7 @@ def __init__(self, bag_filename): self.reader.open(storage_options, converter_options) self.publisher = self.create_publisher(String, 'chatter', 10) + # ignore timestamp and publish at a fixed rate (10 Hz). self.timer = self.create_timer(0.1, self.timer_callback) def timer_callback(self): From 1f53c6afb5dc911719fb1b7dc29728e073751fe3 Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Sat, 16 Mar 2024 16:53:53 +0000 Subject: [PATCH 14/19] divide examples into two cases: with and without deserialisation Signed-off-by: Sangtaek Lee --- .../src/simple_bag_player.cpp | 23 +++++++++++++------ .../rosbag2_examples_py/simple_bag_player.py | 12 +++++++--- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp index 0959f1bb42..6cd00abf52 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp @@ -51,13 +51,20 @@ class SimpleBagPlayer : public rclcpp::Node continue; } - rclcpp::SerializedMessage serialized_msg(*msg->serialized_data); - std_msgs::msg::String::SharedPtr ros_msg = std::make_shared(); - - serialization_.deserialize_message(&serialized_msg, ros_msg.get()); - - publisher_->publish(*ros_msg); - std::cout << ros_msg->data << "\n"; + if (message_needs_to_be_edit_before_send_) + { + rclcpp::SerializedMessage serialized_msg(*msg->serialized_data); + std_msgs::msg::String::SharedPtr ros_msg = std::make_shared(); + + serialization_.deserialize_message(&serialized_msg, ros_msg.get()); + ros_msg->data += "[edited]"; + publisher_->publish(*ros_msg); + std::cout << ros_msg->data << "\n"; + } + else + { + publisher_->publish(*msg->serialized_data); + } break; } @@ -68,6 +75,8 @@ class SimpleBagPlayer : public rclcpp::Node rclcpp::Serialization serialization_; rosbag2_cpp::Reader reader_; + + bool message_needs_to_be_edit_before_send_ {true}; }; int main(int argc, char ** argv) diff --git a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py index 46a9ba80c0..d4bac76bdc 100644 --- a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py +++ b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py @@ -34,15 +34,21 @@ def __init__(self, bag_filename): self.publisher = self.create_publisher(String, 'chatter', 10) # ignore timestamp and publish at a fixed rate (10 Hz). self.timer = self.create_timer(0.1, self.timer_callback) + self.message_needs_to_be_edit_before_send_ = True def timer_callback(self): while self.reader.has_next(): msg = self.reader.read_next() if msg[0] != 'chatter': continue - ros_msg = deserialize_message(msg[1], String) - self.publisher.publish(ros_msg) - self.get_logger().info(ros_msg.data) + + if self.message_needs_to_be_edit_before_send_: + ros_msg = deserialize_message(msg[1], String) + ros_msg.data += '[edited]' + self.publisher.publish(ros_msg) + self.get_logger().info(ros_msg.data) + else: + self.publisher.publish(msg[1]) def main(args=None): From 77eb62ad45602271d37abfc29edc20f40cbcf691 Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Wed, 20 Mar 2024 20:48:16 +0000 Subject: [PATCH 15/19] fix test failure Signed-off-by: Sangtaek Lee --- .../rosbag2_examples_cpp/src/simple_bag_player.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp index 6cd00abf52..13d84c9b38 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp @@ -51,8 +51,7 @@ class SimpleBagPlayer : public rclcpp::Node continue; } - if (message_needs_to_be_edit_before_send_) - { + if (message_needs_to_be_edit_before_send_) { rclcpp::SerializedMessage serialized_msg(*msg->serialized_data); std_msgs::msg::String::SharedPtr ros_msg = std::make_shared(); @@ -60,9 +59,7 @@ class SimpleBagPlayer : public rclcpp::Node ros_msg->data += "[edited]"; publisher_->publish(*ros_msg); std::cout << ros_msg->data << "\n"; - } - else - { + } else { publisher_->publish(*msg->serialized_data); } From 2ea728208cd8c42b3a80f33343c647eff61d2ed0 Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Fri, 22 Mar 2024 19:26:45 +0000 Subject: [PATCH 16/19] divide recoder example into two cases: with and without serialisation Signed-off-by: Sangtaek Lee --- .../src/simple_bag_recorder.cpp | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) 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 b2910ab0c4..f691d8a5ad 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp @@ -31,8 +31,13 @@ class SimpleBagRecorder : public rclcpp::Node writer_->open("my_bag"); - subscription_ = create_subscription( - "chatter", 10, std::bind(&SimpleBagRecorder::topic_callback, this, _1)); + if (message_needs_to_be_edit_before_write_) { + subscription_ = create_subscription( + "chatter", 10, std::bind(&SimpleBagRecorder::topic_edit_callback, this, _1)); + } else { + subscription_ = create_subscription( + "chatter", 10, std::bind(&SimpleBagRecorder::topic_callback, this, _1)); + } } private: @@ -42,8 +47,21 @@ class SimpleBagRecorder : public rclcpp::Node writer_->write(msg, "chatter", "std_msgs/msg/String", time_stamp); } + void topic_edit_callback(std_msgs::msg::String::UniquePtr msg) const + { + rclcpp::Time time_stamp = this->now(); + msg->data += "[edited]"; + rclcpp::SerializedMessage serialized_msg; + serialization_.serialize_message(msg.get(), &serialized_msg); + writer_->write(serialized_msg, "chatter", "std_msgs/msg/String", time_stamp); + } + + rclcpp::Subscription::SharedPtr subscription_; + rclcpp::Serialization serialization_; std::unique_ptr writer_; + + bool message_needs_to_be_edit_before_write_ {true}; }; int main(int argc, char * argv[]) From 0690d6abf28a57c50e78b28e010dc36a0a518894 Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Sun, 24 Mar 2024 09:22:31 +0000 Subject: [PATCH 17/19] Fix compile warning Signed-off-by: Sangtaek Lee --- .../rosbag2_examples_cpp/src/simple_bag_recorder.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 f691d8a5ad..2ccacbdba6 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp @@ -51,8 +51,8 @@ class SimpleBagRecorder : public rclcpp::Node { rclcpp::Time time_stamp = this->now(); msg->data += "[edited]"; - rclcpp::SerializedMessage serialized_msg; - serialization_.serialize_message(msg.get(), &serialized_msg); + auto serialized_msg = std::make_shared(); + serialization_.serialize_message(msg.get(), serialized_msg.get()); writer_->write(serialized_msg, "chatter", "std_msgs/msg/String", time_stamp); } From e172f3545b6638c597765dddf114db62a8585020 Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Mon, 1 Apr 2024 19:58:12 +0000 Subject: [PATCH 18/19] Add a parameter Signed-off-by: Sangtaek Lee --- .../rosbag2_examples_cpp/src/simple_bag_player.cpp | 9 ++++++--- .../rosbag2_examples_cpp/src/simple_bag_recorder.cpp | 5 ++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp index 13d84c9b38..704a71fa84 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_player.cpp @@ -30,12 +30,15 @@ class SimpleBagPlayer : public rclcpp::Node explicit SimpleBagPlayer(const std::string & bag_filename) : Node("simple_bag_player") { + declare_parameter("edit", false); + message_needs_to_be_edit_before_send_ = get_parameter("edit").as_bool(); + publisher_ = this->create_publisher("chatter", 10); // ignore timestamp and publish at a fixed rate (10 Hz). timer_ = this->create_wall_timer( 100ms, - [this]() {return this->timer_callback();} + [this]() {this->timer_callback();} ); reader_.open(bag_filename); @@ -73,12 +76,12 @@ class SimpleBagPlayer : public rclcpp::Node rclcpp::Serialization serialization_; rosbag2_cpp::Reader reader_; - bool message_needs_to_be_edit_before_send_ {true}; + bool message_needs_to_be_edit_before_send_; }; int main(int argc, char ** argv) { - if (argc != 2) { + if (argc < 2) { std::cerr << "Usage: " << argv[0] << " " << std::endl; return 1; } 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 2ccacbdba6..2463df9734 100644 --- a/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp +++ b/rosbag2_examples/rosbag2_examples_cpp/src/simple_bag_recorder.cpp @@ -27,6 +27,9 @@ class SimpleBagRecorder : public rclcpp::Node SimpleBagRecorder() : Node("simple_bag_recorder") { + declare_parameter("edit", false); + message_needs_to_be_edit_before_write_ = get_parameter("edit").as_bool(); + writer_ = std::make_unique(); writer_->open("my_bag"); @@ -61,7 +64,7 @@ class SimpleBagRecorder : public rclcpp::Node rclcpp::Serialization serialization_; std::unique_ptr writer_; - bool message_needs_to_be_edit_before_write_ {true}; + bool message_needs_to_be_edit_before_write_; }; int main(int argc, char * argv[]) From 828237141de42b757037512167cd25c5d2ba108d Mon Sep 17 00:00:00 2001 From: Sangtaek Lee Date: Mon, 1 Apr 2024 20:02:36 +0000 Subject: [PATCH 19/19] add a parameter in python examples Signed-off-by: Sangtaek Lee --- .../rosbag2_examples_py/simple_bag_player.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py index d4bac76bdc..09b8146ed7 100644 --- a/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py +++ b/rosbag2_examples/rosbag2_examples_py/rosbag2_examples_py/simple_bag_player.py @@ -34,7 +34,8 @@ def __init__(self, bag_filename): self.publisher = self.create_publisher(String, 'chatter', 10) # ignore timestamp and publish at a fixed rate (10 Hz). self.timer = self.create_timer(0.1, self.timer_callback) - self.message_needs_to_be_edit_before_send_ = True + self.declare_parameter("edit", False) + self.message_needs_to_be_edit_before_send_ = self.get_parameter("edit").get_parameter_value().bool_value def timer_callback(self): while self.reader.has_next():