Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
lifecycle_bringup_commandline.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 <vector>
16 #include <string>
17 #include <iostream>
18 #include <cstdlib>
19 #include <chrono>
20 #include "rclcpp/rclcpp.hpp"
21 #include "nav2_util/lifecycle_service_client.hpp"
22 
23 using std::cerr;
24 using namespace std::chrono_literals; // NOLINT
25 using namespace nav2_util; // NOLINT
26 
27 void usage()
28 {
29  cerr << "Invalid command line.\n\n";
30  cerr << "This command will take a set of unconfigured lifecycle nodes through the\n";
31  cerr << "CONFIGURED to the ACTIVATED state\n";
32  cerr << "The nodes are brought up in the order listed on the command line\n\n";
33  cerr << "Usage:\n";
34  cerr << " > lifecycle_startup <node name> ...\n";
35  std::exit(1);
36 }
37 
38 #define RETRY(fn, retries) \
39  { \
40  int count = 0; \
41  while (true) { \
42  try { \
43  fn; \
44  break; \
45  } catch (const std::runtime_error & e) { \
46  ++count; \
47  if (count > (retries)) { \
48  throw e;} \
49  } \
50  } \
51  }
52 
53 inline void startupLifecycleNode(
54  rclcpp::Node::SharedPtr node, // nosemgrep
55  const std::string & node_name,
56  const std::chrono::seconds service_call_timeout,
57  const int retries)
58 {
59  LifecycleServiceClient sc(node_name, node);
60 
61  // Despite waiting for the service to be available and using reliable transport
62  // service calls still frequently hang. To get reliable startup it's necessary
63  // to timeout the service call and retry it when that happens.
64  RETRY(
65  sc.change_state(lifecycle_msgs::msg::Transition::TRANSITION_CONFIGURE, service_call_timeout),
66  retries);
67  RETRY(
68  sc.change_state(lifecycle_msgs::msg::Transition::TRANSITION_ACTIVATE, service_call_timeout),
69  retries);
70 }
71 
72 inline void startup_lifecycle_nodes(
73  rclcpp::Node::SharedPtr node, // nosemgrep
74  const std::vector<std::string> & node_names,
75  const std::chrono::seconds service_call_timeout,
76  const int retries = 3)
77 {
78  for (const auto & node_name : node_names) {
79  startupLifecycleNode(node, node_name, service_call_timeout, retries);
80  }
81 }
82 
83 
84 int main(int argc, char * argv[])
85 {
86  if (argc == 1) {
87  usage();
88  }
89  rclcpp::init(0, nullptr);
90  auto node = std::make_shared<rclcpp::Node>("lifecycle_bringup_client"); // nosemgrep
91  startup_lifecycle_nodes(
92  node,
93  std::vector<std::string>(argv + 1, argv + argc),
94  10s);
95  rclcpp::shutdown();
96 }
Helper functions to interact with a lifecycle node.