How to Replay ros2 bags with changed Quality of Service

Written by Bayode Aderinola

11/08/2022

In this post, you will learn how to replay ros2 bags with changed Quality of Service (QoS) setting. You’ll discover how to set the QoS before recording the bag, and how to change the QoS when playing back the bag file.

Step 1: Get a Copy of the ROS package containing the code used in the post

Click here to copy the project. It would be copied to your cloud account at The Construct. That done, open the project using the Run button. This might take a few moments, please be patient.

Run rosject

PS: If you don’t have an account on the The Construct, you would need to create one. Once you create an account or log in, you will be able to follow the steps to read and write parameters in ros1 and ros2.

You might also want to try this on a local PC if you have ROS installed. In that case you need to read on and duplicate the source code of the package in your own local workspace. However, please note that we cannot support local PCs and you will have to fix any errors you run into on your own.

Step 2: Start a simulation and the Quality of Service publisher

Open Code Editor
Open a web shell

Open a web shell (1) and run the following commands to start a simulation:

export GAZEBO_RESOURCE_PATH=/home/user/ros2_ws/src/turtlebot/turtlebot3_simulations/turtlebot3_gazebo:${GAZEBO_RESOURCE_PATH}
export GAZEBO_MODEL_PATH=/home/user/ros2_ws/src/turtlebot/turtlebot3_simulations/turtlebot3_gazebo/models:${GAZEBO_MODEL_PATH}
export TURTLEBOT3_MODEL=waffle
ros2 launch turtlebot3_gazebo turtlebot3_world.launch.py

You should see a simulation like this. If the robot does not show up, shutdown using Ctrl+C and run the above command again.

TurtleBot3 simulation
TurtleBot3 Simulation

Now make the robot move by publishing to the /cmd_vel topic, in another web shell (2). You should see the robot move.

ros2 topic pub --once /cmd_vel geometry_msgs/msg/Twist "{linear: {x: 0.1, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.0}}"

Now start the QoS publisher in the same web shell (2) where you published to /cmd_vel. Note that we set the reliability to reliable. The QoS publisher publishes to the /robot_pose topic.

ros2 run qos_pose_publisher qos_pose_publisher -reliability reliable

Now let’s look at the reliability of the /robot_pose topic in a new web shell (3):

ros2 topic info /robot_pose --verbose

You should see something like this as part of the output:

QoS profile:
  Reliability: RELIABLE
  Durability: VOLATILE
  Lifespan: 9223372036854775807 nanoseconds
  Deadline: 9223372036854775807 nanoseconds
  Liveliness: AUTOMATIC
  Liveliness lease duration: 9223372036854775807 nanoseconds

Step 3: Record and play back a ros2 bag file of a topic

Now let’s record a ros2 bag file of a topic in web shell (3). We are using the /robot_pose topic.

ros2 bag record -o ros2bag_qos_as_published /robot_pose

Let it run for 5 seconds and then kill it, in web shell (3). You should have a new file created:

ros2bag_qos_as_published

Now let’s play the ros2 bag. But before that kill the qos_pose_publisher node in web shell (2) by pressing Ctrl+C. In the same web shell (2), run the following command:

ros2 bag play ros2bag_qos_as_published

Now let’s examine the QoS of the /robot_pose topic (now being published to from the ros2 bag), in web shell (3):

ros2 topic info /robot_pose --verbose

You should see something like we saw before:

QoS profile:
  Reliability: RELIABLE
  Durability: VOLATILE
  Lifespan: 9223372036854775807 nanoseconds
  Deadline: 9223372036854775807 nanoseconds
  Liveliness: AUTOMATIC
  Liveliness lease duration: 9223372036854775807 nanoseconds

Step 4: Change the Quality of Service profile for ros2 bag playback

Create a profile file for ros2 bag playback. In web shell (3):

touch override.yaml

Open override.yaml in the code editor and paste in the following content:

/robot_pose:
  history: keep_last
  depth: 10
  reliability: best_effort
  durability: volatile
  deadline:
    # unspecified/infinity
    sec: 0
    nsec: 0
  lifespan:
    # unspecified/infinity
    sec: 0
    nsec: 0
  liveliness: system_default
  liveliness_lease_duration:
    # unspecified/infinity
    sec: 0
    nsec: 0
  avoid_ros_namespace_conventions: false

Now go to web shell (2), stop the ros2 bag playback (if it’s still running), and run the following command instead:

ros2 bag play --qos-profile-overrides-path override.yaml ros2bag_qos_as_published 

Now, let’s see the quality of service of the /robot_pose topic. In web shell (3):

ros2 topic info /robot_pose --verbose

You should now see something similar to the following in the output:

QoS profile:
  Reliability: BEST_EFFORT
  Durability: VOLATILE
  Lifespan: 9223372036854775807 nanoseconds
  Deadline: 9223372036854775807 nanoseconds
  Liveliness: AUTOMATIC
  Liveliness lease duration: 9223372036854775807 nanoseconds

And that’s it! That’s how to replay ros2 bags with changed quality of service setting.

Step 5: Check your learning

  1. Do you understand how to set the QoS for the topic before recording a bag?
  2. Do you understand how to replay ros2 bags with changed quality of service setting?

If you didn’t get any of the points above, please go over the post again, more carefully this time.

(Extra) Step 6: Watch the video to understand how to create ros2 XML launch files

Here you go:

Feedback

Did you like this post? Do you have any questions about how to read and write parameters in ros1 and ros2? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know in the comments area and we will do a video or post about it.

Masterclass 2023 batch2 blog banner

Check Out These Related Posts

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This