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

Revision history [back]

click to hide/show revision 1
initial version

It seems the ros::spin() is outside the while(1).

There is also no ros::Rate::sleep() in the body of either of your while-loops.

Finally: if you need to run a while-loop, make sure to use something like while (ros::ok()) { .. }. This makes your node check whether it has been requested to exit.

See also wiki/roscpp/Overview/Callbacks and Spinning.

It seems the ros::spin() is outside the while(1).

There is also no ros::Rate::sleep() in the body of either of your while-loops.

Finally: if you need to run a while-loop, make sure to use something like while (ros::ok()) { .. }. This makes your node check whether it has been requested to exit.

See also wiki/roscpp/Overview/Callbacks and Spinning.


Edit: (nested) while-loops are not necessarily a good idea with an event-based, asynchronous framework such as ROS. You may want to re-evaluate your approach and possibly restructure your node.

It seems the ros::spin() is outside the while(1).. That is ok, but I'm also slightly confused by the structure of your node: what is the while-loop supposed to do?

There is also no ros::Rate::sleep() in the body of either of your while-loops.

Finally: if you need to run a while-loop, make sure to use something like while (ros::ok()) { .. }. This makes your node check whether it has been requested to exit.

See also wiki/roscpp/Overview/Callbacks and Spinning.


Edit: (nested) while-loops are not necessarily a good idea with an event-based, asynchronous framework such as ROS. You may want to re-evaluate your approach and possibly restructure your node.

It seems the ros::spin() is outside the while(1). That is ok, but I'm also slightly confused by the structure of your node: what is the while-loop supposed to do?

There is also no ros::Rate::sleep() in the body of either of your while-loops.

Random other note: you have two calls to ros::init(..) in there, I'm not sure that is going to work (or at the very least: it's not needed).

Finally: if you need to run a while-loop, make sure to use something like while (ros::ok()) { .. }. This makes your node check whether it has been requested to exit.

See also wiki/roscpp/Overview/Callbacks and Spinning.


Edit: (nested) while-loops are not necessarily a good idea with an event-based, asynchronous framework such as ROS. You may want to re-evaluate your approach and possibly restructure your node.

It

and sending a message to the topic results in nothing

This is most likely caused by the fact that your node does not handle any incoming events (ie: msgs on topics).

To remedy that you'd need to either call ros::spin() or periodically call ros::spinOnce().

Your node does the former, but only after it has progressed through the while-loops. And this is the problem, as in order to get through the loops, your node needs to handle incoming events (otherwise message will never be set to 1). This seems the ros::spin() to be a deadlock situation.

I would suggest to avoid the two nested while-loops, as I believe they are not needed and only complicate things.

What are you actually trying to do and what are the two while-loops supposed to achieve?

Some other things I noticed:

  • there is outside the while(1). That is ok, but I'm also slightly confused by the structure of your node: what is the while-loop supposed to do?

    There is also no ros::Rate::sleep() in the body of either of your while-loops.

    Random other note: -loops (busy-wait, 100% cpu usage)

  • you have two calls to ros::init(..) in there, I'm not sure that is going to work (or at the very least: it's not needed).

    Finally: needed)

However, when i use this node, it cannot be close by crtl-c

if you need to run a while-loop, make sure to use something like while (ros::ok()) { .. }. This makes your node check whether it has been requested to exit.

See also wiki/roscpp/Overview/Callbacks and Spinning.


Edit: (nested) while-loops are not necessarily a good idea with an event-based, asynchronous framework such as ROS. You may want to re-evaluate your approach and possibly restructure your node.

and sending a message to the topic results in nothing

This is most likely caused by the fact that your node does not handle any incoming events (ie: msgs on topics).

To remedy that you'd need to either call ros::spin() or periodically call ros::spinOnce().

Your node does the former, but only after it has progressed through the while-loops. And this is the problem, as in order to get through the loops, your node needs to handle incoming events (otherwise message will never be set to 1). This seems to be a deadlock situation.

I would suggest to avoid the two nested while-loops, as I believe they are not needed and only complicate things.

What are you actually trying to do and what are the two while-loops supposed to achieve?

Some other things I noticed:

  • there is no ros::Rate::sleep() in the body of either of your while-loops (busy-wait, 100% cpu usage)
  • you have two calls to ros::init(..) in there, I'm not sure that is going to work (or at the very least: it's not needed)
  • avoid using final for anything (unless you want to declare something as final). It is a C++11 keyword: final specifier (since C++11)

However, when i use this node, it cannot be close by crtl-c

if you need to run a while-loop, make sure to use something like while (ros::ok()) { .. }. This makes your node check whether it has been requested to exit.

See also wiki/roscpp/Overview/Callbacks and Spinning.


Edit: (nested) while-loops are not necessarily a good idea with an event-based, asynchronous framework such as ROS. You may want to re-evaluate your approach and possibly restructure your node.