Link offical repo
54.7 KB
72.3 KB
134 KB
70.9 KB
136 KB
/******************************************************************************* | ||
* Copyright (c) Max Paul Bauer - Robert Bosch GmbH - 2021 | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
#ifndef AGNOSTIC_BEHAVIOR_TREE_BEHAVIOR_NODE_H | ||
#define AGNOSTIC_BEHAVIOR_TREE_BEHAVIOR_NODE_H | ||
#include "agnostic_behavior_tree/scoped_blackboard.h" | ||
#include <exception> | ||
#include <string> | ||
namespace yase { | ||
// Execution states of a node | ||
enum class NodeStatus { kIdle = 0, kRunning, kSuccess, kFailure }; | ||
// Convert the status into an ouput string | ||
std::string toStr(NodeStatus status, bool use_ansi_escape_code = false); | ||
// Simulator specific Extensions can be inherited and composed to behavior nodes | ||
class Extension { | ||
public: | ||
Extension() = default; | ||
virtual ~Extension() = default; | ||
using Ptr = std::shared_ptr<Extension>; | ||
}; | ||
// Abstract base class for Behavior Tree Nodes. | ||
class BehaviorNode { | ||
public: | ||
virtual ~BehaviorNode() { m_blackboard = nullptr; }; | ||
using Ptr = std::shared_ptr<BehaviorNode>; | ||
// Method to invoke tick from the outside while ensuring the consistency of the node status and tick cycle tracking | ||
NodeStatus executeTick() { | ||
// Track current tick cycle | ||
if (m_parent_node != nullptr) { | ||
if (m_tick_cycle >= m_parent_node->m_tick_cycle) { | ||
std::string error_msg = "Error while executing tick() of behavior node ["; | ||
error_msg.append(name()); | ||
error_msg.append("]: Tick cycle [") | ||
.append(std::to_string(m_tick_cycle)) | ||
.append("] of this node is greater/equal then its parent tick cycle ["); | ||
error_msg.append(std::to_string(m_parent_node->m_tick_cycle)) | ||
.append("] - A child is not allowed to proceed faster then its parent"); | ||
throw std::logic_error(error_msg); | ||
} | ||
m_tick_cycle = m_parent_node->m_tick_cycle; | ||
} else { | ||
(m_tick_cycle)++; | ||
} | ||
// Clear execution info before step | ||
m_execution_info.clear(); | ||
m_status = tick(); | ||
if (m_status == NodeStatus::kIdle) { | ||
std::string error_msg = "Error while executing tick() of behavior node ["; | ||
error_msg.append(name()); | ||
error_msg.append("]: Returned invalid status NodeStatus::kIdle!"); | ||
throw std::logic_error(error_msg); | ||
} | ||
return status(); | ||
}; | ||
// Called once right before behavior is ticked the first time. | ||
// THE CALL MUST RESET THE BEHAVIOR with all its necessary values! | ||
virtual void onInit() = 0; | ||
// Called after behavior is called the last time | ||
virtual void onTerminate(){}; | ||
// Get node status | ||
NodeStatus status() const { return m_status; }; | ||
< |