0
$\begingroup$

Rosanswers logo

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?


Originally posted by Andromeda on ROS Answers with karma: 893 on 2021-07-14

Post score: 2

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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).


Originally posted by mgruhler with karma: 12390 on 2021-07-15

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by Andromeda on 2021-07-15:
Have you ever tried to run that tutorial? It doesn't work... the tutorial itself is buggy

Comment by Andromeda on 2021-07-15:
I meant... it doesn't even compile

Comment by mgruhler on 2021-07-16:
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.