AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
executor_base.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 <chrono>
18#include <functional>
19#include <future>
20#include <memory>
21
22#include "auto_apms_behavior_tree/exceptions.hpp"
23#include "auto_apms_behavior_tree/executor/state_observer.hpp"
24#include "auto_apms_behavior_tree_core/definitions.hpp"
25#include "behaviortree_cpp/loggers/groot2_publisher.h"
26#include "rclcpp/node.hpp"
27
29{
30
40class TreeExecutorBase : public std::enable_shared_from_this<TreeExecutorBase>
41{
42public:
46 enum class ExecutionState : uint8_t
47 {
53 };
54
58 enum class ControlCommand : uint8_t
59 {
64 };
65
69 enum class TreeExitBehavior : uint8_t
70 {
73 };
74
85
86private:
87 using TerminationCallback = std::function<void(ExecutionResult, const std::string &)>;
88
89public:
97 rclcpp::Node::SharedPtr node_ptr, rclcpp::CallbackGroup::SharedPtr tree_node_callback_group_ptr = nullptr);
98
99 virtual ~TreeExecutorBase() = default;
100
116 std::shared_future<ExecutionResult> startExecution(
117 TreeConstructor make_tree, double tick_rate_sec = 0.1, int groot2_port = -1);
118
134 template <typename TimeRepT = int64_t, typename TimeT = std::milli>
135 std::shared_future<ExecutionResult> startExecution(
136 TreeConstructor make_tree, const std::chrono::duration<TimeRepT, TimeT> & tick_rate, int groot2_port = -1);
137
138private:
139 void tick_callback_(TerminationCallback termination_callback);
140
141 /* Virtual methods */
142
147 virtual bool onInitialTick();
148
153 virtual bool onTick();
154
159 virtual bool afterTick();
160
167 virtual TreeExitBehavior onTreeExit(bool success);
168
176 virtual void onTermination(const ExecutionResult & result);
177
178public:
184 virtual bool clearGlobalBlackboard();
185
193
198 bool isBusy();
199
205
212 std::string getTreeName();
213
225 TreeBlackboardSharedPtr getGlobalBlackboardPtr();
226
233
238 rclcpp::Node::SharedPtr getNodePtr();
239
244 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr get_node_base_interface();
245
250 rclcpp::CallbackGroup::SharedPtr getTreeNodeWaitablesCallbackGroupPtr();
251
256 rclcpp::executors::SingleThreadedExecutor::SharedPtr getTreeNodeWaitablesExecutorPtr();
257
258protected:
260 rclcpp::Node::SharedPtr node_ptr_;
262 const rclcpp::Logger logger_;
263
264private:
265 rclcpp::CallbackGroup::SharedPtr tree_node_waitables_callback_group_ptr_;
266 rclcpp::executors::SingleThreadedExecutor::SharedPtr tree_node_waitables_executor_ptr_;
267 TreeBlackboardSharedPtr global_blackboard_ptr_;
268 std::unique_ptr<Tree> tree_ptr_;
269 std::unique_ptr<BT::Groot2Publisher> groot2_publisher_ptr_;
270 std::unique_ptr<TreeStateObserver> state_observer_ptr_;
271 rclcpp::TimerBase::SharedPtr execution_timer_ptr_;
272 ExecutionState prev_execution_state_;
273 ControlCommand control_command_;
274 bool execution_stopped_;
275 std::string termination_reason_;
276};
277
278std::string toStr(TreeExecutorBase::ExecutionState state);
279
280std::string toStr(TreeExecutorBase::ControlCommand cmd);
281
282std::string toStr(TreeExecutorBase::TreeExitBehavior behavior);
283
284std::string toStr(TreeExecutorBase::ExecutionResult result);
285
286// #####################################################################################################################
287// ################################ DEFINITIONS ##############################################
288// #####################################################################################################################
289
290template <typename TimeRepT, typename TimeT>
291inline std::shared_future<TreeExecutorBase::ExecutionResult> TreeExecutorBase::startExecution(
292 TreeConstructor make_tree, const std::chrono::duration<TimeRepT, TimeT> & tick_rate, int groot2_port)
293{
294 return startExecution(
295 make_tree, std::chrono::duration_cast<std::chrono::duration<double>>(tick_rate).count(), groot2_port);
296}
297
298} // namespace auto_apms_behavior_tree
Base class that offers basic functionality for executing behavior trees.
ExecutionState
Enum representing possible behavior tree execution states.
@ RUNNING
Executor is busy and tree has been ticked at least once.
@ PAUSED
Execution routine is active, but tree is not being ticked.
@ HALTED
Execution routine is active, but tree is not being ticked and has been halted before.
virtual void onTermination(const ExecutionResult &result)
Callback invoked when the execution routine terminates.
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.
ExecutionState getExecutionState()
Get a status code indicating the current state of execution.
bool isBusy()
Determine whether this executor is currently executing a behavior tree.
rclcpp::Node::SharedPtr node_ptr_
Shared pointer to the parent ROS 2 node.
virtual TreeExitBehavior onTreeExit(bool success)
Callback invoked last thing when the execution routine completes because the behavior tree is finishe...
virtual bool onInitialTick()
Callback invoked once before the behavior tree is ticked for the very first time.
rclcpp::executors::SingleThreadedExecutor::SharedPtr getTreeNodeWaitablesExecutorPtr()
Get the ROS 2 executor instance used for spinning waitables registered by behavior tree nodes.
rclcpp::CallbackGroup::SharedPtr getTreeNodeWaitablesCallbackGroupPtr()
Get the callback group used for all waitables registered by behavior tree nodes.
void setControlCommand(ControlCommand cmd)
Set the command that handles the control flow of the execution routine.
virtual bool onTick()
Callback invoked every time before the behavior tree is ticked.
virtual bool afterTick()
Callback invoked every time after the behavior tree is ticked.
TreeExitBehavior
Enum representing possible options for what to do when a behavior tree is completed.
virtual bool clearGlobalBlackboard()
Reset the global blackboard and clear all entries.
ControlCommand
Enum representing possible commands for controlling the behavior tree execution routine.
@ TERMINATE
Halt the currently executing tree and terminate the execution routine.
@ HALT
Halt the currently executing tree and pause the execution routine.
TreeStateObserver & getStateObserver()
Get a reference to the current behavior tree state observer.
TreeExecutorBase(rclcpp::Node::SharedPtr node_ptr, rclcpp::CallbackGroup::SharedPtr tree_node_callback_group_ptr=nullptr)
Constructor.
rclcpp::Node::SharedPtr getNodePtr()
Get a shared pointer to the parent ROS 2 node.
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr get_node_base_interface()
Get the node's base interface. Is required to be able to register derived classes as ROS2 components.
TreeBlackboardSharedPtr getGlobalBlackboardPtr()
Get a shared pointer to the global blackboard instance.
std::string getTreeName()
Get the name of the tree that is currently executing.
ExecutionResult
Enum representing possible behavior tree execution results.
@ TREE_SUCCEEDED
Tree completed with BT::NodeStatus::SUCCESS.
@ TERMINATED_PREMATURELY
Execution terminated before the tree was able to propagate the tick to all its nodes.
@ TREE_FAILED
Tree completed with BT::NodeStatus::FAILURE.
const rclcpp::Logger logger_
Logger associated with the parent ROS 2 node.
State observer for a particular behavior tree object that writes introspection and debugging informat...
Powerful tooling for incorporating behavior trees for task development.
Definition behavior.hpp:32