current position:Home>1.2 Ubuntu18. 04 implementation of ROS TCP / IP client communication

1.2 Ubuntu18. 04 implementation of ROS TCP / IP client communication

2022-01-26 23:00:34 youcheng_ RGC

Ubuntu18.04 ROS tcp/ip Client Communication implementation

This section describes tcp/ip Client Sending and receiving data , The test platform is Ubuntu18.04 And Windows The network debugging assistant on the system carries out communication test , The debugging assistant uses human technology USR-TCP232-Test-V1.3.
The next section introduces Ubuntu18.04 ROS udp server Communication implementation . Follow up udp client,Android And Ubuntu Network communication , adopt Android Cooperate with industrial computer to realize vehicle remote control operation .

Test process and effect

The test process is the same as that in the previous section 1.1 Ubuntu18.04 ROS tcp/ip Server Communication implementation be similar .
The test process is :
1. Ensure that the two computers are on the same network , And look at Ubuntu Of this machine IP, Set up ->wifi-> You can view , Here's the picture ,, For this machine IP.
 Insert picture description here

  1. mutual ping Another computer ip, The general rule indicates that the two computers are successfully connected under the same network .windows Open the network serial port assistant to automatically obtain the local information IP. Here's the picture . how ping Failed, please check whether the protective wall is closed .
     Insert picture description here
    ping The successful pictures are as follows
     Insert picture description here
     Insert picture description here
    The above is for reference 1.1 Server Communication process , Here are the actual test results ,ip There are some differences from the above , But the process is the same .
    3 The test results , Open the network debugging assistant , choice tcp server Server mode , monitor . The following is the test effect diagram
     Insert picture description here
     Insert picture description here

ROS Creation of work area and function package

ROS There are many online materials for the creation of work sections and function packages , Here is a brief explanation . It uses RoboWare Studio, This process becomes simpler .

# Create a workspace 
mkdir catkin_ws # Interval name 
cd catkin_ws 
mkdir src # Create code space 
cd src
catkin_init_workspace # Initialize a ROS working space 
cd ..
source ~/catkin_ws/devel/setup.bash

The above is the creation of workspace , Use 1.1 The workspace created by the section can be . use RoboWare Studio The process is simpler .

# Create Feature Pack 
cd ~/catkin_ws/src
#1.1  Section uses the same feature pack 
catkin_create_pkg ros_socket std_msgs rospy roscpp
# Create a communication node 
# stay src Open the terminal in the directory 
touch client_node.cpp
# stay CMakeLists.txt Add the following 
add_dependencies(client_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

ROS tcp/ip Server Implementation code

It includes the following steps
1. Create a socket
2. Connect to server IP, This process must be server IP.
3. Determine whether the connection is successful .
4. Send and receive data test .
5. Finally close socket Interface .
The program notes are clear , Beginners can read the following carefully .

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <ros/ros.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <iostream>
#include <ros/ros.h>

using namespace std;

//  Specify the dynamic library lib file 
//#pragma comment(lib,"ws2_32.lib")

// TCP client 
int main(int argc, char** argv)
    ros::init(argc, argv, "client_port");
    // Create handle ( Although this handle is not used later , But if you don't create , The runtime process will make an error )
    ros::NodeHandle n;
	// 1. establish TCP socket, Streaming socket 
	int ss = socket(AF_INET, SOCK_STREAM, 0);

	// 2.  Link server 
	sockaddr_in addr; //  Not recommended sockaddr, Suggest using sockaddr_in
	addr.sin_port = htons(1024); //  Network byte order 
	addr.sin_addr.s_addr  = inet_addr(""); //  Network byte order 
	addr.sin_family = AF_INET; //  Address family 
	int len = sizeof(sockaddr_in);
	// Determine if the connection is successful 
	if(connect(ss, (sockaddr*)&addr, len) == -1)
		cout << "connect error:"  << endl;
		return 0;

// 3. Receive send message 
	int ret=0;
	ros::Rate loop_rate(50);//50Hz Conduct while loop 

			//  Receive the message array of the server 
			char buf[64] = {
			ret = recv(ss, buf, 64, 0); //  Receive data to buf, And get the received length ret.

			cout << "recv_data: " <<buf << endl;// Print the received data 

			//  send out 
			ret = send(ss, "I am Client!", strlen("I am Client!"), 0);// Send data to the server 

			ros::spinOnce(); //ROS Message callback handler 

		return 0;

Welcome criticism !!!

copyright notice
author[youcheng_ RGC],Please bring the original link to reprint, thank you.

Random recommended