Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
lifecycle_manager.hpp
1 // Copyright (c) 2019 Intel Corporation
2 // Copyright (c) 2022 Samsung Research America
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef NAV2_LIFECYCLE_MANAGER__LIFECYCLE_MANAGER_HPP_
17 #define NAV2_LIFECYCLE_MANAGER__LIFECYCLE_MANAGER_HPP_
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <thread>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include "nav2_util/lifecycle_service_client.hpp"
27 #include "nav2_ros_common/node_thread.hpp"
28 #include "nav2_ros_common/publisher.hpp"
29 #include "nav2_ros_common/service_server.hpp"
30 #include "rclcpp/callback_group.hpp"
31 #include "rclcpp/duration.hpp"
32 #include "rclcpp/node.hpp"
33 #include "rclcpp/node_options.hpp"
34 #include "rclcpp/time.hpp"
35 #include "rclcpp/timer.hpp"
36 #include "std_msgs/msg/bool.hpp"
37 #include "std_srvs/srv/empty.hpp"
38 #include "nav2_msgs/srv/manage_lifecycle_nodes.hpp"
39 #include "std_srvs/srv/trigger.hpp"
40 #include "bondcpp/bond.hpp"
41 #include "diagnostic_updater/diagnostic_updater.hpp"
42 #include "nav2_ros_common/lifecycle_node.hpp"
43 
44 
45 namespace nav2_lifecycle_manager
46 {
47 using namespace std::chrono_literals; // NOLINT
48 
49 using nav2_msgs::srv::ManageLifecycleNodes;
50 
52 enum NodeState
53 {
54  UNCONFIGURED,
55  ACTIVE,
56  INACTIVE,
57  FINALIZED,
58  UNKNOWN,
59 };
60 
67 class LifecycleManager : public rclcpp::Node // nosemgrep
68 {
69 public:
74  explicit LifecycleManager(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
79 
80 protected:
81  // Callback group used by services and timers
82  rclcpp::CallbackGroup::SharedPtr callback_group_;
83  std::unique_ptr<nav2::NodeThread> service_thread_;
84 
85  // The services provided by this node
86  nav2::ServiceServer<ManageLifecycleNodes>::SharedPtr manager_srv_;
87  nav2::ServiceServer<std_srvs::srv::Trigger>::SharedPtr is_active_srv_;
88 
89  // Latched publisher for is_active status
90  nav2::Publisher<std_msgs::msg::Bool>::SharedPtr is_active_pub_;
91 
98  void managerCallback(
99  const std::shared_ptr<rmw_request_id_t> request_header,
100  const std::shared_ptr<ManageLifecycleNodes::Request> request,
101  std::shared_ptr<ManageLifecycleNodes::Response> response);
109  void isActiveCallback(
110  const std::shared_ptr<rmw_request_id_t> request_header,
111  const std::shared_ptr<std_srvs::srv::Trigger::Request> request,
112  std::shared_ptr<std_srvs::srv::Trigger::Response> response);
113 
114  // Support functions for the service calls
119  bool startup();
124  bool configure();
129  bool cleanup();
134  bool shutdown();
139  bool reset(bool hard_reset = false);
144  bool pause();
149  bool resume();
150 
156  void onRclPreshutdown();
157 
158  // Support function for creating service clients
162  void createLifecycleServiceClients();
163 
164  // Support function for creating service servers
168  void createLifecycleServiceServers();
169 
170  // Support function for creating publishers
174  void createLifecyclePublishers();
175 
176  // Support functions for shutdown
180  void shutdownAllNodes();
184  void destroyLifecycleServiceClients();
188  void destroyLifecyclePublishers();
189 
190  // Support function for creating bond timer
194  void createBondTimer();
195 
196  // Support function for creating bond connection
200  bool createBondConnection(const std::string & node_name);
201 
202  // Support function for killing bond connections
206  void destroyBondTimer();
207 
208  // Support function for checking on bond connections
213  void checkBondConnections();
214 
215  // Support function for checking if bond connections come back after respawn
220  void checkBondRespawnConnection();
221 
225  bool changeStateForNode(
226  const std::string & node_name,
227  std::uint8_t transition);
228 
232  bool changeStateForAllNodes(std::uint8_t transition, bool hard_change = false);
233 
234  // Convenience function to highlight the output on the console
238  void message(const std::string & msg);
239 
240  // Diagnostics functions
244  void CreateDiagnostic(diagnostic_updater::DiagnosticStatusWrapper & stat);
245 
252  void registerRclPreshutdownCallback();
253 
257  void setState(const NodeState & state);
258 
262  bool isActive();
263 
267  void publishIsActiveState();
268 
269  // Timer thread to look at bond connections
270  rclcpp::TimerBase::SharedPtr init_timer_;
271  rclcpp::TimerBase::SharedPtr bond_timer_;
272  rclcpp::TimerBase::SharedPtr bond_respawn_timer_;
273  std::chrono::milliseconds bond_timeout_;
274  std::chrono::milliseconds service_timeout_;
275  double bond_heartbeat_period_;
276 
277  // A map of all nodes to check bond connection
278  std::map<std::string, std::shared_ptr<bond::Bond>> bond_map_;
279 
280  // A map of all nodes to be controlled
281  std::map<std::string, std::shared_ptr<nav2_util::LifecycleServiceClient>> node_map_;
282 
283  std::map<std::uint8_t, std::string> transition_label_map_;
284 
285  // A map of the expected transitions to primary states
286  std::unordered_map<std::uint8_t, std::uint8_t> transition_state_map_;
287 
288  // The names of the nodes to be managed, in the order of desired bring-up
289  std::vector<std::string> node_names_;
290 
291  // Whether to automatically start up the system
292  bool autostart_;
293  bool attempt_respawn_reconnection_;
294 
295  NodeState managed_nodes_state_{NodeState::UNCONFIGURED};
296  diagnostic_updater::Updater diagnostics_updater_;
297 
298  rclcpp::Time bond_respawn_start_time_{0};
299  rclcpp::Duration bond_respawn_max_duration_{10s};
300 };
301 
302 } // namespace nav2_lifecycle_manager
303 
304 #endif // NAV2_LIFECYCLE_MANAGER__LIFECYCLE_MANAGER_HPP_
Implements service interface to transition the lifecycle nodes of Nav2 stack. It receives transition ...