AutoAPMS
Streamlining behaviors in ROS 2
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#include "std_srvs/srv/trigger.hpp"
27
29{
30
38{
39public:
46 TreeExecutorNodeOptions(const rclcpp::NodeOptions & ros_node_options);
47
54 TreeExecutorNodeOptions & enableScriptingEnumParameters(bool from_overrides, bool dynamic);
55
63 TreeExecutorNodeOptions & enableGlobalBlackboardParameters(bool from_overrides, bool dynamic);
64
70 TreeExecutorNodeOptions & setDefaultBuildHandler(const std::string & name);
71
76 rclcpp::NodeOptions getROSNodeOptions() const;
77
78private:
79 friend class TreeExecutorNode;
80
81 rclcpp::NodeOptions ros_node_options_;
82 bool scripting_enum_parameters_from_overrides_ = true;
83 bool scripting_enum_parameters_dynamic_ = true;
84 bool blackboard_parameters_from_overrides_ = true;
85 bool blackboard_parameters_dynamic_ = true;
86 std::map<std::string, rclcpp::ParameterValue> custom_default_parameters_;
87};
88
118{
119public:
120 using Options = TreeExecutorNodeOptions;
121 using ExecutorParameters = executor_params::Params;
122 using ExecutorParameterListener = executor_params::ParamListener;
125 using TreeBuilder = core::TreeBuilder;
126
127 inline static const std::string PARAM_VALUE_NO_BUILD_HANDLER = "none";
128 inline static const std::string SCRIPTING_ENUM_PARAM_PREFIX = "enum";
129 inline static const std::string BLACKBOARD_PARAM_PREFIX = "bb";
130
137 TreeExecutorNode(const std::string & name, TreeExecutorNodeOptions executor_options);
138
143 explicit TreeExecutorNode(rclcpp::NodeOptions options);
144
145 virtual ~TreeExecutorNode() override = default;
146
148
162 std::shared_future<ExecutionResult> startExecution(
163 const std::string & build_request, const std::string & entrypoint = "",
164 const core::NodeManifest & node_manifest = {});
165
166private:
167 /* Virtual methods */
168
186 virtual void preconfigureBuilder(
187 TreeBuilder & builder, const std::string & build_request, const std::string & entrypoint,
188 const core::NodeManifest & node_manifest);
189
190protected:
191 /* Utility methods */
192
201 std::map<std::string, rclcpp::ParameterValue> getParameterValuesWithPrefix(const std::string & prefix);
202
210 static std::string stripPrefixFromParameterName(const std::string & prefix, const std::string & param_name);
211
228 const std::map<std::string, rclcpp::ParameterValue> & value_map, bool simulate = false);
229
247 const std::map<std::string, rclcpp::ParameterValue> & value_map, bool simulate = false);
248
256 void loadBuildHandler(const std::string & name);
257
270 TreeConstructor makeTreeConstructor(
271 const std::string & build_request, const std::string & entrypoint = "",
272 const core::NodeManifest & node_manifest = {});
273
279 virtual bool clearGlobalBlackboard() override;
280
281private:
282 /* Executor specific virtual overrides */
283
284 bool onTick() override final;
285
286 bool afterTick() override final;
287
288 void onTermination(const ExecutionResult & result) override final;
289
290 /* Internal callbacks */
291
292 rcl_interfaces::msg::SetParametersResult on_set_parameters_callback_(
293 const std::vector<rclcpp::Parameter> & parameters);
294
295 void parameter_event_callback_(const rcl_interfaces::msg::ParameterEvent & event);
296
297 rclcpp_action::GoalResponse handle_start_goal_(
298 const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const StartActionContext::Goal> goal_ptr);
299 rclcpp_action::CancelResponse handle_start_cancel_(std::shared_ptr<StartActionContext::GoalHandle> goal_handle_ptr);
300 void handle_start_accept_(std::shared_ptr<StartActionContext::GoalHandle> goal_handle_ptr);
301
302 rclcpp_action::GoalResponse handle_command_goal_(
303 const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const CommandActionContext::Goal> goal_ptr);
304 rclcpp_action::CancelResponse handle_command_cancel_(
305 std::shared_ptr<CommandActionContext::GoalHandle> goal_handle_ptr);
306 void handle_command_accept_(std::shared_ptr<CommandActionContext::GoalHandle> goal_handle_ptr);
307
308 const TreeExecutorNodeOptions executor_options_;
309 ExecutorParameterListener executor_param_listener_;
310 rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_parameters_callback_handle_ptr_;
311 std::shared_ptr<rclcpp::ParameterEventHandler> parameter_event_handler_ptr_;
312 rclcpp::ParameterEventCallbackHandle::SharedPtr parameter_event_callback_handle_ptr_;
313 core::NodeRegistrationLoader::SharedPtr tree_node_loader_ptr_;
314 TreeBuildHandlerLoader::UniquePtr build_handler_loader_ptr_;
315 TreeBuilder::UniquePtr builder_ptr_;
316 TreeBuildHandler::UniquePtr build_handler_ptr_;
317 std::string current_build_handler_name_;
318 std::map<std::string, int> scripting_enums_;
319 std::map<std::string, rclcpp::ParameterValue> translated_global_blackboard_entries_;
320 TreeConstructor tree_constructor_;
321
322 // Interface objects
323 rclcpp_action::Server<StartActionContext::Type>::SharedPtr start_action_ptr_;
324 StartActionContext start_action_context_;
325 rclcpp_action::Server<CommandActionContext::Type>::SharedPtr command_action_ptr_;
326 rclcpp::TimerBase::SharedPtr command_timer_ptr_;
327 rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr clear_blackboard_service_ptr_;
328};
329
330} // 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.
std::shared_future< ExecutionResult > startExecution(const std::string &build_request, const std::string &entrypoint="", const core::NodeManifest &node_manifest={})
Start the behavior tree that is specified by a particular build request.
bool updateGlobalBlackboardWithParameterValues(const std::map< std::string, rclcpp::ParameterValue > &value_map, bool simulate=false)
Update the global blackboard using parameter values.
virtual void preconfigureBuilder(TreeBuilder &builder, const std::string &build_request, const std::string &entrypoint, const core::NodeManifest &node_manifest)
Callback invoked every time before any behavior trees are built.
static std::string stripPrefixFromParameterName(const std::string &prefix, const std::string &param_name)
Get the name of a parameter without its prefix.
TreeConstructor makeTreeConstructor(const std::string &build_request, const std::string &entrypoint="", const core::NodeManifest &node_manifest={})
Create a callback that builds a behavior tree according to a specific request.
virtual bool clearGlobalBlackboard() override
Reset the global blackboard and clear all entries. This also unsets the corresponding parameters.
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 used when a behavior tree is created.
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.
Powerful tooling for incorporating behavior trees for task development.
Definition behavior.hpp:32