ROS Actions are defined in action/*.action files. For each action ROS requires to have 3 separate messages: Goal, Feedback, Result (see ROS documentation for defining action in ROS2 or ROS1). On top of that ROS will create for each such message an Action message where the original message (Goal, Feedback, Result) + action metadata is stored (goal id, status, etc.).
In order to work with ROS actions from jrosactionlib users need to:
To simplify this process users are encouraged to use msgmonster application.
In this example we are going to execute actions in Fibonacci action server using Java.
Before moving further we need to create and run Fibonacci action server based on instructions from ROS documentation:
ROS action file Fibonacci.action looks something like this:
int32 order --- int32[] sequence --- int32[] partial_sequence
We need to map it to corresponding jrosactionlib action definitions. Use final results of these definitions for ROS2 or ROS1
Once we have Fibonacci action definitions are ready we can use them to execute actions on Fibonacci action server.
Required Java dependencies:
Code:
var clientFactory = new JRos2ClientFactory(); var actionClientFactory = new JRos2ActionClientFactory(); try (var client = clientFactory.createClient(); var actionClient = actionClientFactory .createClient(client, new FibonacciActionDefinition(), "fibonacci")) { var goal = new FibonacciGoalMessage().withOrder(new Int32Message().withData(13)); var result = actionClient.sendGoalAsync(goal).get(); System.out.println(result); }
Required Java dependencies:
var clientFactory = new JRos1ClientFactory(); var actionClientFactory = new JRos1ActionClientFactory(); try (var client = clientFactory.createClient("http://127.0.0.1:11311/"); var actionClient = actionClientFactory .createClient(client, new FibonacciActionDefinition(), "fibonacci")) { var goal = new FibonacciGoalMessage().withOrder(new Int32Message().withData(5)); var result = actionClient.sendGoalAsync(goal).get(); System.out.println(result); }