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
90class BehaviorModeExecutor : public px4_ros2::ModeExecutorBase
91{
92public:
94
105
106 struct Config
107 {
108 BehaviorSpec spec;
111 bool defer_failsafes{false};
112 };
113
124 BehaviorOwnedMode & owned_mode, const px4_ros2::ModeExecutorBase::Settings & settings,
125 std::function<Config()> config_provider, auto_apms_behavior_tree::GenericTreeExecutorNode & engine);
126
127 void onActivate() override;
128 void onDeactivate(DeactivateReason reason) override;
129
134 static CompletionReaction reactionFromString(const std::string & str);
135
143 void onExecutionResult(ExecutionResult result);
144
145private:
146 void performReaction(CompletionReaction reaction, ExecutionResult result);
147
155 void scheduleLoiter();
156
157 rclcpp::Node & node_;
158 BehaviorOwnedMode & owned_mode_;
159 auto_apms_behavior_tree::GenericTreeExecutorNode & behavior_executor_;
160 std::function<Config()> config_provider_;
161 Config config_;
162};
163
192class BehaviorModeExecutorNode : public auto_apms_behavior_tree::GenericTreeExecutorNode
193{
194public:
195 explicit BehaviorModeExecutorNode(const rclcpp::NodeOptions & options);
196
197protected:
198 void onTermination(const ExecutionResult & result) override;
199
200private:
201 static px4_ros2::ModeExecutorBase::Settings::Activation activationFromString(const std::string & str);
202
203 std::unique_ptr<BehaviorOwnedMode> owned_mode_ptr_;
204 std::unique_ptr<BehaviorModeExecutor> executor_ptr_;
205 ModeRegistrationHandler registration_handler_;
206};
207
208} // 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 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.