ROS communication mechanism

ROS is a distributed framework that provides users with communication services between multiple nodes (processes). All software functions and tools are built on this distributed communication mechanism, so the communication mechanism of ROS is the lowest and most core technology. . In most application scenarios, although we do not need to pay attention to the underlying communication implementation mechanism, understanding its related principles will definitely help us
use ROS better during the development process. The following is an introduction to the three core communication mechanisms of ROS.

Topic communication mechanism

Topics are used most frequently in ROS, and their communication models are also relatively complex.
There are two nodes in ROS:

One is the publisher Talker and
the other is the subscriber Listener.

The two nodes publish and subscribe to the same topic respectively. There is no mandatory requirement for the startup sequence. It is assumed here that Talker is started first. The detailed process of establishing communication can be analyzed in seven steps as shown in the figure.

Insert image description here
1. Talker registration
Talker is started, and uses RPC to register the publisher's information with ROS Master through port 1234, including the topic name of the published message; ROS Master will add the node's registration information to the registration list.

2. Listener registration
Listener starts, and also registers subscriber information with ROS Master through RPC, including the name of the topic that needs to be subscribed.

3.ROS Master performs information matching

The Master searches from the registration list based on the Listener's subscription information. If no matching publisher is found, it waits for the publisher to join; if the matching publisher information is found, the Talker's RPC address information is sent to the Listener through RPC.

4. Listener sends a connection request.
Listener receives the Talker address information sent back by Master, tries to send a connection request to Talker through RPC, and transmits the subscribed topic name, message type and communication protocol (TCP/UDP).

5. Talker confirms the connection request.
After Talker receives the connection request sent by Listener, it continues to confirm the connection information to Listener through RPC, which includes its own TCP address information.

6.Listener tries to establish a network connection with Talker

After receiving the confirmation message, the Listener uses TCP to try to establish a network connection with the Talker.

7. Talker publishes data to Listener.
After successfully establishing the connection, Talker starts sending topic message data to Listener.
From the above analysis, we can find that the communication protocols used in the first five steps are all RPC, and TCP is used in the final process of publishing data.
ROS Master plays an important role in the process of establishing connections between nodes, but does not participate in the final data transmission between nodes . After the nodes establish a connection, you can turn off the ROS Master. Data transmission between nodes will not be affected, but other nodes will not be able to join the network between the two nodes.

2. Service communication mechanism

Service is a communication mechanism with responses. The communication principle is shown in Figure 2-11. Compared with topic communication, it reduces the RPC communication between Listener and Talker.

Insert image description here
1. Talker registration
Talker starts, and uses RPC to register the publisher's information with ROS Master through port 1234, including the provided service name; ROS Master will add the node's registration information to the registration list.

2. Listener registration
Listener starts, and also registers subscriber information with ROS Master through RPC, including the service name to be found.

3.ROS Master performs information matching

The Master searches from the registration list based on the Listener's subscription information. If no matching service provider is found, it waits for the provider of the service to join; if the matching service provider information is found, the Talker's TCP address is sent to the Listener through RPC. information.

4. Listener establishes a network connection with Talker.
After receiving the confirmation message, Listener uses TCP to try to establish a network connection with Talker and sends the service request data.

5. Talker publishes service response data to Listener.
After Talker receives the service request and parameters, it starts to execute the service function. After the execution is completed, it sends response data to Listener.


Parameter management mechanism

Parameters are similar to global variables in ROS and are managed by ROS Master. Its communication mechanism is relatively simple and does not involve TCP/UDP communication.

Insert image description here

1. Talker sets variables.
Talker uses RPC to send parameter setting data to ROS Master, including parameter names and parameter values; ROS Master will save parameter names and parameter values ​​to the parameter list.

2. Listener queries parameter values.
Listener sends a parameter search request to ROS Master through RPC, including the parameter name it is looking for.

3. ROS Master sends parameter values ​​to Listener.
Master searches from the parameter list according to Listener's search request. After finding the parameters, it uses RPC to send the parameter values ​​to Listener.

What needs to be noted here is that if the Talker updates the parameter value to the Master, the Listener cannot know that the parameter value has been updated without re-querying the parameter value. Therefore, in many application scenarios, a dynamic parameter update mechanism is needed.

Guess you like

Origin blog.csdn.net/hai411741962/article/details/133383111