AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
generic_executor_node.hpp
1// Copyright 2026 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 "auto_apms_behavior_tree/build_handler/build_handler_loader.hpp"
18#include "auto_apms_behavior_tree/executor/executor_base.hpp"
19#include "auto_apms_behavior_tree/executor/options.hpp"
20#include "auto_apms_behavior_tree/executor_params.hpp"
21#include "auto_apms_behavior_tree_core/builder.hpp"
22#include "auto_apms_behavior_tree_core/node/node_registration_loader.hpp"
23#include "auto_apms_interfaces/action/command_tree_executor.hpp"
24#include "auto_apms_util/action_context.hpp"
25#include "rclcpp/rclcpp.hpp"
26#include "std_srvs/srv/trigger.hpp"
27
29{
30
31static const std::vector<std::string> TREE_EXECUTOR_EXPLICITLY_ALLOWED_PARAMETERS{
32 _AUTO_APMS_BEHAVIOR_TREE__EXECUTOR_PARAM_ALLOW_OTHER_BUILD_HANDLERS,
33 _AUTO_APMS_BEHAVIOR_TREE__EXECUTOR_PARAM_ALLOW_DYNAMIC_BLACKBOARD,
34 _AUTO_APMS_BEHAVIOR_TREE__EXECUTOR_PARAM_ALLOW_DYNAMIC_SCRIPTING_ENUMS,
35 _AUTO_APMS_BEHAVIOR_TREE__EXECUTOR_PARAM_EXCLUDE_PACKAGES_NODE,
36 _AUTO_APMS_BEHAVIOR_TREE__EXECUTOR_PARAM_EXCLUDE_PACKAGES_BUILD_HANDLER,
37 _AUTO_APMS_BEHAVIOR_TREE__EXECUTOR_PARAM_BUILD_HANDLER,
38 _AUTO_APMS_BEHAVIOR_TREE__EXECUTOR_PARAM_TICK_RATE,
39 _AUTO_APMS_BEHAVIOR_TREE__EXECUTOR_PARAM_GROOT2_PORT,
40 _AUTO_APMS_BEHAVIOR_TREE__EXECUTOR_PARAM_STATE_CHANGE_LOGGER};
41
42static const std::vector<std::string> TREE_EXECUTOR_EXPLICITLY_ALLOWED_PARAMETERS_WHILE_BUSY{
43 _AUTO_APMS_BEHAVIOR_TREE__EXECUTOR_PARAM_ALLOW_DYNAMIC_BLACKBOARD,
44 _AUTO_APMS_BEHAVIOR_TREE__EXECUTOR_PARAM_STATE_CHANGE_LOGGER};
45
60{
61public:
62 using Options = TreeExecutorNodeOptions;
63 using ExecutorParameters = executor_params::Params;
64 using ExecutorParameterListener = executor_params::ParamListener;
66
67 inline static const std::string SCRIPTING_ENUM_PARAM_PREFIX = "enum";
68 inline static const std::string BLACKBOARD_PARAM_PREFIX = "bb";
69
71 inline static const std::string PARAM_VALUE_NO_BUILD_HANDLER = "none";
72
78 GenericTreeExecutorNode(const std::string & name, Options options);
79
84 explicit GenericTreeExecutorNode(rclcpp::NodeOptions options);
85
86 virtual ~GenericTreeExecutorNode() override = default;
87
89
97 std::shared_future<ExecutionResult> startExecution(
98 const std::string & build_request, const std::string & entry_point = "",
99 const core::NodeManifest & node_manifest = {});
100
101private:
102 /* Virtual methods to be overridden by derived classes */
103
116 virtual void preBuild(
117 core::TreeBuilder & builder, const std::string & build_request, const std::string & entry_point,
118 const core::NodeManifest & node_manifest, TreeBlackboard & bb);
119
128 virtual void postBuild(Tree & tree);
129
130protected:
131 /* Utility methods */
132
137 ExecutorParameters getExecutorParameters() const;
138
144 std::map<std::string, rclcpp::ParameterValue> getParameterValuesWithPrefix(const std::string & prefix);
145
152 static std::string stripPrefixFromParameterName(const std::string & prefix, const std::string & param_name);
153
161 const std::map<std::string, rclcpp::ParameterValue> & value_map, bool simulate = false);
162
170 const std::map<std::string, rclcpp::ParameterValue> & value_map, bool simulate = false);
171
176 void loadBuildHandler(const std::string & name);
177
187 TreeConstructor makeTreeConstructor(
188 const std::string & build_request, const std::string & entry_point = "",
189 const core::NodeManifest & node_manifest = {});
190
195 core::TreeBuilder::SharedPtr createTreeBuilder();
196
201 virtual bool clearGlobalBlackboard() override;
202
203 bool onTick() override;
204
205 bool afterTick() override;
206
207private:
208 /* Internal callbacks */
209
210 rcl_interfaces::msg::SetParametersResult on_set_parameters_callback_(
211 const std::vector<rclcpp::Parameter> & parameters);
212
213 void parameter_event_callback_(const rcl_interfaces::msg::ParameterEvent & event);
214
215 rclcpp_action::GoalResponse handle_command_goal_(
216 const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const CommandActionContext::Goal> goal_ptr);
217 rclcpp_action::CancelResponse handle_command_cancel_(
218 std::shared_ptr<CommandActionContext::GoalHandle> goal_handle_ptr);
219 void handle_command_accept_(std::shared_ptr<CommandActionContext::GoalHandle> goal_handle_ptr);
220
221protected:
222 const Options executor_options_;
223 ExecutorParameterListener executor_param_listener_;
224
225 core::NodeRegistrationLoader::SharedPtr tree_node_loader_ptr_;
226 TreeBuildHandlerLoader::UniquePtr build_handler_loader_ptr_;
227 core::TreeBuilder::UniquePtr builder_ptr_;
228 TreeBuildHandler::UniquePtr build_handler_ptr_;
229 std::string current_build_handler_name_;
230 std::map<std::string, int> scripting_enums_;
231 std::map<std::string, rclcpp::ParameterValue> translated_global_blackboard_entries_;
232
233private:
234 rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_parameters_callback_handle_ptr_;
235 std::shared_ptr<rclcpp::ParameterEventHandler> parameter_event_handler_ptr_;
236 rclcpp::ParameterEventCallbackHandle::SharedPtr parameter_event_callback_handle_ptr_;
237
238 // Command action interface (optional)
239 rclcpp_action::Server<CommandActionContext::Type>::SharedPtr command_action_ptr_;
240 rclcpp::TimerBase::SharedPtr command_timer_ptr_;
241
242 // Clear blackboard service (optional)
243 rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr clear_blackboard_service_ptr_;
244};
245
246} // namespace auto_apms_behavior_tree
bool updateGlobalBlackboardWithParameterValues(const std::map< std::string, rclcpp::ParameterValue > &value_map, bool simulate=false)
Update the global blackboard using parameter values.
static const std::string PARAM_VALUE_NO_BUILD_HANDLER
Value indicating that no build handler is loaded.
GenericTreeExecutorNode(const std::string &name, Options options)
Constructor.
static std::string stripPrefixFromParameterName(const std::string &prefix, const std::string &param_name)
Get the name of a parameter without its prefix.
virtual bool clearGlobalBlackboard() override
Reset the global blackboard and clear all entries.
core::TreeBuilder::SharedPtr createTreeBuilder()
Create a tree builder for building the behavior tree.
std::map< std::string, rclcpp::ParameterValue > getParameterValuesWithPrefix(const std::string &prefix)
Assemble all parameters of this node that have a specific prefix.
bool updateScriptingEnumsWithParameterValues(const std::map< std::string, rclcpp::ParameterValue > &value_map, bool simulate=false)
Update the internal buffer of scripting enums.
ExecutorParameters getExecutorParameters() const
Get a copy of the current executor parameters.
std::shared_future< ExecutionResult > startExecution(const std::string &build_request, const std::string &entry_point="", const core::NodeManifest &node_manifest={})
Start the behavior tree specified by a particular build request.
virtual void postBuild(Tree &tree)
Callback invoked after the behavior tree has been instantiated.
virtual void preBuild(core::TreeBuilder &builder, const std::string &build_request, const std::string &entry_point, const core::NodeManifest &node_manifest, TreeBlackboard &bb)
Callback invoked before building the behavior tree.
void loadBuildHandler(const std::string &name)
Load a particular behavior tree build handler plugin.
TreeConstructor makeTreeConstructor(const std::string &build_request, const std::string &entry_point="", const core::NodeManifest &node_manifest={})
Create a callback that builds a behavior tree according to a specific request.
std::shared_future< ExecutionResult > startExecution(TreeConstructor make_tree, double tick_rate_sec=0.1, int groot2_port=-1)
Start a behavior tree that is built using a callback.
TreeExecutorBase(rclcpp::Node::SharedPtr node_ptr, rclcpp::CallbackGroup::SharedPtr tree_node_callback_group_ptr=nullptr)
Constructor.
Configuration options for GenericTreeExecutorNode.
Definition options.hpp:32
Data structure for information about which behavior tree node plugin to load and how to configure the...
Class for configuring and instantiating behavior trees.
Definition builder.hpp:55
Helper class that stores contextual information related to a ROS 2 action.
Powerful tooling for incorporating behavior trees for task development.
Definition behavior.hpp:32