当前位置:网站首页>ROS series (IV): ROS communication mechanism series (3): parameter server
ROS series (IV): ROS communication mechanism series (3): parameter server
2022-04-23 03:37:00 【Yi Lanjun】
Parameter server in ROS Mainly used in Realize data sharing between different nodes . The parameter server is then equivalent to a common container independent of all nodes , You can store data in this container , Called by different nodes , Of course, different nodes can also store data , The typical application scenarios of the parameter server are as follows :
When navigation is implemented , We'll do path planning , such as : Global path planning , Design an approximate path from the starting point to the target point . Local path planning , The travel path will be generated according to the current road conditions . Global path planning and local path planning , The parameter server will be used : Path planning , It is necessary to refer to the size of the trolley , We can store these size information in the parameter server , Global path planning nodes and local path planning nodes can middle note these parameters from the parameter server. .
Parameter server , It is generally applicable to some application scenarios with data sharing , Similar to global variables .
1 Parameter server theoretical model
The implementation of parameter server is the simplest , The model is shown in the figure below , There are three roles involved in the model :
- ROS Master ( managers )
- Talker ( Parameter setter )
- Listener ( Parameter caller )
ROS Master Save parameters as a public container ,Talker You can set parameters to the container ,Listener You can get parameters .
The whole process is realized by the following steps :
1.1 Talker Set parameters
Talker adopt RPC Send parameters to the parameter server ( Including parameter name and parameter value ),ROS Master Save the parameter to the parameter list .
1.2 Listener To obtain parameters
Listener adopt RPC Send a parameter lookup request to the parameter server , The request contains the name of the parameter to be found .
1.3 ROS Master towards Listener Send parameter value
ROS Master Follow the steps 2 Request the parameter name provided to find the parameter value , And pass the query results through RPC Send to Listener.
1.4 Data type of parameter :
- 32-bit integers
- booleans
- strings
- doubles
- iso8601 data
- lists
- base64-encoded binary data
- Dictionaries
Be careful : The parameter server is not designed for high performance , Therefore, it is best to store static non binary simple data
2 Parameter server operation (C++)
demand : Add, delete, change and query parameters of the parameter server , You can use two sets of API Realization :
- ros::NodeHandle
- ros::param
2.1 Parameter added to the server ( modify ) Parameters
/* Addition and modification of parameter server operation ( both of them API equally )_C++ Realization : stay roscpp Two sets of API Implement parameter operation ros::NodeHandle setParam(" key ", value ) ros::param set(" key "," value ") Example : Set the shaping parameters separately 、 floating-point 、 character string 、bool、 list 、 Dictionary and other type parameters modify ( The same key , Different values ) */
#include "ros/ros.h"
int main(int argc, char *argv[])
{
ros::init(argc,argv,"set_update_param");
std::vector<std::string> stus;
stus.push_back("zhangsan");
stus.push_back(" Li Si ");
stus.push_back(" Wang Wu ");
stus.push_back(" Grandson big head ");
std::map<std::string,std::string> friends;
friends["guo"] = "huang";
friends["yuang"] = "xiao";
//NodeHandle--------------------------------------------------------
ros::NodeHandle nh;
nh.setParam("nh_int",10); // integer
nh.setParam("nh_double",3.14); // floating-point
nh.setParam("nh_bool",true); //bool
nh.setParam("nh_string","hello NodeHandle"); // character string
nh.setParam("nh_vector",stus); // vector
nh.setParam("nh_map",friends); // map
// Modify the presentation ( The same key , Different values )
nh.setParam("nh_int",10000);
//param--------------------------------------------------------
ros::param::set("param_int",20);
ros::param::set("param_double",3.14);
ros::param::set("param_string","Hello Param");
ros::param::set("param_bool",false);
ros::param::set("param_vector",stus);
ros::param::set("param_map",friends);
// Modify the presentation ( The same key , Different values )
ros::param::set("param_int",20000);
return 0;
}
2.2 Get parameters from the parameter server
/* Query of parameter server operation _C++ Realization : stay roscpp Two sets of API Implement parameter operation ros::NodeHandle param( key , The default value is ) There is , Return the corresponding result , Otherwise, return the default value getParam( key , Variables that store results ) There is , return true, And assign the value to the parameter 2 If the key does not exist , So the return value is zero false, And not a parameter 2 assignment getParamCached key , Variables that store results )-- Improve variable acquisition efficiency There is , return true, And assign the value to the parameter 2 If the key does not exist , So the return value is zero false, And not a parameter 2 assignment getParamNames(std::vector<std::string>) Get all keys , And stored in the parameter vector in hasParam( key ) Whether to include a key , There is returned true, Otherwise return to false searchParam( Parameters 1, Parameters 2) The search button , Parameters 1 Is the key to be searched , Parameters 2 Variables that store search results ros::param ----- And NodeHandle similar */
#include "ros/ros.h"
int main(int argc, char *argv[])
{
setlocale(LC_ALL,"");
ros::init(argc,argv,"get_param");
//NodeHandle--------------------------------------------------------
/* ros::NodeHandle nh; // param function int res1 = nh.param("nh_int",100); // Key exists int res2 = nh.param("nh_int2",100); // The key doesn't exist ROS_INFO("param To get the results :%d,%d",res1,res2); // getParam function int nh_int_value; double nh_double_value; bool nh_bool_value; std::string nh_string_value; std::vector<std::string> stus; std::map<std::string, std::string> friends; nh.getParam("nh_int",nh_int_value); nh.getParam("nh_double",nh_double_value); nh.getParam("nh_bool",nh_bool_value); nh.getParam("nh_string",nh_string_value); nh.getParam("nh_vector",stus); nh.getParam("nh_map",friends); ROS_INFO("getParam Results obtained :%d,%.2f,%s,%d", nh_int_value, nh_double_value, nh_string_value.c_str(), nh_bool_value ); for (auto &&stu : stus) { ROS_INFO("stus Elements :%s",stu.c_str()); } for (auto &&f : friends) { ROS_INFO("map Elements :%s = %s",f.first.c_str(), f.second.c_str()); } // getParamCached() nh.getParamCached("nh_int",nh_int_value); ROS_INFO(" Get data through cache :%d",nh_int_value); //getParamNames() std::vector<std::string> param_names1; nh.getParamNames(param_names1); for (auto &&name : param_names1) { ROS_INFO(" Name resolution name = %s",name.c_str()); } ROS_INFO("----------------------------"); ROS_INFO(" There is nh_int Do you ? %d",nh.hasParam("nh_int")); ROS_INFO(" There is nh_intttt Do you ? %d",nh.hasParam("nh_intttt")); std::string key; nh.searchParam("nh_int",key); ROS_INFO(" The search button :%s",key.c_str()); */
//param--------------------------------------------------------
ROS_INFO("++++++++++++++++++++++++++++++++++++++++");
int res3 = ros::param::param("param_int",20); // There is
int res4 = ros::param::param("param_int2",20); // Does not exist. Return to default
ROS_INFO("param To get the results :%d,%d",res3,res4);
// getParam function
int param_int_value;
double param_double_value;
bool param_bool_value;
std::string param_string_value;
std::vector<std::string> param_stus;
std::map<std::string, std::string> param_friends;
ros::param::get("param_int",param_int_value);
ros::param::get("param_double",param_double_value);
ros::param::get("param_bool",param_bool_value);
ros::param::get("param_string",param_string_value);
ros::param::get("param_vector",param_stus);
ros::param::get("param_map",param_friends);
ROS_INFO("getParam Results obtained :%d,%.2f,%s,%d",
param_int_value,
param_double_value,
param_string_value.c_str(),
param_bool_value
);
for (auto &&stu : param_stus)
{
ROS_INFO("stus Elements :%s",stu.c_str());
}
for (auto &&f : param_friends)
{
ROS_INFO("map Elements :%s = %s",f.first.c_str(), f.second.c_str());
}
// getParamCached()
ros::param::getCached("param_int",param_int_value);
ROS_INFO(" Get data through cache :%d",param_int_value);
//getParamNames()
std::vector<std::string> param_names2;
ros::param::getParamNames(param_names2);
for (auto &&name : param_names2)
{
ROS_INFO(" Name resolution name = %s",name.c_str());
}
ROS_INFO("----------------------------");
ROS_INFO(" There is param_int Do you ? %d",ros::param::has("param_int"));
ROS_INFO(" There is param_intttt Do you ? %d",ros::param::has("param_intttt"));
std::string key;
ros::param::search("param_int",key);
ROS_INFO(" The search button :%s",key.c_str());
return 0;
}
2.3 The parameter server deletes the parameter
/* Parameter server operation deletion _C++ Realization : ros::NodeHandle deleteParam(" key ") Delete the parameter according to the key , Delete successful , return true, otherwise ( Parameter does not exist ), return false ros::param del(" key ") Delete the parameter according to the key , Delete successful , return true, otherwise ( Parameter does not exist ), return false */
#include "ros/ros.h"
int main(int argc, char *argv[])
{
setlocale(LC_ALL,"");
ros::init(argc,argv,"delete_param");
ros::NodeHandle nh;
bool r1 = nh.deleteParam("nh_int");
ROS_INFO("nh Delete result :%d",r1);
bool r2 = ros::param::del("param_int");
ROS_INFO("param Delete result :%d",r2);
return 0;
}
版权声明
本文为[Yi Lanjun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220602231787.html
边栏推荐
- Common net HP UNIX system FTP server listfiles returns null solution.
- If statement format flow
- Key point detection of human hand based on mediapipe
- QT learning summary
- Wechat applet canvas draws a simple asymptotic color of the dashboard
- L3-011 直捣黄龙 (30 分)
- 打卡:4.23 C语言篇 -(1)初识C语言 - (12)结构体
- A hundred dollars for a hundred chickens
- Batch download of files ---- compressed and then downloaded
- Chapter 8 exception handling, string handling and file operation
猜你喜欢
随机推荐
Identifier, keyword, data type
第四次作业
C abstract class
The art of concurrent programming (6): explain the principle of reentrantlock in detail
The content of the website is prohibited from copying, pasting and saving as JS code
Chapter 8 exception handling, string handling and file operation
Openvino only supports Intel CPUs of generation 6 and above
51 single chip microcomputer: D / a digital to analog conversion experiment
Cmake qmake simple knowledge
Applet - more than two pieces of folding and expansion logic
Concepts of objects and classes
Test questions and some space wars
2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)
2022 group programming ladder game simulation L2-4 Zhezhi game (25 points)
2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)
Raspberry pie 3B logs into the wired end of Ruijie campus network through mentohust, creates WiFi (open hotspot) for other devices, and realizes self startup at the same time
Leetcode punch in diary day 01
JS changes the words separated by dashes into camel style
Paddlepaddle does not support arm64 architecture.
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径