Nav2 Navigation Stack - rolling  main
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  const std::string & node_name,
55  const std::chrono::seconds service_call_timeout,
56  const int retries)
57 {
58  LifecycleServiceClient sc(node_name);
59 
60  // Despite waiting for the service to be available and using reliable transport
61  // service calls still frequently hang. To get reliable startup it's necessary
62  // to timeout the service call and retry it when that happens.
63  RETRY(
64  sc.change_state(lifecycle_msgs::msg::Transition::TRANSITION_CONFIGURE, service_call_timeout),
65  retries);
66  RETRY(
67  sc.change_state(lifecycle_msgs::msg::Transition::TRANSITION_ACTIVATE, service_call_timeout),
68  retries);
69 }
70 
71 inline void startup_lifecycle_nodes(
72  const std::vector<std::string> & node_names,
73  const std::chrono::seconds service_call_timeout,
74  const int retries = 3)
75 {
76  for (const auto & node_name : node_names) {
77  startupLifecycleNode(node_name, service_call_timeout, retries);
78  }
79 }
80 
81 
82 int main(int argc, char * argv[])
83 {
84  if (argc == 1) {
85  usage();
86  }
87  rclcpp::init(0, nullptr);
88  startup_lifecycle_nodes(
89  std::vector<std::string>(argv + 1, argv + argc),
90  10s);
91  rclcpp::shutdown();
92 }
Helper functions to interact with a lifecycle node.