Robotics StackExchange | Archived questions

ROS2 Node sleeps forever after setting parameter

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.

Asked by RodBelaFarin on 2020-04-22 07:41:14 UTC

Comments

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?

Asked by gvdhoorn on 2020-04-22 07:51:44 UTC

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.

Asked by RodBelaFarin on 2020-04-22 08:08:17 UTC

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

Asked by gvdhoorn on 2020-04-22 08:10:58 UTC

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

Asked by RodBelaFarin on 2020-04-22 08:14:33 UTC

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

Asked by gvdhoorn on 2020-04-22 08:23:48 UTC

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

Asked by felipecode on 2021-04-19 16:15:43 UTC

Did you fix this problem?

Asked by Andromeda on 2022-05-24 05:58:17 UTC

I have the same poblem. Isn't it related to rate.sleep() itself, rather than setting the parameter.

Asked by Fellfalla on 2022-09-14 14:46:37 UTC

Answers

Rather than rate.sleep()

try something like rclpy.spin_once(myfirstnode, timeout_sec=1.0)

Asked by Fellfalla on 2022-09-14 14:54:36 UTC

Comments