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

rosserial spinOnce() blocks if not connected to ROS

asked 2011-09-17 14:53:56 -0500

dan gravatar image

Using rosserial with an Arduino, I find that spinOnce() blocks if not connected to ROS, which makes sense (since how can you locally know when to go), so I am wondering if the standard call (for nodeHandle nh) inside the loop() should be:

if (nh.connected()) nh.spinOnce();
else { with having disconnected from ROS and make the robot safe....then wait for reconnection....}

I saw on the logging page:

//wait until you are actually connected
while (!nh.connected() ){

but from what I am seeing with the arduino, you would get the same result with


alone, since it blocks. Is that correct?

Once ROS has disconnected, is there some way to put the arduino in a state where it can accept a reconnect cleanly?

Thanks again for all your help with this.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2011-09-18 17:12:01 -0500

adasta gravatar image

Hi Dan,

spinOnce should never block. It just checks to see if new serial data has come in and fires callback messages if it has received a full message. It is supposed to work just like spinOnce in roscpp. All it handles is com and callbacks.

Under what situations are you seeing it block?

edit flag offensive delete link more

answered 2020-02-24 07:46:48 -0500

Small typo, please use:

if (!nh.connected()) nh.spinOnce();
edit flag offensive delete link more


Instead of?

gvdhoorn gravatar image gvdhoorn  ( 2020-02-24 10:15:12 -0500 )edit

instead of the original post if (nh.connected()) nh.spinOnce();

tdam2112 gravatar image tdam2112  ( 2020-02-25 15:37:07 -0500 )edit

Is this a good method for safety though? I'm struggling to find safety implementations anywhere with regards to ros arduino communication.

tdam2112 gravatar image tdam2112  ( 2020-02-25 15:39:04 -0500 )edit

answered 2011-09-19 16:44:15 -0500

dan gravatar image

As I worked more with the code, I see that you are correct, it was not spinOnce() that was blocking, but rather my proposed "solution," using nh.connected() that was actually causing the block. Also, I did not realize the importance of getting spinOnce() called frequently enough, so I was losing the connection during some tasks. Can you give me a rough time limit between those calls?

So my question boils down to what is the best way to determine if the connection to ROS is lost? Also, do you have a suggestion for the best method for waiting for reconnection other than

if (!nh.connected()) spinOnce();

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2011-09-17 14:53:56 -0500

Seen: 3,664 times

Last updated: Feb 24 '20