In this article by Anil Mahtani, Luis Sánchez, Enrique Fernández, and Aaron Martinez, authors of the book Effective Robotics Programming with ROS, Third Edition, you will learn the structure of ROS and the parts it is made up of. Furthermore, you will start to create nodes and packages and use ROS with examples using Turtlesim.
The ROS architecture has been designed and divided into three sections or levels of concepts:
(For more resources related to this topic, see here.)
The first level is the Filesystem level. In this level, a group of concepts are used to explain how ROS is internally formed, the folder structure, and the minimum number of files that it needs to work.
The second level is the Computation Graph level where communication between processes and systems happens. In this section, we will see all the concepts and mechanisms that ROS has to set up systems, handle all the processes, and communicate with more than a single computer, and so on.
The third level is the Community level, which comprises a set of tools and concepts to share knowledge, algorithms, and code between developers. This level is of great importance; as with most open source software projects, having a strong community not only improves the ability of newcomers to understand the intricacies of the software as well as solve the most common issues, it is also the main force driving its growth.
The ROS Filesystem is one of the strangest concepts to grasp when starting to develop projects in ROS, but with time and patience, the reader will easily become familiar with it and realize its value for managing projects and its dependencies. The main goal of the ROS Filesystem is to centralize the build process of a project while at the same time provide enough flexibility and tooling to decentralize its dependencies.
Similar to an operating system, an ROS program is divided into folders, and these folders have files that describe their functionalities:
In the following screenshot, you can see the content of the turtlesim package. What you see is a series of files and folders with code, images, launch files, services, and messages. Keep in mind that the screenshot was edited to show a short list of files; the real package has more:
In general terms, the workspace is a folder which contains packages, those packages contain our source files and the environment or workspace provides us with a way to compile those packages. It is useful when you want to compile various packages at the same time and it is a good way to centralize all of our developments.
A typical workspace is shown in the following screenshot. Each folder is a different space with a different role:
You have two options with regard to building packages with catkin. The first one is to use the standard CMake workflow. With this, you can compile one package at a time, as shown in the following commands:
$ cmakepackageToBuild/
$ make
If you want to compile all your packages, you can use the catkin_make command line, as shown in the following commands:
$ cd workspace
$ catkin_make
Both commands build the executable in the build space directory configured in ROS.
Another interesting feature of ROS is its overlays. When you are working with a package of ROS, for example, turtlesim, you can do it with the installed version, or you can download the source file and compile it to use your modified version.
ROS permits you to use your version of this package instead of the installed version. This is very useful information if you are working on an upgrade of an installed package.
Usually, when we talk about packages, we refer to a typical structure of files and folders. This structure looks as follows:
To create, modify, or work with packages, ROS gives us tools for assistance, some of which are as follows:
To move between packages and their folders and files, ROS gives us a very useful package called rosbash, which provides commands that are very similar to Linux commands. The following are a few examples:
Every package must contain a package.xml file, as it is used to specify information about the package. If you find this file inside a folder, it is very likely that this folder is a package or a metapackage.
If you open the package.xml file, you will see information about the name of the package, dependencies, and so on. All of this is to make the installation and the distribution of these packages easy.
Two typical tags that are used in the package.xml file are <build_depend> and <run _depend>.
The <build_depend> tag shows which packages must be installed before installing the current package. This is because the new package might use functionality contained in another package.
The <run_depend> tag shows the packages that are necessary to run the code of the package. The following screenshot is an example of the package.xml file:
As we have shown earlier, metapackages are special packages with only one file inside; this file is package.xml. This package does not have other files, such as code, includes, and so on.
Metapackages are used to refer to others packages that are normally grouped following a feature-like functionality, for example, navigation stack, ros_tutorials, and so on.
You can convert your stacks and packages from ROS Fuerte to Kinetic and catkin using certain rules for migration. These rules can be found at http://wiki.ros.org/catkin/migrating_from_rosbuild.
In the following screenshot, you can see the content from the package.xml file in the ros_tutorialsmetapackage. You can see the <export> tag and the <run_depend> tag. These are necessary in the package manifest, which is also shown in the following screenshot:
If you want to locate the ros_tutorialsmetapackage, you can use the following command:
$ rosstack find ros_tutorials
The output will be a path, such as /opt/ros/kinetic/share/ros_tutorials.
To see the code inside, you can use the following command line:
$ vim /opt/ros/kinetic/ros_tutorials/package.xml
Remember that Kinetic uses metapackages, not stacks, but the rosstack find command-line tool is also capable of finding metapackages.
ROS uses a simplified message description language to describe the data values that ROS nodes publish. With this description, ROS can generate the right source code for these types of messages in several programming languages.
ROS has a lot of messages predefined, but if you develop a new message, it will be in the msg/ folder of your package. Inside that folder, certain files with the .msg extension define the messages.
A message must have two main parts: fields and constants. Fields define the type of data to be transmitted in the message, for example, int32, float32, and string, or new types that you have created earlier, such as type1 and type2. Constants define the name of the fields.
An example of an msg file is as follows:
int32 id
float32vel
string name
In ROS, you can find a lot of standard types to use in messages, as shown in the following table list:
Primitive type | Serialization | C++ | Python |
bool (1) | unsigned 8-bit int | uint8_t(2) | bool |
int8 | signed 8-bit int | int8_t | int |
uint8 | unsigned 8-bit int | uint8_t | int(3) |
int16 | signed 16-bit int | int16_t | int |
uint16 | unsigned 16-bit int | uint16_t | int |
int32 | signed 32-bit int | int32_t | int |
uint32 | unsigned 32-bit int | uint32_t | int |
int64 | signed 64-bit int | int64_t | long |
uint64 | unsigned 64-bit int | uint64_t | long |
float32 | 32-bit IEEE float | float | float |
float64 | 64-bit IEEE float | double | float |
string | ascii string (4) | std::string | string |
time | secs/nsecs signed 32-bit ints | ros::Time | rospy.Time |
duration | secs/nsecs signed 32-bit ints | ros::Duration | rospy.Duration |
A special type in ROS is the header type. This is used to add the time, frame, and sequence number. This permits you to have the messages numbered, to see who is sending the message, and to have more functions that are transparent for the user and that ROS is handling.
The header type contains the following fields:
uint32seq
time stamp
string frame_id
You can see the structure using the following command:
$ rosmsg show std_msgs/Header
Thanks to the header type, it is possible to record the timestamp and frame of what is happening with the robot.
ROS provides certain tools to work with messages. The rosmsg tool prints out the message definition information and can find the source files that use a message type.
In upcoming sections, we will see how to create messages with the right tools.
ROS uses a simplified service description language to describe ROS service types. This builds directly upon the ROS msg format to enable request/response communication between nodes. Service descriptions are stored in .srv files in the srv/ subdirectory of a package.
To call a service, you need to use the package name, along with the service name; for example, you will refer to the sample_package1/srv/sample1.srv file as sample_package1/sample1.
Several tools exist to perform operations on services. The rossrv tool prints out the service descriptions and packages that contain the .srv files, and finds source files that use a service type.
If you want to create a service, ROS can help you with the service generator. These tools generate code from an initial specification of the service. You only need to add the gensrv() line to your CMakeLists.txt file.
In upcoming sections, you will learn how to create your own services.
ROS creates a network where all the processes are connected. Any node in the system can access this network, interact with other nodes, see the information that they are sending, and transmit data to the network:
The basic concepts in this level are nodes, the master, Parameter Server, messages, services, topics, and bags, all of which provide data to the graph in different ways and are explained in the following list:
In the following diagram, you can see the graphic representation of this level. It represents a real robot working in real conditions. In the graph, you can see the nodes, the topics, which node is subscribed to a topic, and so on. This graph does not represent messages, bags, Parameter Server, and services. It is necessary for other tools to see a graphic representation of them. The tool used to create the graph is rqt_graph.
These concepts are implemented in the ros_comm repository.
Nodes are executable that can communicate with other processes using topics, services, or the Parameter Server. Using nodes in ROS provides us with fault tolerance and separates the code and functionalities, making the system simpler.
ROS has another type of node called nodelets. These special nodes are designed to run multiple nodes in a single process, with each nodelet being a thread (light process). This way, we avoid using the ROS network among them, but permit communication with other nodes. With that, nodes can communicate more efficiently, without overloading the network. Nodelets are especially useful for camera systems and 3D sensors, where the volume of data transferred is very high.
A node must have a unique name in the system. This name is used to permit the node to communicate with another node using its name without ambiguity. A node can be written using different libraries, such as roscpp and rospy; roscpp is for C++ and rospy is for Python. Throughout we will use roscpp.
ROS has tools to handle nodes and give us information about it, such as rosnode. The rosnode tool is a command-line tool used to display information about nodes, such as listing the currently running nodes. The supported commands are as follows:
A powerful feature of ROS nodes is the possibility of changing parameters while you start the node. This feature gives us the power to change the node name, topic names, and parameter names. We use this to reconfigure the node without recompiling the code so that we can use the node in different scenes.
An example of changing a topic name is as follows:
$ rosrun book_tutorials tutorialX topic1:=/level1/topic1
This command will change the topic name topic1 to /level1/topic1. To change parameters in the node, you can do something similar to changing the topic name. For this, you only need to add an underscore (_) to the parameter name; for example:
$ rosrun book_tutorials tutorialX _param:=9.0
The preceding command will set param to the float number 9.0.
Bear in mind that you cannot use names that are reserved by the system. They are as follows:
Topics are buses used by nodes to transmit data. Topics can be transmitted without a direct connection between nodes, which means that the production and consumption of data is decoupled. A topic can have various subscribers and can also have various publishers, but you should be careful when publishing the same topic with different nodes as it can create conflicts.
Each topic is strongly typed by the ROS message type used to publish it, and nodes can only receive messages from a matching type. A node can subscribe to a topic only if it has the same message type.
The topics in ROS can be transmitted using TCP/IP and UDP. The TCP/IP-based transport is known as TCPROS and uses the persistent TCP/IP connection. This is the default transport used in ROS.
The UDP-based transport is known as UDPROS and is a low-latency, lossy transport. So, it is best suited to tasks such as teleoperation.
ROS has a tool to work with topics called rostopic. It is a command-line tool that gives us information about the topic or publishes data directly on the network. This tool has the following parameters:
We will learn to use this command-line tool in upcoming sections.
When you need to communicate with nodes and receive a reply, in an RPC fashion, you cannot do it with topics; you need to do it with services.
Services are developed by the user, and standard services don't exist for nodes. The files with the source code of the services are stored in the srv folder.
Similar to topics, services have an associated service type that is the package resource name of the .srv file. As with other ROS filesystem-based types, the service type is the package name and the name of the .srv file.
ROS has two command-line tools to work with services: rossrv and rosservice. With rossrv, we can see information about the services' data structure, and it has exactly the same usage as rosmsg.
With rosservice, we can list and query services. The supported commands are as follows:
A node publishes information using messages which are linked to topics. The message has a simple structure that uses standard types or types developed by the user.
Message types use the following standard ROS naming convention; the name of the package, then /, and then the name of the .msg file. For example, std_msgs/ msg/String.msg has the std_msgs/String message type.
ROS has the rosmsg command-line tool to get information about messages. The accepted parameters are as follows:
A bag is a file created by ROS with the .bag format to save all of the information of the messages, topics, services, and others. You can use this data later to visualize what has happened; you can play, stop, rewind, and perform other operations with it.
The bag file can be reproduced in ROS just as a real session can, sending the topics at the same time with the same data. Normally, we use this functionality to debug our algorithms.
To use bag files, we have the following tools in ROS:
The ROS master provides naming and registration services to the rest of the nodes in the ROS system. It tracks publishers and subscribers to topics as well as services. The role of the master is to enable individual ROS nodes to locate one another. Once these nodes have located each other, they communicate with each other in a peer-to-peer fashion. You can see in a graphic example the steps performed in ROS to advertise a topic, subscribe to a topic, and publish a message, in the following diagram:
The master also provides Parameter Server. The master is most commonly run using the roscore command, which loads the ROS master, along with other essential components.
Parameter Server is a shared, multivariable dictionary that is accessible via a network. Nodes use this server to store and retrieve parameters at runtime.
Parameter Server is implemented using XMLRPC and runs inside the ROS master, which means that its API is accessible via normal XMLRPC libraries. XMLRPC is a Remote Procedure Call (RPC) protocol that uses XML to encode its calls and HTTP as a transport mechanism.
Parameter Server uses XMLRPC data types for parameter values, which include the following:
ROS has the rosparam tool to work with Parameter Server. The supported parameters are as follows:
The ROS Community level concepts are the ROS resources that enable separate communities to exchange software and knowledge. These resources include the following:
This article provided you with general information about the ROS architecture and how it works. You saw certain concepts and tools of how to interact with nodes, topics, and services.
Remember that if you have queries about something, you can use the official resources of ROS from http://www.ros.org. Additionally, you can ask the ROS Community questions at http://answers.ros.org.