AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
executor_node.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// https://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_params.hpp"
20#include "auto_apms_behavior_tree_core/builder.hpp"
21#include "auto_apms_behavior_tree_core/node/node_registration_loader.hpp"
22#include "auto_apms_interfaces/action/command_tree_executor.hpp"
23#include "auto_apms_interfaces/action/start_tree_executor.hpp"
24#include "auto_apms_util/action_context.hpp"
25#include "rclcpp/rclcpp.hpp"
26
28{
29
37{
38public:
45 TreeExecutorNodeOptions(const rclcpp::NodeOptions & ros_node_options);
46
53 TreeExecutorNodeOptions & enableScriptingEnumParameters(bool from_overrides, bool dynamic);
54
62 TreeExecutorNodeOptions & enableGlobalBlackboardParameters(bool from_overrides, bool dynamic);
63
69 TreeExecutorNodeOptions & setDefaultBuildHandler(const std::string & name);
70
75 rclcpp::NodeOptions getROSNodeOptions() const;
76
77private:
78 friend class TreeExecutorNode;
79
80 rclcpp::NodeOptions ros_node_options_;
81 bool scripting_enum_parameters_from_overrides_ = true;
82 bool scripting_enum_parameters_dynamic_ = true;
83 bool blackboard_parameters_from_overrides_ = true;
84 bool blackboard_parameters_dynamic_ = true;
85 std::map<std::string, rclcpp::ParameterValue> custom_default_parameters_;
86};
87
117{
118public:
119 using Options = TreeExecutorNodeOptions;
120 using ExecutorParameters = executor_params::Params;
121 using ExecutorParameterListener = executor_params::ParamListener;
124 using TreeBuilder = core::TreeBuilder;
125
126 inline static const std::string PARAM_VALUE_NO_BUILD_HANDLER = "none";
127 inline static const std::string SCRIPTING_ENUM_PARAM_PREFIX = "enum";
128 inline static const std::string BLACKBOARD_PARAM_PREFIX = "bb";
129
136 TreeExecutorNode(const std::string & name, TreeExecutorNodeOptions executor_options);
137
142 explicit TreeExecutorNode(rclcpp::NodeOptions options);
143
144 virtual ~TreeExecutorNode() override = default;
145
147
159 std::shared_future<ExecutionResult> startExecution(const std::string & tree_build_request);
160
161private:
162 /* Virtual methods */
163
175 virtual void setUpBuilder(TreeBuilder & builder, const core::NodeManifest & node_manifest);
176
177protected:
178 /* Utility methods */
179
188 std::map<std::string, rclcpp::ParameterValue> getParameterValuesWithPrefix(const std::string & prefix);
189
197 static std::string stripPrefixFromParameterName(const std::string & prefix, const std::string & param_name);
198
215 const std::map<std::string, rclcpp::ParameterValue> & value_map, bool simulate = false);
216
234 const std::map<std::string, rclcpp::ParameterValue> & value_map, bool simulate = false);
235
243 void loadBuildHandler(const std::string & name);
244
259 TreeConstructor makeTreeConstructor(
260 const std::string & build_handler_request, const std::string & root_tree_name,
261 const core::NodeManifest & node_manifest = {}, const core::NodeManifest & node_overrides = {});
262
263private:
264 /* Executor specific virtual overrides */
265
266 bool onTick() override final;
267
268 bool afterTick() override final;
269
270 void onTermination(const ExecutionResult & result) override final;
271
272 /* Internal callbacks */
273
274 rcl_interfaces::msg::SetParametersResult on_set_parameters_callback_(
275 const std::vector<rclcpp::Parameter> & parameters);
276
277 void parameter_event_callback_(const rcl_interfaces::msg::ParameterEvent & event);
278
279 rclcpp_action::GoalResponse handle_start_goal_(
280 const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const StartActionContext::Goal> goal_ptr);
281 rclcpp_action::CancelResponse handle_start_cancel_(std::shared_ptr<StartActionContext::GoalHandle> goal_handle_ptr);
282 void handle_start_accept_(std::shared_ptr<StartActionContext::GoalHandle> goal_handle_ptr);
283
284 rclcpp_action::GoalResponse handle_command_goal_(
285 const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const CommandActionContext::Goal> goal_ptr);
286 rclcpp_action::CancelResponse handle_command_cancel_(
287 std::shared_ptr<CommandActionContext::GoalHandle> goal_handle_ptr);
288 void handle_command_accept_(std::shared_ptr<CommandActionContext::GoalHandle> goal_handle_ptr);
289
290 const TreeExecutorNodeOptions executor_options_;
291 ExecutorParameterListener executor_param_listener_;
292 rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_parameters_callback_handle_ptr_;
293 std::shared_ptr<rclcpp::ParameterEventHandler> parameter_event_handler_ptr_;
294 rclcpp::ParameterEventCallbackHandle::SharedPtr parameter_event_callback_handle_ptr_;
295 core::NodeRegistrationLoader::SharedPtr tree_node_loader_ptr_;
296 TreeBuildHandlerLoader::UniquePtr build_handler_loader_ptr_;
297 TreeBuilder::UniquePtr builder_ptr_;
298 TreeBuildHandler::UniquePtr build_handler_ptr_;
299 std::string current_build_handler_name_;
300 std::map<std::string, int> scripting_enums_;
301 std::map<std::string, rclcpp::ParameterValue> translated_global_blackboard_entries_;
302 TreeConstructor tree_constructor_;
303
304 // Interface objects
305 rclcpp_action::Server<StartActionContext::Type>::SharedPtr start_action_ptr_;
306 StartActionContext start_action_context_;
307 rclcpp_action::Server<CommandActionContext::Type>::SharedPtr command_action_ptr_;
308 rclcpp::TimerBase::SharedPtr command_timer_ptr_;
309};
310
311} // namespace auto_apms_behavior_tree
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.
ExecutionResult
Enum representing possible behavior tree execution results.
Configuration options for TreeExecutorNode.
TreeExecutorNodeOptions & enableScriptingEnumParameters(bool from_overrides, bool dynamic)
Configure whether the executor node accepts scripting enum parameters.
rclcpp::NodeOptions getROSNodeOptions() const
Get the ROS 2 node options that comply with the given options.
TreeExecutorNodeOptions(const rclcpp::NodeOptions &ros_node_options)
Constructor.
TreeExecutorNodeOptions & enableGlobalBlackboardParameters(bool from_overrides, bool dynamic)
Configure whether the executor node accepts global blackboard parameters.
TreeExecutorNodeOptions & setDefaultBuildHandler(const std::string &name)
Specify a default behavior tree build handler that will be used initially.
bool updateGlobalBlackboardWithParameterValues(const std::map< std::string, rclcpp::ParameterValue > &value_map, bool simulate=false)
Update the global blackboard using parameter values.
static std::string stripPrefixFromParameterName(const std::string &prefix, const std::string &param_name)
Get the name of a parameter without its prefix.
virtual void setUpBuilder(TreeBuilder &builder, const core::NodeManifest &node_manifest)
Callback invoked every time before any behavior trees are built.
std::map< std::string, rclcpp::ParameterValue > getParameterValuesWithPrefix(const std::string &prefix)
Assemble all parameters of this node that have a specific prefix.
std::shared_future< ExecutionResult > startExecution(const std::string &tree_build_request)
Start the behavior tree that is specified by a particular build request.
bool updateScriptingEnumsWithParameterValues(const std::map< std::string, rclcpp::ParameterValue > &value_map, bool simulate=false)
Update the internal buffer of scripting enums used when a behavior tree is created.
TreeConstructor makeTreeConstructor(const std::string &build_handler_request, const std::string &root_tree_name, const core::NodeManifest &node_manifest={}, const core::NodeManifest &node_overrides={})
Create a callback that builds a behavior tree according to a specific request.
void loadBuildHandler(const std::string &name)
Load a particular behavior tree build handler plugin.
TreeExecutorNode(const std::string &name, TreeExecutorNodeOptions executor_options)
Constructor allowing to specify a custom node name and executor options.
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.
Useful tooling for incorporating behavior trees for task development.
Definition builder.hpp:30