getTransform from ROS2 bag

14 views (last 30 days)
Antoine Harlé
Antoine Harlé on 21 Jun 2022
I want to access Transform from a ROS2bag, the same way you can with the tf2_msgs/LookupTransform action.
I first tried to use getTransform but it only seems to work with ROS1Bags.
>> bag = ros2bag(rosbag_path);
Check for incorrect argument data type or missing argument in call to function 'getTransform'.
I can still access the tf2_msgs/TFMessage in the bag so I worked out a way to convert them to a ROS1bag and access the TF.
bag_tf = readMessages(select(bag,"Topic",["/tf", "/tf_static"]));
bag_writer = rosbagwriter('tf_ros1.bag');
tf2Msg = rosmessage('tf2_msgs/TFMessage',"DataFormat","struct");
for bag_idx = 1:length(bag_tf)
for tf_idx = 1:length(bag_msg.transforms) %Some message contain multiple TFs
tf = bag_msg.transforms(tf_idx);
tf2Msg.Transforms.Header.Stamp.Sec = uint32(tf.header.stamp.sec);
tf2Msg.Transforms.Header.Stamp.Nsec = uint32(tf.header.stamp.nanosec);
tf2Msg.Transforms.Header.FrameId = tf.header.frame_id;
tf2Msg.Transforms.ChildFrameId = tf.child_frame_id;
tf2Msg.Transforms.Transform.Translation.X = tf.transform.translation.x;
tf2Msg.Transforms.Transform.Translation.Y = tf.transform.translation.y;
tf2Msg.Transforms.Transform.Translation.Z = tf.transform.translation.z;
tf2Msg.Transforms.Transform.Rotation.X = tf.transform.rotation.x;
tf2Msg.Transforms.Transform.Rotation.Y = tf.transform.rotation.y;
tf2Msg.Transforms.Transform.Rotation.Z = tf.transform.rotation.z;
tf2Msg.Transforms.Transform.Rotation.W = tf.transform.rotation.w;
write(bag_writer, "/tf", tf2Msg.Transforms.Header.Stamp, tf2Msg);
clear bag_writer
reader = rosbagreader('tf_ros1.bag');
It does work but it's really unpractical. So i was wondering if I missed something to make getTransform directly work with a ROS2bag ?
Thank you in advance for any help !
Antoine H.

Answers (1)

Jagadeesh Konakalla
Jagadeesh Konakalla on 23 Jun 2022
Hi Antonie,
Unfortunately, we do not have easy workaround to provide you.
We are working on adding getTransofromations capability to ros2bag. This feature will be available in future releases of MATLAB.
Jagadeesh K.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by