ROS参数服务器是一种共享的、多变量字典,能够通过网络访问的字典。节点运行的时候将通过这个服务器来存储、检索参数,他不是为高性能涉及的,最好将这些变量用于静态的(static),非二进制的数据(non-binary),最适合的就是一些配置参数。服务器上的数据是全局可见的,我们可以轻而易举的检索系统的配置状态并在必要时修改。
rosparam list
列出参数清单rosparam get para_key
显示参数值rosparam set param_key param_value
设置参数值rosparam dump file_name
保存参数到文件rosparam load file_name
从文件读取参数rosparam delete param_key
删除参数如果使用的参数比较多,可以使用YAML文件,大概长这个样子:
background_b:255
background_g:86
background_r:69
rosdistro:'melodic'
roslaunch:
...
启动roscore
和turtlesim_node
。
junwu@Room:~$ rosparam list /rosdistro /roslaunch/uris/host_room__38269 /rosversion /run_id /turtlesim/background_b /turtlesim/background_g /turtlesim/background_r
rosparam get /turtlesim/background_b
显示某个参数的值junwu@Room:~$ rosparam get /turtlesim/background_b 255- `rosparam set /turtlesim/background_b 100`设置海龟背景板b参数值为100
如果此时输入rosservice call /clear "{}"
,背景颜色就会改变:
rosparam dump param.yaml
保存当前服务器的所有参数为param.yaml
默认会保存在~
rosparam load param.yaml
读取参数文件内容到core
你可以直接修改*.yaml的值,并将其读到core(间接修改了属于是)
源代码如下:
#include
#include
#include
int main(int argc,char **argv)
{
int red,green,blue;
ros::init(argc,argv,"paraTestNode");
ros::NodeHandle n;
ros::param::get("/turtlesim/background_r",red);
ros::param::get("/turtlesim/background_g",green);
ros::param::get("/turtlesim/background_b",blue);
ROS_INFO("Get Background Color[%d,%d,%d]",red,green,blue);
ros::param::set("/turtlesim/background_r",255);
ros::param::set("/turtlesim/background_g",255);
ros::param::set("/turtlesim/background_b",255);
ROS_INFO("Set Background Color[%d,%d,%d]",255,255,255);
ros::param::get("/turtlesim/background_r",red);
ros::param::get("/turtlesim/background_g",green);
ros::param::get("/turtlesim/background_b",blue);
ROS_INFO("Reget Background Color[%d,%d,%d]",red,green,blue);
ros::service::waitForService("/clear");
ros::ServiceClient clear_background=n.serviceClient<std_srvs::Empty>("/clear");
std_srvs::Empty srv;
clear_background.call(srv);
sleep(1);
return 0;
}
加上以下两句进行编译:
add_executable(changePara src/parameterChange.cpp)
target_link_libraries(changePara ${catkin_LIBRARIES})