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" // TODO Uncomment if generate_parameter_library 0.4.0 is
20// available
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_interfaces/action/start_tree_executor.hpp"
25#include "auto_apms_util/action_context.hpp"
26#include "executor_params.hpp" // TODO Remove if generate_parameter_library 0.4.0 is available
27#include "rclcpp/rclcpp.hpp"
28
30{
31
39{
40public:
47 TreeExecutorNodeOptions(const rclcpp::NodeOptions & ros_node_options);
48
55 TreeExecutorNodeOptions & enableScriptingEnumParameters(bool from_overrides, bool dynamic);
56
64 TreeExecutorNodeOptions & enableGlobalBlackboardParameters(bool from_overrides, bool dynamic);
65
71 TreeExecutorNodeOptions & setDefaultBuildHandler(const std::string & name);
72
77 rclcpp::NodeOptions getROSNodeOptions() const;
78
79private:
80 friend class TreeExecutorNode;
81
82 rclcpp::NodeOptions ros_node_options_;
83 bool scripting_enum_parameters_from_overrides_ = true;
84 bool scripting_enum_parameters_dynamic_ = true;
85 bool blackboard_parameters_from_overrides_ = true;
86 bool blackboard_parameters_dynamic_ = true;
87 std::map<std::string, rclcpp::ParameterValue> custom_default_parameters_;
88};
89
119{
120public:
121 using Options = TreeExecutorNodeOptions;
122 using ExecutorParameters = executor_params::Params;
123 using ExecutorParameterListener = executor_params::ParamListener;
126 using TreeBuilder = core::TreeBuilder;
127
128 inline static const std::string PARAM_VALUE_NO_BUILD_HANDLER = "none";
129 inline static const std::string SCRIPTING_ENUM_PARAM_PREFIX = "enum";
130 inline static const std::string BLACKBOARD_PARAM_PREFIX = "bb";
131
138 TreeExecutorNode(const std::string & name, TreeExecutorNodeOptions executor_options);
139
144 explicit TreeExecutorNode(rclcpp::NodeOptions options);
145
146 virtual ~TreeExecutorNode() override = default;
147
149
161 std::shared_future<ExecutionResult> startExecution(const std::string & tree_build_request);
162
163private:
164 /* Virtual methods */
165
177 virtual void setUpBuilder(TreeBuilder & builder, const core::NodeManifest & node_manifest);
178
179protected:
180 /* Utility methods */
181
190 std::map<std::string, rclcpp::ParameterValue> getParameterValuesWithPrefix(const std::string & prefix);
191
199 static std::string stripPrefixFromParameterName(const std::string & prefix, const std::string & param_name);
200
217 const std::map<std::string, rclcpp::ParameterValue> & value_map, bool simulate = false);
218
236 const std::map<std::string, rclcpp::ParameterValue> & value_map, bool simulate = false);
237
245 void loadBuildHandler(const std::string & name);
246
261 TreeConstructor makeTreeConstructor(
262 const std::string & build_handler_request, const std::string & root_tree_name,
263 const core::NodeManifest & node_manifest = {}, const core::NodeManifest & node_overrides = {});
264
265private:
266 /* Executor specific virtual overrides */
267
268 bool onTick() override final;
269
270 bool afterTick() override final;
271
272 void onTermination(const ExecutionResult & result) override final;
273
274 /* Internal callbacks */
275
276 rcl_interfaces::msg::SetParametersResult on_set_parameters_callback_(
277 const std::vector<rclcpp::Parameter> & parameters);
278
279 void parameter_event_callback_(const rcl_interfaces::msg::ParameterEvent & event);
280
281 rclcpp_action::GoalResponse handle_start_goal_(
282 const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const StartActionContext::Goal> goal_ptr);
283 rclcpp_action::CancelResponse handle_start_cancel_(std::shared_ptr<StartActionContext::GoalHandle> goal_handle_ptr);
284 void handle_start_accept_(std::shared_ptr<StartActionContext::GoalHandle> goal_handle_ptr);
285
286 rclcpp_action::GoalResponse handle_command_goal_(
287 const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const CommandActionContext::Goal> goal_ptr);
288 rclcpp_action::CancelResponse handle_command_cancel_(
289 std::shared_ptr<CommandActionContext::GoalHandle> goal_handle_ptr);
290 void handle_command_accept_(std::shared_ptr<CommandActionContext::GoalHandle> goal_handle_ptr);
291
292 const TreeExecutorNodeOptions executor_options_;
293 ExecutorParameterListener executor_param_listener_;
294 rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_parameters_callback_handle_ptr_;
295 std::shared_ptr<rclcpp::ParameterEventHandler> parameter_event_handler_ptr_;
296 rclcpp::ParameterEventCallbackHandle::SharedPtr parameter_event_callback_handle_ptr_;
297 core::NodeRegistrationLoader::SharedPtr tree_node_loader_ptr_;
298 TreeBuildHandlerLoader::UniquePtr build_handler_loader_ptr_;
299 TreeBuilder::UniquePtr builder_ptr_;
300 TreeBuildHandler::UniquePtr build_handler_ptr_;
301 std::string current_build_handler_name_;
302 std::map<std::string, int> scripting_enums_;
303 std::map<std::string, rclcpp::ParameterValue> translated_global_blackboard_entries_;
304 TreeConstructor tree_constructor_;
305
306 // Interface objects
307 rclcpp_action::Server<StartActionContext::Type>::SharedPtr start_action_ptr_;
308 StartActionContext start_action_context_;
309 rclcpp_action::Server<CommandActionContext::Type>::SharedPtr command_action_ptr_;
310 rclcpp::TimerBase::SharedPtr command_timer_ptr_;
311};
312
313} // 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.
void onTermination(const ExecutionResult &result) override final
Callback invoked when the execution routine terminates.
static std::string stripPrefixFromParameterName(const std::string &prefix, const std::string &param_name)
Get the name of a parameter without its prefix.
bool afterTick() override final
Callback invoked every time after the behavior tree is ticked.
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 onTick() override final
Callback invoked every time before the behavior tree is ticked.
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