Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
lifecycle_manager_client.cpp
1 // Copyright (c) 2019 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "nav2_lifecycle_manager/lifecycle_manager_client.hpp"
16 
17 #include <cmath>
18 #include <memory>
19 #include <string>
20 #include <utility>
21 
22 #include "nav2_util/geometry_utils.hpp"
23 
24 namespace nav2_lifecycle_manager
25 {
26 using nav2_util::geometry_utils::orientationAroundZAxis;
27 
28 bool
29 LifecycleManagerClient::startup(const std::chrono::nanoseconds timeout)
30 {
31  return callService(ManageLifecycleNodes::Request::STARTUP, timeout);
32 }
33 
34 bool
35 LifecycleManagerClient::shutdown(const std::chrono::nanoseconds timeout)
36 {
37  return callService(ManageLifecycleNodes::Request::SHUTDOWN, timeout);
38 }
39 
40 bool
41 LifecycleManagerClient::pause(const std::chrono::nanoseconds timeout)
42 {
43  return callService(ManageLifecycleNodes::Request::PAUSE, timeout);
44 }
45 
46 bool
47 LifecycleManagerClient::resume(const std::chrono::nanoseconds timeout)
48 {
49  return callService(ManageLifecycleNodes::Request::RESUME, timeout);
50 }
51 
52 bool
53 LifecycleManagerClient::reset(const std::chrono::nanoseconds timeout)
54 {
55  return callService(ManageLifecycleNodes::Request::RESET, timeout);
56 }
57 
58 bool
59 LifecycleManagerClient::configure(const std::chrono::nanoseconds timeout)
60 {
61  return callService(ManageLifecycleNodes::Request::CONFIGURE, timeout);
62 }
63 
64 bool
65 LifecycleManagerClient::cleanup(const std::chrono::nanoseconds timeout)
66 {
67  return callService(ManageLifecycleNodes::Request::CLEANUP, timeout);
68 }
69 
70 SystemStatus
71 LifecycleManagerClient::is_active(const std::chrono::nanoseconds timeout)
72 {
73  auto request = std::make_shared<std_srvs::srv::Trigger::Request>();
74  auto response = std::make_shared<std_srvs::srv::Trigger::Response>();
75 
76  RCLCPP_DEBUG(
77  logger_, "Waiting for the %s service...",
78  active_service_name_.c_str());
79 
80  if (!is_active_client_->wait_for_service(std::chrono::seconds(1))) {
81  return SystemStatus::TIMEOUT;
82  }
83 
84  RCLCPP_DEBUG(
85  logger_, "Sending %s request",
86  active_service_name_.c_str());
87 
88  try {
89  response = is_active_client_->invoke(request, timeout);
90  } catch (std::runtime_error &) {
91  return SystemStatus::TIMEOUT;
92  }
93 
94  if (response->success) {
95  return SystemStatus::ACTIVE;
96  } else {
97  return SystemStatus::INACTIVE;
98  }
99 }
100 
101 bool
102 LifecycleManagerClient::callService(uint8_t command, const std::chrono::nanoseconds timeout)
103 {
104  auto request = std::make_shared<ManageLifecycleNodes::Request>();
105  request->command = command;
106 
107  RCLCPP_DEBUG(
108  logger_, "Waiting for the %s service...",
109  manage_service_name_.c_str());
110 
111  while (!manager_client_->wait_for_service(timeout)) {
112  if (!rclcpp::ok()) {
113  RCLCPP_ERROR(logger_, "Client interrupted while waiting for service to appear");
114  return false;
115  }
116  RCLCPP_DEBUG(logger_, "Waiting for service to appear...");
117  }
118 
119  RCLCPP_DEBUG(
120  logger_, "Sending %s request",
121  manage_service_name_.c_str());
122  try {
123  auto future_result = manager_client_->invoke(request, timeout);
124  return future_result->success;
125  } catch (std::runtime_error &) {
126  return false;
127  }
128 }
129 
130 } // namespace nav2_lifecycle_manager
ResponseType::SharedPtr invoke(typename RequestType::SharedPtr &request, const std::chrono::nanoseconds timeout=std::chrono::nanoseconds(-1), const std::chrono::nanoseconds wait_for_service_timeout=std::chrono::seconds(10))
Invoke the service and block until completed or timed out.
bool wait_for_service(const std::chrono::nanoseconds timeout=std::chrono::nanoseconds::max())
Block until a service is available or timeout.
bool cleanup(const std::chrono::nanoseconds timeout=std::chrono::nanoseconds(-1))
Make cleanup service call.
bool pause(const std::chrono::nanoseconds timeout=std::chrono::nanoseconds(-1))
Make pause service call.
SystemStatus is_active(const std::chrono::nanoseconds timeout=std::chrono::nanoseconds(-1))
Check if lifecycle node manager server is active.
bool shutdown(const std::chrono::nanoseconds timeout=std::chrono::nanoseconds(-1))
Make shutdown service call.
bool reset(const std::chrono::nanoseconds timeout=std::chrono::nanoseconds(-1))
Make reset service call.
bool startup(const std::chrono::nanoseconds timeout=std::chrono::nanoseconds(-1))
Make start up service call.
bool configure(const std::chrono::nanoseconds timeout=std::chrono::nanoseconds(-1))
Make configure service call.
bool callService(uint8_t command, const std::chrono::nanoseconds timeout=std::chrono::nanoseconds(-1))
A generic method used to call startup, shutdown, etc.
bool resume(const std::chrono::nanoseconds timeout=std::chrono::nanoseconds(-1))
Make resume service call.