Skip to content

Commit 012e592

Browse files
committed
Action/Server timeout option through ports
1 parent 13682b4 commit 012e592

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

behaviortree_ros2/include/behaviortree_ros2/bt_action_node.hpp

+35-4
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ class RosActionNode : public BT::ActionNodeBase
105105
static PortsList providedBasicPorts(PortsList addition)
106106
{
107107
PortsList basic = {
108-
InputPort<std::string>("action_name", "__default__placeholder__", "Action server name")
108+
InputPort<std::string>("action_name", "__default__placeholder__", "Action server name"),
109+
InputPort<int>("server_timeout", "Action server goal timeout (mSec)"),
110+
InputPort<int>("wait_for_server_timeout", "Action server discovery timeout (mSec)")
109111
};
110112
basic.insert(addition.begin(), addition.end());
111113
return basic;
@@ -170,8 +172,8 @@ class RosActionNode : public BT::ActionNodeBase
170172
std::shared_ptr<rclcpp::Node> node_;
171173
std::string prev_action_name_;
172174
bool action_name_may_change_ = false;
173-
const std::chrono::milliseconds server_timeout_;
174-
const std::chrono::milliseconds wait_for_server_timeout_;
175+
std::chrono::milliseconds server_timeout_;
176+
std::chrono::milliseconds wait_for_server_timeout_;
175177

176178
private:
177179

@@ -203,13 +205,42 @@ template<class T> inline
203205
server_timeout_(params.server_timeout),
204206
wait_for_server_timeout_(params.wait_for_server_timeout)
205207
{
208+
// update server_timeout_ if set throuh port and greater than 0
209+
auto portIt = config().input_ports.find("server_timeout");
210+
if(portIt != config().input_ports.end())
211+
{
212+
int timeout = 0;
213+
getInput("server_timeout", timeout);
214+
if(timeout > 0) {
215+
server_timeout_ = std::chrono::milliseconds(timeout);
216+
}
217+
else {
218+
RCLCPP_WARN(node_->get_logger(), "%s: Port `server_timeout` is not greater than zero. "
219+
"Defaulting to %d mSec.", name().c_str(), static_cast<int>(server_timeout_.count()));
220+
}
221+
}
222+
// update wait_for_server_timeout_ if set throuh port and greater than 0
223+
portIt = config().input_ports.find("wait_for_server_timeout");
224+
if(portIt != config().input_ports.end())
225+
{
226+
int timeout = 0;
227+
getInput("wait_for_server_timeout", timeout);
228+
if(timeout > 0) {
229+
wait_for_server_timeout_ = std::chrono::milliseconds(timeout);
230+
}
231+
else {
232+
RCLCPP_WARN(node_->get_logger(), "%s: Port `wait_for_server_timeout` is not greater than zero. "
233+
"Defaulting to %d mSec.", name().c_str(), static_cast<int>(wait_for_server_timeout_.count()));
234+
}
235+
}
236+
206237
// Three cases:
207238
// - we use the default action_name in RosNodeParams when port is empty
208239
// - we use the action_name in the port and it is a static string.
209240
// - we use the action_name in the port and it is blackboard entry.
210241

211242
// check port remapping
212-
auto portIt = config().input_ports.find("action_name");
243+
portIt = config().input_ports.find("action_name");
213244
if(portIt != config().input_ports.end())
214245
{
215246
const std::string& bb_action_name = portIt->second;

behaviortree_ros2/include/behaviortree_ros2/bt_service_node.hpp

+36-5
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ class RosServiceNode : public BT::ActionNodeBase
9494
static PortsList providedBasicPorts(PortsList addition)
9595
{
9696
PortsList basic = {
97-
InputPort<std::string>("service_name", "__default__placeholder__", "Service name")
97+
InputPort<std::string>("service_name", "__default__placeholder__", "Service name"),
98+
InputPort<int>("server_timeout", "Service server goal timeout (mSec)"),
99+
InputPort<int>("wait_for_server_timeout", "Service server discovery timeout (mSec)")
98100
};
99101
basic.insert(addition.begin(), addition.end());
100102
return basic;
@@ -133,7 +135,7 @@ class RosServiceNode : public BT::ActionNodeBase
133135
* It must return either SUCCESS or FAILURE.
134136
*/
135137
virtual BT::NodeStatus onFailure(ServiceNodeErrorCode /*error*/)
136-
{
138+
{
137139
return NodeStatus::FAILURE;
138140
}
139141

@@ -142,8 +144,8 @@ class RosServiceNode : public BT::ActionNodeBase
142144
std::shared_ptr<rclcpp::Node> node_;
143145
std::string prev_service_name_;
144146
bool service_name_may_change_ = false;
145-
const std::chrono::milliseconds service_timeout_;
146-
const std::chrono::milliseconds wait_for_service_timeout_;
147+
std::chrono::milliseconds service_timeout_;
148+
std::chrono::milliseconds wait_for_service_timeout_;
147149

148150
private:
149151

@@ -174,8 +176,37 @@ template<class T> inline
174176
service_timeout_(params.server_timeout),
175177
wait_for_service_timeout_(params.wait_for_server_timeout)
176178
{
179+
// update service_timeout_ if set throuh port and greater than 0
180+
auto portIt = config().input_ports.find("server_timeout");
181+
if(portIt != config().input_ports.end())
182+
{
183+
int timeout = 0;
184+
getInput("server_timeout", timeout);
185+
if(timeout > 0) {
186+
service_timeout_ = std::chrono::milliseconds(timeout);
187+
}
188+
else {
189+
RCLCPP_WARN(node_->get_logger(), "%s: Port `server_timeout` is not greater than zero. "
190+
"Defaulting to %d mSec.", name().c_str(), static_cast<int>(service_timeout_.count()));
191+
}
192+
}
193+
// update wait_for_service_timeout_ if set throuh port and greater than 0
194+
portIt = config().input_ports.find("wait_for_server_timeout");
195+
if(portIt != config().input_ports.end())
196+
{
197+
int timeout = 0;
198+
getInput("wait_for_server_timeout", timeout);
199+
if(timeout > 0) {
200+
wait_for_service_timeout_ = std::chrono::milliseconds(timeout);
201+
}
202+
else {
203+
RCLCPP_WARN(node_->get_logger(), "%s: Port `wait_for_server_timeout` is not greater than zero. "
204+
"Defaulting to %d mSec.", name().c_str(), static_cast<int>(wait_for_service_timeout_.count()));
205+
}
206+
}
207+
177208
// check port remapping
178-
auto portIt = config().input_ports.find("service_name");
209+
portIt = config().input_ports.find("service_name");
179210
if(portIt != config().input_ports.end())
180211
{
181212
const std::string& bb_service_name = portIt->second;

0 commit comments

Comments
 (0)