ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

Cannot retrieve parameters from YAML file (even if the file is found)

asked 2021-07-14 14:56:52 -0600

Andromeda gravatar image

Hi,

I wrote a very simple program in python to read parameters from a YAML file. The YAML file is the following:

mqtt_node:
  ros__parameters:
    variable: 9494.98

and the minimal code of the node is this one:

class mqtt_class(Node):

    def __init__(self):

        super().__init__('mqtt_class')

        print()
        print(self.get_parameter('variable')._value)   # <- here is the problem
        print()
)


def main(args = None):

    rclpy.init(args = args)

    my_publisher = mqtt_class()

    rclpy.spin(my_publisher)


    my_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':

    main()

This is the error I get:

 Traceback (most recent call last):
   File "/home/kkk/workspace/install/xxx/lib/xxx/xxx", line 11, in <module>
   load_entry_point('xxx', 'console_scripts', 'xxx')()
 File "/home/kkk/workspace/build/xxx/xxx/xxx.py", line 108, in main
  my_publisher = mqtt_class()
  File "/home/kkk/workspace/build/xxx/xxx/xxx.py", line 54, in __init__
    print(self.get_parameter('variable')._value)
   File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/node.py", line 512, in get_parameter
    raise ParameterNotDeclaredException(name)
 rclpy.exceptions.ParameterNotDeclaredException: ('Invalid access to undeclared parameter(s)', 'variable')
[ERROR] [zzzz-2]: process has died [pid 19805, exit code 1, cmd '/home/kkk/workspace/install/xxx/lib/xxx/xxx --ros-args -r __xxx:=xxx --params-file /home/kkk/workspace/install/xxx/share/xxx/params.yaml'].

What is wrong with the code?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-07-15 00:32:27 -0600

mgruhler gravatar image

The error message states

"Invalid access to undeclared parameter(s)"

I don't see any self.declare_parameter(...) call in your code. I guess you missed that one (see also the docs).

edit flag offensive delete link more

Comments

Have you ever tried to run that tutorial? It doesn't work... the tutorial itself is buggy

Andromeda gravatar image Andromeda  ( 2021-07-15 15:08:21 -0600 )edit

I meant... it doesn't even compile

Andromeda gravatar image Andromeda  ( 2021-07-15 15:10:08 -0600 )edit

I have. And everything works.

I can also reproduce your error by commenting the declare_parameter call.

I'd doublecheck that you actually have the call in your code. Also, if you could provide the error message of the "doesn't even compile" step, we could probably help with that.

mgruhler gravatar image mgruhler  ( 2021-07-16 07:38:48 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2021-07-14 14:56:52 -0600

Seen: 1,021 times

Last updated: Jul 15 '21