-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathbt_async_service_node.h
More file actions
179 lines (138 loc) · 5.61 KB
/
bt_async_service_node.h
File metadata and controls
179 lines (138 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2019 Samsung Research America
// Copyright (c) 2020 Davide Faconti
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef BEHAVIOR_TREE_BT_SERVICE_NODE_HPP_
#define BEHAVIOR_TREE_BT_SERVICE_NODE_HPP_
#include <behaviortree_cpp_v3/action_node.h>
#include <behaviortree_cpp_v3/bt_factory.h>
#include <ros/ros.h>
#include <ros/service_client.h>
namespace BT
{
/**
* Base Action to implement a ROS Service
*/
template<class ServiceT>
class RosAsyncServiceNode : public BT::ActionNodeBase
{
protected:
RosAsyncServiceNode(ros::NodeHandle& nh, const std::string& name, const BT::NodeConfiguration & conf):
BT::ActionNodeBase(name, conf), node_(nh) {
}
RosAsyncServiceNode(ros::NodeHandle& nh,const std::string& service_name, ros::Duration timeout, const std::string& name, const BT::NodeConfiguration & conf):
BT::ActionNodeBase(name, conf), node_(nh) ,service_name_(service_name), timeout_(timeout) {
std::cout << "RosAsyncServiceNode constructor" << std::endl;
std::cout << "service_name: " << service_name << std::endl;
service_client_ = node_.serviceClient<ServiceT>( service_name_ );
}
public:
using BaseClass = RosAsyncServiceNode<ServiceT>;
using ServiceType = ServiceT;
using RequestType = typename ServiceT::Request;
using ResponseType = typename ServiceT::Response;
RosAsyncServiceNode() = delete;
virtual ~RosAsyncServiceNode() = default;
/// These ports will be added automatically if this Node is
/// registered using RegisterRosAction<DeriveClass>()
static PortsList providedPorts()
{
return {
InputPort<std::string>("service_name", "name of the ROS service"),
InputPort<unsigned>("timeout", 100, "timeout to connect to server (milliseconds)")
};
}
/// User must implement this method.
virtual void sendRequest(RequestType& request) = 0;
/// Method (to be implemented by the user) to receive the reply.
/// User can decide which NodeStatus it will return (SUCCESS or FAILURE).
virtual NodeStatus onResponse( const ResponseType& rep) = 0;
enum FailureCause{
MISSING_SERVER = 0,
FAILED_CALL = 1
};
/// Called when a service call failed. Can be overriden by the user.
virtual NodeStatus onFailedRequest(FailureCause failure)
{
return NodeStatus::FAILURE;
}
protected:
ros::ServiceClient service_client_;
std::string service_name_;
RequestType request_;
ResponseType reply_;
// The node that will be used for any ROS operations
ros::NodeHandle& node_;
ros::Duration timeout_;
std::future< std::pair<bool,bool> > future_;
std::pair<bool, bool> call_service(RequestType &request, ros::Duration timeout){
bool connected = service_client_.waitForExistence(timeout);
if( !connected ){
return std::make_pair(false, false);
}
bool received = service_client_.call( request, reply_ );
return std::make_pair(true, received);
}
BT::NodeStatus tick() override
{
if( !service_client_.isValid() ){
service_client_ = node_.serviceClient<ServiceT>( service_name_ );
}
// first step to be done only at the beginning of the Action
if (status() == BT::NodeStatus::IDLE) {
// setting the status to RUNNING to notify the BT Loggers (if any)
setStatus(BT::NodeStatus::RUNNING);
sendRequest(request_);
future_ = std::async(std::launch::async, &RosAsyncServiceNode::call_service, this, std::ref(request_), timeout_);
}
if( future_.valid() ){
if (future_.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {
return BT::NodeStatus::RUNNING;
}
auto result = future_.get();
if( !result.first ){
return onFailedRequest(MISSING_SERVER);
}
if( !result.second ){
return onFailedRequest(FAILED_CALL);
}
return onResponse(reply_);
}
throw BT::LogicError("future is not valid, this should never happen");
return BT::NodeStatus::RUNNING;
}
//halt just resets the future so that result will be ignored, and next tick will start a new future
virtual void halt() override{
future_ = std::future< std::pair<bool,bool> >();
}
};
/// Method to register the service into a factory.
/// It gives you the opportunity to set the ros::NodeHandle.
template <class DerivedT> static
void RegisterRosAsyncService(BT::BehaviorTreeFactory& factory,
const std::string& registration_ID,
ros::NodeHandle& node_handle, std::string service_name, ros::Duration timeout)
{
NodeBuilder builder = [&node_handle, service_name , timeout](const std::string& name, const NodeConfiguration& config) {
return std::make_unique<DerivedT>(node_handle, service_name, timeout, name, config );
};
TreeNodeManifest manifest;
manifest.type = getType<DerivedT>();
manifest.ports = DerivedT::providedPorts();
manifest.registration_ID = registration_ID;
//const auto& basic_ports = RosAsyncServiceNode< typename DerivedT::ServiceType>::providedPorts();
//manifest.ports.insert( basic_ports.begin(), basic_ports.end() );
factory.registerBuilder( manifest, builder );
}
} // namespace BT
#endif // BEHAVIOR_TREE_BT_SERVICE_NODE_HPP_