How to Create a Workspace | ROS 2 Foxy Fitzroy

In this post, we will learn how to create a workspace for your ROS 2 application. A ROS 2 workspace is a directory that contains ROS 2 packages. 

Software in ROS 2 is organized into packages. Each package might contain a mixture of code (e.g. ROS 2 nodes), data, libraries, images, documentation, etc. Every program you write in ROS 2 will need to be inside a package.

ROS 2 packages promote software reuse. The goal of a ROS 2 package is to be large enough to provide a specific useful functionality, but not so large and complicated that nobody wants to reuse it for their own project.

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 create a new workspace. You can call it any name, but we will call it “dev_ws”, which stands for “development workspace.” Inside the workspace, we will create a source (i.e. src) directory. This is where your packages will go.

mkdir -p ~/dev_ws/src

Navigate to the workspace.

cd ~/dev_ws/src

Now, I want to add pre-existing packages into this folder. We will add the ros_tutorials repository to our workspace. It contains the following packages: roscpp_tutorials, rospy_tutorials, ros_tutorials, and turtlesim.

git clone https://github.com/ros/ros_tutorials.git -b foxy-devel
1-clone-packageJPG

If you type the following command, you can see that the package is in the src folder inside the dev_ws workspace.

dir
2-type-dir

To see what packages are inside this repository, type:

ls ros_tutorials
3-ls-packages

Resolve Package Dependencies

Since we just added a bunch of packages inside our workspace, we need to make sure each package has the software dependencies it needs in order to run.

Type the following command:

cd ~/dev_ws/

Resolve the dependencies.

rosdep install -i --from-path src --rosdistro foxy -y
4-resolve-dependenciesJPG

We can see that all the dependencies our packages need are already installed.

Build Your Workspace

Let’s build the packages. Go to the root of the workspace.

cd ~/dev_ws/

Make sure colcon is installed. Colcon is a tool used to build software packages.

sudo apt update
sudo apt install python3-colcon-common-extensions

Press Y and Enter to complete the install.

Build the packages.

colcon build
5-colcon-build

Type the following command to see what new directories were created.

dir
6-new-directories-created

Source the Workspace

Finally, we need to add the packages in this workspace to the current environment.

If you don’t have gedit installed, be sure to install it before you run the command above.

sudo apt-get install gedit

Open a new terminal window, and open your .bashrc file:

gedit ~/.bashrc

Add this line of code to the very bottom of the .bashrc file.

source ~/dev_ws/install/setup.bash
7-source-workspace

Save the file, and close it.

That’s it! In the next tutorial, we’ll take a look at how to create a package.

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.