Rosanswers logo


I would like to know how to use rosbag2 (CLI, launch file, SequentialWriter) with option to choose where rosbag2 will be saved.

  1. with CLI:

ros2 bag record /chatter -o ~/rosbags_folder

I can easily use the -o option there.

  1. with launch file like this:

    from launch import LaunchDescription from launch.actions import ExecuteProcess

    def generate_launch_description(): # recording all topics with ros2 bag record return LaunchDescription( [ ExecuteProcess( cmd=["ros2", "bag", "record", "-a"], output="screen", ) ] )

How should I change it so that's after the -a in cmd I can have -o with an argument given during launch, e.g. cmd = ["ros2", "bag", "record", "-a", "-o", MyAwesomePathSetThroughTerminal]? Or should I rather use YAML files with parameters like in this tutorial: https://roboticsbackend.com/ros2-yaml-params/ for this?

  1. With this code from ROS Documentation (Recording a bag from your own node (Python))

    import rclpy from rclpy.node import Node from rclpy.serialization import serialize_message from std_msgs.msg import String

    import rosbag2_py

    class SimpleBagRecorder(Node): def init(self): super().init('simple_bag_recorder') self.writer = rosbag2_py.SequentialWriter()

         storage_options = rosbag2_py._storage.StorageOptions(
         converter_options = rosbag2_py._storage.ConverterOptions('', '')
         self.writer.open(storage_options, converter_options)
         topic_info = rosbag2_py._storage.TopicMetadata(
         self.subscription = self.create_subscription(
     def topic_callback(self, msg):

    def main(args=None): rclpy.init(args=args) sbr = SimpleBagRecorder() rclpy.spin(sbr) rclpy.shutdown()

    if name == 'main': main()

Where can I change the default options inside SequentialWriter etc. so it change saving place from ./ to some_global_or_local_PATH/ (path set in the outside code)? I can workaround it with setting uri='my_bag' to uri='/some_PATH/my_bag', but it feels like cheating. Is there a better option?

Originally posted by ljaniec on ROS Answers with karma: 3064 on 2022-03-29

Post score: 1

Original comments

Comment by ljaniec on 2022-03-29:
I think maybe some answer from this question could help: https://answers.ros.org/question/363763/ros2-how-best-to-conditionally-include-a-prefix-in-a-launchpy-file/ but I will look into it later


1 Answer 1


Rosanswers logo

I have somewhat solved my problem with use of this hack:

ros2ws_dir = "/".join(get_package_share_directory('bag_recorder_pkg').split('/')[:-4]) 
# it gives e.g. /home/user/ros2_ws/

Then in the code of I give in __init__ of SimpleBagRecorder uri equal to timestamp + ros2ws_dir + "/rosbags/". It saves the bag in my ros2_wsin the folder rosbags/.

Originally posted by ljaniec with karma: 3064 on 2022-03-31

This answer was ACCEPTED on the original site

Post score: 0


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.