ROS series: parameter server (1)

ROS series

76-80



foreword

The parameter server is mainly used to realize a data storage and data sharing between different nodes


1. Parameter Server Composition

ROS Master
Talker: parameter setter
Listener: parameter caller

2. Use steps

1. Write data: add, modify

The code is as follows (c++):


#include <ros/ros.h>
/**
 * @brief
 *  需要实现参数的新增与修改
 *  需求:首先设置机器人的共享参数,类型、半径(0.15m)
 *      在修改半径(0.2m)
 *  实现:
 *      ros::NodeHandle
 *          setParam("键",值)
 *      ros::param
 *          set("键",值)
 *      修改只需要先查rosparam list / get 获取,然后在程序里set修改即可
 */

int main(int argc, char* argv[])
{
    
    
    // 初始化ros节点
    ros::init(argc, argv, "set_param_c");
    //创建ROS节点句柄
    ros::NodeHandle nh;
    //参数增
    // 方案一
    nh.setParam("type","xiaoHuang");
    nh.setParam("radius", 0.15);

    // 方案二
    ros::param::set("type_param","xiaoBai");
    ros::param::set("radius_param",0.15);
    //参数修改
    // 方案一
    nh.setParam("radius",0.1);
    // 方案二
    ros::param::set("radius_param",0.1);
    return 0;
}

rosparam operation in the terminal

rosparam list
rosparam get

Effect:
insert image description here

2. Read data: query

The code is as follows (example):

#include <ros/ros.h>
/**
 * @brief
 *  演示参数查询

 *  实现:
 *      ros::NodeHandle---------------------------------------
 *          param(键,默认值)
            存在,返回对应结果,否则返回默认值

        getParam(键,存储结果的变量)
            存在,返回 true,且将值赋值给参数2
            若果键不存在,那么返回值为 false,且不为参数2赋值

        getParamCached键,存储结果的变量)--提高变量获取效率
            存在,返回 true,且将值赋值给参数2
            若果键不存在,那么返回值为 false,且不为参数2赋值

        getParamNames(std::vector<std::string>)
            获取所有的键,并存储在参数 vector 中

        hasParam(键)
            是否包含某个键,存在返回 true,否则返回 false

        searchParam(参数1,参数2)
            搜索键,参数1是被搜索的键,参数2存储搜索结果的变量
 *      ros::param
 */
int main(int argc, char* argv[])
{
    
    
    setlocale(LC_ALL,"");
    ros::init(argc, argv, "get_param_c");
    ros::NodeHandle nh;
    // ros::NodeHandle-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
    // 1.param
    double radius = nh.param("radius",0.5);
    ROS_INFO("radius=%.2f",radius);
    // 2.getparam
    double radius2=0.0;
    bool result = nh.getParam("radius_param", radius2);
    if(result){
    
    
        ROS_INFO("radius2 = %.2f", radius2);
    }else{
    
    
        ROS_INFO("查询变量不存在");
    }
    // 3.getParamCached键
    // 4.getParamNames
    std::vector<std::string> names;  //创建一个vector用于保存节点名称 名称类型string
    nh.getParamNames(names);
    for (auto && name : names)       //&&右值引用
    {
    
    
        ROS_INFO("遍历的元素是:%s",name.c_str());//遍历以后转换成c形式字符串
    }
    // 5.hasParam
    bool flag1 = nh.hasParam("radius");
    bool flag2 = nh.hasParam("radiusxxx");
    ROS_INFO("radius 存在与否:%d", flag1);
    ROS_INFO("radiusxxx 存在与否:%d", flag2);
    // 6.
    std::string key;    // 定义一个变量来接收键值
    nh.searchParam("radius",key);
    ROS_INFO("搜索结果:%s",key.c_str());
    
    return 0;
}

1. param
param query, exists, return the corresponding result, otherwise return the default value, at this time the default value is 0.5, that is, if there is no radius in the search parameter server, return 0.5 to know that the parameter server does not have the data of this name

   
    double radius = nh.param("radius",0.5);
    ROS_INFO("radius=%.2f",radius);

2. getParam (key, the variable that stores the result)
exists, returns true, and assigns the value to parameter 2.
If the key does not exist, the return value is false, and does not assign a value to parameter 2.

  
    double radius2=0.0;
    bool result = nh.getParam("radius_param", radius2);
    if(result){
    
    
        ROS_INFO("radius2 = %.2f", radius2);
    }else{
    
    
        ROS_INFO("查询变量不存在");
    }

3. getParamCached key, the variable that stores the result) – improve the efficiency of variable acquisition
Exist, return true, and assign the value to parameter 2
If the key does not exist, then the return value is false, and do not assign a value to parameter 2
与2一样,只是提高了性能,但肉眼看不出来
4. getParamNames(std ::vectorstd::string)
get all the keys and store them in the parameter vector

	std::vector<std::string> names;  //创建vector用于保存节点名称 
    nh.getParamNames(names);
    for (auto && name : names)       //&&右值引用
    {
    
    
        ROS_INFO("遍历的元素是:%s",name.c_str());//遍历以后转换成c形式字符串
    }

5. Whether hasParam(key)
contains a certain key, returns true if it exists, otherwise returns false

    bool flag1 = nh.hasParam("radius");
    bool flag2 = nh.hasParam("radiusxxx");
    ROS_INFO("radius 存在与否:%d", flag1);
    ROS_INFO("radiusxxx 存在与否:%d", flag2);
  1. searchParam (parameter 1, parameter 2)
    search key, parameter 1 is the key to be searched, and parameter 2 is the variable that stores the search results
    std::string key;    // 定义一个变量来接收键值
    nh.searchParam("radius",key);
    ROS_INFO("搜索结果:%s",key.c_str());

running resultinsert image description here

Summarize

Similar to global variables;
using RPC protocol communication, not designed for high performance, it is recommended to store non-binary static simple data:
32-bit
doubles
bool
strings
iso8601 time

Guess you like

Origin blog.csdn.net/TianHW103/article/details/127116914