ROS2 Node sleeps forever after setting parameter

asked 2020-04-22 07:41:14 -0500

RodBelaFarin gravatar image

In the following simple ROS2 Python Node I am just trying to change a parameter from the CLI and print it within the Node to the terminal.

As soon as I use ros2 param set and change the value, I am stuck in sleep() function of the while loop.

I am using Ubuntu 18.04 in a VM with ROS2 Eloquent installed.

#!/usr/bin/env python3

import rclpy

def main():
    rclpy.init()
    myfirstnode = rclpy.create_node('my_param_node')
    myfirstnode.declare_parameter('my_param', 13)
    rate = myfirstnode.create_rate(1.0) #define a rate of 1 Hz
    while rclpy.ok():
        try:
            print('===1===')
            rclpy.spin_once(myfirstnode)
            print('===2===')
        except KeyboardInterrupt:
            pass
        print('===3===')
        print(myfirstnode.get_parameter('my_param').value)
        print('===4===')
        rate.sleep()
        print('===5===')
    myfirstnode.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

I set the value of my_param to 42 and the terminal output looks like that:

===1===
===2===
===3===
13
===4===
===5===
===1===
===2===
===3===
13
===4===
===5===
===1===
===2===
===3===
42
===4===

And then it stops.

edit retag flag offensive close merge delete

Comments

1

I set the value of my_param to 42 [..] and then it stops.

this makes complete sense to me: you already have the answer, so what would the node still have to do?

gvdhoorn gravatar image gvdhoorn  ( 2020-04-22 07:51:44 -0500 )edit

No, I don't get it. It prints 42, it prints ===4=== and then it sleeps forever. I set the rate of the Node to 1.0, so I am expecting it to print another ===5=== after the time period, which is not happening. Next it should loop again, because rclpy is still ok(). Next it should go into the try block and print another ===1=== and so on.

RodBelaFarin gravatar image RodBelaFarin  ( 2020-04-22 08:08:17 -0500 )edit
1

Explaining it ruins it, but: 42 (number).

gvdhoorn gravatar image gvdhoorn  ( 2020-04-22 08:10:58 -0500 )edit

ah, I have the answer. You should have highlighted that ;)

RodBelaFarin gravatar image RodBelaFarin  ( 2020-04-22 08:14:33 -0500 )edit

Well, you already emphasised 42 often enough .. :)

gvdhoorn gravatar image gvdhoorn  ( 2020-04-22 08:23:48 -0500 )edit

I have the same issue. It freezes after a few iterations.

felipecode gravatar image felipecode  ( 2021-04-19 16:15:43 -0500 )edit