-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add simple bag player examples #1580
base: rolling
Are you sure you want to change the base?
Changes from all commits
b6b62a9
2ba6546
b2c9893
ed349be
2a2b97b
7f36044
21876b5
67fe8fb
cbac862
76f6b4b
6b276b1
c2e0716
6cfd645
1f53c6a
77eb62a
2ea7282
0690d6a
e172f35
8282371
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// 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 <chrono> | ||
#include <iostream> | ||
#include <memory> | ||
#include <string> | ||
|
||
#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 SimpleBagPlayer : public rclcpp::Node | ||
{ | ||
public: | ||
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<std_msgs::msg::String>("chatter", 10); | ||
|
||
// ignore timestamp and publish at a fixed rate (10 Hz). | ||
timer_ = this->create_wall_timer( | ||
100ms, | ||
[this]() {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 != "chatter") { | ||
continue; | ||
} | ||
|
||
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<std_msgs::msg::String>(); | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sangteak601 As far as I recall we need another type of publisher aka |
||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
rclcpp::TimerBase::SharedPtr timer_; | ||
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_; | ||
|
||
rclcpp::Serialization<std_msgs::msg::String> serialization_; | ||
rosbag2_cpp::Reader reader_; | ||
|
||
bool message_needs_to_be_edit_before_send_; | ||
}; | ||
|
||
int main(int argc, char ** argv) | ||
{ | ||
if (argc < 2) { | ||
std::cerr << "Usage: " << argv[0] << " <bag>" << std::endl; | ||
return 1; | ||
} | ||
|
||
rclcpp::init(argc, argv); | ||
rclcpp::spin(std::make_shared<SimpleBagPlayer>(argv[1])); | ||
rclcpp::shutdown(); | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# 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. | ||
# 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=bag_filename, | ||
storage_id='sqlite3') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if i am not mistaken, this is intentionally enabled as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was set as Maybe we can delete There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, i think that is okay with current code. this also includes an example how to specify the storage id, i think that is fine. |
||
converter_options = rosbag2_py._storage.ConverterOptions('', '') | ||
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) | ||
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(): | ||
msg = self.reader.read_next() | ||
if msg[0] != 'chatter': | ||
continue | ||
|
||
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): | ||
rclpy.init(args=args) | ||
sbr = SimpleBagPlayer(sys.argv[1]) | ||
rclpy.spin(sbr) | ||
rclpy.shutdown() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment for the documentation. this needs to be described how to enable with command line parameter argument. so that user can understand we can publish either publishing serialized data as it is or editing data before publishing in the example.