Ubuntu18.04 ROS tcp/ip Server通信实现
此小节介绍tcp/ip Server接收数据,并将截取到底信息通过话题方式发布出去。下一节介绍Ubuntu18.04 ROS tcp/ip client通信实现。 后续介绍Android与Ubuntu的网络通信,通过Android与工控机配合实现车辆遥控操作。
测试过程和效果
测试平台为Ubuntu18.04 与Windows系统上的网络调试助手进行通信测试,调试助手采用的有人科技的USR-TCP232-Test-V1.3。 测试过程为: 1.保证两台电脑在同一个网络下,并查看Ubuntu的本机IP,在设置->wifi->中可查看,如下图,192.168.x.xxx,为本机IP。
- 相互ping另外一台电脑的ip,通则说明两台电脑在同一网络下连接成功。windows打开网络串口助手可自动获取本机IP。如下图。如何ping失败请查看防护墙是否关闭。
ping成功的图片如下 Windows Ubuntu 3 通信效果测试,首先Ubuntu运行roscore,然后启动服务器节点,然后打开网络调试助手,输入服务器IP,通信成功的截图如下:
ROS工作区间和功能包的创建
ROS工作区间和功能包的创建网上资料比较多,这里简单说明。其中使用RoboWare Studio,这个过程变的更简单。
mkdir catkin_ws
cd catkin_ws
mkdir src
cd src
catkin_init_workspace
cd ..
catkin_make
source ~/catkin_ws/devel/setup.bash
cd ~/catkin_ws/src
catkin_create_pkg ros_socket std_msgs rospy roscpp
touch server_node.cpp
add_executable(server_node
srcrver_node.cpp
)
add_dependencies(server_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(server_node
${catkin_LIBRARIES}
)
``
ROS tcp/ip Server的实现代码
包括以下步骤 1.创建一个socket 2.准备通讯地址(必须是服务器的)也就算是本机的IP 3.bind()绑定 4.监听客户端listen()函数 5.等待客户端的连接accept(),返回用于交互的socket描述符 程序注释的比较清楚,不在多说了。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<iostream>
#include <ros/ros.h>
#include "navgation_pack/androidyaokong.h"
using namespace std;
navgation_pack::androidyaokong wangluo;
int main(int argc, char** argv)
{
ros::init(argc, argv, "server_port");
ros::NodeHandle n;
ros::Publisher tcp_jie =n.advertise<navgation_pack::androidyaokong>("/android_para", 100);
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd == -1)
{
cout << "socket 创建失败: "<< endl;
exit(1);
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(1024);
addr.sin_addr.s_addr = inet_addr("192.168.1.113");
int res = bind(socket_fd,(struct sockaddr*)&addr,sizeof(addr));
if (res == -1)
{
cout << "bind创建失败: " << endl;
exit(-1);
}
cout << "bind ok 等待客户端的连接" << endl;
listen(socket_fd,30);
struct sockaddr_in client;
socklen_t len = sizeof(client);
int fd = accept(socket_fd,(struct sockaddr*)&client,&len);
if (fd == -1)
{
cout << "accept错误\n" << endl;
exit(-1);
}
char *ip = inet_ntoa(client.sin_addr);
cout << "客户: 【" << ip << "】连接成功" << endl;
write(fd, " Welcome to visit ", 17);
char buffer[255]={};
ros::Rate loop_rate(50);
while (ros::ok())
{
int size = read(fd, buffer, sizeof(buffer));
if (size>=1)
{
string jieshou = buffer;
string jieshou_chuli = jieshou.substr(0,3);
string qia = "qia";
string hou = "hou";
string zuo = "zuo";
string you = "you";
string tin = "tin";
cout << jieshou << endl;
if (strcmp(jieshou_chuli.c_str(),qia.c_str())==0)
{
string jieshou_p = jieshou.substr(7,1);
int q = atoi(jieshou_p.c_str());
wangluo.qianjin = q ;
cout << jieshou_p<<"qian "<< q << endl;
}
if (strcmp(jieshou_chuli.c_str(),hou.c_str())==0)
{
string jieshou_p = jieshou.substr(6,1);
int q = atoi(jieshou_p.c_str());
wangluo.houtui = q ;
wangluo.qianjin = 0 ;
cout << jieshou_p<<"hou "<< q << endl;
}
if (strcmp(jieshou_chuli.c_str(),zuo.c_str())==0)
{
string jieshou_p = jieshou.substr(8,1);
int q = atoi(jieshou_p.c_str());
wangluo.zuozhuan = q ;
wangluo.youzhang = 0;
cout << jieshou_p<<" zuo "<< q << endl;
}
if (strcmp(jieshou_chuli.c_str(),you.c_str())==0)
{
string jieshou_p = jieshou.substr(8,1);
int q = atoi(jieshou_p.c_str());
wangluo.youzhang = q ;
wangluo.zuozhuan = 0;
cout << jieshou_p<<"you "<< q << endl;
}
if (strcmp(jieshou_chuli.c_str(),tin.c_str())==0)
{
string jieshou_p = jieshou.substr(7,1);
int q = atoi(jieshou_p.c_str());
wangluo.houtui = 0 ;
wangluo.zuozhuan = 0 ;
wangluo.tingzhi = 0 ;
wangluo.youzhang = 0 ;
cout << jieshou_p<<"tin "<< q << endl;
}
tcp_jie.publish(wangluo);
}
ros::spinOnce();
loop_rate.sleep();
}
close(fd);
close(socket_fd);
return 0;
}
欢迎大家批评指正!!!
|