Docsity

ROS - Basic Concepts, Manuais, Projetos, Pesquisas de Robótica e Sistemas de Autônomos

Toggle sidebar
bg1
ROS:
1. Fundamental concepts: Nodes and Topics:
•The first is the concept of a "ROS Node". A ROS node is a data or information
processing software unit. All functional requirements for a ROS application will be
implemented as ROS Nodes. Typically, a ROS Node is implemented in Python or
C++ and the idea of ROS nodes ensures that we can have functional modularity in
large ROS projects. In other words, ROS nodes are building blocks in the context of
a large software application.
•The second fundamental concept is that of a "ROS Topic". A ROS topic is an entity
that is used to transport information between nodes. This way multiple ROS nodes
can interact with each other by exchanging relevant information as required by the
ROS application.
•Here is a graphical representation of the concepts
of ROS Nodes and ROS Topics where two nodes
(in ellipses) are communicating with each other via
a ROS topic (in the rectangular box). We will revisit this graphical representation in
further detail in the next module. Now, in the real world, when we are tasked to work
with or design a ROS application, we will have to often deal with lots of ROS nodes
and topics.
•In such a scenario, it is important to first find out, which nodes are talking to each
other? If so, what topics are being passed around between nodes? Often, these are
the first questions we face as beginners in ROS, after we are done following the
instructions/tutorials on the ROS Wiki and started an example application.
•Typically, we face the situation of starting something up on a terminal, seeing a lot
of log messages and then we stare at the terminal screen wondering, Okay...now
what? Well, let's learn how to find out what's "actually" going on!
Remainder: Nodes: These are software processes that do 'stuff' (e.g. process data,
command hardware, execute algorithms). Nodes provide modularity to robotic projects that
use ROS. They are often written in C++ or Python. In this course, we will use Python to
write them. ROS is only compatible with Python 2.7.x. Topics: Transport information
between nodes, in the form of messages. In a real robot application you will often have to
deal with a large number of nodes and topics. It is important to know which nodes are
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Pré-visualização parcial do texto

Baixe ROS - Basic Concepts e outras Manuais, Projetos, Pesquisas em PDF para Robótica e Sistemas de Autônomos, somente na Docsity!

ROS:

1. Fundamental concepts: Nodes and Topics:

  • The first is the concept of a "ROS Node". A ROS node is a data or information processing software unit. All functional requirements for a ROS application will be implemented as ROS Nodes. Typically, a ROS Node is implemented in Python or C++ and the idea of ROS nodes ensures that we can have functional modularity in large ROS projects. In other words, ROS nodes are building blocks in the context of a large software application.
  • The second fundamental concept is that of a "ROS Topic". A ROS topic is an entity that is used to transport information between nodes. This way multiple ROS nodes can interact with each other by exchanging relevant information as required by the ROS application. - Here is a graphical representation of the concepts of ROS Nodes and ROS Topics where two nodes (in ellipses) are communicating with each other via a ROS topic (in the rectangular box). We will revisit this graphical representation in further detail in the next module. Now, in the real world, when we are tasked to work with or design a ROS application, we will have to often deal with lots of ROS nodes and topics.
  • In such a scenario, it is important to first find out, which nodes are talking to each other? If so, what topics are being passed around between nodes? Often, these are the first questions we face as beginners in ROS, after we are done following the instructions/tutorials on the ROS Wiki and started an example application.
  • Typically, we face the situation of starting something up on a terminal, seeing a lot of log messages and then we stare at the terminal screen wondering, Okay...now what? Well, let's learn how to find out what's "actually" going on! Remainder : Nodes: These are software processes that do 'stuff' (e.g. process data, command hardware, execute algorithms). Nodes provide modularity to robotic projects that use ROS. They are often written in C++ or Python. In this course, we will use Python to write them. ROS is only compatible with Python 2.7.x. Topics: Transport information between nodes, in the form of messages. In a real robot application you will often have to deal with a large number of nodes and topics. It is important to know which nodes are

talking to each other, and what topics are being used to pass the information (messages) between nodes.

1.1 Nodes: find information about ROS Nodes running

  • First and foremost, we setup the ROS environment by sourcing the workspace setup files. Then, we have several ways to proceed forward. In this module we will learn three commands that help us find our way around.
  • The first command is, " rosnode list ". This command prints a list of all running nodes on the terminal. The next command is " rqt_graph ", which provides a visual representation of the different nodes and the way they interact with each other. And finally, the command " **rosnode info ** ". This command prints node specific information on the terminal.
  • As the name suggests, the first command rosnode list , lists all actively running nodes on the terminal. The information is displayed as a list of node names and these names can be used to find further information about a specific node.
  • Instead of displaying the list of running nodes on a terminal, we can also have a graphical representation of the same using the " rqt_graph " command. This command generates a graphical representation of the various nodes that are running and also those that are interacting with each other via topics. Note that, there could be a stand-alone node that does not interact with any other node, but this is not very common. In the generated graphical representation, nodes are represented within ellipses and the topics are represented within rectangles.
  • Finally, the last command which can be used to obtain additional information of a running ROS node is, **rosnode info **. This command prints node specific information on the terminal such as the topics that a node is interacting with and the linux process identifier associated with the node. There is quite a lot of information that we see here.
  • I assure you we will revisit this again in a future module to understand the various sections of information that we see here. For now, what is important to note is, the information regarding the topic interaction is very useful in the context of this course and hence we will focus on that in the remaining modules. Particularly, we will learn that a node can "publish to (or write to)" a topic and "subscribe to (or read from)" a topic.
  • So far, we focused on finding information regarding one of the fundamental entities in a ROS application, namely ROS nodes. Let us now turn our attention towards finding information about the topics that are being exchanged between actively running nodes. Remainder: Don't forget to source your workspace if you want to use ROS commands! If you've followed the procedure under the Course Setup section correctly, you only need to execute the following in the shell: $ source $HOME/hrwros_ws/devel/setup.bash Summary: There are three main ways to inspect a ROS node:
  • Following this, information regarding which nodes are communicating via this topic is published. In this case, we see that /node_1 and /node_2 are interacting with each other via /topic_1. And the last command **rostopic echo **. This command continuously prints the contents of the specified topic on to the terminal screen. In this example, the contents of /topic_1 is a string which has the content "My first ROS topic". It is relevant to note that the printing continues indefinitely until this command is stopped by pressing Ctrl+C.
  • A quick recap of what we have learned thus far. Three utility commands to find information regarding nodes once we have started a ROS application namely: **rosnode list, rqt_graph, and rosnode info **. Then, three more utility commands for finding information on ROS topics namely: rostopic list, rostopic **info and rostopic echo **. For more such utility commands you can refer to the ROS Cheatsheet in the supplementary documents section of the course. Lastly, a very important detail. There is always a forward slash before a node name or a topic name. This is a ROS convention. So! Whenever you are faced with a situation of having installed and started up a ROS application or a ROS demo, you can now keep calm and say, "I got this! I know how to find my way around!". Reminder: ROS topics transport information between nodes. This information is organized as a data structure and can have different data types. Topics can be identified by their name and their type. You can think of topics as strongly typed message buses. Any node can connect to it to send or receive data, as long as they are the right type. As with nodes, there are three ways to inspect topics: Don't forget to source your workspace if you want to use ROS commands! If you've followed the procedure under the Course Standards section correctly, you only need to execute the following in the shell: $ source $HOME/hrwros_ws/devel/setup.bash
  • Display a list of all topics that are currently being exchanged between active nodes list
  • Display information about the data structure of a specific topic rostopic info
  • Print the current content of a topic in the terminal rostopic echo Important detail: Do not forget the "/" before node and topic names!

2 ROS Application:

So far, we learned how to find our way around, "given" a ROS application. But this is not always going to be the case in the real world, as we will soon be requiredto develop our own ROS applications. So, what is a ROS application? What components constitute a ROS application? How do we build our own ROS application? We will learn how to do so in this module.

  • Particularly, we will see that there are 4 fundamental types of ROS nodes that can be used to build a ROS application namely, Publishers, Subscribers, Services and Actions. First, we

will focus on publisher and subscriber" ROS nodes. The words publisher and subscriber do seem somewhat familiar already to us although we didn't explicitly study them yet!

  • Uhmmm....We saw these words in the previous module when we learned the command rostopic info
  • . Or, in a slightly different form with the same meaning in the command rosnode info . Hmmm...let's get to know them better now! Reminder: It's time to build your own ROS application structure! There are four fundamental types of ROS nodes that can be used to build a ROS application:
  • Publishers
  • Subscribers
  • Services
  • Actions Please keep in mind, that here we refer to Services and Actions as fundamental types of nodes. That is, we are referring to nodes that use services and actions for communication. Publishers, Subscribers, Services and Actions are used by ROS nodes to communicate between each other. First, we will focus on Publisher and Subscriber nodes.

2.1 APPLICATION NODES: PUBLISHER & SUBSCRIBER:

We will first start with the publisher node. We studied that nodes are information or data processing units. Sure, but where does the information come from? Who generates the information?

  • A ROS node that generates information is called a Publisher. A publisher uses one or more ROS topics to communicate the generated information to other nodes. In the context of ROS-based robotic application, these nodes are typically used to process raw sensory information, for example, from a camera, motor encoders, distance sensors and so on. Once the raw sensory information is processed according to the needs of the desired application, it is published to a ROS topic.
  • Hence the name, publisher. With this basic idea, let's now look at the example output of the rosnode info /node_1 command. We first see that the node name is displayed as /node_1. Then, in the publications section, we can see the topics that the node is publishing and the associated topic type.
  • A little further below in the output, we can also see a confirmation of the same in the Connections section. It can be seen that /topic_1 has an " outbound " direction to / node_2. In other words, /node_1 writes to /topic_1. We have seen information about a publisher node, but how do we implement one?We will now look at an example code for a ROS publisher.
  • Before we proceed, a small disclaimer: In all explanations where there is code, myself along with other instructors will only explain the relevant portions in the context of this course. The portions we skip in some code sections are of course

Subsequently, the subscriber node is initialized with the rospy.init_node() function. Following the node initialization, the subscriber object is created using the rospy.Subscriber() function call. This function takes the topic to which the node subscribes to, the topic type and the subscriber callback function as arguments.

  • And finally, the ROS and python specific definitions and function calls. Alright, we have now learned about two important building blocks of a ROS application, namely the publisher node and a subscriber node. The publisher node "writes to" one or more ROS topics. And the subscriber node "reads from" one or more ROS topics with each subscription processed via a respective callback function. Now we know how to write publisher and subscriber nodes with standard ROS topic types. Reminder: Publisher Nodes A ROS node that generates information is called a publisher. A publisher sends information to nodes via topics. With robotics often these publishers are connected with sensors like cameras, encoders, etc. If you use rosnode info you can see to which topics a node is connected, and if these are outbound or inbound connections. Subscriber Nodes A ROS node that receives information is called a subscriber. It's subscribed to information a topic and uses topic callback functions to process the received information. With robotics, subscribers typically monitor system states such as triggering an alert when the robot reaches joint limits. You can again use rosnode info to find information about a subscriber node. Don't forget to source your workspace! PUBLISHER:

Node to publish a string topic.

import rospy from std_msgs.msg import String def simplePublisher(): simple_publisher = rospy.Publisher('topic_1', String, queue_size = 10) rospy.init_node('node_1', anonymous = False) rate = rospy.Rate(10)

The string to be published on the topic.

topic1_content = "my first ROS topic" while not rospy.is_shutdown(): simple_publisher.publish(topic1_content) rate.sleep() if name== 'main': try: simplePublisher() except rospy.ROSInterruptException: pass

SUBSCRIBER:

Node to subscribe to a string and print the string on terminal.

import rospy from std_msgs.msg import String

Topic callback function.

def stringListenerCallback(data): rospy.loginfo(' The contents of topic1: %s', data.data) def stringListener(): rospy.init_node('node_2' , anonymous = False) rospy.Subscriber('topic_1' , String, stringListenerCallback)

spin() simply keeps python from exiting until this node is stopped

rospy.spin() if name == 'main': stringListener()

2.2 ROS File System:

Explored some utility commands for finding information regarding a running ROS application. In order to be able to build our own application, we will have to write our own code files. Where do we put these files? How are they organized? Let's find out!

  • Let me begin with introducing two terms used in a ROS file system. First, a ROS workspace, in particular, a catkin workspace. And within the context of a ROS workspace, there is, a source space, a devel space, a build space and something we will not focus on in this course, an install space. And the second term is: ROS package. Which can be used to organize different functional modules in a ROS software application. Let's understand these terms one by one.
  • We will first learn what is a catkin workspace in ROS. First, a ROS workspace is a folder in your computer where you can organize various ROS project files. Second, catkin is a build tool that compiles the source code files in a project into executable binaries. Well, it does a lot more, but this understanding is sufficient in the context of this course. So, a catkin workspace is a ROS workspace that uses catkin as a build tool. Let's now look at an example catkin workspace and its contents before we create one of our own.
  • Here, we see a typical folder structure within a ROS catkin workspace. All "your" code files go in the source folder, written as src and called as the source space. Code from others can also go in here, ONLY if "you" are changing it. Otherwise, simply install the corresponding binaries and keep your source space clean! The log files containing debug information associated with the various software modules in a project are in the logs folder, called the log space.
  • The files for setting up a project specific ROS environment are in the devel folder. Along with these files, the devel folder also contains the executables of your source files for development and testing. The build space used by the build tool catkin to

2.2.1 SRC Folder: Now that we know what's inside a ROS workspace and how to create a catkin workspace for a ROS application, let's go one step further into the source space.

  • The source space contains ROS packages. All the code that we write or some of those that we use from others with some changes, are organized in ROS packages. Here is an example of a few ROS packages within a source workspace. Notice that all the ROS packages starting with the name "hrwros" are organized in the folder hrwros. This is only a matter of convenience and file organization sanity. hrwros is just a folder. What's inside that folder are ROS packages. Now, one of the major benefit of working with ROS is that we can re-use ROS packages developed by other people or organizations, if they are useful for our application.
  • These three packages have been modified from the "gilbreth" ROS package from the South West Research Institute in United States. And then we have some ROS packages developed by the course instructors. Remember, only put those ROS packages in your source space, if you are modifying it.
  • Not everything you need goes in a source space! If you are not modifying it, you can simply install the binaries of the package and we will soon see how to do so.
  • Let's now dive in the hrwros_week1 ROS package. The package contains the implementations of all functionalities that we develop during this week of our course. As we go along the course, we will develop one ROS package per week. Notice that, there are two files namely, CMakeLists.txt and package.xml. The presence of these two files are what makes a normal folder into a ROS package.
  • We will cover the contents of package.xml very shortly and we will cover the contents of CMakeLists file at a later point when it is necessary.
  • Normally, t he src folder is used for functionality implemented as C++ source files. And the scripts folder is used for functionality implemented as Python scripts. Since, we will use only Python programming in this course, all our magic will happen in the scripts folders of our ROS packages.
  • Now let's find out how to create a new ROS package. A new ROS package can be created using the command catkin_create_pkg . Just as a side note, for those of you who are unfamiliar with the way the commands are shown, the text in the angle brackets needs to be replaced with the actual name you want to use and the angle brackets are to be removed.
  • For example, here we see the output after creating a new ROS package. First and foremost, we can see that the catkin_create_pkg command is executed inside the folder hrwros in the source space.
  • Then, we call our new ROS package hrwros_week2. And then we state that the new package depends on the std_msgs package. This simply means, we will be using some modules of the std_msgs package within hrwros_week2 package. All dependencies of a newly created ROS package are automatically listed in the package.xml file inside the ROS package folder.
  • In case we realize that our application depends on more ROS packages that we did not foresee at the time of creation, we can simply add that to this file.
  • A couple of minutes ago, I told that you can simply install ROS packages that your application depends on, if you are not modifying anything in those packages. Here, we will learn how to do so.
  • And we saw just now that package.xml contains a list of ROS packages that your newly created ROS package depends on. With the command rosdep install package name, you automatically install all dependencies of your new package.
  • An even better option would be to do this for all your ROS packages at once. This can also be done (with a slightly more detailed) rosdep command. And I once again say, install ROS package dependencies as much as you can. You don't need the source files of all dependencies of your ROS package.
  • Finally, a quick sneak peek into the devel space. The devel space contains the binary executables of the functionality coded in the source space. Also, the devel space houses the most important files to setup our project specific ROS environment on our linux terminals. These are the setup files. It is very common to use the bash shell in linux terminals. And hence, the command to source the project specific setup files from the devel space. Alright, we have now acquainted ourselves with the ROS file system. Let's now make use of this knowledge in creating our own ROS application. Remember: ROS packages in the 'src' space. In ROS, software is organised in ROS packages. A ROS package typically contains the following things:
  • CMakeList.txt
  • package.xml(These two files indicate that the folder is a ROS package file. More on these two later in the course)
  • scripts/(This folder contains all Python scripts. We will only use Python in this course)
  • src/(This folder contains all C++ source files. We will not use these in this course) To create a new ROS package, we will use catkin: $ cd /src $ catkin_create_pkg hrwros_week2 std_msgs Please keep in mind that you don't need to place the source files of any dependencies, such as std_msgs in the line above. Instead, you can simply install their binaries with the rosdep command: $ install You can also install all ROS package dependencies in one command: $ cd /src $ rosdep install --from-paths. --ignore-src -y src is not the only space in your workspace: there is also the 'devel' space. This contains all binary executables from src.

field for the sensor part number. This is implemented in code in the SensorInformation.msg file as shown in this code snippet.

  • In the first line we define the sensor message type provided by ROS. In the second line we define the string type for manufacturer name and in the third line we define the unsigned integer type for the sensor part number! And, we are done! Wait, what? How do we know what types of ROS messages are supported for sensors? What is in those message types? How do we use this in code? Reminder: Nodes can process and share information through topics. The information they pass through these topics needs to be structured in some way, to make it actually usable. Such a structure is known as a data structure. In ROS, we can abstract these data structures as ROS message types. Common data structures in ROS are for example floats, integers, and strings. In ROS, we can easily combine multiple data structures using derived message types. For example, to represent a position in 3D space we will need 3 floating point values: X, Y and Z. The derived message type will then be a struct of three floating point numbers: {float x, float y, float z}. These (derived) message types are defined in message files. These message files are typically located in /msg, with the filename .msg. For example, look in the $HOME/hrwros_ws/src/hrwros/hrwros_msgs/msg folder for some examples. Example: Ultrasound distance sensor. We want to construct a new message type SensorInformation It should contain:
  • message type for interfacing with distance sensors
  • A string containing the manufacturer name
  • An unsigned integer containing the sensor part number We would create the following file: $HOME/hrwros_ws/src/hrwros/hrwros_msgs/msg/SensorInformation.msg. It will contain the following: sensor_msgs/Range sensor_data string maker_name uint32 part_number We can see something really interesting here: We use an already derived message, sensor_msgs/Range, and simply add a string and an integer to it. So we can create new message types using already existing derived message types! This idea of stacking is really useful in ROS, since you can easily re-use what already exists. So in the above example, the sensor_msgs/Range already contains everything we need from the range sensor itself, and we only add the maker name and the part number.