Introduction to ROS learning_ Topic communication mechanism

1, Principle of topic communication

The implementation model of topic communication is complex, as shown in the figure below. The model involves three roles:

ROS Master (Manager)
Talker (publisher)
Listener (subscriber)
The ROS Master is responsible for keeping the registered information of Talker and Listener, matching Talker and Listener with the same topic, and helping Talker establish a connection with Listener. After the connection is established, Talker can publish messages, and the published messages will be subscribed by Listener.

Insert picture description here

(1) Publisher Talker information registration

talker Registration: Publisher Talker Through port 1234 master Registration information(advertise Process of),Including topic name and node information, ROS Master Will record Talker Node information for

(2) Subscriber Listener registration

Listener registration: the subscriber listener registers relevant information with the node manager ROS Master through subscribe, including topic and node information. The Master will record the listener's node information

(3)ROS Master information matching

ROS Master information matching: search in the registration list through the subscription topic information of listener. If the publisher is not found, wait for the publisher to join; If found, publish the RPC address information of talker to the listener through RPC.

(4) Listen send connection request

After the ROS Master matches, it will send the talker's address to the listener, who will send the connection request, topic name, message type and communication protocol to the talker through RPC.

(5)Talker confirms the connection request

After receiving the Listener's request, Talker also confirms the connection information to the Listener through RPC and sends its own TCP address information.

(6)Listener establishes connection with Talker

The Listener will confirm the information according to the information sent by Talker, and then establish a connection.

(7)Talker sends a message to Listener

After the connection is established successfully, Talker will send a message to the Listener.

2, Practice of topic communication

According to the principle, topic communication involves three aspects (1)Talker (2)Listener (3)ROS Master, in which ROS Master does not need to be implemented by us.

(1) Create Talker publisher

Before creating Talker, we need to identify three issues
(1) The name of the topic, which should be consistent with the Listener.
(2) The depth of the message. The message channel is implemented by the queue. The maximum number of messages saved in the queue. If it exceeds this threshold, the first one will be destroyed first (the earlier one will be destroyed first)
(3) The data type of the message.

  1. int8, int16, int32, int64 (or unsigned type: uint *)
  2. float32, float64
  3. string time, duration
  4. other msg files
  5. variable-length array[] andfixed-length array[C]

The event confirmed in this event is
The name of the topic is "chatter"
The depth of the message is 10
The message type is string

The interface for creating Talker ROS is: advertise

#include "ros/ros.h"
#include "std_msgs/String.h" / / messages of normal text type
#include <sstream>

int main(int argc, char  *argv[])
{   
    
    setlocale(LC_ALL,"");
    ros::init(argc,argv,"talker");
    ros::NodeHandle nh;//This class encapsulates some common functions in ROS   
    ros::Publisher pub = nh.advertise<std_msgs::String>("chatter",10); 
    std_msgs::String msg;
    std::string msg_front = "Hello Hello!"; //Message prefix
    int count = 0; //Message counter
    //Logic (10 times a second)
    ros::Rate r(1);
    //Node immortality
    while (ros::ok())
    {
        //Splicing strings and numbers using stringstream
        std::stringstream ss;
        ss << msg_front << count;
        msg.data = ss.str();
        //Release news
        pub.publish(msg);
        //Add debugging and print the sent message
        ROS_INFO("Messages sent:%s",msg.data.c_str());

        //Automatic sleep according to the previously formulated transmission frequency. Sleep time = 1 / frequency;
        r.sleep();
        count++;//Before the end of the cycle, let the count increase automatically
        //No application
        ros::spinOnce();
    }
    return 0;
}

Create Listener subscriber

The interface for creating Listener subscribers is subscribe
The callback function is involved. The principle of the callback function is:
The essence of callback function is function pointer. Function pointer means that a pointer points to a function, which can be understood as an address, but the address stores the entry address of the function.

Callback function uses the principle of function pointer. The address of function A is used as the entry of function B. When we call function B, we will find A through function address and then call A.

Call the callback function in subscribe, receive the data, and then process the data.

#include "ros/ros.h"
#include "std_msgs/String.h"

void doMsg(const std_msgs::String::ConstPtr& msg_p){
    ROS_INFO("I heard:%s",msg_p->data.c_str());
    // ROS_INFO("I heard:% s", (* msg_p) data. c_ str());
}
int main(int argc, char  *argv[])
{
    setlocale(LC_ALL,"");
    //2. Initialize ROS node: name (unique)
    ros::init(argc,argv,"listener");
    //3. Instantiate ROS handle
    ros::NodeHandle nh;

    //4. Instantiate subscriber object
    ros::Subscriber sub = nh.subscribe<std_msgs::String>("chatter",10,doMsg);
    //5. Process subscribed messages (callback function)

    //     6. Set loop call callback function
    ros::spin();//Loop to read the received data and call the callback function for processing

    return 0;
}

(3) Modify cmakelists Txt file

add_executable(Hello_pub
  src/Hello_pub.cpp
)
add_executable(Hello_sub
  src/Hello_sub.cpp
)

target_link_libraries(Hello_pub
  ${catkin_LIBRARIES}
)
target_link_libraries(Hello_sub
  ${catkin_LIBRARIES}
)

Keywords: Linux slam Autonomous vehicles

Added by adyre on Thu, 27 Jan 2022 09:06:24 +0200