forked from BehaviorTree/BehaviorTree.ROS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bt_service_node.h
140 lines (111 loc) · 4.16 KB
/
bt_service_node.h
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
// 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 RosServiceNode : public BT::SyncActionNode
{
protected:
RosServiceNode(ros::NodeHandle& nh, const std::string& name, const BT::NodeConfiguration & conf):
BT::SyncActionNode(name, conf), node_(nh) { }
public:
using BaseClass = RosServiceNode<ServiceT>;
using ServiceType = ServiceT;
using RequestType = typename ServiceT::Request;
using ResponseType = typename ServiceT::Response;
RosServiceNode() = delete;
virtual ~RosServiceNode() = 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_;
typename ServiceT::Response reply_;
// The node that will be used for any ROS operations
ros::NodeHandle& node_;
BT::NodeStatus tick() override
{
if( !service_client_.isValid() ){
std::string server;
if (!getInput<std::string>("service_name", server))
throw BT::RuntimeError("RosServiceNode "+name()+": 'service_name' attribute must be set");
service_client_ = node_.serviceClient<ServiceT>( server, true );
}
unsigned msec;
getInput("timeout", msec);
ros::Duration timeout(static_cast<double>(msec) * 1e-3);
bool connected = service_client_.waitForExistence(timeout);
if( !connected ){
return onFailedRequest(MISSING_SERVER);
}
typename ServiceT::Request request;
sendRequest(request);
bool received = service_client_.call( request, reply_ );
if( !received )
{
return onFailedRequest(FAILED_CALL);
}
return onResponse(reply_);
}
};
/// Method to register the service into a factory.
/// It gives you the opportunity to set the ros::NodeHandle.
template <class DerivedT> static
void RegisterRosService(BT::BehaviorTreeFactory& factory,
const std::string& registration_ID,
ros::NodeHandle& node_handle)
{
NodeBuilder builder = [&node_handle](const std::string& name, const NodeConfiguration& config) {
return std::make_unique<DerivedT>(node_handle, name, config );
};
TreeNodeManifest manifest;
manifest.type = getType<DerivedT>();
manifest.ports = DerivedT::providedPorts();
manifest.registration_ID = registration_ID;
const auto& basic_ports = RosServiceNode< 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_