Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
lifecycle_service_client.hpp
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 #ifndef NAV2_UTIL__LIFECYCLE_SERVICE_CLIENT_HPP_
16 #define NAV2_UTIL__LIFECYCLE_SERVICE_CLIENT_HPP_
17 
18 #include <chrono>
19 #include <memory>
20 #include <string>
21 
22 #include "lifecycle_msgs/srv/change_state.hpp"
23 #include "lifecycle_msgs/srv/get_state.hpp"
24 #include "nav2_ros_common/service_client.hpp"
25 #include "nav2_ros_common/node_utils.hpp"
26 
27 
28 namespace nav2_util
29 {
30 
31 using namespace std::chrono_literals; // NOLINT
32 
35 {
36 public:
37  explicit LifecycleServiceClient(
38  const std::string & lifecycle_node_name);
39 
40  template<typename NodeT>
41  explicit
43  const string & lifecycle_node_name,
44  NodeT parent_node)
45  : change_state_(lifecycle_node_name + "/change_state", parent_node,
46  true /*creates and spins an internal executor*/),
47  get_state_(lifecycle_node_name + "/get_state", parent_node,
48  true /*creates and spins an internal executor*/)
49  {
50  // Block until server is up
51  rclcpp::Rate r(20);
52  while (!get_state_.wait_for_service(2s)) {
53  RCLCPP_INFO(
54  parent_node->get_logger(),
55  "Waiting for service %s...", get_state_.getServiceName().c_str());
56  r.sleep();
57  }
58  }
59 
61 
64  bool change_state(
65  const uint8_t transition, // takes a lifecycle_msgs::msg::Transition id
66  const std::chrono::milliseconds transition_timeout = std::chrono::milliseconds(-1),
67  const std::chrono::milliseconds wait_for_service_timeout = std::chrono::milliseconds(5000));
68 
70 
73  uint8_t get_state(const std::chrono::milliseconds timeout = std::chrono::milliseconds(2000));
74 
75 protected:
76  rclcpp::Node::SharedPtr node_; // Node only used if parent_node is not provided
79 };
80 
81 } // namespace nav2_util
82 
83 #endif // NAV2_UTIL__LIFECYCLE_SERVICE_CLIENT_HPP_
Helper functions to interact with a lifecycle node.