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, Second Edition

You're reading from   ROS Robotics By Example, Second Edition Learning to control wheeled, limbed, and flying robots using ROS Kinetic Kame

Arrow left icon
Product type Paperback
Published in Nov 2017
Publisher Packt
ISBN-13 9781788479592
Length 484 pages
Edition 2nd Edition
Languages
Tools
Concepts
Arrow right icon
Authors (3):
Arrow left icon
Dr. Thomas L. Harman Dr. Thomas L. Harman
Author Profile Icon Dr. Thomas L. Harman
Dr. Thomas L. Harman
Lentin Joseph Lentin Joseph
Author Profile Icon Lentin Joseph
Lentin Joseph
Carol Fairchild Carol Fairchild
Author Profile Icon Carol Fairchild
Carol Fairchild
Arrow right icon
View More author details
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. Controlling Baxter with MATLAB© 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: Finds and starts a requested node in a package
  • rosmsg: Shows information about messages
  • rosservice: Displays runtime information about nodes and can pass data between nodes in a request/response mode
  • rosparam: 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] [1489616730.714683337]: Starting turtlesim with node name /turtlesim
[ INFO] [1489616730.727083554]: 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://D158-45929:38895/ ...
Pid: 29981
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 the 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. 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.

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
/statistics
/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. 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 the /turtlesim node

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

Move the turtle by publishing /turtle1/cmd_vel

Start the Master and execute the turtlesim_node by typing:

$ roscore

In a second terminal window, issue the following command:

$ rosrun turtlesim turtlesim_node

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:

$ rostopic type /turtle1/cmd_vel

which displays the message type as the Twist message from the geometry_msgs package:

geometry_msgs/Twist

Next, the format of the message can be determined with the command:

$ rosmsg show geometry_msgs/Twist

The output shows that the message format allows six floating-point values which determine the linear and angular velocity of the turtle:

geometry_msgs/Vector3 linear
  float64 x
  float64 y
  float64 z
geometry_msgs/Vector3 angular
  float64 x
  float64 y
  float64 z

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

$ rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'

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:

$ rosrun turtlesim turtle_teleop_key

activates the keyboard control of the turtle with this output:

        
Reading from keyboard
---------------------------
Use arrow keys to move the turtle.
Up arrow       Turtle up
Down arrow     Turtle down
Right arrow    Rotate CW
Left  arrow    Rotate CCW

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.

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 the /turtlesim node

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

$ rosparam list

Output of the preceding command is as follows:

/background_b
/background_g
/background_r
/rosdistro
/roslaunch/uris/host_d158_45929__34807
/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 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.

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: 'kinetic

  '
roslaunch:
uris: {host_d158_45929__34807: 'http://D158-45929:34807/'}
rosversion: '1.12.7

  '
run_id: 9c3776b4-09cd-11e7-bb39-1866da2351d7

rosparam set

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

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

$ rostopic type /turtle1/pose

The output is as follows:

turtlesim/Pose

To determine the format and meaning of the fields in the message, type:

$ 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, Second Edition - Second Edition
Published in: Nov 2017
Publisher: Packt
ISBN-13: 9781788479592
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