Skip to content

Commit

Permalink
Add Action Client C++ examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobperron committed Oct 11, 2018
1 parent 2fb606c commit 95f6ad3
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion articles/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,62 @@ Other features from ROS 1 Simple Action Server could also be incorporated in the
#### Action client usage
TODO
Creating an action client:
```c++
#include "example_interfaces/action/fibonacci.hpp"
using Fibonacci = example_interfaces::action::Fibonacci;
...
auto node = rclcpp::Node::make_shared("my_node");
auto action_client = node->create_action_client<Fibonacci>("fibonacci");
```

Sending a goal:

```c++
// Populate goal request
auto goal_msg = Fibonacci::Goal();
// ...

// Send goal (synchronous)
std::shared_ptr<rclcpp::GoalID> goal_id = action_client->send_goal(goal_msg);

// Check if accepted
if (goal_id)
{
// Goal accepted!
}
else
{
// Goal rejected :(
}
```
Similar to ROS services, sending a goal is synchronous.
Optionally, callbacks for goal feedback and goal results can be registered:
```c++
// Send goal (synchronous)
std::shared_ptr<rclcpp::GoalID> goal_id = action_client->send_goal(goal_msg, feedback_callback, result_callback);
```

RFC: Alternatively, a future could be used in replace of the result callback (and feedback callback?).

This comment has been minimized.

Copy link
@sloretz

sloretz Oct 11, 2018

Contributor

Recommend auto future = action_client->async_send_goal(goal_msg, feedback_callback) to match service client->async_send_request(...) https://github.com/ros2/examples/blob/3b60ee1b2e4712d329511531107ec8d88eb03cd7/rclcpp/minimal_client/main.cpp#L38



Requesting a goal be canceled:

```c++
// Synchronous
if (action_client->cancel_goal(goal_id))
{
// Accepted
}
else
{
// Rejected :(
}
```
### Python
Expand Down

0 comments on commit 95f6ad3

Please sign in to comment.