Skip to content

Commit a438883

Browse files
doisygGuillaume DoisySteveMacenski
authored
[lifecycle_manager] expose service_timeout (#4838)
* [lifecycle_manager] expose service_timeout Signed-off-by: Guillaume Doisy <[email protected]> * restore original change_state, and detect non_default_timeout Signed-off-by: Guillaume Doisy <[email protected]> * lint Signed-off-by: Guillaume Doisy <[email protected]> * spell Signed-off-by: Guillaume Doisy <[email protected]> * collapse change_state and remove non_default_timeout logic Signed-off-by: Guillaume Doisy <[email protected]> * Update nav2_util/src/lifecycle_service_client.cpp Co-authored-by: Steve Macenski <[email protected]> Signed-off-by: Guillaume Doisy <[email protected]> --------- Signed-off-by: Guillaume Doisy <[email protected]> Signed-off-by: Guillaume Doisy <[email protected]> Co-authored-by: Guillaume Doisy <[email protected]> Co-authored-by: Steve Macenski <[email protected]>
1 parent df92ffd commit a438883

File tree

4 files changed

+26
-26
lines changed

4 files changed

+26
-26
lines changed

nav2_lifecycle_manager/include/nav2_lifecycle_manager/lifecycle_manager.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ class LifecycleManager : public rclcpp::Node
239239
rclcpp::TimerBase::SharedPtr bond_timer_;
240240
rclcpp::TimerBase::SharedPtr bond_respawn_timer_;
241241
std::chrono::milliseconds bond_timeout_;
242+
std::chrono::milliseconds service_timeout_;
242243

243244
// A map of all nodes to check bond connection
244245
std::map<std::string, std::shared_ptr<bond::Bond>> bond_map_;

nav2_lifecycle_manager/src/lifecycle_manager.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ LifecycleManager::LifecycleManager(const rclcpp::NodeOptions & options)
4242
declare_parameter("node_names", rclcpp::PARAMETER_STRING_ARRAY);
4343
declare_parameter("autostart", rclcpp::ParameterValue(false));
4444
declare_parameter("bond_timeout", 4.0);
45+
declare_parameter("service_timeout", 5.0);
4546
declare_parameter("bond_respawn_max_duration", 10.0);
4647
declare_parameter("attempt_respawn_reconnection", true);
4748

@@ -54,6 +55,11 @@ LifecycleManager::LifecycleManager(const rclcpp::NodeOptions & options)
5455
bond_timeout_ = std::chrono::duration_cast<std::chrono::milliseconds>(
5556
std::chrono::duration<double>(bond_timeout_s));
5657

58+
double service_timeout_s;
59+
get_parameter("service_timeout", service_timeout_s);
60+
service_timeout_ = std::chrono::duration_cast<std::chrono::milliseconds>(
61+
std::chrono::duration<double>(service_timeout_s));
62+
5763
double respawn_timeout_s;
5864
get_parameter("bond_respawn_max_duration", respawn_timeout_s);
5965
bond_respawn_max_duration_ = rclcpp::Duration::from_seconds(respawn_timeout_s);
@@ -254,8 +260,9 @@ LifecycleManager::changeStateForNode(const std::string & node_name, std::uint8_t
254260
{
255261
message(transition_label_map_[transition] + node_name);
256262

257-
if (!node_map_[node_name]->change_state(transition) ||
258-
!(node_map_[node_name]->get_state() == transition_state_map_[transition]))
263+
if (!node_map_[node_name]->change_state(transition, std::chrono::milliseconds(-1),
264+
service_timeout_) ||
265+
!(node_map_[node_name]->get_state(service_timeout_) == transition_state_map_[transition]))
259266
{
260267
RCLCPP_ERROR(get_logger(), "Failed to change state for node: %s", node_name.c_str());
261268
return false;
@@ -547,7 +554,7 @@ LifecycleManager::checkBondRespawnConnection()
547554
}
548555

549556
try {
550-
node_map_[node_name]->get_state(); // Only won't throw if the server exists
557+
node_map_[node_name]->get_state(service_timeout_); // Only won't throw if the server exists
551558
live_servers++;
552559
} catch (...) {
553560
break;

nav2_util/include/nav2_util/lifecycle_service_client.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "nav2_util/service_client.hpp"
2525
#include "nav2_util/node_utils.hpp"
2626

27+
2728
namespace nav2_util
2829
{
2930

@@ -43,16 +44,14 @@ class LifecycleServiceClient
4344
*/
4445
bool change_state(
4546
const uint8_t transition, // takes a lifecycle_msgs::msg::Transition id
46-
const std::chrono::seconds timeout);
47-
48-
/// Trigger a state change, returning result
49-
bool change_state(std::uint8_t transition);
47+
const std::chrono::milliseconds transition_timeout = std::chrono::milliseconds(-1),
48+
const std::chrono::milliseconds wait_for_service_timeout = std::chrono::milliseconds(5000));
5049

5150
/// Get the current state as a lifecycle_msgs::msg::State id value
5251
/**
5352
* Throws std::runtime_error on failure
5453
*/
55-
uint8_t get_state(const std::chrono::seconds timeout = std::chrono::seconds(2));
54+
uint8_t get_state(const std::chrono::milliseconds timeout = std::chrono::milliseconds(2000));
5655

5756
protected:
5857
rclcpp::Node::SharedPtr node_;

nav2_util/src/lifecycle_service_client.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "lifecycle_msgs/srv/get_state.hpp"
2323

2424
using nav2_util::generate_internal_node;
25-
using std::chrono::seconds;
25+
using std::chrono::milliseconds;
2626
using std::make_shared;
2727
using std::string;
2828
using namespace std::chrono_literals;
@@ -67,33 +67,26 @@ LifecycleServiceClient::LifecycleServiceClient(
6767

6868
bool LifecycleServiceClient::change_state(
6969
const uint8_t transition,
70-
const seconds timeout)
70+
const milliseconds transition_timeout,
71+
const milliseconds wait_for_service_timeout)
7172
{
72-
if (!change_state_.wait_for_service(timeout)) {
73+
if (!change_state_.wait_for_service(wait_for_service_timeout)) {
7374
throw std::runtime_error("change_state service is not available!");
7475
}
7576

7677
auto request = std::make_shared<lifecycle_msgs::srv::ChangeState::Request>();
7778
request->transition.id = transition;
78-
auto response = change_state_.invoke(request, timeout);
79-
return response.get();
80-
}
81-
82-
bool LifecycleServiceClient::change_state(
83-
std::uint8_t transition)
84-
{
85-
if (!change_state_.wait_for_service(5s)) {
86-
throw std::runtime_error("change_state service is not available!");
79+
if (transition_timeout > 0ms) {
80+
auto response = change_state_.invoke(request, transition_timeout);
81+
return response.get();
82+
} else {
83+
auto response = std::make_shared<lifecycle_msgs::srv::ChangeState::Response>();
84+
return change_state_.invoke(request, response);
8785
}
88-
89-
auto request = std::make_shared<lifecycle_msgs::srv::ChangeState::Request>();
90-
auto response = std::make_shared<lifecycle_msgs::srv::ChangeState::Response>();
91-
request->transition.id = transition;
92-
return change_state_.invoke(request, response);
9386
}
9487

9588
uint8_t LifecycleServiceClient::get_state(
96-
const seconds timeout)
89+
const milliseconds timeout)
9790
{
9891
if (!get_state_.wait_for_service(timeout)) {
9992
throw std::runtime_error("get_state service is not available!");

0 commit comments

Comments
 (0)