Nav2 Navigation Stack - lyrical  lyrical
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 #include "rclcpp/rate.hpp"
27 
28 
29 namespace nav2_util
30 {
31 
32 using namespace std::chrono_literals; // NOLINT
33 
36 {
37 public:
38  template<typename NodeT>
39  explicit
41  const string & lifecycle_node_name,
42  NodeT parent_node)
43  : change_state_(lifecycle_node_name + "/change_state", parent_node,
44  true /*creates and spins an internal executor*/),
45  get_state_(lifecycle_node_name + "/get_state", parent_node,
46  true /*creates and spins an internal executor*/)
47  {
48  // Block until server is up
49  rclcpp::Rate r(20);
50  while (rclcpp::ok() && !get_state_.wait_for_service(2s)) {
51  RCLCPP_INFO(
52  parent_node->get_logger(),
53  "Waiting for service %s...", get_state_.getServiceName().c_str());
54  r.sleep();
55  }
56  }
57 
59  {
60  change_state_.stop();
61  get_state_.stop();
62  }
63 
65 
68  bool change_state(
69  const uint8_t transition, // takes a lifecycle_msgs::msg::Transition id
70  const std::chrono::milliseconds transition_timeout = std::chrono::milliseconds(-1),
71  const std::chrono::milliseconds wait_for_service_timeout = std::chrono::milliseconds(5000));
72 
74 
77  uint8_t get_state(const std::chrono::milliseconds timeout = std::chrono::milliseconds(2000));
78 
79 protected:
82 };
83 
84 } // namespace nav2_util
85 
86 #endif // NAV2_UTIL__LIFECYCLE_SERVICE_CLIENT_HPP_
Helper functions to interact with a lifecycle node.