AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
behavior_mode_executor.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 <functional>
18#include <memory>
19#include <string>
20
21#include "auto_apms_behavior_tree/executor/generic_executor_node.hpp"
22#include "auto_apms_px4/mode_registration.hpp"
23#include "px4_ros2/components/mode.hpp"
24#include "px4_ros2/components/mode_executor.hpp"
25#include "px4_ros2/control/setpoint_types/direct_actuators.hpp"
26#include "rclcpp/rclcpp.hpp"
27
29{
30
39{
40 std::string build_request;
41 std::string entry_point;
42 std::string node_manifest;
43};
44
57class BehaviorOwnedMode : public px4_ros2::ModeBase
58{
59public:
60 BehaviorOwnedMode(rclcpp::Node & node, const px4_ros2::ModeBase::Settings & settings);
61
62 void onActivate() override {}
63 void onDeactivate() override {}
64 void updateSetpoint(float dt_s) override;
65
70 void finish(px4_ros2::Result result) { completed(result); }
71
72private:
73 std::shared_ptr<px4_ros2::DirectActuatorsSetpointType> actuator_setpoint_ptr_;
74};
75
94class BehaviorModeExecutor : public px4_ros2::ModeExecutorBase
95{
96public:
98
109
110 struct Config
111 {
112 BehaviorSpec spec;
115 bool defer_failsafes{false};
116 };
117
129 BehaviorOwnedMode & owned_mode, const px4_ros2::ModeExecutorBase::Settings & settings,
130 std::function<Config()> config_provider, auto_apms_behavior_tree::GenericTreeExecutorNode & engine);
131
132 void onActivate() override;
133 void onDeactivate(DeactivateReason reason) override;
134
145 void prepareTree();
146
151 static CompletionReaction reactionFromString(const std::string & str);
152
160 void onExecutionResult(ExecutionResult result);
161
162private:
163 void performReaction(CompletionReaction reaction, ExecutionResult result);
164
172 void scheduleLoiter();
173
174 rclcpp::Node & node_;
175 BehaviorOwnedMode & owned_mode_;
176 auto_apms_behavior_tree::GenericTreeExecutorNode & behavior_executor_;
177 std::function<Config()> config_provider_;
178 Config config_;
180 std::unique_ptr<auto_apms_behavior_tree::Tree> prepared_tree_ptr_;
181};
182
211class BehaviorModeExecutorNode : public auto_apms_behavior_tree::GenericTreeExecutorNode
212{
213public:
214 explicit BehaviorModeExecutorNode(const rclcpp::NodeOptions & options);
215
216protected:
217 void onTermination(const ExecutionResult & result) override;
218
219private:
220 static px4_ros2::ModeExecutorBase::Settings::Activation activationFromString(const std::string & str);
221
222 std::unique_ptr<BehaviorOwnedMode> owned_mode_ptr_;
223 std::unique_ptr<BehaviorModeExecutor> executor_ptr_;
224 ModeRegistrationHandler registration_handler_;
225};
226
227} // namespace auto_apms_px4
Flexible and configurable ROS 2 behavior tree executor node.
ExecutionResult
Enum representing possible behavior tree execution results.
static CompletionReaction reactionFromString(const std::string &str)
Parse a string into a CompletionReaction.
CompletionReaction
Reaction performed when a behavior terminates or fails, using the in-charge executor API.
@ HOLD
Schedule the owned mode (hold position) and stay in charge.
@ COMPLETE
Report the owned mode as completed to the FMU (relinquish charge).
void onExecutionResult(ExecutionResult result)
Handle the termination of the behavior running on the in-process executor.
BehaviorModeExecutor(BehaviorOwnedMode &owned_mode, const px4_ros2::ModeExecutorBase::Settings &settings, std::function< Config()> config_provider, auto_apms_behavior_tree::GenericTreeExecutorNode &engine)
Constructor.
void prepareTree()
Build the configured behavior tree and keep it detached, ready to be executed on the next activation.
void finish(px4_ros2::Result result)
Report completion of the mode to the FMU. Only takes effect while the mode is active.
Handles the registration of a PX4 mode with the FMU and announces its dynamically assigned nav_state.
Implementation of PX4 mode peers offered by px4_ros2_cpp enabling integration with AutoAPMS.
Which behavior to run and how to build it.
std::string entry_point
Single point of entry for behavior execution.
std::string build_request
Behavior build request (e.g. a registered behavior resource identity or XML).
std::string node_manifest
Encoded node manifest specifying additional nodes to load.