'How to access a topic in a ROS2 action server

In an ROS2 action server callback function, I need to access data which is published on some ROS topic. How can I achieve this, in a straightforward and clean way.

Subscribing to the wanted topic in the action server would require a returning callback, which is not straightforward (in my opinion). Is there an easier way to achieve this?



Solution 1:[1]

Subscribing to the wanted topic in the action server would require a returning callback, which is not straightforward (in my opinion). Is there an easier way to achieve this?

you can create a global variable where the type is ros message that you are subscribing, then in callback update the variable with latest received message, in your action server you can use the the global variable, You might wanted to use a mutex if the ros message you are receiving is something like image or pointcloud message

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fet.atas