问题
Migrating ros1 package to ros2 and couldn't figure how to launch with a paramter in ros2.
With ros1 I have a launch file that refers to a config file and from cpp code I use node.getParam
launch file:
<launch>
<arg name="node_name" default="collector" />
<arg name="config_file" default="" />
<node name="$(arg node_name)" pkg="collector" type="collector" respawn="true">
<rosparam if="$(eval config_file!='')" command="load" file="$(arg config_file)"/>
</node>
</launch>
config file:
my_param: 5
cpp code:
double my_param = 0;
n.getParam("my_param", my_param);
My question is how would that translate to ROS2?
回答1:
For ROS2, please see this link and this link.
For reference, as of this post, ROS2 has had three releases. While Crystal Clemmens supports Python nodes, the previous release, Bouncy Bolson, does not. Similarly, future releases may add additional support for how/where to input config files.
The ROS2 Wiki references using a .yaml config file to store parameters and, when running the ROS2 node, passing the config file as a command line parameter.
ROS2 Wiki: Node Arguments
Example demo_params.yaml
file:
talker:
ros__parameters:
some_int: 42
a_string: "Hello world"
some_lists:
some_integers: [1, 2, 3, 4]
some_doubles : [3.14, 2.718]`
Then run the following:
ros2 run demo_nodes_cpp talker __params:=demo_params.yaml
回答2:
I wanted to expand a bit on the answer from @PSA which is really good.
As stated, put your parameters in a .yaml
file
talker:
ros__parameters:
some_int: 42
a_string: "Hello world"
some_lists:
some_integers: [1, 2, 3, 4]
some_doubles : [3.14, 2.718]
To use it in a ROS2 launch file, now built in Python, pass the .yaml
file path as an argument parameters
as described here https://github.com/ros2/launch/blob/master/launch_ros/launch_ros/actions/node.py like so:
ld = LaunchDescription([
launch_ros.actions.Node(
package='nmea_navsat_driver', node_executable='nmea_serial_driver_node', output='screen',
parameters=["config.yaml"])
])
here config.yaml
is the parameter file.
If you want to start your node manually, start it as stated in @PSAs answer:
ros2 run demo_nodes_cpp talker __params:=demo_params.yaml
update: parameters expect a list of config files.
来源:https://stackoverflow.com/questions/53917469/how-to-launch-a-node-with-a-parameter-in-ros2