3 Ubuntu18. 04 implementation of ROS UDP communication

Posted by richever on Sat, 18 Dec 2021 04:46:59 +0100

Ubuntu18. 04 implementation of ROS UDP server communication

This section describes the udp Server sending and receiving data. UDP communication belongs to frame transmission. In the process of frame transmission, there is no requirement for the order and arrival of messages, and no verification is carried out. Therefore, UDP belongs to unreliable transmission, but due to the lack of verification time, UDP communication has high communication efficiency in some environments (such as wireless transmission and poor network signal). This communication method is usually used for the return of sensor information with high real-time requirements (such as the return of video stream and voice stream).
The next section describes Ubuntu 18 04 / communication implementation of ROS UDP client. Ubuntu 18 will be introduced later 04 the communication between ROS and Android tcp/ip is realized, and the vehicle remote control operation is realized through the cooperation of Android and industrial computer.
The test tools in this section are changed, usr-tcp232-test-v1 3. The default UDP communication is the server, Ubuntu 18 The server written by 04 cannot connect to it. In this section, Windows uses TCP & UDP test tool to connect to Ubuntu 18 04 / ROS UDP / server server.

Test process and effect

The test platform is Ubuntu 18 04 conduct communication test with TCP & UDP test tool on Windows system.
1. Ensure that the two computers are on the same network, and check the local IP of Ubuntu, which can be viewed in settings - > WiFi - > as shown in the following figure, 192.168 2.204, local IP.

  1. ping the IP address of another computer. The general rule indicates that the two computers are successfully connected under the same network. windows opens the network serial port assistant to automatically obtain the local IP, or view the IP in network status - > details. As shown in the figure below, if ping fails, please check whether the protective wall is closed.

3. After the Ping is successful, conduct the communication effect test. First run roscore for Ubuntu, then start the server node and start the TCP & UDP test tool. The process is as follows:
Click actions - > create connection

Enter the server IP and port number and click create

Click create and enter the data to be sent.

4. The test sent hello. The screenshot of successful communication is as follows:

Creation of ROS work area and function package

There are many online materials for the creation of ROS work interval and function package, which are briefly explained here. Using RoboWare Studio, the process becomes simpler.

#Create workspace
mkdir catkin_ws #Interval name
cd catkin_ws 
mkdir src #Create code space
cd src
catkin_init_workspace #Initialize bit ROS workspace
cd ..
catkin_make
source ~/catkin_ws/devel/setup.bash
#Create Feature Pack
cd ~/catkin_ws/src
catkin_create_pkg ros_socket std_msgs rospy roscpp
#Create communication node
#Open the terminal in src directory
touch udp_server.cpp
#At cmakelists Txt
add_executable(udp_server
  src/udp_server.cpp
)
add_dependencies(udp_server ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(udp_server
  ${catkin_LIBRARIES}
)

``

Implementation code of ROS UDP Server

The following steps are included
1. Create a socket
2. Bind the local IP port
3.bind() binding
4. Wait for the connection of the client and receive the client data
The program notes are relatively clear. You can have a look.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <syspes.h>
#include <sys/stat.h>
#include <string.h>
#include <arpa/inet.h>
#include <ros/ros.h>
int main(int argc, char** argv)
{
    ros::init(argc, argv, "udp_port");
    //Create a handle (although this handle is not used later, if it is not created, the runtime process will make an error)
    ros::NodeHandle n;

    //1 create socket
    int fd = socket(AF_INET, SOCK_DGRAM, 0);
    if(fd == -1)
    {
        perror("socket error");
        exit(1);
    }
    // fd bind local IP and port
    struct sockaddr_in serv;
    memset(&serv, 0, sizeof(serv));
    serv.sin_family = AF_INET;
    serv.sin_port = htons(1024);//Set server port number
    serv.sin_addr.s_addr = htonl(INADDR_ANY);//Get native ip
     //3.bind() binding
    //Parameter 1: return value of 0 (fd)
    //Parameter 2: (struct SOCKADDR *) & addr front structure, i.e. address
    //Parameter 3: length of addr structure
    int ret = bind(fd, (struct sockaddr*)&serv, sizeof(serv));
    if(ret == -1)
    {
        perror("bind error");
        exit(1);
    }

    struct sockaddr_in client;
    socklen_t cli_len = sizeof(client);
    // signal communication
    char buf[1024] = {0}; //Create an array of received data

    ros::Rate loop_rate(50);//while circulates at 50Hz

    while(ros::ok()) 
    {
        int recvlen = recvfrom(fd, buf, sizeof(buf), 0, 
                               (struct sockaddr*)&client, &cli_len);//The received data is saved to buf and the data length is returned.
        if(recvlen == -1)
        {
            perror("recvform error");
            exit(1);
        }
        printf("received data: %s\n", buf);
        char ip[64] = {0};
        printf(" Client IP: %s, Port: %d\n",
        inet_ntop(AF_INET, &client.sin_addr.s_addr, ip, sizeof(ip)),
        ntohs(client.sin_port));

        // Send data to client
        sendto(fd, buf, strlen(buf)+1, 0, (struct sockaddr*)&client, sizeof(client));

         ros::spinOnce(); 
        loop_rate.sleep(); 
    }
    close(fd);
    return 0;
}



Welcome to criticize and correct!!!

Topics: network Network Protocol udp