如何使用 xml 文档中的启动文件来获取要在 cpp 文件中使用的参数
How to use an launch file in xml document to get the parameters to be used in a cpp file
在ROS的.cpp文件中需要设置一些参数。这些参数将从 launch file.How 中获取,以使用 ros
从 .cpp 文件中的 launch 文件设置这些参数
首先,我建议您遵循 this simple tutorial and read the rosparam and roscpp Parameter Server 文档。一切都应该变得很简单。
一个简单的启动文件示例如下:
<launch>
<rosparam command="load" file="$(find pkg_name)/path/file_name.yaml" />
<node pkg="pkg_name" type="node_type" name="node_name" />
</launch>
你应该在哪里设置你自己的包名和其他变量。您可以在以下链接中找到所有这些参数的含义:param and node.
你必须从 C++ 源代码 中检索这些参数(看看 here):
/* this handle let you access all the parameters of the node (and other stuffs) */
ros::NodeHandle node_handle = new ros::NodeHandle("~");
/* for example if I need to retrieve a list of double written in the YAML file,
I could call the getParam() method to store it into my_double_list std::vector */
std::vector<double> my_double_list;
node_handle.getParam("name_of_the_list_in_YAML_file", my_double_list);
/* print data retrieved */
std::copy(my_double_list.begin(), my_double_list.end(), std::ostream_iterator<double>(std::cout, " "));
YAML 文件 应该是这样的(示例中有 4 个元素):
name_of_the_list_in_YAML_file: [123.11, 0.1, 2013.441, 1.23015e+3]
更多详情:YAML. You can also find the ros::NodeHandle
documentation here.
在ROS的.cpp文件中需要设置一些参数。这些参数将从 launch file.How 中获取,以使用 ros
从 .cpp 文件中的 launch 文件设置这些参数首先,我建议您遵循 this simple tutorial and read the rosparam and roscpp Parameter Server 文档。一切都应该变得很简单。
一个简单的启动文件示例如下:
<launch>
<rosparam command="load" file="$(find pkg_name)/path/file_name.yaml" />
<node pkg="pkg_name" type="node_type" name="node_name" />
</launch>
你应该在哪里设置你自己的包名和其他变量。您可以在以下链接中找到所有这些参数的含义:param and node.
你必须从 C++ 源代码 中检索这些参数(看看 here):
/* this handle let you access all the parameters of the node (and other stuffs) */
ros::NodeHandle node_handle = new ros::NodeHandle("~");
/* for example if I need to retrieve a list of double written in the YAML file,
I could call the getParam() method to store it into my_double_list std::vector */
std::vector<double> my_double_list;
node_handle.getParam("name_of_the_list_in_YAML_file", my_double_list);
/* print data retrieved */
std::copy(my_double_list.begin(), my_double_list.end(), std::ostream_iterator<double>(std::cout, " "));
YAML 文件 应该是这样的(示例中有 4 个元素):
name_of_the_list_in_YAML_file: [123.11, 0.1, 2013.441, 1.23015e+3]
更多详情:YAML. You can also find the ros::NodeHandle
documentation here.