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
81 GenericTreeExecutorNode(rclcpp::Node::SharedPtr node_ptr, Options options);
82
88 GenericTreeExecutorNode(const std::string & name, Options options);
89
94 GenericTreeExecutorNode(const std::string & name);
95
96 virtual ~GenericTreeExecutorNode() override = default;
97
99
107 std::shared_future<ExecutionResult> startExecution(
108 const std::string & build_request, const std::string & entry_point = "",
109 const core::NodeManifest & node_manifest = {});
110
111protected:
112 /* Virtual methods to be overridden by derived classes */
113
126 virtual void preBuild(
127 core::TreeBuilder & builder, const std::string & build_request, const std::string & entry_point,
128 const core::NodeManifest & node_manifest, TreeBlackboard & bb);
129
138 virtual void postBuild(Tree & tree);
139
140 virtual bool onTick() override;
141
142 virtual bool afterTick() override;
143
144public:
145 /* Utility methods */
146
151 ExecutorParameters getExecutorParameters() const;
152
158 std::map<std::string, rclcpp::ParameterValue> getParameterValuesWithPrefix(const std::string & prefix);
159
166 static std::string stripPrefixFromParameterName(const std::string & prefix, const std::string & param_name);
167
175 const std::map<std::string, rclcpp::ParameterValue> & value_map, bool simulate = false);
176
184 const std::map<std::string, rclcpp::ParameterValue> & value_map, bool simulate = false);
185
190 void loadBuildHandler(const std::string & name);
191
201 TreeConstructor makeTreeConstructor(
202 const std::string & build_request, const std::string & entry_point = "",
203 const core::NodeManifest & node_manifest = {});
204
209 core::TreeBuilder::SharedPtr createTreeBuilder();
210
215 virtual bool clearGlobalBlackboard() override;
216
217private:
218 /* Internal callbacks */
219
220 rcl_interfaces::msg::SetParametersResult on_set_parameters_callback_(
221 const std::vector<rclcpp::Parameter> & parameters);
222
223 void parameter_event_callback_(const rcl_interfaces::msg::ParameterEvent & event);
224
225 rclcpp_action::GoalResponse handle_command_goal_(
226 const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const CommandActionContext::Goal> goal_ptr);
227 rclcpp_action::CancelResponse handle_command_cancel_(
228 std::shared_ptr<CommandActionContext::GoalHandle> goal_handle_ptr);
229 void handle_command_accept_(std::shared_ptr<CommandActionContext::GoalHandle> goal_handle_ptr);
230
231protected:
232 const Options executor_options_;
233 ExecutorParameterListener executor_param_listener_;
234
235 core::NodeRegistrationLoader::SharedPtr tree_node_loader_ptr_;
236 TreeBuildHandlerLoader::UniquePtr build_handler_loader_ptr_;
237 core::TreeBuilder::UniquePtr builder_ptr_;
238 TreeBuildHandler::UniquePtr build_handler_ptr_;
239 std::string current_build_handler_name_;
240 std::map<std::string, int> scripting_enums_;
241 std::map<std::string, rclcpp::ParameterValue> translated_global_blackboard_entries_;
242
243private:
244 rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_parameters_callback_handle_ptr_;
245 std::shared_ptr<rclcpp::ParameterEventHandler> parameter_event_handler_ptr_;
246 rclcpp::ParameterEventCallbackHandle::SharedPtr parameter_event_callback_handle_ptr_;
247
248 // Command action interface (optional)
249 rclcpp_action::Server<CommandActionContext::Type>::SharedPtr command_action_ptr_;
250 rclcpp::TimerBase::SharedPtr command_timer_ptr_;
251
252 // Clear blackboard service (optional)
253 rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr clear_blackboard_service_ptr_;
254};
255
256} // 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.
static std::string stripPrefixFromParameterName(const std::string &prefix, const std::string &param_name)
Get the name of a parameter without its prefix.
GenericTreeExecutorNode(rclcpp::Node::SharedPtr node_ptr, Options options)
Constructor using an existing ROS 2 node.
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