|
| 1 | +// Copyright (c) 2019 Samsung Research America |
| 2 | +// Copyright (c) 2020 Davide Faconti |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +#ifndef BEHAVIOR_TREE_BT_ACTION_NODE_HPP_ |
| 17 | +#define BEHAVIOR_TREE_BT_ACTION_NODE_HPP_ |
| 18 | + |
| 19 | +#include <behaviortree_cpp_v3/action_node.h> |
| 20 | +#include <behaviortree_cpp_v3/bt_factory.h> |
| 21 | +#include <ros/ros.h> |
| 22 | +#include <actionlib/client/simple_action_client.h> |
| 23 | + |
| 24 | +namespace BT |
| 25 | +{ |
| 26 | + |
| 27 | +template<class ActionT> |
| 28 | +class RosActionNode : public BT::ActionNodeBase |
| 29 | +{ |
| 30 | +protected: |
| 31 | + |
| 32 | + RosActionNode(ros::NodeHandle& nh, const std::string& name, const BT::NodeConfiguration & conf): |
| 33 | + BT::ActionNodeBase(name, conf), node_(nh) |
| 34 | + { |
| 35 | + const std::string server_name = getInput<std::string>("server_name").value(); |
| 36 | + action_client_ = std::make_shared<ActionClientType>( node_, server_name, true ); |
| 37 | + } |
| 38 | + |
| 39 | +public: |
| 40 | + |
| 41 | + using BaseClass = RosActionNode<ActionT>; |
| 42 | + using ActionClientType = actionlib::SimpleActionClient<ActionT>; |
| 43 | + using ActionType = ActionT; |
| 44 | + using GoalType = typename ActionT::_action_goal_type::_goal_type; |
| 45 | + using ResultType = typename ActionT::_action_result_type::_result_type; |
| 46 | + |
| 47 | + RosActionNode() = delete; |
| 48 | + |
| 49 | + virtual ~RosActionNode() = default; |
| 50 | + |
| 51 | + static PortsList providedPorts() |
| 52 | + { |
| 53 | + return { |
| 54 | + InputPort<std::string>("server_name", "name of the Action Server"), |
| 55 | + InputPort<unsigned>("timeout", 500, "timeout to connect (milliseconds)") |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + virtual bool sendGoal(GoalType& goal) = 0; |
| 60 | + |
| 61 | + /// Method (to be implemented by the user) to receive the reply. |
| 62 | + /// User can decide which NodeStatus it will return (SUCCESS or FAILURE). |
| 63 | + virtual NodeStatus onResult( const ResultType& res) = 0; |
| 64 | + |
| 65 | + |
| 66 | + enum FailureCause{ |
| 67 | + MISSING_SERVER = 0, |
| 68 | + ABORTED_BY_SERVER = 1, |
| 69 | + REJECTED_BY_SERVER = 2 |
| 70 | + }; |
| 71 | + |
| 72 | + /// Called when a service call failed. Can be overriden by the user. |
| 73 | + virtual NodeStatus onFailedRequest(FailureCause failure) |
| 74 | + { |
| 75 | + return NodeStatus::FAILURE; |
| 76 | + } |
| 77 | + |
| 78 | + virtual void halt() override |
| 79 | + { |
| 80 | + if( status() == NodeStatus::RUNNING ) |
| 81 | + { |
| 82 | + action_client_->cancelGoal(); |
| 83 | + } |
| 84 | + setStatus(NodeStatus::IDLE); |
| 85 | + } |
| 86 | + |
| 87 | +protected: |
| 88 | + |
| 89 | + std::shared_ptr<ActionClientType> action_client_; |
| 90 | + |
| 91 | + ros::NodeHandle& node_; |
| 92 | + |
| 93 | + BT::NodeStatus tick() override |
| 94 | + { |
| 95 | + |
| 96 | + unsigned msec = getInput<unsigned>("timeout").value(); |
| 97 | + ros::Duration timeout(static_cast<double>(msec) * 1e-3); |
| 98 | + |
| 99 | + bool connected = action_client_->waitForServer(timeout); |
| 100 | + if( !connected ){ |
| 101 | + return onFailedRequest(MISSING_SERVER); |
| 102 | + } |
| 103 | + |
| 104 | + // first step to be done only at the beginning of the Action |
| 105 | + if (status() == BT::NodeStatus::IDLE) { |
| 106 | + // setting the status to RUNNING to notify the BT Loggers (if any) |
| 107 | + setStatus(BT::NodeStatus::RUNNING); |
| 108 | + |
| 109 | + GoalType goal; |
| 110 | + bool valid_goal = sendGoal(goal); |
| 111 | + if( !valid_goal ) |
| 112 | + { |
| 113 | + return NodeStatus::FAILURE; |
| 114 | + } |
| 115 | + |
| 116 | + action_client_->sendGoal(goal); |
| 117 | + } |
| 118 | + |
| 119 | + // RUNNING |
| 120 | + auto action_state = action_client_->getState(); |
| 121 | + |
| 122 | + // Please refer to these states |
| 123 | + |
| 124 | + if( action_state == actionlib::SimpleClientGoalState::PENDING || |
| 125 | + action_state == actionlib::SimpleClientGoalState::ACTIVE ) |
| 126 | + { |
| 127 | + return NodeStatus::RUNNING; |
| 128 | + } |
| 129 | + else if( action_state == actionlib::SimpleClientGoalState::SUCCEEDED) |
| 130 | + { |
| 131 | + return onResult( *action_client_->getResult() ); |
| 132 | + } |
| 133 | + else if( action_state == actionlib::SimpleClientGoalState::ABORTED) |
| 134 | + { |
| 135 | + return onFailedRequest( ABORTED_BY_SERVER ); |
| 136 | + } |
| 137 | + else if( action_state == actionlib::SimpleClientGoalState::REJECTED) |
| 138 | + { |
| 139 | + return onFailedRequest( REJECTED_BY_SERVER ); |
| 140 | + } |
| 141 | + else |
| 142 | + { |
| 143 | + // FIXME: is there any other valid state we should consider? |
| 144 | + throw std::logic_error("Unexpected state in RosActionNode::tick()"); |
| 145 | + } |
| 146 | + } |
| 147 | +}; |
| 148 | + |
| 149 | + |
| 150 | +/// Method to register the service into a factory. |
| 151 | +/// It gives you the opportunity to set the ros::NodeHandle. |
| 152 | +template <class DerivedT> static |
| 153 | + void RegisterRosAction(BT::BehaviorTreeFactory& factory, |
| 154 | + const std::string& registration_ID, |
| 155 | + ros::NodeHandle& node_handle) |
| 156 | +{ |
| 157 | + NodeBuilder builder = [&node_handle](const std::string& name, const NodeConfiguration& config) { |
| 158 | + return std::make_unique<DerivedT>(node_handle, name, config ); |
| 159 | + }; |
| 160 | + |
| 161 | + TreeNodeManifest manifest; |
| 162 | + manifest.type = getType<DerivedT>(); |
| 163 | + manifest.ports = DerivedT::providedPorts(); |
| 164 | + manifest.registration_ID = registration_ID; |
| 165 | + const auto& basic_ports = RosActionNode< typename DerivedT::ActionType>::providedPorts(); |
| 166 | + manifest.ports.insert( basic_ports.begin(), basic_ports.end() ); |
| 167 | + factory.registerBuilder( manifest, builder ); |
| 168 | +} |
| 169 | + |
| 170 | + |
| 171 | +} // namespace BT |
| 172 | + |
| 173 | +#endif // BEHAVIOR_TREE_BT_ACTION_NODE_HPP_ |
0 commit comments