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.
this makes complete sense to me: you already have the answer, so what would the node still have to do?
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.
Explaining it ruins it, but: 42 (number).
ah, I have the answer. You should have highlighted that ;)
Well, you already emphasised 42 often enough .. :)
I have the same issue. It freezes after a few iterations.
Did you fix this problem?
I have the same poblem. Isn't it related to
rate.sleep()
itself, rather than setting the parameter.