0
$\begingroup$

Rosanswers logo

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.

  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(
             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?


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

$\endgroup$

1 Answer 1

0
$\begingroup$

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

$\endgroup$

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.