ROS parameter loading mechanism

You can use roslaunch to start the ros node to load the yaml file parameters. You only need to write the param related information in the .launch file, as follows:

<launch>
  <arg name="ns" default="" />
  <node pkg="planning_node" name="planning_node" type="planning_node" output="screen" ns="/$(arg ns)">
    <rosparam command="load" file="$(find planning_node)/param/planning_node.yaml"></rosparam>
  </node>
</launch>

This involves the writing position of rosparam. If it is written in the middle of <node></node>, it means loading the planning_node.yaml parameter into the local namespace, that is, under /planning_node. There is also an ns parameter required in <node> here. Note that this is the prefix of the local namespace and can be added manually during roslaunch. If ns="foo" is set, the parameters of planning_node.yaml will be loaded into /foo/planning_node.

If rosparam is written outside <node></node>, it will be as follows:

<launch>
  <arg name="ns" default="" />
  <rosparam command="load" file="$(find planning_node)/param/planning_node.yaml"></rosparam>
  <node pkg="planning_node" name="planning_node" type="planning_node" output="screen" ns="/$(arg ns)">
  </node>
</launch>

It means loading the planning_node.yaml parameters into the global namespace, that is, /under

At this point, the parameter loading process is completed. The parameter loading results can rosparam listbe viewed through commands. Note that the parameters will be stored in roscore. If you want to completely clear the parameter cache, you need to restart roscore. Later, you need to read the parameters in the program, and you need to use it ros::NodeHandlehere to read the parameters.

There are basically two ways to initialize ros::NodeHandle. One is to call the default constructor, that is ros::NodeHandle n1, here n1 is the nodehandle in the global namespace. The other is to construct with the "~" parameter, that is ros::NodeHandle n2("~"), here n2 is the nodehandle under the local namespace. After creating the nodehandle, you can use the methods in the object getParamto read parameters.

n1.getParam("bar", temp_str);
n2.getParam("bar", temp_str);

Here, the get of n1 is used to obtain the parameters in the global namespace, that is, the parameters under "/bar" are found. The get of n2 gets the parameters under the local namespace, that is, it looks for the parameters under "/planning_node/bar". If ns is set to "ns=foo", it looks for "/foo/planning/bar" " parameters under.

In order to avoid conflicts in the namespaces of multiple nodes, it is recommended that both loading parameters and reading parameters be written in local namespaces.

Guess you like

Origin blog.csdn.net/zmhzmhzm/article/details/130265424