What is the use of while loop when sending action messages

In unit 8 of ROS Basics in 5 Days CPP, i saw the following code within the exercise answers. I was confused as to the use of the while loop in this case.

image

I tried the same code, without the while loop and it still worked fine. So what is the use of the while loop?

1 Like

Hi Izzat,
your question is very good. That part of code is just a dirty hack to avoid one problem of ROS.

It happens that, sometimes, when you try to publish in a publisher that has just been created, the connection to the publisher is still not completed, hence, your publish will fail. If you check the solution, you will see that the publisher is created just a few lines above that code that you are indicating.

In order to avoid that problem, we just decided to retry for 4 times, once every second. That ensures that the publish will be achieved.

That is a dirty solution! If you want the professional solution to that problem, check this other post of the Forum: How to publish once (only one message) into a topic and get it to work

2 Likes