Ask Your Question
0

rosbag2 - how to save bags in a place of my choice?

asked 2022-03-29 07:21:45 -0500

ljaniec gravatar image

updated 2022-03-29 12:08:41 -0500

Hello,

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.

2) 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... for this?

3) 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(
            uri='my_bag',
            storage_id='sqlite3')
        converter_options = rosbag2_py._storage.ConverterOptions('', '')
        self.writer.open(storage_options, converter_options)

        topic_info = rosbag2_py._storage.TopicMetadata(
            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):
    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?

edit retag flag offensive close merge delete

Comments

I think maybe some answer from this question could help: https://answers.ros.org/question/3637... but I will look into it later

ljaniec gravatar image ljaniec  ( 2022-03-29 12:09:49 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-03-31 11:06:17 -0500

ljaniec gravatar image

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/.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2022-03-29 07:21:45 -0500

Seen: 82 times

Last updated: Mar 31