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

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

Did you fix this problem?

Andromeda gravatar image Andromeda  ( 2022-05-24 05:58:17 -0500 )edit

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

Fellfalla gravatar image Fellfalla  ( 2022-09-14 14:46:37 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-09-14 14:54:36 -0500

Fellfalla gravatar image

Rather than rate.sleep()

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

edit flag offensive delete link more

Question Tools

1 follower

Stats

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

Seen: 1,277 times

Last updated: Sep 14 '22