Navigation

    蓝鲸ROS机器人论坛

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • Popular
    ROS交流群
    ROS Group
    产品服务
    Product Service
    开源代码库
    Github
    官网
    Official website
    技术交流
    Technological exchanges
    激光雷达
    LIDAR
    ROS教程
    ROS Tourials
    深度学习
    Deep Learning
    机器视觉
    Computer Vision

    在ROS中使用UDP进行通信

    ROS教程
    1
    1
    1488
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • weijiz
      weijiz last edited by

      ROS的网络通信提供了两种方式,一种是TCP协议,一种是UDP协议。默认采用TCP进行通信。但是在实际的WIFI网络使用中发现用户经常反馈客户端和机器人连接中断且无法重新建立连接。在ROS wiki中官方也有说明,ROSTCP更适合有线网连接的网络,而ROSUDP更适合wifi等网络不可靠的无线网络。下面介绍一下如何在ROS中使用UDP连接。

      首先你的消息发布者必须是用roscpp写的。rospy不支持udp连接。然后在订阅的时候添加 ros::TransportHints指定连接方式。如下面的代码

      
      #include <ros/ros.h>
      #include <std_msgs/String.h>
      
      void print_message(const std_msgs::String data)
      {
          ROS_INFO_STREAM("received: " << data);
      }
      
      int main(int argc, char **argv)
      {
          ros::init(argc, argv, "udp_test_node");
          ros::AsyncSpinner spinner(4);
          spinner.start();
          ros::NodeHandle private_nh("~");
          ros::Subscriber chatter_sub = private_nh.subscribe("/chatter", 10, print_message,
                                                             ros::TransportHints().unreliable().maxDatagramSize(1000));
          while (ros::ok())
          {
              sleep(1);
          }
      }
      

      unreliable 会指定采用udp连接。

      详细的例子可以参照这个项目

      1 Reply Last reply Reply Quote 0
      • First post
        Last post
      Copyright © 2015-2021 BlueWhale community