Behavior tree 编程实战
写在前面的话
本文主要在前文的基础上,通过《Behavior trees in ROS and AI》以及小明工坊中所提供的部分代码进行实战化分析。
此处给出两处代码地址以及原文:
古月居小明工坊:https://www.guyuehome.com/5311
Behavior trees in ROS and AI:
https://behaviortree.github.io/BehaviorTree.CPP/tutorial_01_first_tree/
如何建立一棵行为树
行为树枝干的编辑
建立行为树的方法主要有两种,一种是静态编译,一种是XML动态生成,这里主要剖析的是静态编译的方法。
建立一棵行为树的最基本的架构在
ROS-BehaviorTree/behavior_tree_core/src/gtest/external_ros_nodes_test.cpp
中得到了详细的体现:
#include <behavior_tree.h>
int main(int argc, char **argv)
{
ros::init(argc, argv, "BehaviorTree");
try
{
int TickPeriod_milliseconds = 1000;
BT::ROSAction* action = new BT::ROSAction("action");
BT::NegationNode* decorator = new BT::NegationNode("decorator");
BT::ROSCondition* condition = new BT::ROSCondition("condition");
BT:: SequenceNode* sequence1 = new BT::SequenceNode("seq1");
sequence1->AddChild(decorator);
decorator->AddChild(condition);
sequence1->AddChild(action);
Execute(sequence1, TickPeriod_milliseconds); // from BehaviorTree.cpp
}
catch (BT::BehaviorTreeException& Exception)
{
std::cout << Exception.what() << std::endl;
}
return 0;
}
代码心得:
behavior_tree.h 文件为mit所开发的一款关于行为树创建的库,可以