Ros同一局域网下多机通信

Ros同一局域网下多机通信

由于 ROS 底层是基于 TCP/IP 的,所以如果希望节点运行在不同的机器上,就需要设置好机器之间的网络连接。

1:

  • 查看IP地址:ifconfig
  • 查看主机名称:hostname

2:修改/etc/hosts文件:

目的是将 ip 与两台电脑的 hostname 绑定,两台电脑之间通过 hostname 就可以找到对方。修改之后,ping ip 和 ping hostname 都可

以找到对方的信息。

将本机 ip 和本机 hostname 以及另外机器的 ip 和 hostname 都添加进 /etc/hosts。注意,两个之间是 tab 键,不是空格。(所有机器都需要做)

image-20210129110431836

注:ubuntu和zbxavier-desktop是hostname

3:重启网络: sudo /etc/init.d/networking restart

4:两台电脑上装上 chrony 包,实现同步 sudo apt-get install chrony

5:在计划作为 master 的电脑端输入$sudo gedit .bashrc 打开 bash 文件,添加

export ROS_HOSTNAME=ubuntu #ubuntu hostname 
export ROS_MASTER_URI=http://ubuntu:11311

6:在从机的电脑端输入$sudo gedit .bashrc 打开 bash 文件,添加

export ROS_HOSTNAME=zbxavier #从机用户名 
export ROS_MASTER_URI=http://ubuntu:11311 # 主机 URI

7:在主机端输入roscore 运行 ROS_MASTER,从机不能输入​roscore 运行 ROS_MASTER。

(从机需要运行时roscore,注释掉export ROS_MASTER_URI=http://ubuntu:11311 即可【但是这样的话两台机器,分别输入rostopic list会发现两者的topic不一致,所以不建议】, 或者主机不要通过roscore启动ros,而是通过launch文件来启动,比如说通过启动realsense相机【这种方式两台机器输入rostopic list 发现topic list 完全一致,所以建议以这种方式】)

8:主/从机端发布/订阅话题

如:

rosrun ros_test_pkg image_publisher XXX

rosrun ros_test_pkg image_listener

9:传输图象时候是否压缩以及压缩算法的选择都可参考源码,写的很详细!

对应c++代码

# 图片发布代码 image_publisher.cpp

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include<ros/time.h>
//#include"opencv2/imgcodecs/legacy/constants_c.h"
int main(int argc, char** argv)
{
  ros::init(argc, argv, "image_publisher");
  ros::NodeHandle nh;
  image_transport::ImageTransport it(nh);

  auto pub = nh.advertise<sensor_msgs::CompressedImage>("a", 1);
  cv::Mat image = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR);
  ROS_INFO("image size [%d, %d]", image.cols, image.rows);
  cv_bridge::CvImage cvimg(std_msgs::Header(), "bgr8", image);

  ros::Rate loop_rate(5);
  while (nh.ok()) {
    ros::Time begin = ros::Time::now();
    double secbegin=begin.toSec();
    ROS_INFO("I Published [%f]",secbegin);
    auto msg = cvimg.toCompressedImageMsg(cv_bridge::JPG);
    msg->header.stamp = begin;
    pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
  }
}

# 图片订阅代码 image_listener.cpp


#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <cv_bridge/cv_bridge.h>
#include <ros/time.h>
//#include"opencv2/imgcodecs/legacy/constants_c.h"

cv::Mat img_orig;

void imageCallback(const sensor_msgs::CompressedImageConstPtr& msg)
{
  try
  {
    // 解压缩
    auto received_img = cv_bridge::toCvCopy(msg, "bgr8")->image;
    // 计时并显示
    ROS_INFO("recive cost :[%f sec]", ros::Time::now().toSec() - msg->header.stamp.toSec());
    ROS_INFO("image size %d, %d", received_img.cols, received_img.rows);
    ROS_INFO("img orig size %d %d", img_orig.cols, img_orig.rows);
    // 显示接收到的图像
    //cv::imshow("received_img", received_img);

    // 将原图与接收到的图像进行相减
    auto diff = cv::abs(img_orig - received_img);

    cv::Mat diff_norm;
    cv::normalize(diff, diff_norm, 0, 255, cv::NORM_MINMAX);
    cv::imwrite("diff_norm.bmp", diff_norm);

    cv::Mat diff_gray;
    cv::cvtColor(diff, diff_gray, cv::COLOR_BGR2GRAY);
    auto meanval = cv::mean(diff);
    double maxval;
    cv::Point maxloc;
    cv::minMaxLoc(diff_gray, 0, &maxval, 0, &maxloc);
    ROS_INFO("b mean %lf, g mean %lf, r mean %lf", meanval[0], meanval[1], meanval[2]);
    ROS_INFO("diff val %lf @ (%d, %d)", maxval, maxloc.x, maxloc.y);

    cv::imwrite("diff.bmp", diff);
    // 显示相减后的图像
    //cv::imshow("diff", diff);
    
  }
  catch (cv_bridge::Exception& e)
  {

  }
}

int main(int argc, char** argv)
{
  ros::init(argc, argv, "image_listener");
  ros::NodeHandle nh;
  //cv::namedWindow("received_img");
  //cv::namedWindow("diff");
  cv::startWindowThread();

  // 读取原图,必须与发送的图像一样,而且发送是BMP格式
  img_orig = cv::imread("1.bmp"); // 一定要发送bmp文件

  auto sub = nh.subscribe<sensor_msgs::CompressedImage>("a", 1, imageCallback);
  ros::spin();
  //cv::destroyWindow("received_img");
  //cv::destroyWindow("diff");
}

CMakeList.txt

cmake_minimum_required(VERSION 3.0.2)
project(ros_test_pkg)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  image_transport
  roscpp
  rospy
  std_msgs
  sensor_msgs
)
find_package(OpenCV REQUIRED)


###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES ros_test_pkg
#  CATKIN_DEPENDS roscpp std_msgs
#  DEPENDS system_lib
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)



#添加 ${OpenCV_INCLUDE_DIRS}
include_directories(
 	./include
  	${OpenCV_INCLUDE_DIRS}
	${catkin_INCLUDE_DIRS}
        ${OpenCV_LIBRARY_DIRS}
)



add_executable(image_publisher   ~/catkin_ws2/src/ros_test_pkg/src/image_publisher.cpp)
target_link_libraries(image_publisher
	${OpenCV_LIBRARIES} 
	${catkin_LIBRARIES}
)
# add_dependencies(image_publisher ) 

add_executable(image_listener  ~/catkin_ws2/src/ros_test_pkg/src/image_listener.cpp)
target_link_libraries(image_listener  
	${OpenCV_LIBRARIES} 
	${catkin_LIBRARIES}
)
# add_dependencies(image_listener )





package.xml

<?xml version="1.0"?>
<package format="2">
  <name>ros_test_pkg</name>
  <version>0.0.0</version>
  <description>The ros_test_pkg package</description>

  <!-- One maintainer tag required, multiple allowed, one person per tag -->
  <!-- Example:  -->
  <!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
  <maintainer email="grzhang@todo.todo">grzhang</maintainer>


  <!-- One license tag required, multiple allowed, one license per tag -->
  <!-- Commonly used license strings: -->
  <!--   BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
  <license>TODO</license>


  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>cv_bridge</build_depend>
  <build_depend>image_transport</build_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_depend>opencv2</build_depend>
 
  <build_export_depend>cv_bridge</build_export_depend>
  <build_export_depend>image_transport</build_export_depend>
  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>rospy</build_export_depend>
  <build_export_depend>std_msgs</build_export_depend>
  <build_export_depend>opencv2</build_export_depend> 
 
  <exec_depend>cv_bridge</exec_depend>
  <exec_depend>image_transport</exec_depend>
  <exec_depend>roscpp</exec_depend>
  <exec_depend>rospy</exec_depend>
  <exec_depend>std_msgs</exec_depend>
  <exec_depend>opencv2</exec_depend>


  <!-- The export tag contains other, unspecified, tags -->
  <export>
    <!-- Other tools can request additional information be placed here -->

  </export>
</package>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值