Create a Hello World Project in ROS

In this section, we will develop our first ROS application. We are not going to do anything fancy here…just a basic “Hello World” project to get our feet wet with ROS. But before we get started on that, let’s cover some basic ROS terminology on a high level.

You Will Need

In order to complete this tutorial, you will need:

Directions

Our goal in this project is to get two pieces of ROS software (called nodes) to talk to each other. You can think of nodes as small single-purpose programs within a larger robotic system. One way nodes communicate with each other is by using messages. These messages are passed via channels called topics.

Nodes that send data are known as publisher nodes, and nodes that receive data are known as subscriber nodes. The node that keeps track (i.e. a register) of which nodes are publisher nodes and which nodes are subscriber nodes is called the ROS Master. Without the ROS Master, nodes would not be able to communicate with each other.

Nodes that are interested in a particular piece of data subscribe to the relevant topic; nodes that generate data publish to the relevant topic. There can be multiple publishers and subscribers to a topic. You can think of topics like a middle man between publishers (nodes that generate data) and subscribers (nodes that receive data). The communication is anonymous, so nodes do not know what nodes they are sending data to/receiving data from.

Here is a basic high-level diagram of the ROS Topic communication architecture I described above.

ros_master_1

A good analogy is to think of YouTube (or even other social media sites like Twitter or Instagram). YouTubers (publisher nodes) publish videos (messages) to a channel (topic), and you (subscriber node) can subscribe to that channel (topic) so that you receive all the videos (messages) on that channel (topic). YouTube (ROS Master) keeps track of who is a publisher and who is a subscriber. One thing to keep in mind is that (in contrast to YouTube) in ROS there can be multiple publishers to the same topic, and publishers and subscribers don’t know each other.

In the robotic world, we need different parts of a robot to send data to each other so that the robot can sense the world, process what it sees, and then act on the world. For example, suppose we are working on a humanoid robot. We might have one small program (publisher node) responsible for reading sensor data from a camera mounted on the head of the robot. That program might send that data (i.e. message) via a topic called “vision scan” to another program (subscriber node) which is responsible for controlling the joint in the robot’s arm. Note that messages in ROS can be a variety of data types, including strings, floats, integers, etc.

So with that terminology under our belts, we are ready to get started on our Hello World project. Let’s develop an application that consists of two nodes: talker and listener. The talker node will publish a “Hello World” message to the /chatter topic. The listener will subscribe to the /chatter topic so that it can receive the “Hello World” message. Here is a diagram of what I described above.

ros_master_2

To launch ROS, open a new Linux terminal window and type the following command:

roscore
10-roscore-1

roscore is the main process that is responsible for managing everything in ROS. Anytime you are trying to run a program in ROS, roscore needs to be running. roscore ensures that nodes (i.e. programs you write in Python and C++) are able to communicate with each other.

Open up a new terminal window, and start the talker node.

rosrun roscpp_tutorials talker

You should see hello world messages repeatedly printing to the screen.

11-roscpp-tutorials-talker

Use this command on a new terminal tab (File -> New Tab) to see a list of current active topics.

rostopic list
12-rostopic

The topic of the hello world messages is /chatter.

Now let’s start the listener node. The listener node will subscribe to the /chatter topic so that it can receive the hello world messages published by talker.

Open up a new terminal tab and type:

rosrun roscpp_tutorials listener
13-listener-output

The listener “hears” hello world.

Now, let’s run the two nodes side-by-side just to see what that looks like. Open a new terminal tab and type:

roslaunch roscpp_tutorials talker_listener.launch
14-hello-world-input-output

The roslaunch command is useful when you want to launch multiple ROS nodes all at once.

To stop ROS, press this command on your keyboard:

Ctrl+C 

Then type the following command in the terminal to close the terminal.

exit

That’s it! Congrats! You have made your first ROS application.

How to Install ROS on Ubuntu Linux in 5 Minutes or Less

In this post, we will get started with ROS, the Robot Operating System. ROS is the most popular robot programming platform. The reason it is so popular is that it is free, open-source, and has a ton of pre-written code that you can use for your robotics project. Sure you could write everything from scratch, but why reinvent the wheel? ROS saves you time from having to write code for common robot capabilities like navigation, motion planning, path planning, perception, control, and manipulation. It also has a huge worldwide community.

You Will Need

In order to complete this tutorial, you will need:

Directions

The official steps for installing ROS are at this link at ROS.org, but I will walk you through the process below so that you can see what each step should look like.

As of the time of this writing, the latest version with long term support (LTS) of ROS is ROS Melodic Morenia. Select that option.

1-latest-version-of-ros

I am using Ubuntu, so I will click on the Ubuntu option, which will land me on this page.

2-select-your-platform

Click the 9 white dots at the bottom left of your screen.

3-nine-white-dots

Search for Software & Updates. Then click on it.

4-software-updates

Make sure main, universe, restricted, and multiverse are all checked. Then click Close.

5-all-checked

Now open up a new terminal window, and type (or copy and paste) the following command:

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
6-sources-list

The command above sets your computer up to accept software from packages.ros.org. 

Now we need to set up the secure keys so that our system accepts what we are going to download. 

For the next step, update the package list on your system.

sudo apt update
8-update-package-list

Now type:

sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
7-add-keys

Now do a full desktop install of ROS. At the time of this writing, the latest version is ROS Melodic Morenia. The command below installs all the software, tools, algorithms, and robot simulators for ROS. After you type the command and press Enter, press Y and hit Enter when asked if you want to continue. It will take a while to download all this stuff, so feel free to take a break while ROS downloads to your system.

sudo apt install ros-melodic-desktop-full

Now initialize rosdep. This is a tool that is required before you can use ROS.

sudo rosdep init
rosdep update

Set up the environment variables.

echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc

Note that anytime you want to see what environment variables ROS is using, you can type the following command:

export | grep ROS

This command will show you the ROS distribution you are using, the version of Python ROS is using, and a bunch of other stuff.

The two key variables are as follows:

  • ROS_MASTER_URI: Shows the URL where the roscore is in execution. This is often your own local computer.
  • ROS_PACKAGE_PATH: Shows the path on your computer where the ROS packages are.

Install some other tools that you will work with in ROS. After you type the command below, press Y and Enter to complete the download process.

sudo apt install python-rosinstall python-rosinstall-generator python-wstool build-essential

Here is the last step of the installation process. Check which version of ROS you have installed. If you see your ROS version as the output, congratulations you have successfully installed ROS!

rosversion -d
9-check-ros-version

ROS has a lot of new vocabulary (e.g nodes, bags, topics). So before you start working with ROS, I recommend you bookmark (no need to read in detail) this page at the ROS Wiki which covers the new vocabulary associated with ROS. That way, when I mention a term you might not understand, you know where to look it up.

Definition of a Robot

If you ask a random roboticist to give you the definition of a robot, everyone will give you a different answer. If you do an online search, here is what you might get for the top result:

A robot is “a machine—especially one programmable by a computer— capable of carrying out a complex series of actions automatically (Wikipedia).” However, this definition falls short of what a robot really is. Just based on that definition, one could consider a dishwasher a robot. But a dishwasher is not a robot.

So back to the original topic of this post. What is a robot? The best definition of a robot I have seen is in Maja Matarić’s book, The Robotics Primer. Maja Matarić is the Director of the Robotics and Autonomous Systems Center at the University of Southern California. She received her PhD in Computer Science and Artificial Intelligence from MIT in 1994, where she studied under Rodney Brooks a founder and former Chief Technical Officer of iRobot, one of the largest consumer robotics companies in the world.

Here is Professor Matarić’s definition:

“A robot is an autonomous system which exists in the physical world, can sense its environment, and can act on it to achieve some goals.”

I love this definition.

  • A robot must be autonomous, which means it acts based on its own decisions.
  • A robot must exist in the physical world, which means it has to deal with gravity and the laws of physics just like we all do.
  • A robot must have sensors (look, hear, smell, and/or touch) to gather information about the world around it.
  • A robot must perform some computation based on what it has sensed and then act on the external world in some way. This is called the sense-think-act model, which has been the dominant paradigm for robotics for over 40 years.

Going back to the dishwasher example I mentioned earlier…a dishwasher is not a robot because it does not act on the external world. However, an autonomous robotic vacuum cleaner like the Roomba is a robot. The Roomba sucks up dirt on the floor.

So the next time you are wondering if something is a robot or just a robot wannabe, come back to this definition for clarification.