本教程介绍了ROS中的参数服务,以及如何通过C++代码的形式获取,设置参数。至于为什么选择deepin而不是ROS通用的ubuntu,也仅仅是为了支持国产系统。鉴于本人水平有限,如哪位攻城狮网友发现本文存在的问题,烦请留言指正,谢谢!
参数的使用与编程
Parameter Server
ROS master中有Parameter server用于存储其他节点的配置参数的全局字典
创建parameter包
catkin_create_pkg learn_parameter roscpp std_srvs
rosparam相关命令行的使用
可用tab进行参数补全
- rosparam list :列出当前的参数
- rosparam get param_key:获取某个参数的值
- rosparam set param_key param_value:设置某个参数的值
- rosparam dump file_name:将参数保存到文件中
- rosparam load file_name:从文件读取参数
- rosparam delete param_key:删除参数
gaoy@msi:~$ rosparam list
/rosdistro
/roslaunch/uris/host_msi__38003
/rosversion
/run_id
/turtlesim/background_b
/turtlesim/background_g
/turtlesim/background_r
gaoy@msi:~$ rosparam get /rosversion
'1.15.14
'
gaoy@msi:~$ rosparam get /rosdistro
gaoy@msi:~$
gaoy@msi:~$ rosparam list
/rosdistro
/roslaunch/uris/host_msi__38003
/rosversion
/run_id
/turtlesim/background_b
/turtlesim/background_g
/turtlesim/background_r
gaoy@msi:~$ rosparam get /turtlesim/background_b
255
gaoy@msi:~$ rosparam set /turtlesim/background_b 120
gaoy@msi:~$ rosparam get /turtlesim/background_b
120
gaoy@msi:~$ rosparam get /rosdistro
'noetic
'
# 下面命令使用后/turtlesim/background_x的参数会生效 用于刷新小乌龟仿真器的背景颜色
gaoy@msi:~$ rosservice call /clear "{}"
# 存储当前环境的参数
gaoy@msi:~$ rosparam dump p.yam
gaoy@msi:~$ cat p.yam
rosdistro: 'noetic
'
roslaunch:
uris: {host_msi__38003: 'http://msi:38003/'}
rosversion: '1.15.14
'
run_id: 58532efc-3695-11ed-a033-f8b54d546b81
turtlesim: {background_b: 120, background_g: 86, background_r: 69}
配置参数基本使用YAML文件形式进行存储
rosdistro: 'noetic
'
roslaunch:
uris: {host_msi__38003: 'http://msi:38003/'}
rosversion: '1.15.14
'
run_id: 58532efc-3695-11ed-a033-f8b54d546b81
turtlesim: {background_b: 0, background_g: 0, background_r: 69}
'
测试源码
#include <string>
#include "ros/ros.h"
#include "std_srvs/Empty.h"
int main(int argc,char ** argv){
int bkg_color_g;
ros::init(argc,argv,"param_config");
ros::NodeHandle n;
ros::param::get("/turtlesim/background_g",bkg_color_g);
ROS_INFO("Get /turtlesim/background_g:%d\n",bkg_color_g);
// ros::param::get("/rosdistro",a);
ros::param::set("/turtlesim/background_g",255);
ros::param::get("/turtlesim/background_g",bkg_color_g);
ROS_INFO("Get /turtlesim/background_g:%d\n",bkg_color_g);
ros::service::waitForService("/clear");
ros::ServiceClient cbc = n.serviceClient<std_srvs::Empty>("/clear");
std_srvs::Empty srv;
cbc.call(srv);
return 0;
}
版权声明:本文为no_pao_spite原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。