In this Robot Operating System Version 2 tutorial, or ROS 2 tutorial, we explain the basics of ROS2 by using a Turtlesim simulation. Turtlesim is a simple model and simulation of a 2D mobile robot. This example is used to illustrate the main ideas and concepts of ROS 2. Consequently, if you are completely new to ROS, you should start with this example.
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
The YouTube tutorial accompanying this webpage tutorial is given below.
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/iron/setup.bash
Next, we need to verify the environmental variables. Do this by typing
printenv | grep -i ROS
You should see something like this
ROS_VERSION=2
ROS_PYTHON_VERSION=3
AMENT_PREFIX_PATH=/opt/ros/iron
ROS_AUTOMATIC_DISCOVERY_RANGE=SUBNET
PYTHONPATH=/opt/ros/iron/lib/python3.10/site-packages
LD_LIBRARY_PATH=/opt/ros/iron/opt/rviz_ogre_vendor/lib:/opt/ros/iron/lib/x86_64-linux-gnu:/opt/ros/iron/lib
PATH=/opt/ros/iron/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
ROS_DISTRO=iron
The next step is to install Turtlesim:
sudo apt update
sudo apt install ros-iron-turtlesim
To check that Turtlesim is properly 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 different subsystems. For example, in a mobile robot, since we have many subsystems, we have many 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 Lidar operation and a node for position localization. Nodes communicate between each other by using topics, actions, services, and parameters.
To start Turtlesim, in the same terminal run this line
ros2 run turtlesim turtlesim_node
You should see the Turtlesim window shown below.
However, we are 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/iron/setup.bash
ros2 run turtlesim turtle_teleop_key
STEP 3: Obtain information about nodes, services, topics, and parameters in ROS2
To see the list of nodes that are currently running open a new terminal, and type
source /opt/ros/iron/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. To get additional information about the Turtlesim node, 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:
To see the list of topics that are currently running, type
ros2 topic list
The output should look like this
/turtle1/cmd_vel
To get more information about /turtle1/cmd_vel type
ros2 topic info /turtle1/cmd_vel
To see the list of services that are currently running, type
ros2 service list