How to Launch the TurtleBot3 Simulation With ROS

In this tutorial, we will launch a virtual robot called TurtleBot3. TurtleBot3 is a low-cost, personal robot kit with open-source software. You can read more about TurtleBot here at the ROS website.

Below is a demo of what you will create in this tutorial. You will get experience with SLAM (Simultaneous localization and mapping) and autonomous navigation.

turtlebot3-simulation

TurtleBot3 is designed to run using just ROS and Ubuntu. It is a popular robot for research and educational purposes.

48-turtlebotsJPG

Table of Contents

Directions

I’m assuming you have ROS installed and are using Linux. If you don’t have ROS installed, install ROS now.

Let’s install the TurtleBot3 simulator now.

Open a terminal window and install the dependent packages. Enter the following commands, one right after the other:

cd ~/catkin_ws/src/
git clone https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git
git clone https://github.com/ROBOTIS-GIT/turtlebot3.git
cd ~/catkin_ws && catkin_make

TurtleBot3 has three models, Burger, Waffle, and Waffle Pi, so you have to set which model you want to use before you launch TurtleBot3. Type this command to open the bashrc file to add this setting:

gedit ~/.bashrc

Add this line at the bottom of the file:

export TURTLEBOT3_MODEL=burger
49-update-bash-settingsJPG

Save the file and close it.

Now reload .bashrc so that you do not have to log out and log back in.

source ~/.bashrc

Now, we need to download the TurtleBot3 simulation files.

cd ~/catkin_ws/src/
git clone https://github.com/ROBOTIS-GIT/turtlebot3_simulations.git
50-turtlebot-simulationsJPG
cd ~/catkin_ws && catkin_make
51-run-catkin-makeJPG

Return to Table of Contents

Simulate TurtleBot3 Using RViz

Now that we have the TurtleBot3 simulator installed, let’s launch the virtual robot using RViz. Type this command in your terminal window:

roslaunch turtlebot3_fake turtlebot3_fake.launch

If you want to move TurtleBot3 around the screen, open a new terminal window, and type the following command:

roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch

Click the terminal window and use the keys below to control the movement of your TurtleBot (e.g. press W key to move forward, X key to move backward and S to stop).

52-move-turtlebot-aroundJPG
52-turtlebot-rvizJPG
53-move-turtlebotJPG

And remember, use the keyboard to move the robot around.

54-how-to-control-turtlebotJPG

Press CTRL+C in all terminal windows.

Return to Table of Contents

Simulate TurtleBot3 Using Gazebo

Now let’s use Gazebo to do the TurtleBot3 simulation.

First, let’s launch TurtleBot3 in an empty environment. Type this command:

roslaunch turtlebot3_gazebo turtlebot3_empty_world.launch

Wait for Gazebo to load. It could take a while. Here is what your screen should look like:

55-gazebo-simulationJPG

Press CTRL+C and close out all windows.

Return to Table of Contents

How to Change the Simulation Environment for TurtleBot3

Let’s look at our TurtleBot3 in a different environment. This environment is often used for testing SLAM and navigation algorithms. Simultaneous localization and mapping (SLAM) concerns the problem of a robot building or updating a map of an unknown environment while simultaneously keeping track its location in that environment.

In a new terminal window type:

roslaunch turtlebot3_gazebo turtlebot3_world.launch
56-gazebo-slam-navigation-viewJPG
57-the-burgerJPG

Press CTRL+C and close out all windows.

We can also simulate TurtleBot3 inside a house. Type this command and wait a few minutes for the environment to load.

roslaunch turtlebot3_gazebo turtlebot3_house.launch
58-turtlebot-in-a-houseJPG

To move the TurtleBot with your keyboard, use this command in another terminal tab:

roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch

Press CTRL+C and close out all windows.

Return to Table of Contents

Autonomous Navigation and Obstacle Avoidance With TurtleBot3

Now let’s implement obstacle avoidance for the TurtleBot3 robot. The goal is to have TurtleBot3 autonomously navigate around a room and avoid colliding into objects.

Open a new terminal and type:

roslaunch turtlebot3_gazebo turtlebot3_world.launch

In another terminal window type:

roslaunch turtlebot3_gazebo turtlebot3_simulation.launch

You should see TurtleBot3 autonomously moving about the world and avoiding obstacles along the way.

59-autonomous-navigation-robotJPG

We can open RViz to visualize the LaserScan topic while TurtleBot3 is moving about in the world. In a new terminal tab type:

roslaunch turtlebot3_gazebo turtlebot3_gazebo_rviz.launch
60-open-rviz-visualize-laserscanJPG

Press CTRL+C and close out all windows.

Return to Table of Contents

Simulating SLAM With TurtleBot3

Let’s take a look at how we can simulate SLAM with TurtleBot3. As a refresher, Simultaneous localization and mapping (SLAM) concerns the problem of a robot building or updating a map of an unknown environment while simultaneously keeping track its location in that environment.

Install the SLAM module in a new terminal window.

sudo apt install ros-melodic-slam-gmapping

Start Gazebo in a new terminal window.

roslaunch turtlebot3_gazebo turtlebot3_world.launch

Start SLAM in a new terminal tab.

roslaunch turtlebot3_slam turtlebot3_slam.launch slam_methods:=gmapping

Start autonomous navigation in a new terminal tab:

roslaunch turtlebot3_gazebo turtlebot3_simulation.launch

Watch the robot create a map of the environment as it autonomously moves from place to place!

61-slam-using-turtlebot3JPG
62-slam-2-using-turtlebot3JPG
62-slam-3-using-turtlebot3JPG

And that is all there is to it.

When you’ve had enough, press CTRL+C and close out all windows.

Return to Table of Contents

That’s it for TurtleBot3. In case you want to try other commands and learn more, check out the official TurtleBot3 tutorials.

Implementing the ROS Turtlesim Project With rospy

You might have seen my previous tutorial where we ran the built-in ROS turtlesim program. We are going to explore this application further in this tutorial. 

Turtlesim isn’t the most exciting application, but it is a popular tool for learning the basics of ROS before working with real robots (Click here for more information on turtlesim from the official ROS website). You can think of the turtle as an actual robot. All of the things you can do with this turtle, you can do with a real, physical robot. Let’s get started!

Table of Contents

Directions

Move the Turtle

Let’s run turtlesim with rospy, the Python library for ROS. 

Open up a new terminal window and type:

roscore

Start the turtlesim node by going to a new terminal window and typing:

rosrun turtlesim turtlesim_node
25-turtlesim-openJPG

Open yet another terminal window and check out the list of topics that are currently active:

rostopic list
26-rostopic-listJPG

To move the turtle, we need to publish to the /turtle1/cmd_vel topic. But what type of messages can we publish to this topic? Let’s find out.

rostopic type /turtle1/cmd_vel

Here is the output:

27-ros-topic-typeJPG

What this means is that we need to publish messages of type geometry_msgs/Twist to the /turtle1/cmd_vel topic to get the turtle to move like we want it to. 

So what does ‘geometry_msgs/Twist’ mean? We need to check out the definition of this data type. 

rosmsg show geometry_msgs/Twist

The message you see on your terminal window expresses the velocity of the turtle in 3D space, broken into its linear and angular parts. With respect to the turtle, the two pieces of data that we can control are the linear velocity in the x-direction and the angular velocity along the z axis because the robot cannot move along either the z or y axes.

Check out this link at the ROS website if you want to dive into more detail about the different kinds of geometric data types, including Twist.

Let’s get the turtle moving. We need to create a node (i.e. program) and make sure that node publishes the velocity values to the /turtle1/cmd_vel topic. Let’s do that now.

Close out turtlesim using CTRL+C.

Open up a new terminal window and open the Linux text editor.

gedit

Type this Python code (credit to Lentin Joseph, author of Robot Operating System for Absolute Beginners).

28-move-turtleJPG

Save the file as move_turtle.py. Make sure to save it to your catkin_ws/src/hello_world/scripts folder.

The code above imports rospy (ROS library for Python), Twist (for us to work with the linear and angular velocity of the turtle), and sys (library that enables us to pass parameters via the Linux command line).

In a terminal window, type:

cd catkin_ws/src/hello_world/scripts

Make the node executable.

chmod +x move_turtle.py
chmod +x move_turtle.py

Open up a new terminal tab, launch ROS.

roscore

Open a new terminal tab, and launch the turtlesim node:

rosrun turtlesim turtlesim_node

Let’s get the turtle to move with a linear velocity of 3.0 m/s and an angular velocity of 1.5 radians/s. We need to pass those two values as arguments in the terminal. Open another terminal window and type:

rosrun hello_world move_turtle.py 3.0 1.5

You should see the turtle moving in circles, around and around.

29-turtle-moving-circlesJPG

Open a new terminal window, and let’s see the ROS computation graph. Type this command:

rqt_graph
30-computation-graphJPG-1

When you have had enough, stop the program by typing CTRL+C, and close all terminal windows.

Return to Table of Contents

Get the Turtle’s Position

Now that we know how to work with the velocity of the turtle, let’s take a look at how we can get the position of the robot. To do that, we need to work with the /turtle1/pose topic.

Launch ROS.

roscore

Start the turtlesim node by going to a new terminal window and typing:

rosrun turtlesim turtlesim_node

Let’s display the messages that are published to the /turtle1/pose topic.

rostopic echo /turtle1/pose

Here is the output to the screen. You can see the position and velocity information.

31-pose-outputJPG

We don’t want to have to type a command every time we want to get position and velocity information, so let’s modify move_turtle.py so that it subscribes to the /turtle1/pose topic. 

First, find out what the message type is of the /turtle1/pose topic.

rostopic type /turtle1/pose

Let’s also get the message definition:

rosmsg show turtlesim/Pose
32-ros-msg-show-poseJPG

Press CTRL+C to stop the processes. You can exit all terminal windows.

Let’s get the pose using an actual Python-based node. Open up a new terminal window and open the Linux text editor.

gedit

Type this Python code (credit to Lentin Joseph, author of Robot Operating System for Absolute Beginners).

Save it as move_turtle_get_pose_.py to your catkin_ws/src/hello_world/scripts folder. Here is the code: 

33-move-turtle-get-poseJPG

In a terminal window, type:

cd catkin_ws/src/hello_world/scripts

Make the node executable.

chmod +x move_turtle_get_pose_.py 
chmod +x move_turtle_get_pose_.py 

Open up a new terminal tab, launch ROS.

roscore

Open a new terminal tab, and launch the turtlesim node:

rosrun turtlesim turtlesim_node

Open a new terminal tab, and launch the move_turtle_get_pose node:

rosrun hello_world move_turtle_get_pose.py 1.0 0.5

You should see data output.

34-velocity-and-position-dataJPG

In a new terminal window, check out the computation graph:

rqt_graph
35-comp_graphJPG

You can see that the move_turtle node is publishing position data to the /turtle1/pose topic. This topic is subscribed by the /turtlesim node. Similarly, the turtlesim node is publishing velocity data to the /turtle1/cmd_vel topic. This topic is being subscribed by the move_turtle node.

Press CTRL+C on all windows, and close the terminal screens.

Return to Table of Contents

Move the Turtle a Specific Distance

Now let’s move the turtle a distance that we specify. 

Open up a new terminal window and open the Linux text editor.

gedit

Type this Python code (credit to Lentin Joseph, author of Robot Operating System for Absolute Beginners).

Save it as move_distance.py to your catkin_ws/src/hello_world/scripts folder. Here is the code: 

35b-robot-spec-distance-methodsJPG

In a terminal window, type:

cd catkin_ws/src/hello_world/scripts

Make the node executable. Type the command twice as sometimes you might get an error if you type it only once.

chmod +x move_distance.py 
chmod +x move_distance.py 

Open up a new terminal tab, launch ROS.

roscore

Open a new terminal tab, and launch the turtlesim node:

rosrun turtlesim turtlesim_node

Open a new terminal window, and launch the move_distance node. The arguments are linear velocity, angular velocity, and distance that you want the turtle to travel:

rosrun hello_world move_distance.py 0.4 0.0 7.0

Here is the data output:

36-robot-spec-distanceJPG

And here is what your simulation screen should look like:

37-visual-robot-move-spec-distanceJPG

Press CTRL+C on all windows, and close the terminal screens.

Return to Table of Contents

Work With ROS Services and ROS Parameters

Ros Services

Up until now, we have been using what is called the publish/subscribe model for inter-node communication. A node publishes to a topic, and nodes that need the data published to that topic have to subscribe to that topic. This one-way communication model is flexible, but it is inefficient when we want two-way communication between nodes … for example if one node requests data from another node and wants a reply from that node. 

Request / reply in ROS is done via what is known as a Service. A service is defined by a pair of messages: one for the request and one for the reply. If you want to know more details about ROS Services, check out this page on the ROS website.

Let’s check out a list of available ROS services in the turtlesim node. Open a new terminal window and type:

roscore

Start the turtlesim node by going to a new terminal tab and typing:

rosrun turtlesim turtlesim_node

Now in another terminal tab, type:

rosservice list
38-ros-service-listJPG

You should see this on your screen. We want to incorporate the reset service into our code. When the code calls the reset service, the workspace resets. Let’s find out the type of the reset service.

rosservice type /reset
39-ros-service-typeJPG

stdsrvs/Empty is the type. It is a built-in service type in ROS. Let’s display some information on this service type.

rossrv show std_srvs/Empty
40-ros-service-emptyJPG

You can see that the field is empty. This is what we expected because no actual data is exchanged during this service. All the /reset service does is reset the workspace. 

ROS Parameters

Before we implement the workspace reset service described above, let’s talk about what ROS parameters are. Parameters are globally available values such as integers, floats, strings or booleans. We can also have parameters like background color. Let’s take a look at that one.

Open a new terminal tab and type the following command to retrieve a list of parameters:

rosparam list
41-ros-parameters-listJPG

Let’s get the value of the red background color parameter, for example:

rosparam get /background_b
42-ros-background-colorJPG

The topic of the background color parameter is /turtle1/color_sensor. Let’s see what this topic is all about by displaying some information on it:

rostopic echo /turtle1/color_sensor
43-rostopic-colorsJPG

Press CTRL+C on all windows, and close the terminal screens.

Return to Table of Contents

Change the Background Color and Reset the Workspace

Let’s use some Python code that modifies the parameter for the background color and resets the workspace by calling the /reset service.

Open up a new terminal window and open the Linux text editor.

gedit

Type this Python code (credit to Lentin Joseph, author of Robot Operating System for Absolute Beginners).

Save it as turtle_service_param.py  to your catkin_ws/src/hello_world/scripts folder.

In a terminal window, type:

cd catkin_ws/src/hello_world/scripts

Make the node executable. 

chmod +x turtle_service_param.py
chmod +x turtle_service_param.py

Open up a new terminal tab, launch ROS.

roscore

Open a new terminal tab, and launch the turtlesim node:

rosrun turtlesim turtlesim_node

Open a new terminal tab, and launch the code:

rosrun hello_world turtle_service_param.py

Here is what your screen should look like:

44-change-background-color-turtlesimJPG

Here is what the terminal outputs:

45-change-bg-color-2JPG

If you press CTRL+C on the same terminal window, and then rerun this command, you will see we get a different color:

rosrun hello_world turtle_service_param.py
46-diff-colorJPG

Each time you run the command above, you will get a message that the service has been executed.

47-executed-serviceJPG

When you are finished, press CTRL+C on all windows, and close the terminal screens.

Congratulations! You have completed the turtlesim tutorial. 

Return to Table of Contents

How to Visualize Nodes Using the RQt GUI Tool

The rqt tool in ROS enables us to visualize the node connections while a launch file is running. Let’s see what happens when we run the launch file we created in this tutorial. Open up a new terminal window.

Directions

Type:

roslaunch hello_world talker_listener.launch

Then in a new terminal window, type:

rqt_graph
24-talker-listenerJPG-1

You can see that the listener_node is subscribed to the /chatter topic. The talker_node is publishing to the /chatter topic. You might also see another node called /rosout (if you don’t see /rosout that is OK). This is where debug statements from nodes are going.

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 move 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

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>

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.

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.

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.

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 Package

In this tutorial, we’ll learn how to create a ROS package. Software in ROS is organized into packages. Each package might contain a mixture of code (e.g. ROS nodes), data, libraries, images, documentation, etc. Here is the official ROS tutorial on how to create a ROS package. I will walk you through this process below.

Directions

Here is the syntax for creating a ROS package. Do not run this piece of code.

catkin_create_pkg <package_name> [depend1] [depend2] [depend3]

Now, open a new terminal window, and move to the source directory of the workspace you created. If you don’t already have a workspace set up, check out this tutorial.

cd ~/catkin_ws/src

Create a ROS package named ‘hello_world’. This package will have three dependencies (i.e. libraries the package depends on in order for the code inside the package to run properly): roscpp (the ROS library for C++), rospy (the ROS library for Python), and std_msgs (common data types that have been predefined in ROS … “standard messages”).

catkin_create_pkg hello_world std_msgs rospy roscpp
6-create-a-package

Type dir , and you will see that we now have a package named hello_world inside the source folder of the workspace. 

Change to the hello_world directory.

cd hello_world

Type:

dir
7-inside-src-folder

You can see that we have four files:

CMakeLists.txt: This text file contains the commands to compile the programs that you write in ROS. It also has the commands to convert your source code and other files into an executable (i.e. the code that your computer can run).

include: Contains package header files. You might remember when we wrote header files in the C++ tutorial…well this folder is where your header files would be stored.

src: This folder will contain the C++ source code. If you are doing a project in Python, you can create a new folder named scripts that will contain Python code. To create this new folder type:

mkdir scripts
8-make-directory-scripts

package.xml: This is an Extensible Markup Language (XML) file. An XML file is a text file written in a language (called XML) that is easy to read by both humans and computers. An XML file does not do anything other than store information in a structured way. 

Our package.xml file contains information about the hello_world package. You can see what is inside it by typing:

gedit package.xml
9-package-xml-file

That’s it! You’re all done.

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

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.

How to Launch Gazebo in Ubuntu

Gazebo is a 3D simulator that is a really good tool if you want to simulate your robot in a complex outdoor or indoor environment. In this post, I will show you how to launch Gazebo in Ubuntu.

You Will Need

Before you launch Gazebo, it is important you have it installed on your Ubuntu Linux distribution. If you already have ROS on your system or you followed my ROS installation tutorial, Gazebo is already installed. Otherwise, check out this link at the official Gazebo website and follow the instructions to install Gazebo on Ubuntu.

Directions

To launch Gazebo for the first time, open up a new terminal window, and type the following command. It normally takes a while to launch the first time, so just sit back and relax while Gazebo does its thing:

gazebo

Here is what your screen should look like.

1-launch-gazeboJPG

But that is not what my screen looked like at all. I got this ugly error message on my terminal window.

2-terminal-messageJPG

The error is:

[Err] [REST.cc:205] Error in REST request

libcurl: (51) SSL: no alternative certificate subject name matches target host name ‘apt.ignitionfuel.org’

To resolve this error, press CTRL+C on your keyboard to close Gazebo.

Open up a new terminal tab and type:

cd ~/.ignition/fuel/
3-config-yamlJPG

Type dir and press Enter.

You should see the config.yaml file.

4-change-urlJPG

Open up that file.

gedit config.yaml 

Change:

url: https://api.ignitionfuel.org

to

url: https://api.ignitionrobotics.org

Click Save.

Close the window.

Now open up a new terminal window and launch Gazebo.

gazebo
5-no-errorJPG

You will see there is now no error. That’s it!

How to Launch RViz and RQt in ROS

Up until now we have been interacting with ROS via the Linux terminal. ROS also has some really cool graphical user interface (GUI) tools that enable you to interact with ROS in a more visual way than we have done so far. Two of these tools are rviz and rqt.

  • rviz is a 3D visualizer for ROS
  • rqt is a ROS visualization tool based on Qt, a free and open-source widget toolkit for creating GUIs.

In this tutorial, I’ll show you how to set up both of these tools.

You Will Need

In order to complete this tutorial, you will need:

Directions

To launch rviz, open a new terminal window and type:

roscore

Open up a new terminal tab and type:

rosrun rviz rviz
26-rviz

To launch rqt, open a new terminal window and type:

roscore

Open up a new terminal tab and type:

rosrun rqt_gui rqt_gui
27-rqt-empty-gui

You can see a list of available Plugins by going to the Plugins option. Let’s go to Plugins -> Visualization -> Plot to get a blank plot.

28-blank-plot

That’s it!

Launch the Turtlesim Robot Simulation in ROS

In this section, we will work with the turtlesim application. This application comes pre-installed with ROS and consists of a 2D simulation of a turtle. You can move the turtle around and do a lot of other cool stuff as described here at the turtlesim ROS Wiki page.

Turtlesim isn’t the most exciting application, but it is a popular tool for learning the basics of ROS before working with real robots. You can think of the turtle as an actual robot. All of the things you can do with this turtle, you can do with a real, physical robot.

15-turtlesim

You Will Need

In order to complete this tutorial, you will need:

Directions

Let’s run this program now with rospy, the Python library for ROS. 

Let’s launch turtlesim now. Open up a new terminal window, and type:

roscore

Open a new terminal tab, and launch the turtlesim application.

rosrun turtlesim turtlesim_node
launch-turtlesim

Let’s see the list of topics. Remember that a topic in ROS is a named bus (or channel) over which a node publishes messages for other nodes to receive.  Open a new terminal tab, and type:

rostopic list
17-rostopic-list

Now, up until now, the model that I have shown you of how ROS nodes communicate with each other is the ROS Topics model, in which a Publisher Node sends messages via a Topic to one or more registered Subscriber nodes. We worked with this model in the Hello World project in the previous section. This model looks like this:

ros-master-nodes

Notice that the flow of information between nodes is one-way, from Publisher to Subscriber. What do we do in a situation where we have a node that wants to request information from another node and receive an immediate reply? How is this two-way communication implemented in ROS? Request/reply in ROS is performed via ROS Services.

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). A client node (i.e. Service Client) calls the service by sending a request message to the service provider. The client node then awaits the reply. Here is what the ROS Service model looks like:

ros-service-model

The ROS tutorials present a good explanation of when you would want to use the ROS Topic model and when you would want to use the ROS Service model.

Now that we understand what a ROS Service is, let’s see a list of all active services in our turtlesim application. Open a new terminal window and type:

rosservice list
18-rosservice-list

Let’s see a list of ROS parameters. Think of parameters as the global settings for our current ROS environment (e.g. things like the background color of the turtlesim screen, version of ROS we are using, etc.).

rosparam list
19-rosparam-list

OK, now that we have covered some more ROS terminology, let’s have some fun and make this turtle move around the blue window. In order to do that, I need to create another ROS node. I have to open up a new terminal tab and type this command:

rosrun turtlesim turtle_teleop_key
20-move-turtle

If you use the arrow keys on your keyboard from within the terminal where you typed the command above, you should see the turtle moving around the screen. Each time you press an arrow key, the teleop_turtle node publishes a message to the /turtle1/cmd_vel topic. turtlesim node is subscribed to that topic. It receives the message and moves the turtle. 

21-move-turtle-2

Open a new terminal window and see a list of active nodes using this command:

rosnode list
23-active-nodes
setup-with-turtle

Here is the block diagram of our application:

teleop-turtle

Now draw a square with turtlesim. Press Ctrl+C to stop the simulation. Close all terminal windows, and start a new terminal window. Type:

roscore
rosrun turtlesim turtlesim_node 
rosrun turtlesim draw_square

The robot will go around and around along a square-shaped path.

24-draw-square
25-draw-square-2

To reset the simulator, type:

rosservice call /reset

Then type:

rosrun turtlesim draw_square

And now you have seen how to launch and run Turtlesim in ROS!