ros param的进阶了解

ROS 参数神器 ROS Param 使用

介绍

launch文件提供参数

yaml文件提供参数

字典使用方法
第一种:使用大括号 {}

直接使用大阔号进行包裹,进行字典编写

node_topic_name:{"topic1":"话题一","topic2":"话题二"}

优势:

  • 简单、书写方便
    劣势
  • rosparam不会以字典的名称node_topic_name进行参数获取,反而会直接出现在下一级,例如node_topic_name/topic1
第二种:使用-标志符进行字典分割
node_topic_name:
 - topic1 : 话题一 # 写时注意 符号,例如:前后之间要有空格
 - topic2 : 话题二

优势:

  • 可以在参数字典中,直接索引node_topic_name,而不是其下一级

劣势:

  • 书写麻烦
第三种利用XmlRpc::XmlRpcValue
node_topic:
  - {node_name : "topic1", topic_name : "topic_name1"}
  - {node_name : "topic2", topic_name : "topic_name2"}

读取

  if(ros::param::has("node_topic"))//检查话题是否存在
  {
      XmlRpc::XmlRpcValue mylist;
      nh->getParam("node_topic",mylist);
      for (int i = 0; i < mylist.size(); ++i) {
          string item_key = mylist[i]["node_name"];
          string item_value = mylist[i]["topic_name"];
      }
  }

优势

  • 在ros2及以后版本进行兼容

猜你喜欢

转载自blog.csdn.net/tiger_panda/article/details/124271804
ROS