How to Create and Execute ROS Launch Files

One way to execute a program in ROS it to launch one node at a time. In fact, that is exactly what we did in my previous post. This is fine if we have just two nodes, but what do we do if we have multiple nodes? Launching each node one-by-one can get inefficient really quickly.

Fortunately, ROS has a tool called roslaunch that enables you to launch multiple nodes all at once. Let’s do that now.

Directions

The first thing we need to do is to open a new terminal window and go to the hello_world package (or whatever package you want to launch).

cd catkin_ws/src/hello_world

Create a folder called ‘launch’.

mkdir launch

Create a new launch file inside the launch directory you just made.

cd launch

Open up the text editor.

gedit

Type the code below into the file, and save it. I’m going to save mine as talker_listener.launch. Save this file to the catkin_ws/src/hello_world/launch folder. This file will run C++ executables.

<launch>
  <node name="listener_node" pkg="hello_world" type="listener" output="screen"/>
  <node name="talker_node" pkg="hello_world" type="talker" output="screen"/>
</launch>

Note in the code above, if we are running a Python script, you will put the name of the script between the double quotes after type=. For example, if we want the launch file to run code named example.py, that line of the launch file code would look like this:

<node name="listener_node" pkg="hello_world" type="example.py" output="screen"></node>

And, to make sure the ROS launch file can find the code, we have to make sure we change the permissions of the Python script before we execute the launch file. The code for doing this would be as follows:

chmod +x example.py

Open up a new terminal window, and type:

cd catkin_ws/src/hello_world/launch

Change the permissions of the launch file we just created. It will ask you for your password.

sudo chmod +x talker_listener.launch

Run the launch file. Note that roslaunch automatically starts up roscore, so you don’t need to type roscore.

roslaunch hello_world talker_listener.launch
21-run-launch-fileJPG

Press CTRL+C on your keyboard to stop the processes.

Now let’s create a launch file that launches the python nodes, talker.py and listener.py. I created these nodes in this tutorial. Open up the text editor.

gedit

Type the code below into the file, and save it as talker_listener_python.launch. Save this file to the catkin_ws/src/hello_world/launch folder. 

<launch>
  <node name="listener_node" pkg="hello_world" type="listener.py" output="screen"/>
  <node name="talker_node" pkg="hello_world" type="talker.py" output="screen"/>
</launch>
22-python-launch-fileJPG

Open up a new terminal window, and type:

cd catkin_ws/src/hello_world/launch

Change the permissions of the launch file we just created. It will ask you for your password.

sudo chmod +x talker_listener_python.launch

Run the launch file.

roslaunch hello_world talker_listener_python.launch
23-launch-python-executeJPG

Press CTRL+C on your keyboard to stop the processes.

There may be times when you try to run a launch file, and ROS isn’t able to locate a new package. To refresh the package list, you execute the following command.

rospack profile

That’s it! You’re all done.

How to Create and Execute Publisher and Subscriber Nodes in ROS

In this post, we will learn how to create and execute publisher and subscriber nodes in ROS using C++ and Python.

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. ROS nodes reside inside ROS packages. ROS packages reside inside the src folder of your catkin workspace (i.e. catkin_ws).

Table of Contents

Directions

How to Create a Publisher Node in ROS Using C++

If you don’t already have a package named “hello_world” set up, set that up now.

Once you have your package set up, you are ready to create a node. Let’s create our first node. This node will be a publisher node. A publisher node is a software program that publishes messages (i.e. data values) to a particular topic. Check out this post, if you need more clarification on what a publisher and a topic are.

Open up a new Linux terminal window. 

Type the following command to open the Linux text editor.

gedit

Insert the C++ code at this link at the ROS.org website into the text file. To understand what each piece of the code does, read this link.

10-publisher-nodeJPG

Save the file as talker.cpp in your catkin_ws/src/hello_world/src folder and close the window.

That’s all there is to it. Now, let’s create a subscriber node.

Return to Table of Contents

How to Create a Subscriber Node in ROS Using C++

We now need a program that subscribes to the data (i.e. “hello world” message) published by the talker. This node will be a subscriber node. A subscriber node is a software program that subscribes to messages (i.e. data values) on a particular topic. 

Open up a new Linux terminal window. 

Type the following command to open the Linux text editor.

gedit

Insert the C++ code at this link at the ROS.org website into the text file. To understand what each piece of the code does, read this link.

11-listener-cppJPG

Save the file as listener.cpp in your catkin_ws/src/hello_world/src folder and close the window.

You’re all set. Now, let’s create the executable.

Return to Table of Contents

How to Create the Executable for ROS C++ Nodes

Our two nodes have been created, but now we need to convert that source code written in C++ into an executable that our machine can understand. To do that, open a new terminal window, and navigate to your catkin_ws/src/hello_world/ folder.

cd catkin_ws/src/hello_world/

Type the dir command to see the list of files in that directory. One of these files is CMakeLists.txt. Open that file.

gedit CMakeLists.txt

Add these lines to the very bottom of your CMakeLists.txt file.

add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})

Move to the catkin_ws folder.

cd ~/catkin_ws

Build the nodes.

catkin_make
12-catkin-makeJPG

You should see a screen that look something like the image below, indicating that the executables were successfully built.

Now, open a new terminal window and go to the catkin_ws/devel/lib/hello_world/ folder.

cd catkin_ws/devel/lib/hello_world
13-listener-talker-executablesJPG

Type dir to see the files listed. You will see the listener and talker executables. Feel free to close the terminal window now.

Let’s execute the nodes. To do that, in a fresh terminal window launch ROS by typing:

roscore

In another window, start the talker node (i.e. Publisher Node) by typing:

rosrun hello_world talker

You should see numbered “hello world” messages printing to the screen.

14-hello-worldJPG

Now, let’s start the listener node (Subscriber Node). Open a new terminal window, and type:

rosrun hello_world listener
15-listener-nodeJPG

Finally, let’s see what ROS topics are currently active. In a new terminal window, type:

rostopic list
16-rostopic-listJPG

Let’s get a list of the nodes that are currently active.

rosnode list
17-rosnode-listJPG

Press CTRL+C at any time to stop the program from running.

Return to Table of Contents

How to Create a Publisher Node in ROS Using Python

Now that we’ve seen how we can create publisher and subscriber nodes using C++, let’s do the same thing using Python.

Open up a new Linux terminal window. 

Type the following command to open the Linux text editor.

gedit

Insert the Python code at this link at the ROS.org website into the text file. To understand what each piece of the code does, read this link.

Save the file as talker.py in your catkin_ws/src/hello_world/scripts folder and close the window.

Return to Table of Contents

How to Create a Subscriber Node in ROS Using Python

Open up a new Linux terminal window. 

Type the following command to open the Linux text editor.

gedit

Insert the Python code at this link at the ROS.org website into the text file. To understand what each piece of the code does, read this link.

Save the file as listener.py in your catkin_ws/src/hello_world/scripts folder and close the window.

Return to Table of Contents

How to Execute ROS Python Nodes

Open up a new terminal window.

Navigate to the catkin_ws/src/hello_world/scripts folder.

cd catkin_ws/src/hello_world/scripts

Make the files executable. Type each command twice, pressing Enter after each.

chmod +x talker.py
chmod +x talker.py
chmod +x listener.py
chmod +x listener.py
18-chmodJPG

In a new terminal window, launch ROS.

roscore

Open a new terminal window, and execute talker.py.

rosrun hello_world talker.py
19-talkerJPG

Open another terminal window and type:

rosrun hello_world listener.py
20-heard-hello-worldJPG

To stop the program, type CTRL+C.

A cool ROS command that you can use at any time to see a list of active nodes is the following:

rosnode list

To find out information about the node, type the following command:

rosnode info /[name of node]

Return to Table of Contents

Congratulations! We have covered a lot of ground. You now know how to build publisher and subscriber nodes using the two most common languages used in robotics, Python and C++.

How to Create a ROS Workspace

Before you start writing code in ROS, you need to create a workspace. A workspace is a set of directories (or folders) where you store related pieces of ROS code. The official name for workspaces in ROS is catkin workspaces. The word ‘catkin’ comes from the tail-shaped flower cluster found on willow trees (see photo below) — a reference to Willow Garage, the original developers of ROS. 

willow_catkins_blossom_bloom

All the ROS packages that you create need to reside inside a catkin workspace. The name of this catkin workspace is typically called catkin_ws.

The official instructions for creating a ROS workspace are at ROS.org, but I will walk you through the process below so you can see how it is done.

Directions

Open up a new terminal window (I’m assuming you are using ROS on Ubuntu Linux), and type the following commands to create and build at catkin workspace.

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make
1-catkin-workspace

Type the dir command, and you will see three folders inside of this directory: build, devel, and src.

2-dir-command

Now we need to source the setup.bash file. This file sets the path of the workspace so that packages and code inside the workspace can be found.

source devel/setup.bash
3-set-path

Make sure the workspace is properly overlayed by the setup script (which we ran above).

echo $ROS_PACKAGE_PATH
4-make-sure

So we don’t have to source the setup.bash file every time we open a new Linux terminal, let’s add the ~/catkin_ws/devel/setup.bash command to the .bashrc file. Open a new Linux terminal window.

Type the following command to edit the .bashrc text file:

gedit ~/.bashrc

Add this line to the end of the .bashrc file:

source ~/catkin_ws/devel/setup.bash
5-edit-bashrc-file

That’s it! You’re all done. Just click Save and exit the text editor.