AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
mode_registration.hpp
1// Copyright 2024 Robin Müller
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#pragma once
16
17#include <cstdint>
18#include <memory>
19#include <string>
20#include <type_traits>
21
22#include "auto_apms_px4_interfaces/msg/registered_mode.hpp"
23#include "px4_ros2/components/mode.hpp"
24#include "px4_ros2/components/mode_executor.hpp"
25#include "rclcpp/rclcpp.hpp"
26
27namespace auto_apms_px4
28{
29
48{
49public:
51 static constexpr auto TOPIC_NAME = "registered_modes";
52
57 explicit ModeRegistrationHandler(rclcpp::Node::SharedPtr node_ptr);
58
61
71 void registerMode(px4_ros2::ModeBase & mode, const std::string & mode_name);
72
85 void registerMode(px4_ros2::ModeExecutorBase & executor, const std::string & mode_name);
86
98
99private:
101 void waitForFmu();
102
104 void announce(const std::string & mode_name, uint8_t nav_state);
105
106 const rclcpp::Node::SharedPtr node_ptr_;
107 auto_apms_px4_interfaces::msg::RegisteredMode registered_mode_msg_;
108 rclcpp::Publisher<auto_apms_px4_interfaces::msg::RegisteredMode>::SharedPtr registered_mode_pub_;
109 rclcpp::TimerBase::SharedPtr announce_timer_;
110};
111
131template <class ModeT>
132class ModeRegistrationFactory
133{
134public:
135 ModeRegistrationFactory(const std::string & mode_name, const rclcpp::NodeOptions & options);
136
137 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr get_node_base_interface();
138
139private:
140 rclcpp::Node::SharedPtr node_ptr_;
141 std::unique_ptr<ModeT> mode_ptr_;
142 ModeRegistrationHandler registration_handler_;
143};
144
145// #####################################################################################################################
146// ################################ DEFINITIONS ##############################################
147// #####################################################################################################################
148
149template <class ModeT>
150ModeRegistrationFactory<ModeT>::ModeRegistrationFactory(
151 const std::string & mode_name, const rclcpp::NodeOptions & options)
152: node_ptr_(std::make_shared<rclcpp::Node>(mode_name + "_node", options)),
153 mode_ptr_(std::make_unique<ModeT>(*node_ptr_, px4_ros2::ModeBase::Settings(mode_name))),
154 registration_handler_(node_ptr_)
155{
156 static_assert(
157 std::is_base_of<px4_ros2::ModeBase, ModeT>::value,
158 "Template argument ModeT must publicly inherit px4_ros2::ModeBase.");
159
160 registration_handler_.registerMode(*mode_ptr_, mode_name);
161}
162
163template <class ModeT>
164rclcpp::node_interfaces::NodeBaseInterface::SharedPtr ModeRegistrationFactory<ModeT>::get_node_base_interface()
165{
166 return node_ptr_->get_node_base_interface();
167}
168
169} // namespace auto_apms_px4
Handles the registration of a PX4 mode with the FMU and announces its dynamically assigned nav_state.
void registerMode(px4_ros2::ModeBase &mode, const std::string &mode_name)
Wait for the FMU, register mode with it and announce the mode's assigned nav_state.
ModeRegistrationHandler(rclcpp::Node::SharedPtr node_ptr)
Constructor.
~ModeRegistrationHandler()
Stops announcing the mode (see stopModeAnnouncement()) if it was ever announced.
static constexpr auto TOPIC_NAME
Shared topic (relative to the node namespace) on which registered modes are announced.
void stopModeAnnouncement()
Stop announcing the mode registered with registerMode().
Implementation of PX4 mode peers offered by px4_ros2_cpp enabling integration with AutoAPMS.