roscpp_tutorials / Tutorials / Parameters

在roscpp中使用参数

http://wiki.ros.org/roscpp_tutorials/Tutorials/Parameters
本教程将向您展示 NodeHandle parameter API,允许您从 Parameter Server 操作参数。

1 检索参数

使用NodeHandle检索参数有两种方法。在下面的示例代码中,n是NodeHandle的一个实例。

1.1 getParam()

getParam() has a number of overloads which all follow the same basic form:

 1 bool getParam (const std::string& key, parameter_type& output_value) const 

key is a Graph Resource Name

output_value is the place to put the retrieved data, where parameter_type is one of bool, int, double, string, or a special XmlRpcValue type which can represent any type and also lists/maps.
Use of getParam() is fairly simple:

   1     std::string s;
   2     n.getParam("my_param", s);

Note that getParam() returns a bool, which provides the ability to check if retrieving the parameter succeeded or not:

   1     std::string s;
   2     if (n.getParam("my_param", s))
   3     {
   4       ROS_INFO("Got param: %s", s.c_str());
   5     }
   6     else
   7     {
   8       ROS_ERROR("Failed to get param 'my_param'");
   9     }

1.2 param()

param() is similar to getParam(), but allows you to specify a default value in the case that the parameter could not be retrieved:

   1   int i;
   2   n.param("my_num", i, 42);

Sometimes the compiler requires a hint for the string type.

   1   std::string s;
   2   n.param<std::string>("my_param", s, "default_value");

2 Setting Parameters

Setting parameters is done through the setParam() methods:

   1   n.setParam("my_param", "hello there");

setParam(), like getParam(), can take bool, int, double, string, and a special XmlRpcValue type

3 Deleting Parameters

Deleting parameters is done through the deleteParam() method:

   1   n.deleteParam("my_param");

4 Checking for Existence

This is not usually necessary, but there is a hasParam() method that allows you to check for a parameter’s existence:

   1   if (!n.hasParam("my_param"))
   2   {
   3     ROS_INFO("No param named 'my_param'");
   4   }

5 Searching for Parameters

The Parameter Server allows you to “search” for parameters, starting at your namespace and working through your parent namespaces.

For example, if the parameter /a/b exists in the parameter server, and your NodeHandle is in the /a/c namespace, searchParam() for b will yield /a/b. However, if parameter /a/c/b is added, searchParam() for b will now yield /a/c/b.

  1   std::string param_name;
   2   if (n.searchParam("b", param_name))
   3   {
   4     // Found parameter, can now query it using param_name
   5     int i = 0;
   6     n.getParam(param_name, i);
   7   }
   8   else
   9   {
  10     ROS_INFO("No param 'b' found in an upward search");
  11   }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值