Ask Your Question
0

Retrieving parameters from nodes (rclpy)

asked 2019-03-07 09:00:02 -0500

Raskkii gravatar image

I've followed the tutorial here on how to pass parameters to ROS2 nodes here:

https://index.ros.org/doc/ros2/Tutori...

I've made it to the point where the following command on my command line:

ros2 run my_package my_node __params:=/path/to/params.yaml

produces the correct result, meaning I can see the parameters on the command-line output.

However, how would you retrieve those parameters from a python script?

I've tried the following:

rclpy.init()
node = rclpy.create_node("my_node")
param = node.get_parameter('~param_from_yaml')
print(str(param.value))

The output of that is always None. What am I doing wrong or can this even my achieved?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2019-03-15 15:59:42 -0500

marguedas gravatar image

I don't think the ~ expansion has been implemented, as the parameters exist on the nodes directly, providing just the name of the parameter (without ~) should be enough.

e.g. params.yaml:

minimal_publisher:
    ros__parameters:
        some_int: 42

diff on publisher_member_function:

--- a/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher/publisher_member_function.py
+++ b/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher/publisher_member_function.py
@@ -33,6 +33,9 @@ class MinimalPublisher(Node):
         self.publisher_.publish(msg)
         self.get_logger().info('Publishing: "%s"' % msg.data)
         self.i += 1
+        print()
+        print(self.get_parameter('some_int')._value)
+        print()


 def main(args=None):

in terminal:

$ ros2 run examples_rclpy_minimal_publisher publisher_member_function __params:=params.yaml
[INFO] [minimal_publisher]: Publishing: "Hello World: 0"

42

[INFO] [minimal_publisher]: Publishing: "Hello World: 1"

42

HTH,

edit flag offensive delete link more

Comments

It worked. Thank you so much! Such a simple thing to overlook.

Raskkii gravatar image Raskkii  ( 2019-03-16 09:29:55 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2019-03-07 09:00:02 -0500

Seen: 1,165 times

Last updated: Mar 15 '19