ros2 async api / figure out if I need to spin

asked 2023-07-24 10:29:23 -0500

JM_ROS gravatar image

Hi,

I don't get how to program a generic library that use any ros2 async API calls under the hood.

As an example I want to program, a generic helper library, that uses service calls under the hood.

void doSomeCall();

As this should be a generic library, we can expect it to be called in 4 relevant scenarios

  • From a constructor of a node (not spinning yet)
  • From a callback (spinning, but we block the spinner)
  • From a callback (spinning, but callback is in a different callback group)
  • From a second thread e.g. driver etc (spinning, but we don't block the spinner)

I managed to get a somewhat usable solution for the first and second case, which is to check,

node.get_node_base_interface()->get_associated_with_executor_atomic()

and depending on this either wait for the future, or call rclcpp::spin_until_future_complete.

But for the other cases I am basically dead in the water. I considered passing in a bool, weather to spin or not, but I don't like this solution a bit, as it breaks abstraction of the helper class, and is super error prone.

Any suggestions, or mystical thread local API calls that can return if you are called from a spinner ?

edit retag flag offensive close merge delete