ROS源代码阅读(2)-网络参数初始化

11 篇文章 3 订阅
3 篇文章 0 订阅

上一节我们讲到ros的切入点,其中有初始化了网络模块

上一节原文链接:https://blog.csdn.net/lxj362343/article/details/126088735


//转换传入参数为remapping后调用调用此函数
void init(const M_string& remappings, const std::string& name, uint32_t options)
{
  if (!g_atexit_registered)
  {
    g_atexit_registered = true;
//atexit函数是一个特殊的函数,它是在正常程序退出时调用的函数,我们把他叫为登记函数,
// 一个进程可以登记32个函数,这些函数由exit自动调用,这些函数被称为终止处理函数,
//atexit函数可以登记这些函数。exit调用终止处理函数的顺序和atexit登记的顺序相反,
//如果一个函数被多次登记,也会被多次调用,也就是说退出时将调用atexitCallback这个函数。
    atexit(atexitCallback);
  }
 
  if (!g_global_queue)
  {
    g_global_queue.reset(new CallbackQueue);
  }
 
  if (!g_initialized)
  {
    g_init_options = options;
    g_ok = true;
 
    ROSCONSOLE_AUTOINIT;
    // Disable SIGPIPE
#ifndef WIN32
    signal(SIGPIPE, SIG_IGN);
#endif
    check_ipv6_environment();
    //network相关的初始化,调用的network命名空间下的init
    network::init(remappings);
    //master相关的初始化
    master::init(remappings);
    // names:: namespace is initialized by this_node
    this_node::init(name, remappings, options);
    file_log::init(remappings);
    param::init(remappings);
 
    g_initialized = true;
  }
}

network::init(remappings);

//network相关的初始化,调用的network命名空间下的init,在下面的文件中可以找到定义

ros_comm/clients/roscpp/src/libros/network.cpp

void init(const M_string& remappings)
{
  //从输入的命令行参数获取__hostname
  M_string::const_iterator it = remappings.find("__hostname");
  if (it != remappings.end())
  {
    g_host = it->second;
  }
  else
  {
   //从输入的命令行参数获取__ip,这个从下面的代码看其实就是环境变量ROS_IP
    it = remappings.find("__ip");
    if (it != remappings.end())
    {
      g_host = it->second;
    }
  }

  it = remappings.find("__tcpros_server_port");
  if (it != remappings.end())
  {
    try
    {
     //lexical_cast库进行”字面值“的转换,类似C中的atoi()函数,可以进行字符串与整数/浮点数之间的字面转换
      g_tcpros_server_port = boost::lexical_cast<uint16_t>(it->second);
    }
    catch (boost::bad_lexical_cast&)
    {
      throw ros::InvalidPortException("__tcpros_server_port [" + it->second + "] was not specified as a number within the 0-65535 range");
    }
  }

  if (g_host.empty())
  { 
    //determineHost这函数在下面,当命令行参数获取不到上面几个参数就从环境变量中读取,也就是说命令行优先级比较高
    g_host = determineHost();
  }
}

使用lexical_cast可以很容易的在数值和字符串之间转换,只需要在目标参数里面指出要转换的目标类型即可(参考自:https://blog.csdn.net/zhizhengguan/article/details/116988456)

int x = lexical_cast<int>("100");
    long y = lexical_cast<long>("2000");
    float pai = lexical_cast<float>("3.14159e5");
    double  e = lexical_cast<double>("2.71828");
    double f = lexical_cast<double>("1.414.x", 5);

环境变量ROS_HOSTNAMEROS_IP的获取

std::string determineHost()
{
  std::string ip_env;
  // First, did the user set ROS_HOSTNAME?
  if ( get_environment_variable(ip_env, "ROS_HOSTNAME")) {
    ROSCPP_LOG_DEBUG( "determineIP: using value of ROS_HOSTNAME:%s:", ip_env.c_str());
    if (ip_env.size() == 0)
    {
      ROS_WARN("invalid ROS_HOSTNAME (an empty string)");
    }
    return ip_env;
  }

  // Second, did the user set ROS_IP?
  if ( get_environment_variable(ip_env, "ROS_IP")) {
    ROSCPP_LOG_DEBUG( "determineIP: using value of ROS_IP:%s:", ip_env.c_str());
    if (ip_env.size() == 0)
    {
      ROS_WARN("invalid ROS_IP (an empty string)");
    }
    return ip_env;
  }

  // Third, try the hostname
  char host[1024];
  memset(host,0,sizeof(host));
  if(gethostname(host,sizeof(host)-1) != 0)
  {
    ROS_ERROR("determineIP: gethostname failed");
  }
  // We don't want localhost to be our ip
  else if(strlen(host) && strcmp("localhost", host))
  {
    return std::string(host);
  }

  // Fourth, fall back on interface search, which will yield an IP address

#ifdef HAVE_IFADDRS_H
  struct ifaddrs *ifa = NULL, *ifp = NULL;
  int rc;
  if ((rc = getifaddrs(&ifp)) < 0)
  {
    ROS_FATAL("error in getifaddrs: [%s]", strerror(rc));
    ROS_BREAK();
  }
  char preferred_ip[200] = {0};
  for (ifa = ifp; ifa; ifa = ifa->ifa_next)
  {
    char ip_[200];
    socklen_t salen;
    if (!ifa->ifa_addr)
      continue; // evidently this interface has no ip address
    if (ifa->ifa_addr->sa_family == AF_INET)
      salen = sizeof(struct sockaddr_in);
    else if (ifa->ifa_addr->sa_family == AF_INET6)
      salen = sizeof(struct sockaddr_in6);
    else
      continue;
    if (getnameinfo(ifa->ifa_addr, salen, ip_, sizeof(ip_), NULL, 0,
                    NI_NUMERICHOST) < 0)
    {
      ROSCPP_LOG_DEBUG( "getnameinfo couldn't get the ip of interface [%s]", ifa->ifa_name);
      continue;
    }
    //ROS_INFO( "ip of interface [%s] is [%s]", ifa->ifa_name, ip);
    // prefer non-private IPs over private IPs
    if (!strcmp("127.0.0.1", ip_) || strchr(ip_,':'))
      continue; // ignore loopback unless we have no other choice
    if (ifa->ifa_addr->sa_family == AF_INET6 && !preferred_ip[0])
      strcpy(preferred_ip, ip_);
    else if (isPrivateIP(ip_) && !preferred_ip[0])
      strcpy(preferred_ip, ip_);
    else if (!isPrivateIP(ip_) &&
             (isPrivateIP(preferred_ip) || !preferred_ip[0]))
      strcpy(preferred_ip, ip_);
  }
  freeifaddrs(ifp);
  if (!preferred_ip[0])
  {
    ROS_ERROR( "Couldn't find a preferred IP via the getifaddrs() call; I'm assuming that your IP "
        "address is 127.0.0.1.  This should work for local processes, "
        "but will almost certainly not work if you have remote processes."
        "Report to the ROS development team to seek a fix.");
    return std::string("127.0.0.1");
  }
  ROSCPP_LOG_DEBUG( "preferred IP is guessed to be %s", preferred_ip);
  return std::string(preferred_ip);
#else
  // @todo Fix IP determination in the case where getifaddrs() isn't
  // available.
  ROS_ERROR( "You don't have the getifaddrs() call; I'm assuming that your IP "
             "address is 127.0.0.1.  This should work for local processes, "
             "but will almost certainly not work if you have remote processes."
             "Report to the ROS development team to seek a fix.");
  return std::string("127.0.0.1");
#endif
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ROS(机器人操作系统)是一个灵活、可扩展的机器人编程框架,适用于各种机器人平台和应用场景。ROS的核心是通过发布与订阅的机制进行消息通信,方便模块化开发和组合。 在ROS中,机器人程序的源代码通常由多个节点(nodes)组成,每个节点负责实现特定的功能。每个节点都是一个独立运行的进程,可以与其他节点进行通信,共享数据或进行协调。 ROS中的程序可以使用多种编程语言编写,如C++、Python等,不同节点之间可以使用相同或不同的编程语言。编写ROS程序时,需要定义和配置各个节点,以及节点间的通信方式。 源代码中通常包含了以下几个主要的元素: 1. 节点定义:定义和初始化每个节点,并指定节点的名称、主题(topic)、服务(service)等。 2. 主循环(main loop):在主循环中,节点会不断接收、处理和发布消息,执行特定的任务。 3. 发布器(Publisher):定义和配置发布器,负责将消息发布到特定的主题上,供其他节点订阅。 4. 订阅器(Subscriber):定义和配置订阅器,负责订阅其他节点发布的消息,并进行相应的处理。 5. 服务(Service):定义和配置服务,可供其他节点调用,实现节点间的相互协作。 6. 回调函数(Callback function):当节点接收到消息或服务调用时,会触发相应的回调函数,进行消息处理或服务响应。 7. 参数配置:通过参数配置,可以动态调整节点的行为和属性,使节点更加灵活和可配置。 通过编写ROS程序,可以实现机器人各种功能,如感知、路径规划、运动控制等。同时,ROS还提供了丰富的开发工具和库,方便进行机器人程序的调试、测试和可视化。 总结来说,ROS机器人程序设计的源代码包含节点定义、消息通信的配置、消息的发布和订阅、服务的定义和调用、回调函数的处理等元素。通过编写和组合这些代码,可以实现各种机器人的功能和任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值