Turtlesim – the first ROS robot simulation
A simple way to learn the basics of ROS is to use the turtlesim simulator that is part of the ROS installation. The simulation consists of a graphical window that shows a turtle-shaped robot. The background color for the turtle's world can be changed using the Parameter Server. The turtle can be moved around on the screen by ROS commands or using the keyboard.
Turtlesim is a ROS package, and the basic concepts of package management were presented in the Exploring the ROS packages section, as discussed earlier. We suggest that you refer to this section before continuing.
We will illustrate a number of ROS commands that explore the nodes, topics, messages, and services used by the turtle simulator. We have already covered the roscore
, rosnode
, and rostopic
commands. These commands will be used with turtlesim also.
Other important ROS terminal commands that will be covered in this section are as follows:
rosrun
: Finds and starts a requested node in a packagerosmsg
: Shows information about messagesrosservice
: Displays runtime information about nodes and can pass data between nodes in a request/response moderosparam
: Used to get and set parameters (data) used by nodes
To start turtlesim with ROS commands, we need to open two separate terminal windows. First, issue the following command in the first window if the Master is not already running:
Wait for the Master to complete startup. You can minimize this window but do not close it because the Master must run to allow the nodes to communicate.
The result on your screen will resemble the output discussed previously in the Invoking the ROS Master using roscore section, where roscore
was described.
To display the turtle on the screen, use the rosrun
command. It takes the arguments [package name][executable name]
, and in this case, turtlesim
as the package and turtlesim_node
as the executable program.
In the second terminal window, issue the following command:
You will see an output similar to this:
Wait for the display screen to appear with the image of a turtle at the center, as shown in the turtlesim screen in the following screenshot. The terminal window can be minimized, but keep the turtle display screen in view. The turtle is called turtle1
since this is the first and only turtle in our display.
After you have started turtlesim by executing the rosrun
command, you will see information about the turtle's position on the screen. The /turtlesim
node creates the screen image and the turtle. Here, the turtle is in the center at about x = 5.5, y = 5.5 with no rotation since angle theta is zero. The origin (0, 0) is at the lower-left corner of the screen:
Let's study the properties of the nodes, topics, services, and messages available with the turtlesim
package in another terminal window. Thus, at this point, you will have three windows active but the first two can be minimized or dragged off to the side or the bottom. They should not be closed.
In the third window, issue the rosnode
command to determine information about any node. First, list the active nodes, using the following command:
The output is as follows:
We will concentrate on the /turtlesim
node. Note the difference in notation between the /turtlesim
node and the turtlesim
package.
To see the publications, subscriptions, and services of the turtlesim
node, type the following command:
The output of the preceding command is as follows:
The following diagram represents a graphical illustration of the relationship of the turtlesim
node in elliptical shapes and topics in the rectangular boxes:
The graph was created using the rqt_graph
command as described in the Introducing RQT tools section in Chapter 3, Driving Around with TurtleBot.
We read the output of the rosnode info
command and the graph of the turtlesim
node and topics in the preceding diagram as follows (ignoring the logging services and the /rosout
and /rosout_agg
nodes):
- The
/turtlesim
node publishes to two topics. These topics control the color of the turtle's screen and the position of the turtle on the screen when messages are sent from /turtlesim
:/turtle1/color_sensor
with the message type [turtlesim/Color]
/turtle1/pose
with the message type [turtlesim/Pose]
- The
/turtlesim
node subscribes to the turtle1/cmd_vel
topic. The /turtlesim
node is waiting for another node to publish on the turtle1/cmd_vel
topic. - There are a number of services associated with the
/turtlesim
node. The services can be used to move the turtle around (teleport), clear the screen, kill the nodes, and perform other functions. The services will be explained later in the section ROS services to move turtle.
Turtlesim topics and messages
A ROS message is a strictly typed data structure. In fact, the message type is not associated with the message contents. For example, one message type is string
, which is just text. Another type of message is uint8
, which means that it is an unsigned 8-bit integer. These are part of the std_msg
package or standard messages. The command form rosmsg list
lists the type of messages on your system; it is a long list! There are packages for messages of the type control, type geometry, and type navigation, among others to control robot actions. There are sensor message types used with laser scanners, cameras, and joysticks to name just a few of the sensors or input devices possible with ROS. Turtlesim uses several of the message types to control its simulated robot—the turtle.
For these exercises, keep the roscore
and /turtlesim
windows active. Open other terminal windows as needed. We will concentrate on the turtle1/color_sensor
topic first. You will be typing in the third window.
Tip
If the screen gets too cluttered, remember the $ clear
command.
Use the $ history
command to see the commands you have used.
For the /turtlesim
node, the topics are listed using the following command:
The output of the preceding command is as follows:
The topic type can be determined for each topic by typing the following command:
The message type in the case of the /turtle1/color_sensor
topic is turtlesim/Color
. This is the format of ROS message type names:
[package name]/[message type]
If a node publishes a message, we can determine the message type and read the message.
The turtlesim
package has two message types that are found with the following command:
The output is as follows:
The rosmsg list
command displays all of the messages. Adding | grep turtlesim
shows all messages of the turtlesim
package. There are only two in the turtlesim
package. From the message type, we can find the format of the message. Make sure that you note that Color
in the message type starts with a capital letter.
The rosmsg show <message type>
command displays the fields in a ROS message type. For the turtlesim/Color
type, the fields are integers:
Output of the preceding command is as follows:
The format of values designating colors red (r
), green (g
), and blue (b
) is unsigned, 8-bit integer.
To understand the message, it is necessary to find the message type. For example, turtlesim/Color
is a message type for turtlesim that has three elements that define the color of the background. For example, the red color in the background is defined by uint8 r
. This indicates that if we wish to modify the red value, an 8-bit unsigned integer is needed. The amount of red in the background is in the range of 0–255.
In general, the formats of numerical data include integers of 8, 16, 32, or 64 bits, floating point numbers, and other formats.
To determine the color mixture of red, green, and blue in the background of our turtle, use the rostopic echo [topic name]
command in the form, as follows:
Output of the preceding command is as follows:
Press Ctrl + C to stop the output.
The website describing the RGB Color Codes Chart and the meaning of the numerical color values can be found at http://www.rapidtables.com/web/color/RGB_Color.htm.
The chart explains how to mix the red, green, and blue (rgb) color values to achieve any desired color. The color values are parameters that can be changed.
A simple table can clarify the relationship between the topics and the messages for the /turtlesim
node:
The table of Topics and messages shows the topics, types, message formats, and data values for the two topics of the /turtlesim
node that we explored. The commands to determine the information are also shown in the table.
Move the turtle by publishing /turtle1/cmd_vel
Start the Master and execute the turtlesim_node
by typing:
In a second terminal window, issue the following command:
Once the turtle screen is visible, there are a number of ways to move the turtle around.
The turtlesim_node
subscribes to the /turtle1/cmd_vel
topic, so the turtle can be commanded to move by sending messages on this topic. First, determine the type of messages for the topic by typing:
which displays the message type as the Twist
message from the geometry_msgs
package:
Next, the format of the message can be determined with the command:
The output shows that the message format allows six floating-point values which determine the linear and angular velocity of the turtle:
For the turtle in turtlesim that moves in a 2D space, the only motion allowed is forward motion in the turtle's x direction or rotation about the turtle's z axis that would extend out from the screen.
To move the turtle in a circle, the command
causes motion forward at 2.0 meters/second as well as rotation at 1.8 radians/second. The command specifies the topic, type of message, the repeat option (-r
) and the data values of the velocities. The data arguments are actually in YAML syntax, which is described in the YAML Command Line documentation at http://wiki.ros.org/ROS/YAMLCommandLine.
Tip
The ROS and Ubuntu command-line commands have tab-completion capability. For the previous rostopic pub
command for example, much typing can be avoided by typing part of the command and hitting the Tab key. In particular, typing out the command up to the data values and hitting the Tab key will yield the format of the data that can be filled in by backspacing and entering the appropriate values.
Move the turtle using the keyboard or joystick
After the turtle screen is visible, type the command:
activates the keyboard control of the turtle with this output:
In Chapter 8, Controlling Your Robots with External Devices in the section Controlling Turtlesim with a custom game controller interface, code is given to allow control of the turtle with a game controller (joystick).
Parameter Server of Turtlesim
The Parameter Server maintains a dictionary of the parameters. Thus, the /turtlesim
node can read and write parameters held by the Parameter Server.
Use the help
option to determine the form of the rosparam
command:
Output of the preceding command is as follows:
rosparam list for the /turtlesim node
To list the parameters for the /turtlesim
node, we will use the following command:
Output of the preceding command is as follows:
Note that the last four parameters were created by invoking the Master with the roscore
command, as discussed previously. Also, the list defines the characteristics of the parameter but not the data value.
Change parameters for the color of the turtle's background
To change the background color parameters for turtlesim, let's change the turtle's background to red. To do this, make the blue and green data values equal to zero and saturate red = 255 using the rosparam set
command. Note that the clear option from rosservice
must be executed before the screen changes color.
The default turtle screen is blue. You can use rosparam get /
to show the data contents of the entire Parameter Server:
Output of the preceding command is as follows:
You can change the colors of the turtle's screen to a full red background using the rosparam set
commands:
You will see a red background on the turtle screen. To check the numerical results, use the rosparam get /
command.
ROS services to move turtle
Another capability of nodes is to provide what in ROS terms is called a service. This feature is used when a node requests information from another node. Thus, there is a two-way communication between the nodes.
You can check the turtle's pose using the /turtle1/pose
topic and the message type, /turtlesim/Pose
. Carefully note the different notations and meanings. To determine the message type, run the following command:
The output is as follows:
To determine the format and meaning of the fields in the message, type:
The output is as follows:
We can find the turtle's position, orientation in angle (theta
), and its velocity using the rostopic echo
command:
The output is as follows:
This command outputs the result continuously and is stopped by pressing the Ctrl + C keys. The result will show that the turtle is at the center of its screen with no rotation at angle zero and no movement since the velocities are zero.
The turtle can be moved using the rosservice
teleport option. The format of the command is rosservice call <service name><service arguments>
. The arguments here will be the turtle's position and orientation as x
, y
, and theta
. The turtle is moved to position [1, 1] with theta
= 0
by running the following command:
The result can be seen in the following screenshot:
The relative teleport option moves the turtle with respect to its present position. The arguments are [linear distance, angle]. Here, the rotation angle is zero. The command for relative movement is as follows:
Your turtle should now move to x=2 and y=1.