【ROS】Parameter server

The parameter server is mainly used to realize data sharing between different nodes in ROS. The parameter server is equivalent to a common container independent of all nodes. Data can be stored in the container and called by different nodes. Of course, different nodes can also store data in it. The typical application scenarios of the parameter server are as follows:

When navigation is implemented, path planning will be carried out, such as: global path planning, to design a rough path from the starting point to the target point. Local path planning, based on the current road conditions to generate the current path of travel

In the above scenario, the parameter server will be used in global path planning and local path planning:

  • When planning a path, you need to refer to the size of the car. We can store the size information in the parameter server. Both the global path planning node and the local path planning node can call these parameters from the parameter server.

The parameter server is generally applicable to some application scenarios where data sharing exists.

concept

The communication mode of data interaction between different nodes is realized in a shared manner.

effect

Store some data shared by multiple nodes, similar to global variables.

Case study

Realize parameter addition, deletion, modification and checking operations.

Please also refer to:

1 Theoretical model of parameter server

The parameter server implementation is the simplest. The model is shown in the figure below. Three roles are involved in the model:

  • ROS Master (Manager)

  • Talker (parameter setter)

  • Listener (parameter caller)

As a public container, ROS Master saves parameters, Talker can set parameters in the container, and Listener can get parameters.

The whole process is realized by the following steps:

(1) Talker setting parameters

Talker sends parameters (including parameter names and parameter values) to the parameter server via RPC, and ROS Master saves the parameters to the parameter list.

(2). Listener gets parameters

The Listener sends a parameter search request to the parameter server via RPC. The request contains the name of the parameter to be searched.

(3) ROS Master sends parameter values ​​to Listener

ROS Master finds the parameter value according to the parameter name provided in the step 2 request, and sends the query result to the Listener via RPC.


Parameters can use data types:

  • 32-bit integers

  • booleans

  • strings

  • doubles

  • iso8601 dates

  • lists

  • base64-encoded binary data

  • dictionary

Note: The parameter server is not designed for high performance, so it is best used to store static non-binary simple data

2 Parameter operation A (C++)

Requirement: Realize the addition, deletion, modification, and checking operation of parameter server parameters.

The addition, deletion, modification, and checking of parameter server data in C++ can be achieved through two sets of APIs:

  • ros :: NodeHandle

  • ros :: param

The following is a specific operation demonstration

(1). New (modified) parameters of the parameter server

#include "ros/ros.h"


/*
    实现参数的新增和修改
    需求:设置机器人的共享参数:类型和半径(0.15)
                 再修改半径(0.2)

    实现:
            ros::NodeHandle     setParam
            ros::Param          set


*/


int main(int argc, char  *argv[])
{
    setlocale(LC_ALL,"");

    //初始化ros节点
    ros::init(argc,argv,"set_param");

    //创建节点句柄
    ros::NodeHandle nh;

    //参数增---------------------------------
    //方式1:nh
    nh.setParam("type","小黄车");
    nh.setParam("radius",0.15);

    //方式2:ros::Param
    ros::param::set("type_param","小黑车");
    ros::param::set("type_radius",0.15);

    //参数改---------------------------------
    //方式1:nh

    //方式2:ros::Param


    return 0;
}

Configuration file

test

roscore
source ./devel/setup.bash
rosrun plumbing_param_server demo01_param_set
rosparam list

modify

Ensure that the key is to be modified, the value will be overwritten

//参数改---------------------------------
    //方式1:nh
    nh.setParam("radius",0.2);

    //方式2:ros::Param
    ros::param::set("type_radius",0.25);

(2) Parameter server obtains parameters

#include "ros/ros.h"

/*
    演示参数查询   
        实现:
            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节点
    ros::init(argc,argv,"get_param");

    //创建节点句柄
    ros::NodeHandle nh;

    //   ros::NodeHandle   --------------------------------------------------
    //1 param
    double radius = nh.param("radius",0.5);
    ROS_INFO("radius = %f",radius);

    return 0;
}


        getParam(键,存储结果的变量)
            存在,返回 true,且将值赋值给参数2
            若果键不存在,那么返回值为 false,且不为参数2赋值
          
    //2  getParam
    double  radius2  = 0.0;
    bool result = nh.getParam("radius",radius2);
    if(result)
    {
        ROS_INFO("获取的半径是%f",radius2);
    }
    else
    {
        ROS_INFO("被查询的变量不存在");
    }

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

//3    getParamCached
    double  radius3  = 0.0;
    result = nh.getParamCached("radius",radius3);
    if(result)
    {
        ROS_INFO("3获取的半径是%f",radius3);
    }
    else
    {
        ROS_INFO("被查询的变量不存在");
    }

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


 //4 getParamNames(std::vector<std::string>)
    std::vector<std::string> names;
    nh.getParamNames(names);

    //遍历
    for(auto &&name:names)
    {
        ROS_INFO("遍历到的元素是:%s",name.c_str());
    }

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


  //5  hasParam(键)
    result = nh.hasParam("radius");
    if(result)
    {
        ROS_INFO("radius存在");
    }
    else
    {
        ROS_INFO("radius不存在");
    }

searchParam(参数1,参数2)
            搜索键,参数1是被搜索的键,参数2存储搜索结果的变量
            
              //6 searchParam(参数1,参数2)
    std::string key;
    nh.searchParam("radius",key);
    ROS_INFO("搜索结果是:%s",key.c_str());

The function of ros::param is similar to ros::NodeHandle

(3) Parameter server deletes parameters

 参数服务器操作之删除_C++实现:

    ros::NodeHandle
        deleteParam("键")
        根据键删除参数,删除成功,返回 true,否则(参数不存在),返回 false

    ros::param
        del("键")
        根据键删除参数,删除成功,返回 true,否则(参数不存在),返回 false
#include "ros/ros.h"

/*
 参数服务器操作之删除_C++实现:

    ros::NodeHandle
        deleteParam("键")
        根据键删除参数,删除成功,返回 true,否则(参数不存在),返回 false

    ros::param
        del("键")
        根据键删除参数,删除成功,返回 true,否则(参数不存在),返回 false
*/


int main(int argc, char  *argv[])
{
    setlocale(LC_ALL,"");

    //初始化ros节点
    ros::init(argc,argv,"param_del");

    //创建节点句柄
    ros::NodeHandle nh;

    //删除,使用NodeHandle----------------------------------------
    bool flag1 = nh.deleteParam("radius");
    if(flag1)
    {
        ROS_INFO("删除成功");
    }
    else
    {
        ROS_INFO("删除失败");
    }

    //删除,使用param-------------------------------------------------
		bool flag2 = ros::param::del("type_radius");
    if(flag2)
    {
        ROS_INFO("type_radius删除成功");
    }
    else
    {
        ROS_INFO("type_radius删除失败");
    }
    return 0;
}

Can only be deleted once

Guess you like

Origin blog.csdn.net/Zhouzi_heng/article/details/114675745