Skip to content

Commit 623ba15

Browse files
authored
Add the ability for ros actions nodes to get access to the result if available on failure (#79)
1 parent 0cc2da2 commit 623ba15

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

behaviortree_ros2/include/behaviortree_ros2/bt_action_node.hpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <memory>
1919
#include <string>
20+
#include <optional>
2021
#include <rclcpp/executors.hpp>
2122
#include <rclcpp/allocator/allocator_common.hpp>
2223
#include "behaviortree_cpp/action_node.h"
@@ -156,8 +157,15 @@ class RosActionNode : public BT::ActionNodeBase
156157
}
157158

158159
/** Callback invoked when something goes wrong.
160+
* The result is provided if it is available.
159161
* It must return either SUCCESS or FAILURE.
160162
*/
163+
virtual BT::NodeStatus onFailure(ActionNodeErrorCode error,
164+
const std::optional<WrappedResult>&)
165+
{
166+
return onFailure(error);
167+
}
168+
161169
virtual BT::NodeStatus onFailure(ActionNodeErrorCode /*error*/)
162170
{
163171
return NodeStatus::FAILURE;
@@ -394,7 +402,7 @@ inline NodeStatus RosActionNode<T>::tick()
394402

395403
if(!setGoal(goal))
396404
{
397-
return CheckStatus(onFailure(INVALID_GOAL));
405+
return CheckStatus(onFailure(INVALID_GOAL, {}));
398406
}
399407

400408
typename ActionClient::SendGoalOptions goal_options;
@@ -437,7 +445,7 @@ inline NodeStatus RosActionNode<T>::tick()
437445
// Check if server is ready
438446
if(!action_client->action_server_is_ready())
439447
{
440-
return onFailure(SERVER_UNREACHABLE);
448+
return onFailure(SERVER_UNREACHABLE, {});
441449
}
442450

443451
future_goal_handle_ = action_client->async_send_goal(goal, goal_options);
@@ -464,7 +472,7 @@ inline NodeStatus RosActionNode<T>::tick()
464472
{
465473
if((now() - time_goal_sent_) > timeout)
466474
{
467-
return CheckStatus(onFailure(SEND_GOAL_TIMEOUT));
475+
return CheckStatus(onFailure(SEND_GOAL_TIMEOUT, {}));
468476
}
469477
else
470478
{
@@ -495,11 +503,11 @@ inline NodeStatus RosActionNode<T>::tick()
495503
{
496504
if(result_.code == rclcpp_action::ResultCode::ABORTED)
497505
{
498-
return CheckStatus(onFailure(ACTION_ABORTED));
506+
return CheckStatus(onFailure(ACTION_ABORTED, result_));
499507
}
500508
else if(result_.code == rclcpp_action::ResultCode::CANCELED)
501509
{
502-
return CheckStatus(onFailure(ACTION_CANCELLED));
510+
return CheckStatus(onFailure(ACTION_CANCELLED, result_));
503511
}
504512
else
505513
{

0 commit comments

Comments
 (0)