AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
executor.hpp
Go to the documentation of this file.
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 <functional>
18#include <future>
19
20#include "behaviortree_cpp/loggers/groot2_publisher.h"
21#include "rclcpp/node.hpp"
22
23#include "executor_params.hpp"
26
28{
29
31{
32public:
33 enum class ExecutionState : uint8_t
34 {
35 IDLE,
37 RUNNING,
38 PAUSED,
39 HALTED
40 };
41 enum class ControlCommand : uint8_t
42 {
43 RUN,
44 PAUSE,
45 HALT,
47 };
48 enum class TreeExitBehavior : uint8_t
49 {
52 };
53 enum class ExecutionResult : uint8_t
54 {
58 ERROR
59 };
60
61private:
62 using TerminationCallback = std::function<void(ExecutionResult, const std::string&)>;
63
64public:
66 using ExecutorParams = executor_params::Params;
67
68 TreeExecutor(rclcpp::Node::SharedPtr node_ptr);
69
70 std::shared_future<ExecutionResult> startExecution(CreateTreeCallback create_tree_cb);
71
72 bool isBusy();
73
75
76 std::string getTreeName();
77
78private:
79 void execution_routine_(TerminationCallback termination_callback);
80
81 /* Virtual member functions */
82
83 virtual bool onInitialTick();
84
85 virtual bool onTick();
86
87 virtual TreeExitBehavior onTreeExit(bool success);
88
89 virtual void onTermination(const ExecutionResult& result);
90
91public:
92 /* Setter functions */
93
95
97
98 /* Getter functions */
99
101 rclcpp::Node::SharedPtr getNodePtr();
102
104 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr get_node_base_interface();
105
113
115
117
118private:
119 rclcpp::Node::SharedPtr node_ptr_;
120 ExecutorParams executor_params_;
121 TreeBlackboardSharedPtr global_blackboard_ptr_;
122 std::unique_ptr<Tree> tree_ptr_;
123 std::unique_ptr<BT::Groot2Publisher> groot2_publisher_ptr_;
124 std::unique_ptr<BTStateObserver> state_observer_ptr_;
125 rclcpp::TimerBase::SharedPtr execution_timer_ptr_;
126 ExecutionState prev_execution_state_;
127 ControlCommand control_command_;
128 bool execution_stopped_;
129 std::string termination_reason_;
130};
131
132std::string toStr(TreeExecutor::ExecutionState state);
133
134std::string toStr(TreeExecutor::ControlCommand cmd);
135
136std::string toStr(TreeExecutor::TreeExitBehavior behavior);
137
138std::string toStr(TreeExecutor::ExecutionResult result);
139
140} // namespace auto_apms_behavior_tree
void setExecutorParameters(const ExecutorParams &p)
Definition executor.cpp:260
executor_params::Params ExecutorParams
Definition executor.hpp:66
std::function< Tree(TreeBlackboardSharedPtr)> CreateTreeCallback
Definition executor.hpp:65
void setControlCommand(ControlCommand cmd)
Definition executor.cpp:255
TreeExecutor(rclcpp::Node::SharedPtr node_ptr)
Definition executor.cpp:25
rclcpp::Node::SharedPtr getNodePtr()
Get a pointer to the internal ROS2 node instance.
Definition executor.cpp:265
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.
Definition executor.cpp:270
TreeBlackboardSharedPtr getGlobalBlackboardPtr()
Get a pointer to the global blackboard instance.
Definition executor.cpp:275
std::shared_future< ExecutionResult > startExecution(CreateTreeCallback create_tree_cb)
Definition executor.cpp:34
std::string toStr(TreeExecutor::ExecutionState state)
Definition executor.cpp:290
std::shared_ptr< TreeBlackboard > TreeBlackboardSharedPtr