1. 说明
param就是个中央注册表,可以进行参数的增删改查
2. roscpp中的使用
roscpp提供了两套,一套是放在ros::param namespace
下,另一套是在ros::NodeHandle
下,这两套API的操作基本一样。
推荐使用ros::param
的方式,因为ros::param
给人感觉,增删改善参数是静态的,实例无关的,这更像参数的本质。
2.1 ros::param 方式
#include<ros/ros.h>
int main(int argc, char **argv)
{
ros::init(argc, argv, "param_demo");
ros::NodeHandle nh;
int parameter1, parameter2, parameter3, parameter4, parameter5;
// get param
bool ifget1 = ros::param::get("param1", parameter1);
// set param
parameter4 = 4;
ros::param::set("param4", parameter4);
// check param
bool ifparam6 = ros::param::has("param6");
if(ifparam6) {
ROS_INFO("Param6 exists");
}
else {
ROS_INFO("Param6 doesn't exist");
}
// delete param
bool ifdeleted6 = ros::param::del("param6");
if(ifdeleted6) {
ROS_INFO("Param6 deleted");
}
else {
ROS_INFO("Param6 not deleted");
}
// get all param names. New in ROS indigo
std::vector<std::string> keys;
ros::param::search(keys);
return 0;
}
2.2 ros::NodeHandle 方式
#include<ros/ros.h>
int main(int argc, char **argv)
{
ros::init(argc, argv, "param_demo");
ros::NodeHandle nh;
int parameter1, parameter2, parameter3, parameter4, parameter5;
// get param 1
bool ifget2 = nh.getParam("param2",parameter2);
// get param 2. 33333 is default value. When no param3, parameter3=33333.
nh.param("param3", parameter3, 33333);
// set param
parameter5 = 5;
nh.setParam("param5",parameter5);
// check param
bool ifparam5 = nh.hasParam("param5");
if(ifparam5) {
ROS_INFO("Param5 exists");
}
else {
ROS_INFO("Param5 doesn't exist");
}
// delete param
bool ifdeleted5 = nh.deleteParam("param5");
if(ifdeleted5) {
ROS_INFO("Param5 deleted");
}
else {
ROS_INFO("Param5 not deleted");
}
// get all param names. New in ROS indigo
std::vector<std::string> keys;
nh.getParamNames(keys);
return 0;
}
2.3 命名空间
有时会看到 ros::NodeHandle n;
和 ros::NodeHandle nh("~");
两种用法。
ros::NodeHandle n;
是全局命名空间。
ros::NodeHandle nh("~");
是局部命名空间
如下一个lauch文件,有两个serial
参数,一个全局的,一个局部
<launch>
<!-- global serial -->
<param name="serial" value="5" />
<node name="name_demo" pkg="name_demo" type="name_demo" output="screen">
<!-- local serial -->
<param name="serial" value="10" />
</node>
</launch>
使用两种命名空间句柄获取全局和局部参数的用法如下:
#include <ros/ros.h>
int main(int argc, char* argv[])
{
int serial_number = -1;
ros::init(argc, argv, "name_demo");
ros::NodeHandle nh_global;
ros::NodeHandle nh_local("~");
// global namespace
nh_global.getParam("serial", serial_number); // get global serial
nh_global.getParam("name_demo/serial", serial_number); // get local serial. add "name_demo/"
// local namespace
nh_local.getParam("serial", serial_number); // get local serial
nh_local.getParam("/serial", serial_number); // get global serial. add "/"
ros::spin();
return 0;
}
3. rospy中的使用
3.1 get param
rospy.get_param()
parameter1 = rospy.get_param("/param1")
parameter2 = rospy.get_param("/param2", default=222)
3.2 set param
rospy.set_param()
rospy.set_param('/param2',2)
3.3 check param
rospy.has_param()
ifparam3 = rospy.has_param('/param3')
if(ifparam3):
rospy.loginfo('/param3 exists')
else:
rospy.loginfo('/param3 does not exist')
3.4 delete param
rospy.delete_param()
rospy.delete_param('/param2')
3.5 get all param names
rospy.get_param_names()
params = rospy.get_param_names()
rospy.loginfo('param list: %s', params)
3.6 search param
rospy.search_param()
param = rospy.search_param('param_name')
v = rospy.get_param(param)
当有多个名为param_name
参数时,找到离当前node最近的参数名称。
假设当前node在/foo/bar
层级, rospy.search_param()
会按如下顺序找
/foo/bar/param_name
/foo/param_name
/param_name
4. 在 launch 文件中载入参数
代码修改参数,必须重新编译。
launch文件可以方便的修改参数。
param_demo.launch
<launch>
<!--param-->
<param name="param1" value="1" />
<param name="param2" value="2" />
<!--rosparam-->
<rosparam>
param3: 3
param4: 4
param5: 5
</rosparam>
<node pkg="param_demo" type="param_demo" name="param_demo" output="screen" />
</launch>
5. 参考
roscpp/Overview/Parameter Server
http://wiki.ros.org/roscpp/Overview/Parameter%20Server
rospy/Overview/Parameter Server
http://wiki.ros.org/rospy/Overview/Parameter%20Server