rosbag2 - how to save bags in a place of my choice?
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?
I think maybe some answer from this question could help: https://answers.ros.org/question/3637... but I will look into it later