From ed5b99e69bef89721b6bf4adba2d667c518a723c Mon Sep 17 00:00:00 2001 From: wuziji <2193177243@qq.com> Date: Mon, 13 Nov 2023 11:53:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8B=AC=E5=8F=B7=E4=B8=8D?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robowaiter/behavior_tree/ptml/ptmlCompiler.py | 14 ++++++------- .../behavior_tree/ptml/test/Default.ptml | 2 +- .../behavior_tree/ptml/test/bracket_ptml.ptml | 11 +++++++--- .../behavior_tree/ptml/test/tab_test.ptml | 12 ----------- robowaiter/robot/Default.ptml | 12 ++++++----- robowaiter/robot/robot.py | 21 +++++++++---------- robowaiter/utils/bt/load.py | 7 +++---- run_robowaiter.py | 2 +- sub_task.ptml | 6 +++--- 9 files changed, 39 insertions(+), 48 deletions(-) delete mode 100644 robowaiter/behavior_tree/ptml/test/tab_test.ptml diff --git a/robowaiter/behavior_tree/ptml/ptmlCompiler.py b/robowaiter/behavior_tree/ptml/ptmlCompiler.py index b3b5b12..0be7cd5 100644 --- a/robowaiter/behavior_tree/ptml/ptmlCompiler.py +++ b/robowaiter/behavior_tree/ptml/ptmlCompiler.py @@ -34,6 +34,7 @@ def load(scene, ptml_path: str, behaviour_lib_path: str): # noting fault, go next ptml_path = format_trans_to_bracket(ptml_path) + # print(ptml_path) input_stream = FileStream(ptml_path, encoding="utf-8") lexer = Lexer(input_stream) @@ -43,9 +44,8 @@ def load(scene, ptml_path: str, behaviour_lib_path: str): walker = ParseTreeWalker() - - sys.path.append(os.path.join(behaviour_lib_path,"cond")) - sys.path.append(os.path.join(behaviour_lib_path,"act")) + sys.path.append(os.path.join(behaviour_lib_path, "cond")) + sys.path.append(os.path.join(behaviour_lib_path, "act")) ptml = ptmlTranslator(scene, behaviour_lib_path) # listener mode walker.walk(ptml, tree) @@ -75,8 +75,7 @@ def format_trans_to_bracket(file_path: str) -> str: if "{" in f: return file_path - - def counter_(input:str) -> int: + def counter_(input: str) -> int: length = 0 for i in range(len(input)): if input[i] == ' ': @@ -86,7 +85,6 @@ def format_trans_to_bracket(file_path: str) -> str: raise TabError('Tab length in ptml file should be 4.') return length - with open(file_path, 'r') as file: ptml_new = '' ptml_tab = file.readlines() @@ -110,9 +108,9 @@ def format_trans_to_bracket(file_path: str) -> str: ptml_new += '}' import re - new_path = re.sub('/[a-zA-Z0-9_]*\.ptml', '/bracket_ptml.ptml',file_path) + new_path = re.sub('\\\[a-zA-Z0-9_]*\.ptml', '/bracket_ptml.ptml', file_path) with open(new_path, 'w+') as file: file.write(ptml_new) return new_path -# format_trans_to_bracket('/home/wu/RoboWaiter/robowaiter/behavior_tree/ptml/test/tab_test.ptml') \ No newline at end of file +# format_trans_to_bracket('C:\\Users\\Estrella\\Desktop\\RoboWaiter\\robowaiter\\behavior_tree\\ptml\\test\\Default.ptml') \ No newline at end of file diff --git a/robowaiter/behavior_tree/ptml/test/Default.ptml b/robowaiter/behavior_tree/ptml/test/Default.ptml index 1580fcd..be4dc5e 100644 --- a/robowaiter/behavior_tree/ptml/test/Default.ptml +++ b/robowaiter/behavior_tree/ptml/test/Default.ptml @@ -9,4 +9,4 @@ selector cond HasSubTask() sequence cond At(Robot,Table) - cond At(Robot,Table) \ No newline at end of file + // cond At(Robot,Table) \ No newline at end of file diff --git a/robowaiter/behavior_tree/ptml/test/bracket_ptml.ptml b/robowaiter/behavior_tree/ptml/test/bracket_ptml.ptml index ae237e0..7ac38d6 100644 --- a/robowaiter/behavior_tree/ptml/test/bracket_ptml.ptml +++ b/robowaiter/behavior_tree/ptml/test/bracket_ptml.ptml @@ -1,6 +1,11 @@ selector { - sequence + selector +{ + cond HasMap() + act ExploreEnv() + +} sequence { cond Chatting() act DealChat() @@ -10,6 +15,6 @@ selector cond HasSubTask() sequence { - act SubTaskPlaceHolder() + cond At(Robot,Table) -} cond At(Talb,ea)}} \ No newline at end of file +} // cond At(Robot,Table)}} \ No newline at end of file diff --git a/robowaiter/behavior_tree/ptml/test/tab_test.ptml b/robowaiter/behavior_tree/ptml/test/tab_test.ptml deleted file mode 100644 index cabe285..0000000 --- a/robowaiter/behavior_tree/ptml/test/tab_test.ptml +++ /dev/null @@ -1,12 +0,0 @@ -selector -// selector -// cond HasMap() -// act ExploreEnv() - sequence - cond Chatting() - act DealChat() - sequence - cond HasSubTask() - sequence - act SubTaskPlaceHolder() - cond At(Talb,ea) \ No newline at end of file diff --git a/robowaiter/robot/Default.ptml b/robowaiter/robot/Default.ptml index 38eb1f3..03d28f0 100644 --- a/robowaiter/robot/Default.ptml +++ b/robowaiter/robot/Default.ptml @@ -1,11 +1,13 @@ selector -// sequence -// cond NeedExplore() -// act ExploreEnv() +{ sequence +{ cond Chatting() act DealChat() - sequence + +} sequence +{ cond HasSubTask() sequence - act SubTaskPlaceHolder() \ No newline at end of file +{ + act SubTaskPlaceHolder()}}} \ No newline at end of file diff --git a/robowaiter/robot/robot.py b/robowaiter/robot/robot.py index 880d55b..2e1253d 100644 --- a/robowaiter/robot/robot.py +++ b/robowaiter/robot/robot.py @@ -1,7 +1,7 @@ import io import contextlib -from robowaiter.utils.bt.load import load_bt_from_ptml,find_node_by_name,print_tree_from_root +from robowaiter.utils.bt.load import load_bt_from_ptml, find_node_by_name, print_tree_from_root from robowaiter.utils.bt.visitor import StatusVisitor from robowaiter.behavior_tree.obtea.OptimalBTExpansionAlgorithm import Action # 调用最优行为树扩展算法 @@ -15,7 +15,7 @@ class Robot(object): scene = None response_frequency = 1 - def __init__(self,ptml_path,behavior_lib_path): + def __init__(self, ptml_path, behavior_lib_path): self.ptml_path = ptml_path self.behavior_lib_path = behavior_lib_path @@ -24,13 +24,12 @@ class Robot(object): self.last_tick_output = "" self.action_list = None - - def set_scene(self,scene): + def set_scene(self, scene): self.scene = scene def load_BT(self): - self.bt = load_bt_from_ptml(self.scene, self.ptml_path,self.behavior_lib_path) - sub_task_place_holder = find_node_by_name(self.bt.root,"SubTaskPlaceHolder") + self.bt = load_bt_from_ptml(self.scene, self.ptml_path, self.behavior_lib_path) + sub_task_place_holder = find_node_by_name(self.bt.root, "SubTaskPlaceHolder") if sub_task_place_holder: sub_task_seq = sub_task_place_holder.parent sub_task_seq.children.pop() @@ -39,8 +38,7 @@ class Robot(object): self.bt_visitor = StatusVisitor() self.bt.visitors.append(self.bt_visitor) - - def expand_sub_task_tree(self,goal): + def expand_sub_task_tree(self, goal): if self.action_list is None: self.action_list = self.collect_action_nodes() print(f"首次运行行为树扩展算法,收集到{len(self.action_list)}个有效动作") @@ -54,7 +52,7 @@ class Robot(object): with open(file_path, 'w') as file: file.write(ptml_string) - sub_task_bt = load_bt_from_ptml(self.scene, file_path,self.behavior_lib_path) + sub_task_bt = load_bt_from_ptml(self.scene, file_path, self.behavior_lib_path) # 加入删除子树的节点 seq = Sequence(name="Sequence", memory=False) @@ -69,7 +67,7 @@ class Robot(object): def collect_action_nodes(self): action_list = [ - Action(name='MakeCoffee', pre={'At(Robot,CoffeeMachine)'}, + Action(name='MakeCoffee()', pre={'At(Robot,CoffeeMachine)'}, add={'At(Coffee,Bar)'}, del_set=set(), cost=1), Action(name='MoveTo(Table)', pre={'At(Robot,Bar)'}, add={'At(Robot,Table)'}, del_set=set(), cost=1), @@ -94,5 +92,6 @@ class Robot(object): print("\n") self.last_tick_output = bt_output + if __name__ == '__main__': - pass \ No newline at end of file + pass diff --git a/robowaiter/utils/bt/load.py b/robowaiter/utils/bt/load.py index cb7f61a..72e829c 100644 --- a/robowaiter/utils/bt/load.py +++ b/robowaiter/utils/bt/load.py @@ -4,7 +4,7 @@ from robowaiter.behavior_tree.ptml import ptmlCompiler def load_bt_from_ptml(scene, ptml_path, behavior_lib_path): ptml_bt = ptmlCompiler.load(scene, ptml_path, behavior_lib_path) - bt = ptree.trees.BehaviourTree(ptml_bt) + bt = ptree.trees.BehaviourTree(ptml_bt) with open(ptml_path, 'r') as f: ptml = f.read() @@ -29,6 +29,7 @@ def print_tree_from_root(node, indent=0): for child in node.children: print_tree_from_root(child, indent + 1) + def find_node_by_name(tree, name): """ Find a node in the behavior tree with the specified name. @@ -46,8 +47,6 @@ def find_node_by_name(tree, name): return result return None - - # class BehaviorTree(ptree): # def __init__(self): -# super().__init__() \ No newline at end of file +# super().__init__() diff --git a/run_robowaiter.py b/run_robowaiter.py index 52cbd1a..1c11b81 100644 --- a/run_robowaiter.py +++ b/run_robowaiter.py @@ -1,7 +1,7 @@ import os from robowaiter import Robot, task_map -TASK_NAME = 'AEM' +TASK_NAME = 'VLM' # create robot project_path = "./robowaiter" diff --git a/sub_task.ptml b/sub_task.ptml index 1b4d81e..b8bf4e1 100644 --- a/sub_task.ptml +++ b/sub_task.ptml @@ -1,7 +1,7 @@ selector{ -cond EnvExplored() +cond At(Coffee,Bar) selector{ -cond At(Robot,Bar) -act ExploreEnv() +cond At(Robot,CoffeeMachine) +act MakeCoffee() } }