How to Record and Play Back Data in ROS 2 Foxy Fitzroy

In this post, we will learn how to record and play back data that is published on topics in your ROS 2 application.

The official tutorial is located in the ROS 2 Foxy documentation, but we’ll run through the entire process step-by-step below.

You Will Need

In order to complete this tutorial, you will need:

Prerequisites

It is helpful if you’ve completed this tutorial on the Turtlesim package in ROS 2, but it is not required.

Setup

Open a new terminal window, and type the following command to launch the turtlesim simulation:

ros2 run turtlesim turtlesim_node

Open another terminal window, and launch the keyboard executable, which will enable us to navigate the robot using the keyboard.

ros2 run turtlesim turtle_teleop_key

Let’s create a new directory to save the data that is published to topics. ROS 2 calls these recordings “bag files”. In another terminal window, type the following commands:

mkdir bag_files
cd bag_files

Select a Topic You Want to Record

Let’s see what the currently active topics are.

ros2 topic list
1-list-ros2-topicsJPG

Type the following command to see the relationship between active nodes and topics.

rqt_graph
2-rqt-graphJPG

You can see that the /teleop_turtle node (which is the keyboard-related node) publishes velocity commands to the /turtle1/cmd_vel topic. The /turtlesim node subscribes to these velocity commands, and the turtle moves around the screen accordingly.

Press CTRL + C in the terminal window to close rqt_graph.

Let’s explore the data that is being published to the /turtle1/cmd_vel topic. To do this, we need to use the ros2 topic echo command.

ros2 topic echo /turtle1/cmd_vel

Select the terminal window where you launched the turtle_teleop_key node.

3-turtle-teleop-key-nodeJPG

Move the turtle around using the keyboard.

Return to the terminal window where you launched the ros2 topic echo /turtle1/cmd_vel command.

You should see a printout of the velocity commands.

4-print-out-of-velocity-commandsJPG

Record Data

Let’s record the data that is published to the /turtle1/cmd_vel topic. Open a new terminal window, and navigate to the bag_files directory you created earlier in this tutorial.

cd bag_files
ros2 bag record /turtle1/cmd_vel

The data that is published to /turtle1/cmd_vel is now being recorded.

5-record-bag-dataJPG

Go back to the teleop terminal window, and move the turtle around.

6-move-turtlesim-aroundJPG

Go back to the terminal where you ran the ros2 bag command, and press CTRL + C to stop recording.

Let’s see if the bag file is recorded properly. Inside the bag_files directory on your computer, type the following command to see what files are inside.

dir
7-bag-file-recorded-properlyJPG

The format for the bag file is:

rosbag2_year_month_day-hour_minute_second

To see detailed information about this bag file type:

ros2 bag info <name_of_bag_file>

8-ros2-bag-infoJPG

Play Back Data

Let’s play back the data that was recorded in the bag file.

Return to the teleop window.

Press CTRL + C to stop keyboard control.

Navigate to the bag_files directory, and play back the bag file you recorded earlier. Make sure you can see the turtlesim window.

ros2 bag play <name_of_bag_file>
9-ros-playbackJPG

You will see the turtle replicate the motion you commanded earlier. 

That’s it for bag files. Keep building!

How to Create a Launch File in ROS 2 Foxy Fitzroy

In this post, we will learn how to create a launch file in ROS 2. A launch file enables you to launch multiple ROS 2 nodes at the same time. These files are created in Python and are run using the ros2 launch command (we’ll cover this later in this tutorial).

The official tutorial is located in the ROS 2 Foxy documentation, but we’ll run through the entire process step-by-step below.

You Will Need

In order to complete this tutorial, you will need:

Prerequisites

It is helpful if you’ve completed this tutorial on the Turtlesim package in ROS 2, but it is not required.

Setup

Open a new terminal window, and type the following command to create a new folder:

mkdir launch

If you don’t have gedit installed, install it now.

sudo apt-get install gedit

Open up a new launch file.

gedit launch/turtlesim_mimic_launch.py

Write the following code inside the launch file.

# This is an example of a launch file for ROS 2

# Import launch modules that are Python-compatible
from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
  """
  This file is the launch description. It will launch three nodes:
  turtlesim1: A turtle
  turtlesim2: Another turtle
  mimic: Causes one turtle to mimic the movements of the other turtle
  """
  return LaunchDescription([
  
    # Launches a window with a turtle in it
    Node(
      package='turtlesim',
      namespace='turtlesim1',
      executable='turtlesim_node',
      name='sim'
    ),
    # Launches another window with a turtle in it
    Node(
      package='turtlesim',
      namespace='turtlesim2',
      executable='turtlesim_node',
      name='sim'
    ),
    # The mimic executable contains code that causes turtlesim2 to mimic
    # turtlesim1
    Node(
      package='turtlesim',
      executable='mimic',
      name='mimic',
      remappings=[
        ('/input/pose', '/turtlesim1/turtle1/pose'),
        ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
      ]
    )
  ])

Click Save and close the file to return to the terminal.

Launch the Launch File

To launch the launch file, move to the launch file:

cd launch

Type:

ros2 launch turtlesim_mimic_launch.py
1_terminal_outputJPG

In a new terminal tab, publish a velocity command to force turtlesim1 to move at a forward velocity of 2.0 meters per second and an angular velocity of -1.8 radians per second. All of this stuff below is a single command.

ros2 topic pub -r 1 /turtlesim1/turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: -1.8}}"

You will see the turtles move around in unison in a circle.

2_turtles_mimicJPG

To see the relationship between nodes, open a new terminal window, and type:

rqt_graph
3-node-relationshipJPG

The hidden node n___ros2cli_4394 represents the command you entered in the command line interface (i.e. the terminal window). This node publishes the velocity command to a topic named /turtlesim1/turtle1/cmd_vel.

The /turtlesim1/sim node subscribes to velocity commands that are published on the /turtlesim1/turtle1/cmd_vel topic.

The /turtlesim1/sim node publishes to /turtlesim1/sim’s pose topic.

/mimic is subscribed to /turtlesim1/sim’s pose topic.

/mimic publishes to /turtlesim2/sim’s velocity command topic, which causes the second turtle to move like the first turtle.

When you’re done, press CTRL+C to shut down everything.

Mastering Turtlesim in ROS 2 Foxy Fitzroy

In this post, we will learn the basics of ROS 2 by working with Turtlesim, a ROS 2-based simulator. Follow each step below, one line at a time. Take your time, so you do everything correctly. There is no hurry.

The official tutorial is located in the ROS 2 Foxy documentation, but we’ll run through the entire process step-by-step below.

You Will Need

In order to complete this tutorial, you will need:

Prerequisites

It is helpful if you’ve completed this tutorial on Linux fundamentals, but it is not required.

Install Turtlesim

Install Turtlesim, by typing the following commands:

sudo apt update
sudo apt install ros-foxy-turtlesim

Check that Turtlesim was installed.

ros2 pkg executables turtlesim

Here is what you should see:

1_turtlesim_installedJPG

Launch Turtlesim

To launch Turtlesim, type:

ros2 run turtlesim turtlesim_node

Here is the window that you should see:

2_turtlesim_launch_windowJPG

In the terminal, you will see the position and orientation of the turtle.

3-turtle-poseJPG

Let’s move the turtle around the blue screen.

ros2 run turtlesim turtle_teleop_key

With this same terminal window selected, use the arrow keys to navigate the turtle around the screen.

To close turtlesim, go to all terminal windows and type:

CTRL + C

Common ROS 2 Commands

Open a new terminal. Let’s type some common ROS 2 commands.

To list the active ROS nodes, type the following command. A node in ROS is just a program (e.g. typically a piece of source code made in C++ or Python) that does some computation. 

ros2 node list
4-ros-2-node-listJPG

Robots require a number of programs to work together to achieve a task. You can think of a node as a small single-purpose program within a larger robotic system. 

One way nodes communicate with each other is by using messages. These messages are passed via channels called topics.

To see a list of active topics in ROS 2, type the following command:

ros2 topic list
5-ros2-topic-listJPG

Don’t worry what all those topics mean. In a later post, I’ll dive deeper into ROS topics.

Let’s see what the active services are. A ROS Service consists of a pair of messages: one for the request and one for the reply. A service-providing ROS node (i.e. Service Server) offers a service (e.g. read sensor data). 

ros2 service list
6-ros2-service-listJPG

Let’s see what actions are active now. I explain the difference between a ROS Action and a ROS Service in this post.

ros2 action list
7-ros-action-listJPG

Here is one more command you should know:

  • ros2 node info <node_name> : Shows information about the node named node_name.

Install rqt

Let’s install rqt. rqt is a tool that enables you to see the connections between nodes.

sudo apt update
sudo apt install ~nros-foxy-rqt*

Press Y and Enter to complete the installation.

Run rqt

Launch turtlesim again.

ros2 run turtlesim turtlesim_node

In another terminal tab, type:

ros2 run turtlesim turtle_teleop_key

To run rqt, open a terminal window and type:

rqt

If this is your first time running rqt, go to Plugins > Services > Service Caller in the menu bar at the top.

Here is what my window looks like:

8-service-dropdownJPG

Click the refresh button to the left of the Service dropdown menu. This button looks like this:

9-service-refreshJPG

Here is what my window looks like now:

10-service-refreshJPG

Click the dropdown list in the middle of the window, and select the /spawn service.

11-select-spawn-serviceJPG

Launch Another Turtle Using the Spawn Service

The /spawn service spawns another turtle. Let’s use this service now.

Set the values under the Request area to the following by double-clicking the values under the ‘Expression’ menu.

In this case, we want to launch a new turtle at coordinate x=1.0, y=1.0. The name of this new turtle will be turtle2.

Call this service by clicking the Call button in the upper right of the panel.

13-call-buttonJPG

You should see a new turtle spawned at coordinate x=1.0, y=1.0.

14-new-turtle-is-spawnedJPG

Click the Refresh button.

You now have new services for turtle2.

15-click-refreshJPG

Using the Set Pen Service

Let’s change the color of the pen for the first turtle, turtle1.

Go to the Service dropdown list, and scroll down to /turtle1/set_pen.

16-width-of-penJPG

We want turtle1’s pen to be green, so we set the g (Green) color value to 255. I also want the width of the pen to be 5.

Click Call to call the service.

If you go to the terminal and select the terminal where you typed the “ros2 run turtlesim turtle_teleop_key” command, you can move turtle1 around the screen with your keyboard. You will see a green pen color.

17-green-lineJPG

Move turtle2

Let’s move turtle2. To do that, we need to perform remapping. We need to remap the velocity command for turtle1 to turtle2. Open a new terminal tab, and type (this is all a single command):

ros2 run turtlesim turtle_teleop_key --ros-args --remap turtle1/cmd_vel:=turtle2/cmd_vel

You can use your keyboard to move turtle2 around the screen.

18-move-turtle2JPG

To close turtlesim, go to all terminals and type:

CTRL + C