2023-09-26 10:01:01 +08:00
|
|
|
|
import py_trees
|
|
|
|
|
from behavior_library import *
|
|
|
|
|
|
2023-09-26 10:14:19 +08:00
|
|
|
|
|
2023-09-26 10:01:01 +08:00
|
|
|
|
def LoadMainTree() -> py_trees.trees.BehaviourTree:
|
|
|
|
|
"""
|
|
|
|
|
此方法用于加载固定的顶层行为树(不包括实际执行)
|
|
|
|
|
|
|
|
|
|
Args: None
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
seq_subtree_0 = py_trees.composites.Sequence(
|
2023-09-26 10:14:19 +08:00
|
|
|
|
name='seq_subtree_0',
|
|
|
|
|
memory=False,
|
2023-09-26 10:01:01 +08:00
|
|
|
|
children=[IsChatting(), Chatting()]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
seq_subtree_1 = py_trees.composites.Sequence(
|
2023-09-26 10:14:19 +08:00
|
|
|
|
name='seq_subtree_1',
|
|
|
|
|
memory=False,
|
2023-09-26 10:01:01 +08:00
|
|
|
|
children=[IsTakingAction(), TakingAction()]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
seq_subtree_2 = py_trees.composites.Sequence(
|
2023-09-26 10:14:19 +08:00
|
|
|
|
name='seq_subtree_2',
|
|
|
|
|
memory=False,
|
2023-09-26 10:01:01 +08:00
|
|
|
|
children=[IsSomethingMore(), TakingMoreAction()]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
root = py_trees.composites.Selector(
|
2023-09-26 10:14:19 +08:00
|
|
|
|
name='selector_root',
|
2023-09-26 10:01:01 +08:00
|
|
|
|
memory=False,
|
|
|
|
|
children=[seq_subtree_0, seq_subtree_1, seq_subtree_2]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return py_trees.trees.BehaviourTree(root)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def LoadSubTree(path: str) -> py_trees.behaviour.Behaviour:
|
|
|
|
|
"""
|
|
|
|
|
此方法用于从ptml文件中加载行为树(不包括实际执行)
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
-- path: ptml文件的路径
|
|
|
|
|
"""
|
|
|
|
|
# TODO
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2023-09-26 11:31:36 +08:00
|
|
|
|
if __name__ == '__main__':
|
2023-09-26 10:01:01 +08:00
|
|
|
|
btree = LoadMainTree()
|
|
|
|
|
|
2023-09-26 10:14:19 +08:00
|
|
|
|
|
2023-09-26 10:01:01 +08:00
|
|
|
|
def print_tree(tree):
|
|
|
|
|
print(py_trees.display.unicode_tree(root=tree.root, show_status=True))
|
|
|
|
|
|
2023-09-26 10:14:19 +08:00
|
|
|
|
|
2023-09-26 10:01:01 +08:00
|
|
|
|
try:
|
|
|
|
|
btree.tick_tock(
|
|
|
|
|
period_ms=500,
|
|
|
|
|
number_of_iterations=py_trees.trees.CONTINUOUS_TICK_TOCK,
|
|
|
|
|
pre_tick_handler=None,
|
|
|
|
|
post_tick_handler=print_tree
|
|
|
|
|
)
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
btree.interrupt()
|