PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
node_with_mode.hpp
1 /****************************************************************************
2  * Copyright (c) 2024 PX4 Development Team.
3  * SPDX-License-Identifier: BSD-3-Clause
4  ****************************************************************************/
5 
6 #pragma once
7 
8 #include <rclcpp/rclcpp.hpp>
9 #include <px4_ros2/components/mode.hpp>
10 #include <px4_ros2/components/wait_for_fmu.hpp>
11 
12 namespace px4_ros2
13 {
31 template<typename ModeT>
32 class NodeWithMode : public rclcpp::Node
33 {
34  static_assert(
35  std::is_base_of<ModeBase, ModeT>::value,
36  "Template type ModeT must be derived from px4_ros2::ModeBase");
37 
38 public:
39  explicit NodeWithMode(std::string node_name, bool enable_debug_output = false)
40  : Node(node_name)
41  {
42  if (enable_debug_output) {
43  auto ret =
44  rcutils_logging_set_logger_level(get_logger().get_name(), RCUTILS_LOG_SEVERITY_DEBUG);
45 
46  if (ret != RCUTILS_RET_OK) {
47  RCLCPP_ERROR(get_logger(), "Error setting severity: %s", rcutils_get_error_string().str);
48  rcutils_reset_error();
49  }
50  }
51 
52  _mode = std::make_unique<ModeT>(*this);
53 
54  if (!_mode->doRegister()) {
55  throw std::runtime_error("Registration failed");
56  }
57  }
58 
59  ModeT & getMode() const
60  {
61  return *_mode;
62  }
63 
64 private:
65  std::unique_ptr<ModeT> _mode;
66 };
67 
86 template<typename ModeExecutorT, typename ModeT>
87 class NodeWithModeExecutor : public rclcpp::Node
88 {
89  static_assert(
90  std::is_base_of<ModeExecutorBase, ModeExecutorT>::value,
91  "Template type ModeExecutorT must be derived from px4_ros2::ModeExecutorBase");
92  static_assert(
93  std::is_base_of<ModeBase, ModeT>::value,
94  "Template type ModeT must be derived from px4_ros2::ModeBase");
95 
96 public:
97  explicit NodeWithModeExecutor(std::string node_name, bool enable_debug_output = false)
98  : Node(node_name)
99  {
100  if (enable_debug_output) {
101  auto ret =
102  rcutils_logging_set_logger_level(get_logger().get_name(), RCUTILS_LOG_SEVERITY_DEBUG);
103 
104  if (ret != RCUTILS_RET_OK) {
105  RCLCPP_ERROR(get_logger(), "Error setting severity: %s", rcutils_get_error_string().str);
106  rcutils_reset_error();
107  }
108  }
109 
110  _mode = std::make_unique<ModeT>(*this);
111  _mode_executor = std::make_unique<ModeExecutorT>(*this, *_mode);
112 
113  if (!_mode_executor->doRegister()) {
114  throw std::runtime_error("Registration failed");
115  }
116  }
117 
118  ModeT & getMode() const
119  {
120  return *_mode;
121  }
122 
123 private:
124  std::unique_ptr<ModeExecutorT> _mode_executor;
125  std::unique_ptr<ModeT> _mode;
126 };
127 
129 } // namespace px4_ros2
A ROS2 node which instantiates a mode executor and its owned mode, and handles their registration wit...
Definition: node_with_mode.hpp:88
A ROS2 node which instantiates a control mode and handles its registration with PX4.
Definition: node_with_mode.hpp:33