In this Robot Operating System (ROS) Version 2 tutorial, or ROS 2 tutorial, we explain the basics of ROS2 by using the Turtlesim simulation. In particular, this tutorial is developed for the ROS2 Jazzy Jalisco version of ROS2. Jazzy Jalisco is the most recent version of ROS2. However, with small modifications, everything explained in this tutorial works for other versions of ROS2.
In this Robot Operating System (ROS) Version 2 tutorial, or ROS 2 tutorial, we explain the basics of ROS2 by using the Turtlesim simulation. In particular, this tutorial is developed for the ROS2 Jazzy Jalisco version of ROS2. Jazzy Jalisco is the most recent version of ROS2. However, with small modifications, everything explained in this tutorial works for other versions of ROS2.
After more than 8 years of teaching and working with Robot Operating System 2 (ROS2), and after creating a number of popular tutorials and working with a number of students and engineers, we realized that the “best” and easiest entry point for learning ROS2 Jazzy might be the built-in Turtlesim simulation. This is a simplified 2D model of a mobile robot. However, the Turtlesim simulation has almost all the components that you need to implement in order to control a real robot using ROS2. By carefully studying this simulation and all the nodes/topics behind the ROS2 communication graph, you will get a solid understanding of how the ROS2 system works behind the scenes.
In this tutorial, we explain:
- How to start nodes in ROS2.
- How to start a Turtlesim node in ROS2.
- How to start a teleop (short for teleoperation) node is ROS2 that will enable us to control the Turtlesim in ROS2.
- How to obtain information about the nodes, topics, services, and parameters that currently running in the ROS2 system.
- How to use the rqt_graph to visualize the topics, nodes, actions, and the structure of the ROS2 system.
The YouTube tutorial is given below.
STEP 0: Verify that you have the proper version of Linux Ubuntu and Proper Version of ROS2
You need to have Linux Ubuntu 24.04. To check the Linux Ubuntu version, open a terminal and type
lsb_release -a
You should see something like this:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.2 LTS
Release: 24.04
Codename: noble
Then, verify that ROS2 is installed. To do that, source the environment and type
source /opt/ros/jazzy/setup.bash
printenv ROS_DISTRO
If ROS2 Jazzy is properly installed, you should see as an output “jazzy”. Another approach is to run this command
printenv | grep -i ROS
If ROS2 Jazzy is installed, you should see something like this:
ROS_VERSION=2
ROS_PYTHON_VERSION=3
GZ_CONFIG_PATH=/opt/ros/jazzy/opt/sdformat_vendor/share/gz
AMENT_PREFIX_PATH=/opt/ros/jazzy
CMAKE_PREFIX_PATH=/opt/ros/jazzy/opt/sdformat_vendor:/opt/ros/jazzy/opt/gz_math_vendor:/opt/ros/jazzy/opt/gz_utils_vendor:/opt/ros/jazzy/opt/gz_tools_vendor:/opt/ros/jazzy/opt/gz_cmake_vendor
ROS_AUTOMATIC_DISCOVERY_RANGE=SUBNET
PYTHONPATH=/opt/ros/jazzy/lib/python3.12/site-packages
LD_LIBRARY_PATH=/opt/ros/jazzy/opt/sdformat_vendor/lib:/opt/ros/jazzy/opt/rviz_ogre_vendor/lib:/opt/ros/jazzy/lib/x86_64-linux-gnu:/opt/ros/jazzy/opt/gz_math_vendor/lib:/opt/ros/jazzy/opt/gz_utils_vendor/lib:/opt/ros/jazzy/opt/gz_tools_vendor/lib:/opt/ros/jazzy/opt/gz_cmake_vendor/lib:/opt/ros/jazzy/lib
PATH=/opt/ros/jazzy/opt/gz_tools_vendor/bin:/opt/ros/jazzy/bin:/home/ahaber/anaconda3/condabin:/usr/local/cuda-12.6/bin:/home/ahaber/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
ROS_DISTRO=jazzy
STEP 1: Source the Environment and Install Turtlesim
The first step is to source the ROS 2 setup file that enables us to use ROS2 functionality. Open a new terminal and type
source /opt/ros/jazzy/setup.bash
The next step is to install Turtlesim:
sudo apt update && sudo apt upgrade
sudo apt install ros-jazzy-turtlesim
To check that Turtlesim is installed, type
ros2 pkg executables turtlesim
The output should look like this
turtlesim draw_square
turtlesim mimic
turtlesim turtle_teleop_key
turtlesim turtlesim_node
This means that turtlesim is installed correctly.
STEP 2: Run Turtlesim simulation
In ROS2, nodes are programs or modules that are responsible and that govern the behavior of a certain subsystem. For example, in a mobile robot, we have a number of nodes. Every node is responsible for a particular subsystem. For example, we have a node that is responsible for controlling wheels, and different sensor nodes, such as a node responsible for a Lidar operation and a node for position localization. Nodes communicate with each other by using topics, actions, services, and parameters.
Turtlesim is a Node that shows a 2D simulation of the turtle on the screen, and that moves the turtle across the screen. This node receives movement commands from another node whose purpose is to monitor the keys pressed on the keyboard and to send commands to the Turtlesim node. To start the Turtlesim node, in the same terminal run this line:
ros2 run turtlesim turtlesim_node
The simulation is shown below.

In the terminal, you should see this
[INFO] [1739905071.759772877] [turtlesim]: Starting turtlesim with node name /turtlesim
[INFO] [1739905071.773135611] [turtlesim]: Spawning turtle [turtle1] at x=[5.544445], y=[5.544445], theta=[0.000000]

The first line tells you that the node name is /turtlesim, and the second line tells you that turtle is created or spawned at the position x, y, and with the orientation angle of theta. These are the coordinates of the center of the turtle and the rotation of the turtle’s body.
However, we are still not able to move the turtle on the screen. This is because we did not start a teleoperation node that will read the keys pressed by the user and send these keys as control actions for moving the turtle.
To run the teleoperation node, open a new terminal and type
source /opt/ros/jazzy/setup.bash
ros2 run turtlesim turtle_teleop_key
You will see the message shown below that tells you how to control the turtle on the screen
Reading from keyboard
---------------------------
Use arrow keys to move the turtle.
Use g|b|v|c|d|e|r|t keys to rotate to absolute orientations. 'f' to cancel a rotation.
'q' to quit.

Play with the Turtlesim simulation in order to build an intuition on how to control it. You can control the turtle by using the arrow keys and the turtle’s trajectory will be described on the computer screen. The simulation is shown in the figure below.

STEP 3: Obtain information about nodes, services, topics, and parameters in ROS2 Jazzy
Keep the previous terminals open. To see the list of nodes that are currently running open a new terminal, and type
source /opt/ros/jazzy/setup.bash
ros2 node list
You will see this response:
/teleop_turtle
/turtlesim
This means that we currently have two nodes that are running at the same time. The /turtlesim node is used to simulate the turtle, and the node /teleop_turtle is the node that reads the keyboard inputs and sends the control information to the /turtlesim node
To get additional information about the Turtlesim node, open a terminal and type
ros2 node info /turtlesim
The output should look like this:
/turtlesim
Subscribers:
/parameter_events: rcl_interfaces/msg/ParameterEvent
/turtle1/cmd_vel: geometry_msgs/msg/Twist
Publishers:
/parameter_events: rcl_interfaces/msg/ParameterEvent
/rosout: rcl_interfaces/msg/Log
/turtle1/color_sensor: turtlesim/msg/Color
/turtle1/pose: turtlesim/msg/Pose
Service Servers:
/clear: std_srvs/srv/Empty
/kill: turtlesim/srv/Kill
/reset: std_srvs/srv/Empty
/spawn: turtlesim/srv/Spawn
/turtle1/set_pen: turtlesim/srv/SetPen
/turtle1/teleport_absolute: turtlesim/srv/TeleportAbsolute
/turtle1/teleport_relative: turtlesim/srv/TeleportRelative
/turtlesim/describe_parameters: rcl_interfaces/srv/DescribeParameters
/turtlesim/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
/turtlesim/get_parameters: rcl_interfaces/srv/GetParameters
/turtlesim/get_type_description: type_description_interfaces/srv/GetTypeDescription
/turtlesim/list_parameters: rcl_interfaces/srv/ListParameters
/turtlesim/set_parameters: rcl_interfaces/srv/SetParameters
/turtlesim/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
Service Clients:
Action Servers:
/turtle1/rotate_absolute: turtlesim/action/RotateAbsolute
Action Clients:
Topics are communication channels that are used to communicate information between nodes. To see the list of topics that are currently running, type
ros2 topic list
The output should look like this
/parameter_events
/rosout
/turtle1/cmd_vel
/turtle1/color_sensor
/turtle1/pose
You can also type this
ros2 topic list -t
to see the data types that is communicated through every topic. The output looks like this:
/parameter_events [rcl_interfaces/msg/ParameterEvent]
/rosout [rcl_interfaces/msg/Log]
/turtle1/cmd_vel [geometry_msgs/msg/Twist]
/turtle1/color_sensor [turtlesim/msg/Color]
/turtle1/pose [turtlesim/msg/Pose]
At this point, for us, the most important topic is /turtle1/cmd_vel. To get more information about /turtle1/cmd_vel type
ros2 topic info /turtle1/cmd_vel
The output should look like this
Type: geometry_msgs/msg/Twist
Publisher count: 1
Subscription count: 1
This output is very informative. First of all, it tells us that we have a single publisher node and a single subscriber node. Then, it also tells us the type of message that is being communicated between the nodes. The type of the message is
geometry_msgs/msg/Twist
Namely, ROS2 has standard data structures that are used to communicate information between nodes. These data structures are called messages. In our case, we are communicating geometry messages of the type Twist. More precisely,
geometry_msgs/msg/Twist
means that in the package geometry_msgs there is a msg called Twist. Next, let us obtain more information. To do that we will use the command
ros2 interface shown <msg_type>
Let us type:
ros2 interface show geometry_msgs/msg/Twist
We can see this reply
# This expresses velocity in free space broken into its linear and angular parts.
Vector3 linear
float64 x
float64 y
float64 z
Vector3 angular
float64 x
float64 y
float64 z
Basically, geometry_msgs/msg/Twist contains information about the linear velocity and angular velocity in free space. In other words, this tells you that the /turtlesim node through the topic /turtle1/cmd_vel expects to receive a message about the velocity which contains linear and angular command velocities. Then, on the basis of this information, the node /turtlesim should move and animate the turtle.
On the other hand, let us see what data is being published on the topic /turtle1/cmd_vel. To do that, we use the command
ros2 topic echo <topic_name>
Now, open a new terminal and type
source /opt/ros/jazzy/setup.bash
ros2 topic echo /turtle1/cmd_vel
and in the terminal in which /teleop_turtle node is opened, move the turtle. You will see that the messages are echoed in the terminal in which ros2 topic echo command is executed. The response will look like this
linear:
x: -2.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0
---
This is precisely the message of the type geometry_msgs/msg/Twist! To see the list of services that are currently running, type
ros2 service list
The output looks like this
/clear
/kill
/reset
/spawn
/teleop_turtle/describe_parameters
/teleop_turtle/get_parameter_types
/teleop_turtle/get_parameters
/teleop_turtle/get_type_description
/teleop_turtle/list_parameters
/teleop_turtle/set_parameters
/teleop_turtle/set_parameters_atomically
/turtle1/set_pen
/turtle1/teleport_absolute
/turtle1/teleport_relative
/turtlesim/describe_parameters
/turtlesim/get_parameter_types
/turtlesim/get_parameters
/turtlesim/get_type_description
/turtlesim/list_parameters
/turtlesim/set_parameters
/turtlesim/set_parameters_atomically
To see the list of actions that are currently running type
ros2 action list
The output looks like this
/turtle1/rotate_absolute
Next, let us learn how to use the rqt graph such that we can visualize the structure of the ROS2 communication network. To install rqt graph, type this
sudo apt update
sudo apt install '~nros-jazzy-rqt*'
To run the rqt_graph, type this
rqt_graph
This graph will show you more information about the structure of the ROS2 system which is shown in the figure below.
