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// 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 <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
156 std::shared_future<ExecutionResult> startExecution(
157 std::unique_ptr<Tree> tree, double tick_rate_sec = 0.1, int groot2_port = -1);
158
170 template <typename TimeRepT = int64_t, typename TimeT = std::milli>
171 std::shared_future<ExecutionResult> startExecution(
172 std::unique_ptr<Tree> tree, const std::chrono::duration<TimeRepT, TimeT> & tick_rate, int groot2_port = -1);
173
174private:
175 void tick_callback_(TerminationCallback termination_callback);
176
177 /* Virtual methods */
178
183 virtual bool onInitialTick();
184
189 virtual bool onTick();
190
195 virtual bool afterTick();
196
203 virtual TreeExitBehavior onTreeExit(bool success);
204
212 virtual void onTermination(const ExecutionResult & result);
213
214public:
220 virtual bool clearGlobalBlackboard();
221
229
234 bool isBusy();
235
241
248 std::string getTreeName();
249
261 TreeBlackboardSharedPtr getGlobalBlackboardPtr();
262
269
274 rclcpp::Node::SharedPtr getNodePtr();
275
280 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr get_node_base_interface();
281
286 rclcpp::CallbackGroup::SharedPtr getTreeNodeWaitablesCallbackGroupPtr();
287
292 rclcpp::executors::SingleThreadedExecutor::SharedPtr getTreeNodeWaitablesExecutorPtr();
293
294protected:
296 rclcpp::Node::SharedPtr node_ptr_;
298 const rclcpp::Logger logger_;
299
300private:
301 rclcpp::CallbackGroup::SharedPtr tree_node_waitables_callback_group_ptr_;
302 rclcpp::executors::SingleThreadedExecutor::SharedPtr tree_node_waitables_executor_ptr_;
303 TreeBlackboardSharedPtr global_blackboard_ptr_;
304 std::unique_ptr<Tree> tree_ptr_;
305 std::unique_ptr<BT::Groot2Publisher> groot2_publisher_ptr_;
306 std::unique_ptr<TreeStateObserver> state_observer_ptr_;
307 rclcpp::TimerBase::SharedPtr execution_timer_ptr_;
308 ExecutionState prev_execution_state_;
309 ControlCommand control_command_;
310 bool execution_stopped_;
311 std::string termination_reason_;
312};
313
314std::string toStr(TreeExecutorBase::ExecutionState state);
315
316std::string toStr(TreeExecutorBase::ControlCommand cmd);
317
318std::string toStr(TreeExecutorBase::TreeExitBehavior behavior);
319
320std::string toStr(TreeExecutorBase::ExecutionResult result);
321
322// #####################################################################################################################
323// ################################ DEFINITIONS ##############################################
324// #####################################################################################################################
325
326template <typename TimeRepT, typename TimeT>
327inline std::shared_future<TreeExecutorBase::ExecutionResult> TreeExecutorBase::startExecution(
328 TreeConstructor make_tree, const std::chrono::duration<TimeRepT, TimeT> & tick_rate, int groot2_port)
329{
330 return startExecution(
331 make_tree, std::chrono::duration_cast<std::chrono::duration<double>>(tick_rate).count(), groot2_port);
332}
333
334template <typename TimeRepT, typename TimeT>
335inline std::shared_future<TreeExecutorBase::ExecutionResult> TreeExecutorBase::startExecution(
336 std::unique_ptr<Tree> tree, const std::chrono::duration<TimeRepT, TimeT> & tick_rate, int groot2_port)
337{
338 return startExecution(
339 std::move(tree), std::chrono::duration_cast<std::chrono::duration<double>>(tick_rate).count(), groot2_port);
340}
341
342} // 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