Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ROS Robotics By Example

You're reading from   ROS Robotics By Example Bring life to your robot using ROS robotic applications

Arrow left icon
Product type Paperback
Published in Jun 2016
Publisher Packt
ISBN-13 9781782175193
Length 428 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Toc

Table of Contents (12) Chapters Close

Preface 1. Getting Started with ROS FREE CHAPTER 2. Creating Your First Two-Wheeled ROS Robot (in Simulation) 3. Driving Around with TurtleBot 4. Navigating the World with TurtleBot 5. Creating Your First Robot Arm (in Simulation) 6. Wobbling Robot Arms Using Joint Control 7. Making a Robot Fly 8. Controlling Your Robots with External Devices 9. Flying a Mission with Crazyflie 10. Extending Your ROS Abilities Index

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 that finds and starts a requested node in a package
  • rosmsg that shows information about messages
  • rosservice that displays runtime information about nodes and can pass data between nodes in a request/response mode
  • rosparam that is used to get and set parameters (data) used by nodes

Starting turtlesim 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:

$ roscore

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.

rosrun command

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:

$ rosrun turtlesim turtlesim_node

You will see an output similar to this:

[ INFO] [1427212356.117628994]: Starting turtlesim with node name /turtlesim
[ INFO] [1427212356.121407419]: Spawning turtle [turtle1] at x=[5.544445], y=[5.544445], theta=[0.000000]

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:

rosrun command

Turtlesim 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.

Turtlesim nodes

In the third window, issue the rosnode command to determine information about any node. First, list the active nodes, using the following command:

$ rosnode list

The output is as follows:

/rosout
/turtlesim

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:

$ rosnode info /turtlesim

The output of the preceding command is as follows:

---------------------------------------------------------------------
Node [/turtlesim]
Publications:
 * /turtle1/color_sensor [turtlesim/Color]
 * /rosout [rosgraph_msgs/Log]
 * /turtle1/pose [turtlesim/Pose]
Subscriptions:
 * /turtle1/cmd_vel [unknown type]
Services:
 * /turtle1/teleport_absolute
 * /turtlesim/get_loggers
 * /turtlesim/set_logger_level
 * /reset
 * /spawn
 * /clear
 * /turtle1/set_pen
 * /turtle1/teleport_relative
 * /kill
contacting node http://Laptop-M1210:38993/ ...
Pid: 2364
Connections:
 * topic: /rosout
    * to: /rosout
    * direction: outbound
    * transport: TCPROS

The following diagram represents a graphical illustration of the relationship of the turtlesim node in elliptical shapes and topics in rectangular boxes:

Turtlesim nodes

/turtlesim node

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 of an unknown type. The type is unknown because another node that publishes on this topic has not yet been executed.
  • 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 this section.

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.

rostopic list

For the /turtlesim node, the topics are listed using the following command:

$ rostopic list

The output of the preceding command is as follows:

/rosout
/rosout_agg
/turtle1/cmd_vel
/turtle1/color_sensor
/turtle1/pose

rostopic type

The topic type can be determined for each topic by typing the following command:

$ rostopic type /turtle1/color_sensor
turtlesim/Color

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.

rosmsg list

The turtlesim package has two message types that are found with the following command:

$ rosmsg list | grep turtlesim

The output is as follows:

turtlesim/Color
turtlesim/Pose

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.

rosmsg show

The rosmsg show <message type> command displays the fields in a ROS message type. For the turtlesim/Color type, the fields are integers:

$ rosmsg show turtlesim/Color

Output of the preceding command is as follows:

uint8 r
uint8 g
uint8 b

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.

rostopic echo

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:

$ rostopic echo /turtle1/color_sensor

Output of the preceding command is as follows:

r: 69
g: 86
b: 255
---

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. An example, which is discussed later, will show you how to change the background color for the turtle. 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:

Topics and Messages for /turtlesim node

Topics name

Topic type

Message format

Message

$ rostopic list

$ rostopic type

[topic name]

$ rosmsg show

[topic type]

$ rostopic echo

[topic name]

/turtle1

/color_sensor

turtlesim/Color

uint8 r

uint8 g

uint8 b

r: 69

g: 86

b: 255

/turtle1/pose

turtlesim/Pose

float32 x

float32 y

float32 theta

float32 linear_velocity

float32 angular_velocity

x: 5.54444456

y: 5.54444456

theta: 0.0

linear_velocity: 0.0

angular_velocity: 0.0

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.

Parameter Server of turtlesim

The Parameter Server maintains a dictionary of the parameters that are used to configure the screen of turtlesim. We will see that these parameters are used to define the color of the background for the turtle. Thus, the turtlesim node can read and write parameters held by the Parameter Server.

rosparam help

Use the help option to determine the form of the rosparam command:

$ rosparam help

Output of the preceding command is as follows:

rosparam is a command-line tool for getting, setting, and deleting parameters from the ROS Parameter Server.   Commands:
  rosparam set       set parameter
  rosparam get       get parameter
  rosparam list      list parameter names
<Edited>

rosparam list for /turtlesim node

To list the parameters for the /turtlesim node, we will use the following command:

$ rosparam list

Output of the preceding code is as follows:

/background_r
/background_g
/background_b
/rosdistro
/roslaunch/uris/host_d125_43873__51759
/rosversion
/run_id

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

rosparam get

The default turtle screen is blue. You can use rosparam get / to show the data contents of the entire Parameter Server:

$ rosparam get /

Output of the preceding command is as follows:

background_b: 255
background_g: 86
background_r: 69
rosdistro: 'indigo
roslaunch:
  uris: {host_d125_43873__60512: 'http://D125-43873:60512/'}
rosversion: '1.11.13
run_id: 2429b792-d23c-11e4-b9ee-3417ebbca982

rosparam set

You can change the colors of the turtle's screen to a full red background using the rosparam set command:

$ rosparam set background_b 0
$ rosparam set background_g 0
$ rosparam set background_r 255
$ rosservice call /clear

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 the message type, run the following command:

$ rostopic type /turtle1/pose

The output is as follows:

turtlesim/Pose
$ rosmsg show turtlesim/Pose

The output is as follows:

float32 x
float32 y
float32 theta
float32 linear_velocity
float32 angular_velocity

We can find the turtle's position, orientation in angle (theta), and its velocity using the rostopic echo command:

$ rostopic echo /turtle1/pose

The output is as follows:

x: 5.544444561
y: 5.544444561
theta: 0.0
linear_velocity: 0.0
angular_velocity: 0.0

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.

rosservice call

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:

$ rosservice call /turtle1/teleport_absolute 1 1 0

The result can be seen in the following screenshot:

rosservice call

turtle after an absolute move

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:

$ rosservice call /turtle1/teleport_relative 1 0

Your turtle should now move to x=2 and y=1.

You have been reading a chapter from
ROS Robotics By Example
Published in: Jun 2016
Publisher: Packt
ISBN-13: 9781782175193
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image